/*
 * plugins.js
 * This file contain any helper functions, or logic that executes within the site.
 */
//===== Safe console.log 
	// (via Paul Irish)  [ example: log('message', this, {param:val}) ]
	var enableDebug = true;  //Switch to false for production
	window.log = function(){
		//log.history = log.history || [];
		//log.history.push(arguments);
		if(this.console && enableDebug){
			//console.log( Array.prototype.slice.call(arguments) );
		}
	};
	
//===== exec and init are from Paul Irish unintrusive loading utility - (read more @ http://goo.gl/FEVOJ)
	UTIL = {
		exec : function(ns,controller,action,args){
			action = (action === undefined) ? 'init' : action;
			if (controller !== '' && ns[controller] && typeof ns[controller][action] == 'function'){
				ns[controller][action](args);
			}
		},
		init : function(namespace){
			var bodyId = document.body.id;
			UTIL.exec(namespace, 'common');
			UTIL.exec(namespace, bodyId);
			$.each(document.body.className.split(/\s+/),function(i,classnm){
				UTIL.exec(namespace, bodyId, classnm);
			});
			UTIL.exec(namespace, 'common', 'finalize');
		},
		convert_csv_to_json : function( url, saveFile ) {
			/* url = string url to find
			 * savefile(boolean) optional save to a file
			*/
			$.get(url, function( data ){
				var csv_array = UTIL.CSVToArray( data );
				var json_str = UTIL.arrayToJSON( csv_array, "string" );
				if( saveFile ) UTIL.saveToFile( json_str );
				return eval(json_str);
			});
		},
		saveToFile : function( str ) {
			post_data = 'fileContent='+str;
			$.post('data/toFile.php', post_data, function(data) {
				log('Save to file complete', { result:data });
			});
		},
		arrayToJSON : function( arr, returnType ){
			var arr_len = arr.length;//-Store len
			var attr_title = arr[0]; //-FIRST ENTRY SETS UP THE ATTR NAMES
			
			var json_str = '{ \n'+'	"events" : [ ';
			$.each(arr, function(i, item){
				if( i > 0 ) {
					json_str += '{ \n';
					item_len = item.length;
					$.each( item, function(i, val){
						var lineEnding = ( i >= (item_len-1) ) ? '\n' : ', \n'
						json_str += '			"'+attr_title[i]+'" : "'+escape($.trim(val))+'"'+lineEnding;
					});
					json_str += '		} , ';
				}
			});
			json_str = json_str.substr(0, (json_str.length-3) );
			json_str += ']\n}';
			
			log('arrayToJSON() ', this, { arr:arr, arr_len:arr_len, arr_title:attr_title, json_str:json_str });
			return json_str;
			//TODO: return ( returnType === 'string' || returnType === null ) ? json_str : eval( json_str+"" );
		},
		CSVToArray : function( strData, strDelimiter ){
			strDelimiter = (strDelimiter || ",");
			var objPattern = new RegExp( (
				// Delimiters.
				"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
				// Quoted fields.
				"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
				// Standard fields.
				"([^\"\\" + strDelimiter + "\\r\\n]*))"
			), "gi");
			
			var arrData = [[]];		// Create an array to hold our data. Give the array a default empty first row.
			var arrMatches = null;	// Create an array to hold our individual pattern matching groups.
			
			while ( arrMatches = objPattern.exec( strData ) ) {

				var strMatchedDelimiter = arrMatches[ 1 ]; // Get the delimiter that was found.
				 
				// Check to see if the given delimiter has a length (is not the start of string) and if it matches field delimiter. If id does not, then we know that this delimiter is a row delimiter.
				if ( strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter) ) {
					arrData.push( [] ); // Since we have reached a new row of data, add an empty row to our data array.
				}
				 
				// Now that we have our delimiter out of the way, let's check to see which kind of value we captured (quoted or unquoted).
				if (arrMatches[ 2 ]){
					// We found a quoted value. When we capture this value, unescape any double quotes.
					var strMatchedValue = arrMatches[ 2 ].replace( new RegExp( "\"\"", "g" ), "\"" ); 
				} else {
					var strMatchedValue = arrMatches[ 3 ]; // We found a non-quoted value.
				}
				 
				// add value string to the data array.
				arrData[ arrData.length - 1 ].push( strMatchedValue );
			}
			return( arrData );
		}
	};
	
//===== Event-attaching / init plugin functions
	EVENTS = {
		startSWFHero : function(filename){
			
			if(swfobject.hasFlashPlayerVersion("10.0.0"))
			{
			    var flashvars = {
					'url_car01': 'products.html', 
					'url_car02': 'events.html' 
				};
				var params = { allowscriptaccess : 'always', wmode : 'transparent' };				
				var attributes = { 'id':'flash_content' };
				swfobject.embedSWF(filename, "flash_content", "1000", "762", "10.0.0", "theme/flash/expressInstall.swf", flashvars, params, attributes);
			}
			else
			{
			   $('#flash_content').show();
			}
		},
		//startProductCarousel : function(item_index){
		//	$('#product_carousel').product_carousel({ 'start_from':item_index });
		//},
		
		
		/**
		 * @param {Number} options.ani_speed		speed in ms fpr animation, default 400
		 * @param {Number} options.start_from		index to start from, default 0
		 * @param {Number} options.num_items		number of items to display, default 2
		*/
		startProductCarousel : function( options ){
			$('#product_carousel').product_carousel(options);
		},		
		startHeroRotator : function(){
			//init and start jquery cycle plugin
			$('#rotator').find('.visuallyhidden').removeClass('visuallyhidden');
			efx = ( $('html').is('.ie6, .ie7, .ie8') ) ? 'none' : 'fade';
			aniSpeed = ( $('html').is('.ie6, .ie7, .ie8') ) ? 0 : 600;
			
			var $rotator = $('#rotator').cycle({
				fx:efx,
				pager: 		'#rotator_nav span',
				slideExpr:	'> .rotator_content',
				cleartypeNoBg:true,
				speed:  	aniSpeed,
				timeout: 	8000
			});
		},
		createNavHover : function(){
			$( '#nav_items a', $( '.ie #nav' ) )
				.mouseenter( function(){ $(this).addClass('hover') } )
				.mouseleave( function(){ $(this).removeClass('hover') } );
		},
		createPrintButtons : function(){
			if( $('.print').length )
				$('.print').click(function(){ window.print(); return false; });
		},
		createBackButtons : function(){
			if( $('.back').length ) {
				$('.back').attr('title', 'Go back to the previous page');
				$('.back').live('click', function(){ history.go(-1); return false; });
			}
		}
	};

//===== PRODUCTS
	PRODUCTS = {
		initWait : 50,
		productJSON : new Object(),
		show_products : function( data ) {
			log( "show_products( data )", data );
			$('#tmplProductImages').tmpl( data ).appendTo( '.carousel_content' );
			$('#tmplProductNotes').tmpl( data ).appendTo( '.carousel_notes div' );
		},
		init : function( url, callback ) {
			/*
			url = url+'?'+Math.round( new Date().getTime() );
			$.getJSON(url, function(result){
				PRODUCTS.productJSON = eval(result);
				PRODUCTS.productJSON = PRODUCTS.productJSON.products
				PRODUCTS.show_products( PRODUCTS.productJSON )
				log('product data loaded from '+url, { prodJSON: PRODUCTS.productJSON });
			});
			*/
			return false;
		}
	};
	
// Source: http://stackoverflow.com/questions/497790
	DATE_UTIL = {
		date_to_str : function( d ){ 
			return ""+d.getFullYear()+"-"+(d.getMonth() + 1)+"-"+d.getDate(); 
		},
		str_to_date : function( str ) { 
			return new Date( CALENDAR.getDatePart( str, 'year' ), (CALENDAR.getDatePart( str, 'month') - 1 ), CALENDAR.getDatePart( str, 'day' )); 
		},
		convert:function( d ) {
	        // Converts the date in d to a date-object. The input can be:
	        //  a date object : returned without modification
	        //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
	        //  a number      : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp) 
	        //  a string      : Any format supported by the javascript engine, like "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
	        //  an object     : Interpreted as an object with year, month and date attributes.  **NOTE** month is 0-11.
	        return (
	            d.constructor === Date ? d :
	            d.constructor === Array ? new Date( d[0], ( parseInt( d[1],10 )-1 ), d[2] ) :
	            d.constructor === Number ? new Date( d ) :
	            d.constructor === String ? new Date( d ) :
	            typeof d === "object" ? new Date( d.year, d.month, d.date ) : NaN
	        );
		},
		compare:function( a, b ) {
	        //  -1 ( a < b ) or 0 ( a = b ) or 1 ( a > b )
	        return ( isFinite( a = DATE_UTIL.str_to_date(a).valueOf() ) && isFinite(b = DATE_UTIL.str_to_date(b).valueOf()) ? (a>b)-(a<b) : NaN );
		},
		inRange:function( d, start, end ) {
		    //    true  : if d is between start and end (inclusive)
		    //    false : if d is before start or after end
		   return (
		        isFinite( d=DATE_UTIL.str_to_date(d).valueOf() ) &&
		        isFinite( start=DATE_UTIL.str_to_date(start).valueOf() ) &&
		        isFinite( end=DATE_UTIL.str_to_date(end).valueOf() ) ? 
		        start <= d && d <= end : NaN );
		},
		create_datepicker:function() {
			$.datepicker.setDefaults( $.datepicker.regional[ '' ] );
			var dates = $( '#from_date, #to_date' ).datepicker({
				dateFormat: 'dd-mm-yy',
				dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
				firstDay : 1,
				minDate : new Date(),
				numberOfMonths: 2,
				beforeShow: function( input, inst ) { 
					$(input).addClass( 'focus' ).val('');
					$( '#from_date, #to_date' ).not( this ).removeClass( 'focus' );
					
					var monthName = CALENDAR.monthName[ inst.selectedMonth ];
					var $context = ( $('#filtered_events > *').length > 0 ) ? $( '#filtered_events' ) : $( '#events_list' );
					var $dates = $( ".startDate", $context );
					//TODO: date hightlights
					/*
					$dates.each( function() {
						var txt = $( this ).val();
						var d_day = parseInt( txt.substring( 0,2 ), 10 );
						var d_month = parseInt( txt.substring( 3,5 ), 10 );
						log( "datepicker:beforeShow()", this, { d_day:d_day, d_month:d_month, txt:txt, selectedMonth:inst.selectedMonth  });
						//$(".ui-datepicker-group-first .ui-datepicker-calendar td:contains('"+d_day+"')").css({color:'red'});
						if( d_month == inst.selectedMonth ){
							log( "datepicker:beforeShow()", this, { d_day:d_day, d_month:d_month, txt:txt, selectedMonth:inst.selectedMonth });
						}
					});
					*/
					
					log( "datepicker:beforeShow()", this, { dates_selector:".date:contains('"+monthName+"')", context:$context, dates:$dates, monthName:monthName, input:input, inst:inst });
				},
				onSelect: function( selectedDate ) {
					var option = (this.id == "from_date") ? "minDate" : "maxDate";
					var instance = $( this ).data( "datepicker" );
					var date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings );
					dates.not( this ).datepicker( "option", option, date );
					$( '#from_date, #to_date' ).removeClass('focus');
				},
				onClose: function( dateText, inst ) {
					$( '#from_date, #to_date' ).removeClass('focus');
					log( "datepicker:onClose()", this, { dateText:dateText, inst:inst } )
				}
			});
		}
	};
	
//===== Functions for formatting the calendar
	//assumes that dates are formatted as DD-MM-YYYY in the datasource
	var calendarData = [];
	var calendarArchive = [];
	CALENDAR = {
		initWait : 50,
		imageDir : 'images/events/',
		monthName : [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
		tmplGetImg : function( eventimg ){ 
			return ( eventimg.length > 0 || eventimg != "" ) ? this.imageDir+eventimg : this.imageDir+'event.gif'; 
		},
		tmplHasDate : function( eventdate ) { 
			return ( eventdate.split('-').length === 3 );
		},
		tmplTruncateTitle : function( str ) {
			return ( str.length <= 43 ) ? str : str.substring( 0, 45 ) + '...';
		},
		tmplFormatDate : function( startDate, endDate ) { 
			if ( startDate.split('-').length < 3 )
				return "tbc";
			
			var result = "";
			var s = this.getDatePart( startDate ); // format: [dd,mm,yyyy]
			var e = this.getDatePart( endDate ); // format: [dd,mm,yyyy]
			var sday = s[0];
			var eday = e[0];
			var smo = this.monthName[ parseInt( s[1], 10 ) - 1 ];
			var emo = this.monthName[ parseInt( s[1], 10 ) - 1 ];
			var eyr = e[2];
			var syr = s[2];
			
			if ( startDate === endDate ) {
				result = sday+' '+smo+' '+syr; //Event is only one day
			} else if ( smo !== emo && syr === eyr ) {
				result = sday+' '+smo+' - '+eday+' '+emo+' '+syr; // ex: 1 March - 12 April 2011
			} else if ( sday !== emo && syr !== eyr ) {
				result = sday+' '+smo+' '+syr+' - '+eday+' '+emo+' '+eyr; // ex: 1 December 2011 - 12 January 2012
			} else {
				result = sday+' - '+eday+' '+smo+' '+syr; // ex: 2 - 4 July 2012
			}
			return result;
		},
		getDatePart : function( eventdate, index ) {
			if ( index == null || index === '' ) {
				return eventdate.split('-'); 
			}
			switch( index ) {
				case 'day': index = 0; break;
				case 'month': index = 1; break;
				case 'year': index = 2; break;
				default: break;
			}
			return  eventdate.split( '-' )[ index ];
		},
		filter_events : function(){
			
			var filter_label = $('#event_filter').dropDown('selected_text');
			var filter_val = $('#event_filter').dropDown('selected');
			var $result = $('#events_list').clone().children();
			
			if( filter_val != 'event' )
				$result = $( 'input.sport[value="'+filter_val+'"]', $result ).parent();
			
			//Date-Filtering
			from = $('#from_date').val();
			to = $('#to_date').val();
			
			if( (from != "" && from.length > 0) && (to != "" && to.length > 0) ) {
				filter_label = filter_label+" from "+from+" to "+to;
				result_len = $result.length-1;
				$date_result = $('<div/>');
				
				$result.each( function(i, value){
					startDate = $(this).find('.startDate').val();
					if( startDate != '' && startDate.length > 0 ){
						if( DATE_UTIL.inRange( $(this).find('.startDate').val(), from, to ) == true )
							$date_result.append( $(this) );
					}
					if( i >= result_len)
						CALENDAR.show_filtered_results( filter_label, $date_result.children() );
				});
			} else {
				CALENDAR.show_filtered_results( filter_label, $result);
			}
			
			var events_selection = '';
			if (filter_val != 'event') {
				events_selection = filter_val.toUpperCase() + ' events ';
			}
			if (from != '' && from.length > 0) {
				if (events_selection == '') {
					events_selection = 'Events from ' + from + ' ';
				} else {
					events_selection += 'from ' + from + ' ';
				}
			}
			if (to != '' && to.length > 0) {
				if (events_selection == '') {
					events_selection = 'Events to ' + to + ' ';
				} else {
					events_selection += 'to ' + to + ' ';
				}
			}
			
			if (events_selection == '') events_selection = 'All Events';
			$('#events_selection').text(events_selection);
			
			return false;
		},
		show_filtered_results : function( x, $result ) {
			CALENDAR.update_filter_label( x );
			$('#events_list').hide();
			$('#filtered_events').empty().show().append( $result ).paginate();
		},
		update_filter_label : function( label ){ return $('#event_results_txt span').text( label ); },
		reset_date_search : function() {
			$( '#from_date, #to_date' ).val('');
			CALENDAR.filter_events();
			return false;
		},
		showEvents : function( o ) {
			var opt = { template:'#tmplEvents', container:'#events_list', init_paging:true };
			$.extend( opt, o );
			
			$(opt.container).empty();
			$(opt.template).tmpl( calendarData.events ).appendTo(opt.container);
			if(opt.init_paging)
				setTimeout( function(){ $(opt.container).paginate(); }, 50);
				
			log('showEvents()', this, { data:calendarData.events, template:opt.template, container:opt.container});
		},
		getData : function( url, showList ) {
			url = url+'?'+Math.round( new Date().getTime() );
			
			$.getJSON(url, function( result ) {
				
				log('data file loaded from '+url);
				results = eval(result);
				var tempArr = [];
				
				$.each(results.events, function(index, item){
					var itemDate = CALENDAR.getDatePart( item.startDate );
					var curDate = new Date();
					
					/*
					itemDate = new Date( itemDate[2], parseInt( itemDate[1],10 )-1, itemDate[0] );
					if( itemDate >= curDate || !CALENDAR.tmplHasDate (item.startDate ) ) {
						tempArr.push( item );
					} else {
						calendarArchive.push( item );
					}*/
					tempArr.push( item );
				});
				
				result.events = tempArr;
				calendarData =  result;
				
				if( showList == true ) {
					CALENDAR.showEvents({ data:calendarData.events });
					DATE_UTIL.create_datepicker();
					$('#event_search .submit').click( CALENDAR.filter_events );
					$('#event_search .reset').click( CALENDAR.reset_date_search );
					$('#event_filter').dropDown({ callback:CALENDAR.filter_events });
				}
			});
			
			return false;
		},
		init : function( url ){
			setTimeout( function(){ CALENDAR.getData( url, true ); }, CALENDAR.initWait );
		}
	};

//===== Custom Jquery plugins
(function($){
	
	//attach modal triggers
	$.fn.createModalTrigger = function(opt){
		var settings = {
			borderSize:'0',
		 	eventType:'click',
			height:500,
			overlayOpacity:'70',
			width:500,
			windowBGColor:'#000',
			windowSource:'ajax'
		}
		
		//opt = $.extend( settings, opt );
		opt = $.extend( {}, settings, opt );
	
		
		return this.each( function() {
			$(this).openDOMWindow(opt);
		});
	};
	
	//SIMPLE CUSTOM DROPDOWN
	$.fn.dropDown = function( method ) {
		
		var settings = { 
			aniSpeed:150,
			callback:function(){}
		};
		
		var dropDown_methods = {
			init : function( opt ) {
				opt = $.extend( settings, opt );
				
				return this.each( function(){
					var filter_wrap = this;
					
					$( '.selected', filter_wrap ).first().data( 'selected', $( '.options a', filter_wrap ).first().attr( 'rel' ) );
					
					var sports = jsonPath(calendarData, "$..sport");
					
					$.each( sports, function(i, sport) {
						option = $('<a rel="'+sport.toLowerCase()+'">'+sport+'</a>');
						if( $('a', filter_wrap).filter(':contains('+sport+')').length <= 0 ){
							$( '.options', filter_wrap ).append( option );
						}
					});
					
					$( '.selected', this ).click( function(e) {
						e.preventDefault();
						$( '.options', filter_wrap ).slideToggle( opt.aniSpeed );
					});
					
					$( 'a', this ).click( function(e){
						e.preventDefault();
						$( this ).parent()
							.slideUp( opt.aniSpeed )
							.siblings( '.selected' )
								.text( $(this).text() ).data( 'selected', $(this).attr('rel') );
						// call the callback and apply the scope:
    					opt.callback.call( filter_wrap );
						
					});
				});
			},
			selected : function() { return $( this ).children( '.selected' ).data( 'selected' ); },
			selected_text : function() { return $( this ).children( '.selected' ).text();  }
		};
		
		// Method calling logic
		if ( dropDown_methods[method] )
			return dropDown_methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
		else if ( typeof method === 'object' || ! method )
			return dropDown_methods.init.apply( this, arguments );
		else
			log( 'Method ' +  method + ' does not exist on jQuery.dropDown' );
	};
	
	//SIMPLE PAGINATION
	$.fn.paginate = function( opt ) {
		var settings = {
			visItems:5,
			startPage:1,
			controls:'pagination',
		 	next_trig:'next',
		 	prev_trig:'prev',
		 	showAbove:true,
		 	showBelow:true
		};
		
		opt = $.extend( settings, opt );
		return this.each( function() {
			var $wrap = $(this).data({ paginate_init:true });
			var $items = $(this).children();
			var num_items = $items.length;
			var num_pages = parseInt( Math.ceil( num_items / opt.visItems ), 10 );
			var cur_page = opt.startPage;
			var $controlContainer = $('<div class="'+opt.controls+' clearfix"/>');
			var $pages_wrap = $('<span/>').addClass('pages');
			var $prev = $('<a href="#" class="'+opt.prev_trig+'">Previous</a>');
			var $next = $('<a href="#" class="'+opt.next_trig+'">Next</a>');
			var $goto_first = $('<a href="#" class="goto goto_first" title="Go to first page">1...</a>');
			var $goto_last = $('<a href="#" class="goto goto_last" title="Go to last page">...'+num_pages+'</a>');
			
			var init = function() {
				if( $('.'+opt.controls).length )
					$('.'+opt.controls).remove();
				
				if( num_pages > 0 ) {
					for(i=0; i<num_pages; i++)
						$pages_wrap.append('<a href="#">'+(i+1)+'</a>');
						
					if(num_pages > 5)
						$controlContainer.append( $prev, $goto_first, $pages_wrap, $goto_last, $next );
					else if(num_pages > 1)
						$controlContainer.append( $prev, $pages_wrap, $next );
					else
						$controlContainer.append( $pages_wrap );
					
					if( opt.showAbove == true )
						$wrap.before( $controlContainer.clone() );
					if( opt.showBelow == true )
						$wrap.after( $controlContainer.clone() );
					
					attachTriggers();
					showItems();
					updateButtons();
					
				} else {
					
					$controlContainer.empty().append( 'No events available' );
					$wrap.before( $controlContainer.clone() );
					
				}
			}
			
			var showItems = function() {
				lower = (cur_page <= 1) ? 0 : ((cur_page-1) * opt.visItems - 1);
				upper = (cur_page <= 1) ? lower+opt.visItems : lower+opt.visItems+1;
				
				$items.hide().removeClass('first last');
				
				$result = $items
					.filter(':lt('+( upper )+')').filter(':gt('+( lower )+')').show()
					.first().addClass('first').end()
					.last().addClass('last').end();
				
				if( (cur_page <= 1) ) $items.removeClass('first').filter(':eq(0)').show().addClass('first');
				
			}
			
			var updateButtons = function() {
				var on_last_page = false;
				//Update prev/next buttons
				$('.'+opt.next_trig+', .'+opt.prev_trig).removeClass('disabled disabled1 disabled2');
				if( cur_page <= 1 )
					$( '.'+opt.prev_trig ).addClass( 'disabled1 disabled' );
				if( cur_page >= num_pages )
					$( '.'+opt.next_trig ).addClass( 'disabled2 disabled' );
				
				//Update first/last page buttons
				$( '.'+opt.controls+' .goto' ).show();
				
				if(cur_page <= 5) 
					$( '.'+opt.controls+' .goto_first' ).hide();
					
				if( Math.floor( num_pages/5 ) * 5 < cur_page ) {
					$('.'+opt.controls+' .goto_last').hide();
					$('.'+opt.controls+' .pages').width( Math.abs( Math.floor( num_pages % 5 ) ) * 27 + 1 );
					on_last_page = true;
				} else {
					$('.'+opt.controls+' .pages').width( 135 );
				}
				
				//Update page indicator
				setTimeout( function() {
					
					$('.'+opt.controls+' .pages').each( function() {
						$(this).find( 'a' ).removeClass('current').filter(':eq('+(cur_page-1)+')').addClass('current');
						var scroll_to_item = ( on_last_page ) ? 'a:last-child' : '.current';
						$(this).scrollTo(scroll_to_item, { axes:'x', duration:200 });
					});
					
				}, 100);
			
			}
			
			var attachTriggers = function() {
				$('.'+opt.controls+' a').click( function( e ) {
					e.preventDefault();
					temp = cur_page;
					
					if( $(this).is('.'+opt.next_trig+', .'+opt.prev_trig) )
						temp = ( $(this).hasClass(opt.next_trig) ) ? temp+1 : temp-1;
					else if( $(this).is('.goto') )
						temp = ( $(this).hasClass('goto_first') ) ? 1 : num_pages;
					else
						temp = $(this).index()+1;
					
					if( (temp != cur_page) && (temp >= 1 && temp <= num_pages) ){
						cur_page = temp;
						updateButtons();
						showItems();
					}
				});
			}
			init();
		});
	};
	
	//===== PRODUCT CAROUSEL ======
	$.fn.product_carousel = function(options){ 
		var settings = {
		  'ani_speed':		400,
		  'start_from':		0,
		  'num_items':		2
		};
		var opt = $.extend( settings, options );
		
		return this.each(function() {
			var cur_index = opt.start_from;
			var num_items = opt.num_items;
			var $car_container = $(this);
			var $car_content = $(this).children('.carousel_content');
			var $car_control = $(this).children('.carousel_control');
			var $car_control_titles = $car_control.children('.product_titles');
			var $car_labels = $(this).children('.carousel_labels');
			var $car_notes = $(this).children('.carousel_notes');
			var $car_copy	= $('.carousel_copy');
			var $car_hilite = $('#product_highlight');
			
			/*** HELPERS ***/
			function set_cur_index(x){ cur_index=x; display_update(); return false; }
			function get_next_index(){ return ( (cur_index+1) > (num_items-1) ) ? 0 : (cur_index+1); }
			function get_prev_index(){ return ( (cur_index-1) < 0 ) ? num_items-1 : (cur_index-1); }
			function init_notes(){
				$car_copy.children('.text').not(':eq('+cur_index+')').removeClass('current');
				$('.note').each( function(){ 
					$(this).data('default_css', { w:$(this).width(), h:$(this).height() });
				});
			}
			function update_section_subnav(){
				/*
				prod_class = "";
				switch( cur_index ){
					case 2: prod_class="lemon"; break;
					case 1: prod_class="orange"; break;
					default: prod_class="blackcurrant"; break;
				}
				*/
				var current_prod = $car_content.find('li.current div.product_img img').attr('class');
				var current_prod = $car_content.find('li.current div.product_img img').attr('class');
				//console.log('current prod: '+current_prod);
				setTimeout( function(){
					$('body').removeClass('blackcurrant lemon orange cannister').addClass( current_prod );
				}, opt.ani_speed );
				return false;
			}
			
			function update_prod_notes(){
				if( $( 'html' ).hasClass( 'ie' ) ) {
					$car_hilite.hide();
					$car_notes.hide();
					setTimeout( function(){ $car_hilite.show() }, opt.ani_speed*2 );
					setTimeout( function(){ $car_notes.show() }, opt.ani_speed*3 );
				} else {
					$car_hilite.stop().fadeTo( opt.ani_speed/2, 0 );
					setTimeout( function() { 
						$car_notes.stop().fadeTo( opt.ani_speed/2, 1 );
					}, opt.ani_speed * 2 );
					
					$car_notes.stop().fadeTo( opt.ani_speed/3, 0 );
					setTimeout( function() {
						$car_hilite.stop().fadeTo( opt.ani_speed, 1 );
					}, opt.ani_speed );
				}
			}
			
			function display_update() {
				$car_content.children().removeClass('current next prev');
				var $cur = $car_content.children(':eq('+cur_index+')').addClass('current');
				var $next = $car_content.children(':eq('+get_next_index()+')').addClass('next');
				var $prev = $car_content.children(':eq('+get_prev_index()+')').addClass('prev');
				//update product notes
				update_prod_notes();
				//animate the products (to emulate a cylindrical carousel type change with opacity set in the css)
				$prev.find('.product_img img').stop().animate({ 'marginLeft': 0, 'marginTop':100, 'width': 72 },{ queue:false, duration:opt.ani_speed });
				$next.find('.product_img img').stop().animate({ 'marginLeft': 320, 'marginTop':100, 'width': 72 },{ queue:false, duration:opt.ani_speed });
				
				//if ie < 9, do a fadeTo so animation works
				if (navigator.userAgent.indexOf("MSIE 7.0") > -1 || navigator.userAgent.indexOf("MSIE 8.0") > -1) {
					$cur.find('.product_img img').stop().animate({ 'marginLeft': 125, 'marginTop':0, 'width': 140 },{ queue:false, duration:opt.ani_speed }).fadeTo( opt.ani_speed, 1, function(){
						if( $( 'html' ).hasClass( 'ie' ) ) {
							$cur.find('.product_img img').stop().removeAttr('style').css({ 'marginLeft': 125, 'marginTop':0, 'width': 140 });
						}			
					});
				} else {
					//if not ie < 9, do a show so opacity of background images is preserved (does not work when opacity is set to value other than 1 when doing fadeTo!!)
					$cur.find('.product_img img').stop().animate({ 'marginLeft': 125, 'marginTop':0, 'width': 140 },{ queue:false, duration:opt.ani_speed }).show( opt.ani_speed, 1, function(){
						if( $( 'html' ).hasClass( 'ie' ) ) {
							$cur.find('.product_img img').stop().removeAttr('style').css({ 'marginLeft': 125, 'marginTop':0, 'width': 140 });
						}			
					});
				}
				//update product copy
				$car_copy.children('.text').removeClass('current');
				$car_copy.children(':eq('+cur_index+')').addClass('current');
				//update product section sub-nav (found under the site's main nav)
				update_section_subnav();
			}
			
			/*** INIT STYLING ***/
			init_notes();
			setTimeout( display_update, 500);
			
			/*** EVENTS ***/
			/*$('#subnav .products a').click( function(e){
				e.preventDefault();
				var cl = $(this).attr('class');
				var index = 0;
				$car_content.children('li').each(function(i) {
					if ($(this).find('div.product_img img').hasClass(cl)) {
						index = i;
					}
				});
				set_cur_index(index);
			});*/
			$car_notes.find('.note').click( function(){ return false; });
			$car_control.children('a').click( function(e){
				e.preventDefault();
				if( $(this).hasClass('next') ) cur_index = ( cur_index >= ( num_items-1 ) ) ? 0 : ( cur_index+1 );
				else cur_index = (  cur_index <= 0 ) ? ( num_items-1 ) : ( cur_index-1 );
				display_update();
			});
			$car_copy.find('.ccontent_nav a').click( function(e){
				e.preventDefault();
				//$(this).parent().parent().find('a').removeClass('cur');
				$(this).parents('ul').find('li').removeClass('cur');
				//$(this).addClass('cur');
				$(this).parent().addClass('cur');
				$(this).parent().parent().siblings('div').addClass('hidden').filter( $(this).attr('href') ).removeClass('hidden');
			})
			
			$car_copy.find('.text > .clearfix').each( function(i){
				$(this).find('> div').not(':first').addClass('hidden');
			})
			
			//determine which img is clicked on by mouse coordinates
			//done this way because the current div overlays the prev/next images so the click event never fires for the prev/next image
			$car_content.click(function(e) {
				var off = $(this).offset();
				var ol = off.left;
				var ot = off.top;
				
				var x = e.pageX;
				var y = e.pageY;
				
				
				var prev_min_x = ol;
				var prev_max_x = ol + 80;
				
				var next_min_x = ol + 310;
				var next_max_x = ol + 500;
				
				var min_y = ot + 100;
				var max_y = ot + 400;
				
				//console.log('left: '+l+', top: '+t+', offset left: '+ol+', offset top: '+ot+', x: '+x+', y: '+y);
				//console.log('prev min x: '+prev_min_x+', prev max x: '+prev_max_x+', next min x: '+next_min_x+', next max x: '+next_max_x+', min y: '+min_y+', max y: '+max_y+', x: '+x+', y: '+y);
				var dir = '';
				if (min_y <= y && y <= max_y) {
					if (prev_min_x <= x && x <= prev_max_x) {
						dir = 'prev';
					}
					if (next_min_x <= x && x <= next_max_x) {
						dir = 'next';
					}
				}
				
				if( dir == 'next' ) cur_index = ( cur_index >= ( num_items-1 ) ) ? 0 : ( cur_index+1 );
				else if (dir == 'prev') cur_index = (  cur_index <= 0 ) ? ( num_items-1 ) : ( cur_index-1 );
				
				if (dir == 'next' || dir == 'prev') display_update();
			});
		});
	};

})(this.jQuery);

log('FILE_LOADED: plugins.js');
