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


$(function(){
//BEGIN jQuery

	//SLIDER THING

	var slideTimer = {};

	$('#businesses').hover(function(){
		iHover = false;
		$.clearTimer(slideTimer);
		$(this).stop([]).animate({width:572},600,'easeInOutExpo',function(){
			slideTimer = $.timer(400,function(){
				$('#businesses').children('.content').stop([]).animate({bottom:0},500,'easeInOutExpo');
			});
		});
	},function(){
		$.clearTimer(slideTimer);
		$(this).children('.content').stop([]).animate({bottom:-212},500,'easeInOutExpo');
		$(this).stop([]).animate({width:309},1000,'easeInOutExpo');
	});

	$('#individuals, .corners.individuals').hover(function(){
		$.clearTimer(slideTimer);
		$('#businesses').stop([]).animate({width:46},600,'easeInOutExpo',function(){
			slideTimer = $.timer(400,function(){
				$('#individuals').children('.content').stop([]).animate({bottom:0},500,'easeInOutExpo');
			});
		});
	},function(){
		$.clearTimer(slideTimer);
		$('#individuals').children('.content').stop([]).animate({bottom:-212},500,'easeInOutExpo');
		$('#businesses').stop([]).animate({width:309},1000,'easeInOutExpo');
	});
	
	
	// FEATURED PANEL
	
	//PROMO SLIDER

	totalPromo = $('#featured_slider div.featured_content').size();
	width = $('#featured').width();
	xPos = 0;
	currentPromo = 1;
	moose = true;
	autoMoose = true;
	promoDelay = 6000;
	easing = 'easeInOutExpo';
	speed = 800;
	
	$('#featured_nav .next a').click(function(){
		if(moose==true && totalPromo>currentPromo){
			xPos = xPos - width;
			currentPromo = currentPromo + 1;
			slideTo(xPos);
		}else if(moose==true){
			currentPromo = 1;
			infiniteLoop('right');
		}
		return false;
	});
	
	$('#featured_nav .prev a').click(function(){
		if(moose==true && currentPromo>1){
			xPos = xPos + width;
			currentPromo = currentPromo - 1;
			slideTo(xPos);
		}else if(moose==true){
			currentPromo = totalPromo;
			infiniteLoop('left');
		}
		return false;
	});
	
	function infiniteLoop(dir){
		
		moose = false;
		
		totalX = width - (width * totalPromo);
		
		if(dir == 'left'){
			$('#featured_slider').prepend('<div class="featured_content">' + $('#featured_slider div.featured_content:last').html() + '</div>').css({left:-width}).animate({left:0},speed,easing,function(){
				$('#featured_slider').css({left:totalX});
				$('#featured_slider div.featured_content:first').remove();
				moose = true;
			});
			xPos = totalX;
		}else if(dir == 'right'){
			$('#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;
			});
			xPos = 0;
		}
	}
	
	$('#featured').hover(function(){
		$.clearTimer(promoTimer);
	},function(){
		autoMagical();
	});
	
	var promoTimer = {};
	
	function autoMagical(){
		promoTimer = $.timer(promoDelay,function(){
			$('#featured_nav .next 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;
};