//------------------------------------
//	HOMEPAGE.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.4.2
//				chez.js
//				Cufon
//				Trade Gothic font
//	Version:	0.1
//------------------------------------

////////////////////////////
// CUFON

Cufon.replace('#food_promo h2, #food_promo p, #food_promo article a',{
	textShadow: 'rgba(255,255,255,.4) 1px 1px'
});

Cufon.replace('#promo .standard *:not(.terms)',{
	textShadow: '#333 1px 1px'
});

Cufon.replace('#promo .cufon',{
	hover: true
});

Cufon.replace('#promo ul li a');


////////////////////////////
// BEGIN JQUERY

$(function(){

	// Ban the moose. BAD MOOSE!
	var mooseBan = false;


	////////////////////////////
	// PROMO SLIDER
	
	function promoSlider(){
		
		// Variables
		var $promo = $('#promo');
		var $text = $('#promo_text div');
		var $img = $('#image_slider');
		var total = $text.find('article').size() - 1;
		var current = 0;
		var s = 800;
		var e = 'easeInOutExpo';
		var delay = 6000;
		var dtimer = {};
		var i = 0;
		
		for ( i = 0; i <= total; i++ ){

			$('<a/>',{
			  	'class': 'image',
				'href': $text.find('article:eq(' + i + ') a').attr('href'),
			  	'css': {
					'background': 'url(' + $text.find('article:eq(' + i + ')').data('image') + ')',
					'opacity': 0
				}
			}).appendTo($img).animate({opacity:1});
			
			if( i == total ){
				$img.siblings('span').remove();
			}
			
		}
		
		// Nav click
		$('#promo ul li a').click(function(){

			if( mooseBan == false ){
				
				mooseBan = true;

				var dir = $(this).parent().attr('class');
				
				if( dir == 'next' ){
					current++;
					if( current > total ) current = 0;
				}else if( dir == 'prev' ){
					current--;
					if( current == -1 ) current = total;
				}
				goTo(current);
			
			}
			
			return false;
		});
		
		var goTo = function(num){
			
			var x = '-' + ( ( num ) * 618 ) + 'px';
			var y = '-' + ( ( num ) * 253 ) + 'px';
			
			$img.animate({left:x},s + 200,e,function(){
				mooseBan = false;
			});
			
			$text.animate({top:y},s,e,function(){
				mooseBan = false;
			});
		}
		
		$promo.mouseenter(function(){
			$.clearTimer(dtimer);
		});
		$promo.mouseleave(function(){
			slideshow();
		});

		var slideshow = function(){
			dtimer = $.timer(delay,function(){
				$.clearTimer(dtimer);
				$promo.find('.next a').click();
				slideshow();
			});
		}
		
		slideshow();
		
	}
	
	promoSlider();
	
	
	////////////////////////////
	// FOOD SLIDER
	
	function foodSlider(){
		
		// Variables
		var $fp = $('#food_promo');
		var total = $fp.find('article').size();
		var current = 1;
		var s = 600;
		var e = 'easeInOutExpo';
		var delay = 4000;
		var dtimer = {};
		
		// Nav click
		$('#food_promo ul li a').click(function(){

			if( mooseBan == false ){
				
				mooseBan = true;

				var dir = $(this).parent().attr('class');
				
				if( dir == 'next' ){
					current++;
					if( current > total ) current = 1;
				}else if( dir == 'prev' ){
					current--;
					if( current == 0 ) current = total;
				}
				
				goTo(current,dir);
			
			}
			
			return false;
		});
		
		var goTo = function(num,dir){
			
			$('<span/>',{
				'class':	'shadow',
				css:		{
					opacity: 0
				}
			}).appendTo($fp).animate({opacity:1},100).delay(400).animate({opacity:0},100,function(){
				$(this).remove();
			});
			
			if( dir == 'next' ){
				var a = -452;
				var b = 452;
			}else if( dir == 'prev' ){
				var a = 452;
				var b = -452;
			}
			
			$fp.find('article.active').animate({left:a}, s, e, function(){
				$(this).removeAttr('style').removeAttr('class');
			});
			$fp.find('article:eq(' + ( num - 1 ) + ')').addClass('active').css({left:b}).animate({left:0}, s, e, function(){
				mooseBan = false;
			});
		}
		
		/*$fp.mouseenter(function(){
			$.clearTimer(dtimer);
		});
		$fp.mouseleave(function(){
			slideshow();
		});

		var slideshow = function(){
			dtimer = $.timer(delay,function(){
				$.clearTimer(dtimer);
				$fp.find('.prev a').click();
				slideshow();
			});
		}
		
		slideshow();*/
	}
	
	foodSlider();

});
