//Init slideShow
var cpt = 0;
function initDiapo(){	
	$('.jcarousel li').hide();
	$('.jcarousel li').eq(cpt).show();
	// $(window).unbind('scroll').bind('scroll',function(){
		// setHeightOverlay();
	// })	
	showNavDiapo();
	// Slide next
	$('#display-popin .next').click(function(){
		cpt++;
		if(cpt!=$('.jcarousel li').length){
			$('.jcarousel li').hide();
			//alert(cpt);
			$('.jcarousel li').eq(cpt).fadeIn();
			setPositionPopin();
		}
		showNavDiapo();
		return false;
	});
	
	// Slide previous
	$('#display-popin .prev').click(function(){
		cpt--;
		if(cpt>=0){
			$('.jcarousel li').hide();
			$('.jcarousel li').eq(cpt).fadeIn();
			setPositionPopin();	
		}
		showNavDiapo();
		return false;
	});
}

function showNavDiapo(){
	$('#display-popin .prev').show();
	$('#display-popin .next').show();	
	if(cpt == 0){
		$('#display-popin .prev').hide();
	}
	if(cpt==$('.jcarousel li').length-1){
		$('#display-popin .next').hide();
	}
}

function setPositionPopin(){
	setHeightOverlay();	
	var scroll = getPageScroll();
	var dim = getPageSize();	
	
	var h=$(window).height();
	var popin = jQuery('.popin');
	var topPopin = ((h-popin.height())/2) + scroll[1];
	if(topPopin <=0){
		topPopin = 70;
	}
	popin.css({ 
		top: topPopin
	});	
	//$('#close').css({top:(10+scroll[1])+'px'});
	
}

function setHeightOverlay(){
	var scroll = getPageScroll();
	var hi = getHeight() + scroll[1];
	//alert(hi);
	$("#popinOverlay").height(hi);
}

// ready
$(function(){

	// remplir la liste de diapo
	if($('#slideshow').length){
		var list = '';
		$.each(tabPictures, function(i,item){
			list += '<li><img src="'+item.image+'" alt ="" /></li>';
		})
		$('#slideshow .jcarousel ul').html(list);
	}

	
	$('.stdDiapo').unbind('click').bind('click',function(){
		$('#main').hide();
		$('#display-popin').show();
		initDiapo();
		setPositionPopin();
		closepoin();	
		//refreshposition();	
		return false;
	})
})
// function refreshposition(){
	// $(window).unbind('scroll');
	// $(window).bind('scroll',function(){
		// setPositionPopin();
	// })
	
	// $(window).resize(function(){
		// setPositionPopin();
	// })
// }
function closepoin(){
	$('#popinOverlay, #close').click(function(){
		$('#main').show();
		$('#display-popin').fadeOut();
		cpt = 0;
		return false;
	})	
}

// return height
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}


