jQuery('document').ready(function(){
	jQuery('[product_carousel=true]').product_carousel()[0].init();
});
function zIndexShiftHelper (dir){
	jQuery('[product_carousel=true]')[0].zIndexShift(dir);
}
jQuery.fn.product_carousel = function(){
	return this.each(function(){
		
		this.init = function(){
			this.autoplay = true;
			this.slides = jQuery(this).children('.container').children('.slide');
			this.current_index = 0;
			this.next_index = 0;
			this.prev_index = 0;
			
			this.prev2_index = 0;
			this.next2_index = 0;
			
			this.createAlphaWall();
			this.setIndexPositions(this.current_index);	
			this.locked = false;
			this.setUpControls();
			this.setContainerWidth();
			this.setUpPositioning();
		}

		this.createAlphaWall = function(){
			var wall = $(this).find('.wall');
			var html = '';
			var i = 0;
			while(i<1){
				i++;
				html += '<div class="alpha" style="z-index:'+i*2+'"></div>';
			}
			wall.append(html);

			$('div.caption-space').append('<div class="caption-wall"></div>');
		}

		this.setContainerWidth = function(){
			var width = this.slides.length*300;
			jQuery(this).children('.container').css('width',width+'px');
		}

		this.setIndexPositions = function(curr_index){
			$(this.slides[this.current_index]).attr('center','false');
			$(this.slides[this.prev_index]).attr('left','false');
			$(this.slides[this.next_index]).attr('right','false');
			
			$(this.slides[this.prev2_index]).attr('left2','false');
			$(this.slides[this.next2_index]).attr('right2','false');
			
			this.current_index = curr_index;
			this.next_index = curr_index+1;
			this.prev_index = curr_index-1;

			if(this.next_index==this.slides.length){	
				this.next_index = 0;
			}
			if(this.prev_index==-1){
				this.prev_index = this.slides.length-1;
			}
			
			this.prev2_index = this.prev_index-1;
			this.next2_index = this.next_index+1;
			
			if(this.next2_index==this.slides.length){	
				this.next2_index = 0;
			}
			if(this.prev2_index==-1){
				this.prev2_index = this.slides.length-1;
			}

			$(this.slides[this.prev2_index]).attr('left2','true');
			$(this.slides[this.next2_index]).attr('right2','true');						
			$(this.slides[this.prev_index]).attr('left','true');
			$(this.slides[this.next_index]).attr('right','true');
			$(this.slides[this.current_index]).attr('center','true');
		}

		this.setUpPositioning = function(){
			$(this).children('.container').children('[center=true]').css({'width':'360px','left':'33px','top':'0px','z-index':'7'});
			$(this).children('.container').children('[center=true]').children('img').css({'height':'270px'});
			$(this).children('.container').children('[center=true]').children('.mask').css({'opacity':'0'});
			
			$(this).children('.container').children('[left=true]').css({'left':'0px','width':'272px','top':'36px','z-index':'3'});
			$(this).children('.container').children('[right=true]').css({'left':'154px','width':'272px','top':'36px','z-index':'3'});
			$(this).children('.container').children('[left=true], [right=true]').children('img').css({'height':'204px'});
			
			$(this).children('.container').children('[left2=true]').css({'left':'68px','width':'204px','top':'53px','z-index':'1','visibility':'hidden'});
			$(this).children('.container').children('[right2=true]').css({'left':'154px','width':'204px','top':'53px','z-index':'1','visibility':'hidden'});
			$(this).children('.container').children('[left2=true], [right2=true]').children('img').css({'height':'153px'});

			$('div.slide').find('div.caption-wall').css({opacity:1});
			$('div.slide[center=true]').find('div.caption-wall').css({opacity:0,display:'none'});
		}

		this.unlock = function(){
			
		}

		this.zIndexShift = function(dir){
			if(dir=="left"){
				$(this).find('[center=true]').css({"z-index":"7"});
				$(this).find('[left=true]').css({"z-index":"3","visibility":"visible"});
				$(this).find('[left2=true]').css({"z-index":"1","visibility":"hidden"});
				$(this).find('[right=true]').css({"z-index":"3","visibility":"visible"});
				$(this).find('[right2=true]').css({"z-index":"1","visibility":"hidden"});
			}
			else{
				$(this).find('[center=true]').css({"z-index":"7"});
				$(this).find('[left=true]').css({"z-index":"3","visibility":"visible"});
				$(this).find('[left2=true]').css({"z-index":"1","visibility":"hidden"});
				$(this).find('[right=true]').css({"z-index":"3","visibility":"visible"});
				$(this).find('[right2=true]').css({"z-index":"1","visibility":"hidden"});
			}
			this.resetEdges();
		}

		this.animate = function(direction){

			this.locked = true;
			var _self = this;
			var next_index = this.current_index;
			var turnLoop;

			if(direction=="left"){

				if(this.current_index==0){
					next_index = this.slides.length-1;
				}
				else{
					next_index--;
				}
				
				turnLoop = setTimeout('zIndexShiftHelper("left")', 250);
				
				$(this).find('[center=true]').animate({
					left: '+=121',
					top: '+=+36',
					width: '+=-88'
				}, 500, 'swing', function(){_self.locked=false;});
				
				$(this).find('[left=true]').animate({
					left: '+=33',
					top: '+=-36',
					width: '+=88'
				}, 500, 'swing');

				$(this).find('[left=true]').children('img').animate({
					height: '+=66'
				}, 500, 'swing');
				
				$(this).find('[left2=true]').animate({
					left: '+=-68',
					top: '+=-17',
					width: '+=68'
				}, 500, 'swing');

				$(this).find('[left2=true]').children('img').animate({
					height: '+=51'
				}, 500, 'swing');
								
				$(this).find('[right=true]').animate({
					left: '+=0',
					top: '+=17',
					width: '+=-68'
				}, 500, 'swing');

				$(this).find('[right=true]').children('img').animate({
					height: '+=-51'
				}, 500, 'swing');

			} else if (direction=="right") {

				if(this.current_index==this.slides.length-1){
					next_index = 0;
				}
				else{
					next_index++;
				}
			
				turnLoop = setTimeout('zIndexShiftHelper("right")', 250);
			
				$(this).find('[center=true]').animate({
					left: '+=-33',
					top: '+=+36',
					width: '+=-88'
				}, 500, 'swing', function(){_self.locked=false;});
								
				$(this).find('[left=true]').animate({
					left: '+=68',
					top: '+=+17',
					width: '+=-68'
				}, 500, 'swing');
			
				$(this).find('[left=true]').children('img').animate({
					height: '+=-51'
				}, 500, 'swing');
			
				$(this).find('[right=true]').animate({
					left: '+=-121',
					top: '+=-36',
					width: '+=88'
				}, 500, 'swing');

				$(this).find('[right=true]').children('img').animate({
					height: '+=66'
				}, 500, 'swing');
				
				$(this).find('[right2=true]').animate({
					left: '+=0',
					top: '+=-17',
					width: '+=68'
				}, 500, 'swing');

				$(this).find('[right2=true]').children('img').animate({
					height: '+=51'
				}, 500, 'swing');
			}
			
			$(this).find('[center=true]').children('img').animate({
				height: '+=-66'
			}, 500, 'swing');
			
			this.setIndexPositions(next_index);
			//this.setUpPositioning();

			$(this).find('[left=true],[right=true]').find('div.caption-wall').stop().css({display:'block',opacity:1});
		}
		this.resetEdges = function(){

			$('div.slide[center=true]').find('div.caption-wall').animate({opacity:0},700,'easeInQuad',function() {$(this).css({display:'none'})});

			$('div.flavor-list div.highlight').removeClass();

			var $flavor = $('div.flavor-list > div[flavorid='+$('div.slide[center=true]').attr('flavorid')+']');
			
			$flavor.addClass('highlight');
			if($flavor.css('display') == 'none')
			{
				$flavor.prevAll('div.group-name:first').triggerHandler('click');
			}

			$(this).children('.container').children('[left2=true]').css({'left':'68px','width':'204px','top':'53px','z-index':'1','visibility':'hidden'});
			$(this).children('.container').children('[right2=true]').css({'left':'154px','width':'204px','top':'53px','z-index':'1','visibility':'hidden'});
			$(this).children('.container').children('[left2=true], [right2=true]').children('img').css({'height':'153px'});		
		}		
		
		this.setUpControls = function(){
			var _self = this;
			var leftButton = jQuery(this).find('.controls .arrows .left');
			var rightButton = jQuery(this).find('.controls .arrows .right');			
			//leftButton.hover(function(){jQuery(this).css('visibility','visibile')}, function(){jQuery(this).css('visibility','hidden')});
			//rightButton.hover(function(){jQuery(this).css('visibility','visibile')}, function(){jQuery(this).css('visibility','hidden')});
			leftButton.click(function(){
				if(!_self.locked){
					_self.animate('left');
				}
			});
			rightButton.click(function(){
				if(!_self.locked){
					_self.animate('right');
				}
			});
		}
		this.switchTo = function(index){
		}
	})
}

