//------------------------------------
//	HOME.JS
//	Author: 	Engage Interactive
//	Requires:	global.js
//				jquery.js
//------------------------------------

$(function(){
//BEGIN jQuery

	// FEATURED PANEL

	totalPromo = $('#featured div.featured_content').size();
	width = $('#featured div.featured_content').width() + 6;
	xPos = 0;
	currentPromo = 1;
	moose = true;
	autoMoose = true;
	promoDelay = 6000;
	easing = 'easeInOutExpo';
	speed = 800;
	
	if(totalPromo>1){
	
		$('#featured').prepend('<ul></ul>');
		
		for(i=1;i<=totalPromo;i++){
			$('#featured ul').append('<li><a href="#" title="Show slide ' + i + '">' + i + '</a></li>');
			if(i==totalPromo){
				$('#featured ul').show().children('li:first').addClass('on');
			}
		}
		
		
		$('#featured ul li a').click(function(){
			if(moose == true){
				$(this).parent('li').addClass('on').siblings('li').removeClass('on');
				currentPromo = $(this).text();
				
				xPos = width - (currentPromo * width);
				
				slideTo(xPos);
			}
			return false;
		});
		
		function infiniteLoop(){
			
			moose = false;
			
			totalX = width - (width * totalPromo);
			
			$('#featured_slider').append('<div class="featured_content">'+$('#featured_slider div.featured_content:first').html()+'</div>').animate({left:totalX-width},speed,easing,function(){
				$('#featured_slider').css({left:0});
				$('#featured_slider div.featured_content:last').remove();
				moose = true;
			});
			
			$('#featured ul li:first').addClass('on').siblings('li').removeClass('on');
			currentPromo = 1;
			xPos = 0;
		}
	
		
		$('#featured').hover(function(){
			$.clearTimer(promoTimer);
		},function(){
			autoMagical();
		});
		
		var promoTimer = {};
		
		function autoMagical(){
			promoTimer = $.timer(promoDelay,function(){
				if(currentPromo == totalPromo){
					infiniteLoop()
					//$('#featured ul li:first a').click();
				}else{
					$('#featured ul li.on').next('li').children('a').click();
				}
				$.clearTimer(promoTimer);
				autoMagical();
			});
		}
		
		autoMagical();
	
		
		function slideTo(newX){
			moose = false;
			$('#featured_slider').animate({left:newX},speed,easing,function(){
				moose = true;
			})
		}
	
	}

//END jQuery
});

/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};