/* Doritos Feature Slide - Main Javascript
=============================================================================================*/

DORITOS.feature_slide = 
{
	page: 1,
	itm_length: 0,
	itm_width: 0,
	margin: 0,
	timer_instance: null,
	timer: 0,
	slider: null,
	page_btns: null,
	
	init: function( bucket, timer )
	{
		var that = DORITOS.feature_slide;
		
		var images = $(bucket+' .itm');
		
		that.itm_length = images.length;
		that.itm_width = $(images[0]).width();
		that.timer = timer;
		that.slider = $(bucket+' .slider');
		that.page_btns = $(bucket+' .pager a');
		
		that.page_btns.click( function(){ return DORITOS.feature_slide.click( this ); } );
		$(that.page_btns[that.page-1]).parent().addClass('on');
		
		that.slider.width( (that.itm_width * that.itm_length) + 100 );
		
		DORITOS.feature_slide.animate();
	},
	
	animate: function()
	{
		var that = DORITOS.feature_slide;
		
		clearInterval(that.timer_instance);
		
		that.timer_instance = window.setInterval( function()
		{
			that.page++;

			that.page_btns.parent().removeClass('on');
			
			if( that.page == that.itm_length+1 )
			{
				that.page = 1;
				that.margin = 0;
				$(that.page_btns[0]).parent().addClass('on');
				that.slider.animate( { marginLeft: that.margin+'px' }, 'slow' );
			}
			else
			{
				that.margin = that.itm_width * (that.page-1);
				$(that.page_btns[that.page-1]).parent().addClass('on');
				that.slider.animate( { marginLeft: '-'+that.margin+'px' }, 1500 );
			}
		}
		, that.timer );
	},
	
	click: function( btn )
	{
		var that = DORITOS.feature_slide;
		var links = $(btn).parent().parent().find('a');
		
		that.page = links.index( btn )+1;
		
		links.parent().removeClass('on');
		$(btn).parent().addClass('on');
		
		that.margin = that.itm_width * (that.page-1);
		
		clearInterval(that.timer_instance);
		
		that.slider.animate( 
			{ marginLeft: '-'+that.margin+'px' }, 
			1500, 
			function(){ window.setTimeout( function(){ DORITOS.feature_slide.animate(); }, that.timer); }
		);
		
		return false;
	}
}


DORITOS.feature_fade = 
{
	page: 1,
	images: null,
	itm_length: 0,
	timer_instance: null,
	timer: 0,
	page_btns: null,
	current: null,
	bottom: null,
	
	init: function( bucket, timer )
	{
		var that = DORITOS.feature_fade;
		var i = 0;
		
		that.images = $(bucket+' .itm');
		that.itm_length = that.images.length;
		that.timer = timer;
		
		that.images.css( { "position": 'absolute', 'z-index': 0 } );
		
		that.current = that.images[0]; 
		that.bottom = that.images[1]; 
		
		$(that.current).css('z-index', 10); 
		$(that.bottom).css('z-index', 5); 
			
		that.page_btns = $(bucket+' .pager a');
		
		that.page_btns.click( function(){ return DORITOS.feature_fade.click( this ); } );
		$(that.page_btns[that.page-1]).parent().addClass('on');
		
		DORITOS.feature_fade.animate();
	},
	
	animate: function()
	{
		var that = DORITOS.feature_fade;
		
		clearInterval(that.timer_instance);
		
		that.timer_instance = window.setInterval( function()
		{
			that.page++;
			
			that.page_btns.parent().removeClass('on');
			
			
			$(that.images[that.current]).fadeOut('slow', function()
			{
				//$(that.images).css('z-index', 0);
				//$(that.images[that.current]).css('z-index', 10);
				//$(that.images[that.bottom]).css('z-index', 5);
			});
						
			if( that.page == that.itm_length+1 )
			{
				that.page = 1;
				$(that.page_btns[0]).parent().addClass('on');
				
				that.current = that.images[that.itm_length-1];
				that.bottom = that.images[0];
			}
			else
			{
				that.margin = that.itm_width * (that.page-1);
				$(that.page_btns[that.page-1]).parent().addClass('on');
				
				that.current = that.images[that.page-1];
				that.bottom = that.images[that.page];
			}
			
		}
		, that.timer );
	},
	
	click: function( btn )
	{
		var that = DORITOS.feature_fade;
		var links = $(btn).parent().parent().find('a');
		
		that.page = links.index( btn )+1;
		
		links.parent().removeClass('on');
		$(btn).parent().addClass('on');
		
		that.margin = that.itm_width * (that.page-1);
		
		clearInterval(that.timer_instance);
		
		that.slider.animate( 
			{ marginLeft: '-'+that.margin+'px' }, 
			1500, 
			function(){ window.setTimeout( function(){ DORITOS.feature_fade.animate(); }, that.timer); }
		);
		
		return false;
	}
}


