//carousel


//Clone what we've got
function cloner(){
	// Pad before
	var smfirst = $('#features_list li:first');
	var smfirstclass = $(smfirst).attr('class');
	$('#features_list_container li').each(function(){$(this).clone(true).insertBefore(smfirst);})
	lgfirst = $('#feature_panes li:first');
	$('#features_panes_container li').each(function(){$(this).clone(true).insertBefore(lgfirst);})

	// Advance to the ones we need
	$('#features_list_wrapper').css('left', (liL * lisW * limb * -1) - (offset * lisW)  + 'px');
	$('#feature_panes_wrapper').css('left', (liL * lilW * limb * -1) + 'px');
	
	// Uncomment if you want them to match
	//$('#feature_panes_wrapper').css('left', (liL * lilW * limb * -1) - (offset * lisW) + 'px');
	
	
	//Account for new length
	t = $('#features_list_container li').length
	
	// Pad after
	$('#features_list_container li:gt(' + ((t/2)-1) + ')').each(function(){
		smlast = $('#features_list li:last');
		$(this).clone(true).insertAfter(smlast);
	})
	$('#features_panes_container li:gt(' + ((t/2)-1) + ')').each(function(){
		lglast = $('#feature_panes li:last');
		$(this).clone(true).insertAfter(lglast);
	})
	
	// We hide the first item so it wont show up when the page loads. After we are done initalizing - we show it.
	$('.' + smfirstclass).css('display', 'block');
	
	//Update the li length
	liL		= $('#features_list_container li').length;
}

function adv(){
	inc = inc + 1;
	if(inc >= 1 && inc <= ((liL/3)*2)){
		move(inc, -1);
	}
}
function rev(){
	inc = inc - 1;
	if(inc >= 1 && inc <= ((liL/3)*2)){
		move(inc, 1);
	}
}


function move(page, d){
	var dir = d;
	
	//Depending on Direction - calculate how much to the Small Feature Bar
	if(d == 1){
		swp = ((page + limb + offset) * lisW * dir);
	}else{
		swp = (page * lisW * dir);
	}
	
	//Depending on Direction - calculate how much to the Large Feature Bar
	if(d == 1){
		lwp = ((page + offset) * lilW * dir);
	}else{
		lwp = ((page - offset) * lilW * dir);
	}
	
	
	if(dir == -1){
		// Amts = (LI Width (large or Small) * direction * li Move By) + large or small Wrapper Position
		var samt = (lisW * dir * limb) + swp;
		var lamt = (lilW * dir * limb) + lwp;
	}else{
		var samt = (lisW * dir * limb) - swp;
		var lamt = (lilW * dir * limb) - lwp;
	}
	
	
	$('#features_list_wrapper').animate({ left : samt  }, 500, function(){
		//FWD
		if(dir == -1){
			// At the 3/4 mark we are going to adjust the CSS
			if(page == ((liL/3)*2)){
				
				// Calculate the amount we need to move (adjust for offset)
				var amt = (-1 * (liL/3) * lisW) - (offset * lisW);
				$('#features_list_wrapper').css({ left : amt  });
				
				// Set Incrementer back to the 1/3 mark (or the first panel that is shown onload)
				inc = (liL / 3);
			}
		}
		//REV
		if(dir == 1){	
			// At the first item we are going to adjust the CSS + be sure to include the offset
			if(page == 1){
				
				// Calculate the amount we need to move (adjust for offset)
				var amt = (-1 * ((liL/3)+offset) * lisW) - (offset * lisW);
				$('#features_list_wrapper').css({ left : amt  });
				
				// Set Incrementer back to the 1/3 mark (or the first panel that is shown onload + 1 because of the offset)
				inc = ((liL / 3) + offset); 
			}
		}
	});

	
	
	$('#feature_panes_wrapper').animate({ left : lamt  }, 500, function(){
		//FWD
		if(dir == -1){
			// At the 3/4 mark we are going to adjust the CSS
			if(page == ((liL/3)*2)){
				
				// Calculate the amount we need to move
				var amt = (-1 * (liL/3) * lilW);
				$('#feature_panes_wrapper').css({ left : amt  });
				
				// Set Incrementer back to the 1/3 mark (or the first panel that is shown onload)
				inc = (liL / 3);
			}
		}
		//REV
		if(dir == 1){
			if(page == 1){
				
				// At the first item we are going to adjust the CSS + be sure to include the offset
				var amt = (-1 * ((liL/3)+offset) * lilW);
				$('#feature_panes_wrapper').css({ left : amt  });
				
				// Set Incrementer back to the 1/3 mark (or the first panel that is shown onload + 1 because of the offset)
				inc = ((liL / 3) + offset); 
			}
		}
	});
	
}

function init(){
	cloner();
	
	jQuery('#feature_arrows li.fwd a').click(function(){
		 animate = false;
		 adv();
		 return false;
	})

	jQuery('#feature_arrows li.rev a').click(function(){
		 animate = false;
		 rev();
		 return false;
	})

}	

$(document).ready(function () {
  
	liL		= $('#features_list_container li').length
	lisW 	= 320; 				// make sure to add margins & padding width = 322px + 1px right-margin
	lilW 	= 980; 				// make sure to add margins & padding width = 322px + 1px right-margin
	limb 	= 1; 				// li to move by
	offset 	= 1;				// Featurette offest (moves X items to the left)
	animate = true;	
	inc 	= liL;				// Set incrementer at the position of the first panel shown onload
	
	
	$(document).everyTime(10000, "features", function(i) {
		if (animate == true) {adv();}
	});

	init();
  
});


//Timer
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		guid: 1,
		global: {},
		regex: /^([0-9]+)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseInt(result[1], 10);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			if (!element.$timers) 
				element.$timers = {};
			
			if (!element.$timers[label])
				element.$timers[label] = {};
			
			fn.$timerID = fn.$timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.$timerID = fn.$timerID;
			
			if (!element.$timers[label][fn.$timerID]) 
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
			
			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = element.$timers, ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.$timerID ) {
							window.clearInterval(timers[label][fn.$timerID]);
							delete timers[label][fn.$timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					element.$timers = null;
			}
		}
	}
});

if (jQuery.browser.msie)
	jQuery(window).one("unload", function() {
		var global = jQuery.timer.global;
		for ( var label in global ) {
			var els = global[label], i = els.length;
			while ( --i )
				jQuery.timer.remove(els[i], label);
		}
	});



