jQuery.fn.exists = function(){return jQuery(this).length>0;}
//diaporama1:
var ImgWidth=960;
var timerPeriod=5000; //in milliseconds
var timerDiapo;
var timerStep=1; //if -1 we slide backwards
//diaporama2:
var ThumbWidth=80; //74px + 6px marginLeft+Right

$(function() {  

	/**///////////////////DIAPORAMA1////////////////////**/	
	if ($(".diaporama1").length>0) {
		refresh_selector(); //we create the picture selector list 
		selectSlide(0); //we select the first picture as a start
		//setTimer(); //we start the timer
		setTimer(2); //we delay the start of the diaporama twice: 2x timerPeriod
		//setTimeout("setTimer()",timerPeriod); //we delay the start of the diaporama twice: 2x timerPeriod
		/*$(".diaporama1 ul#selector li").click(function() {
			clearTimer();
			var index=$(".diaporama1 ul#selector li").index($(this));
			selectSlide(index);
			setTimer(2);//we double the delay for the next slide
			//var t=setTimeout("setTimer()",timerPeriod);//we double the delay for the next slide
		});*/
	}

	/**///////////////////DIAPORAMA2////////////////////**/
   $("#arrow_left").click(function() {  
		var currPos=parseFloat($("#currPos").html());
		var newPos=currPos-1;
		var maxPos=parseFloat($("#maxPos").html());		
		if (newPos>=1) {
			xCoord = ThumbWidth - ThumbWidth*newPos;
			$("#thumb_full").stop(true,true).animate({left: xCoord},{duration: 300});
			$("#currPos").html(newPos);
			if (newPos<maxPos) {activate_arrow('right');} else {deactivate_arrow('right');}
			if (newPos>1) {activate_arrow('left');} else {deactivate_arrow('left');}
		}
   }); 
   
   $("#arrow_right").click(function() {
		var currPos=parseFloat($("#currPos").html());
		var newPos=currPos+1;
		var maxPos=parseFloat($("#maxPos").html());
		if (newPos<=maxPos) {
			xCoord = ThumbWidth - ThumbWidth*newPos;
			$("#thumb_full").stop(true,true).animate({left: xCoord},{duration: 300});
			$("#currPos").html(newPos);
			if (newPos<maxPos) {activate_arrow('right');} else {deactivate_arrow('right');}
			if (newPos>1) {activate_arrow('left');} else {deactivate_arrow('left');}
		}
   }); 

   $("img.thumbnail").click(function() {
		imgID=$(this).attr('id');
		imgCode='<img src="photos/'+imgID+'.jpg" />';
		$("#image_frame").html(imgCode);
   });

});
 
function activate_arrow(direction) {
	$("#arrow_"+direction).attr('class','active');
}
 function deactivate_arrow(direction) {
	$("#arrow_"+direction).attr('class','inactive');
 }
 
function refresh_selector() {
	var numImages=$("#diapo_full img").length;
	var selected=$(".diaporama1 ul#selector li.selected");
	var index=$(".diaporama1 ul#selector li").index(selected);	
	if (index<0) {index=0;}
	if (index>(numImages-1)) {index=numImages-1;}

	$("ul#selector").html("");
	for (var i=0;i<numImages;i++) {
		$("ul#selector").append("<li>&nbsp;</li>");
	}
	selectSlide(index);
	$("ul#selector li").click(function() {
		clearTimer();
		var selected=$(".diaporama1 ul#selector li.selected");
		var curr_index=$(".diaporama1 ul#selector li").index(selected);
		var index=$("ul#selector li").index($(this));
		if (index<curr_index) {timerStep=-1;}
		if (index>curr_index) {timerStep=1;}
		selectSlide(index);
		setTimer(2);
	});
}
function setTimer(n) {
	if (typeof(n)=='undefined') {var n=1;}
	clearTimer();
	timerDiapo=setTimeout("nextSlide()",n*timerPeriod);
}
function clearTimer() {
	if (typeof(timerDiapo)!='undefined') {clearTimeout(timerDiapo);}
}
function nextSlide() {
	var selected=$(".diaporama1 ul#selector li.selected");
	var index=$(".diaporama1 ul#selector li").index(selected);
	var numSlides=$(".diaporama1 ul#selector li").length;
		//var newindex=(index+1)%numSlides;
	if (index>=numSlides-1) {timerStep=-1;}
	if (index<=0) {timerStep=1;}
	var newindex=(index+timerStep)%numSlides;
	selectSlide(newindex);
	setTimer(1);
}
function selectSlide(index) {
		var xCoord = -ImgWidth*index;
		$("#diapo_full").stop(true,true).animate({left: xCoord},{duration: 1200});
		$(".diaporama1 ul#selector li").removeClass("selected");
		$(".diaporama1 ul#selector li:eq("+index+")").addClass("selected");
}
