/**
 * @author trochette at tommyrochette.com
 */
 
$(document).ready(function() {
		//Set actions from main menu
		$(".subnav").each(function () {
				$(this).parent().eq(0).hover(function () {
				$("a",this).first().addClass("open");
				$(".subnav:eq(0)", this).show();
			}, function () {
				$("a",this).first().removeClass("open");
				$(".subnav:eq(0)", this).hide();	
			});
		});
		//Set action for header
		$('#image_1').addClass("selected");
		/*$(".controls").each(function(index, value) { 
  			$(this).bind("click",showImage); 
		});*/
		$('.lightbox').lightBox({imageBtnClose:"http://tr.local:8888/expeditionsnouvellevague.com/assets/skin/site/lightbox-btn-close.gif",
								imageBtnPrev:"http://tr.local:8888/expeditionsnouvellevague.com/assets/skin/site/lightbox-btn-prev.gif",
								imageBtnNext:"http://tr.local:8888/expeditionsnouvellevague.com/assets/skin/site/lightbox-btn-next.gif",
								imageLoading:"http://tr.local:8888/expeditionsnouvellevague.com/assets/skin/site/lightbox-ico-loading.gif",
								imageBlank:"http://tr.local:8888/expeditionsnouvellevague.com/assets/skin/site/lightbox-blank.gif"});
	});
	
var interval = setInterval(rotate,5000);
var count = 1;


function showImage(__e){
	//alert("SHOW IMAGE");
	$('#image_1').removeClass("selected");
	$('#image_2').removeClass("selected");
	$('#image_3').removeClass("selected");
	$(__e.currentTarget).addClass("selected");
	clearInterval(interval);
	interval = setInterval(beginRotate,10000);
	
	if(__e.currentTarget == document.getElementById("image_1")){
		$('#image_one').animate({left: '0%'}, 1400);
		$('#image_two').animate({left: '100%'}, 1400);
		$('#image_three').animate({left: '-100%'}, 1400);
		if(count==3){
			$('#image_one').css("z-index","5");
			$('#image_two').css("z-index","1");
			$('#image_two').css("left","100%");
			$('#image_three').css("z-index","6");
		}else if(count==2){
			$('#image_one').css("z-index","5");
			$('#image_two').css("z-index","6");
			$('#image_three').css("z-index","1");
		}
		count = 1;

	}else if(__e.currentTarget == document.getElementById("image_2")){
		$('#image_one').animate({left: '-100%'}, 1400);
		$('#image_two').animate({left: '0%'}, 1400);
		$('#image_three').animate({left: '100%'}, 1400);
		if(count==1){
			$('#image_one').css("z-index","6");
			$('#image_two').css("z-index","5");
			$('#image_three').css("z-index","1");
		}else if(count==3){
			$('#image_one').css("z-index","1");
			$('#image_two').css("z-index","5");
			$('#image_three').css("z-index","6");
		}
		count = 2;	
	}else{
		
		
		if(count==1){
			$('#image_one').css("left","0%");
			$('#image_three').css("left","100%");
			$('#image_one').animate("left","-100%");
			$('#image_two').animate({left: '100%'}, 1400);
			$('#image_three').animate({left: '0%'}, 1400);
			$('#image_one').css("z-index","5");
			$('#image_two').css("z-index","1");
			$('#image_three').css("z-index","6");

		}else{
			$('#image_one').css("left","100%");
			$('#image_two').animate({left: '-100%'}, 1400);
			$('#image_three').animate({left: '0%'}, 1400);
			$('#image_one').css("z-index","5");
			$('#image_two').css("z-index","4");
			$('#image_three').css("z-index","6");
		}
		count = 3;
	}
}




function beginRotate(){
	clearInterval(interval);
	interval = setInterval(rotate,4000);
}


function rotate(){
	count++
	if(count == 1){
		$('#image_one').animate({left: '0%'}, 1400);
		$('#image_two').animate({left: '100%'}, 1400);
		$('#image_three').animate({left: '-200%'}, 1400);
		$('#image_four').animate({left: '-100%'}, 1400);
		$('#image_one').css("z-index","5");
		$('#image_two').css("z-index","1");
		$('#image_four').css("z-index","6");
		$('#image_three').css("z-index","1");
		$('#image_1').addClass("selected");
		$('#image_4').removeClass("selected");
	}
	
	if(count == 2){
		$('#image_one').animate({left: '-100%'}, 1400);
		$('#image_two').animate({left: '0%'}, 1400);
		$('#image_three').animate({left: '100%'}, 1400);
		$('#image_four').animate({left: '200%'}, 1400);
		$('#image_one').css("z-index","6");
		$('#image_two').css("z-index","6");
		$('#image_three').css("z-index","4");
		$('#image_four').css("z-index","4");
		$('#image_2').addClass("selected");
		$('#image_1').removeClass("selected");
	}
	
	if(count == 3){
		$('#image_one').css("left","100%");
		$('#image_two').animate({left: '-100%'}, 1400);
		$('#image_three').animate({left: '0%'}, 1400);
		$('#image_four').animate({left: '100%'}, 1400);
		$('#image_one').css("z-index","5");
		$('#image_two').css("z-index","4");
		$('#image_three').css("z-index","6");
		$('#image_four').css("z-index","6");
		$('#image_3').addClass("selected");
		$('#image_2').removeClass("selected");
	}
	
	if(count == 4){
		$('#image_one').css("left","100%");
		$('#image_two').animate({left: '-200%'}, 1400);
		$('#image_three').animate({left: '-100%'}, 1400);
		$('#image_four').animate({left: '0%'}, 1400);
		$('#image_one').css("z-index","5");
		$('#image_two').css("z-index","4");
		$('#image_three').css("z-index","6");
		$('#image_four').css("z-index","6");
		$('#image_4').addClass("selected");
		$('#image_3').removeClass("selected");
	}
	
	if(count==4)count=0;
		
}
