// Layer Modal
function openLayer(id){
	
	jQuery('#'+id).modal({onOpen: modalOpen,onClose: modalClose});
	jQuery("#modalContainer").addClass("loginAndFriend");
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	
	// send to friend adapt
	if (id == "sendToFriendLayer"){
		jQuery("#modalContainer div.table2Colonnes").css("width","481px");
		jQuery("#modalContainer .table2Colonnes .box_droite_body").css("width","481px").css("text-align", "left");
	}
	//jQuery("#modalContainer").css("position","absolute");
	//window.scrollTo(0,0);
}

//Layer Modal Login
function openLayer2(){
	jQuery('#basicModalContent2').modal({onOpen: modalOpen,onClose: modalClose});
	jQuery("#modalContainer").addClass("loginAndFriend");
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
}

// Actu en audio
function openIFrameLayer(src, width, height, scroll){
	
	var iframe = "<iframe style='background-color:#FFF;' id='iframeInLayer' src='"+src+"' width='"+width+"' height='"+height+"' scrolling='"+scroll+"' frameborder='0' />";
	jQuery("#iframeLayerContent").html(iframe);
	
	jQuery('#iframeLayer').modal({onOpen: modalOpen,onClose: modalClose});
		
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	jQuery("#modalContainer").addClass("actuAudioModal");
	jQuery("#modalContainer").css("position","absolute");
	window.scrollTo(0,0);
}

function openDynamicLayer(htmlContent){
	jQuery('#dynamicLayerContent').html(htmlContent);
	jQuery('#dynamicLayer').modal({onOpen: modalOpen,onClose: modalClose});
		
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	//alert("onenDynamicLayer !");
	jQuery("#modalContainer").css("position","absolute");
	window.scrollTo(0,0);
}

//Infographie layer
function openInfographieLayer(url, title, description) {
	var width = 700;
	var height = 420;
	
	htmlContent = "<div class='lightboxImage' style='float:left;padding:20px;background-color:white;min-height:100px;width:"+width+"'>";
	htmlContent += "<h1 style='position:relative; top:-20px;'>"+title+"</h1>"
	htmlContent += "<iframe frameborder='0' scrolling='0' width='"+width+"' height='"+height+"' name='infographie' src='"+url+"'>";
	
	if(description != null && description != ''){
		htmlContent += "<p>"+description+"</p>";
	}
	htmlContent += "</div>";
	
	
	jQuery('#dynamicLayerContent').html(htmlContent)
	jQuery('#dynamicLayer').modal({onOpen: modalOpen,onClose: modalClose});
	
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	
	//alert("onenDynamicLayer !");
	jQuery("#modalContainer").css("position","absolute");
	jQuery("#modalContainer").css("width", width+40+"px");
	jQuery("#modalContainer .layer_top, #modalContainer .layer_bottom").css("width", width+40+"px");
	jQuery("#modalContainer .layer_top .repeat, #modalContainer .layer_bottom .repeat").css("width", width+6+"px");
	
	if(isIE6){
		setTimeout("showTheLightBoxImageIE6()", 200);
	}
	window.scrollTo(0,0);
	
	
}


// Image seule
function openImageLayer(imgUrl, title, alttext, width, height, credit, description) {
	
	/*if(width > 640 || height > 640){
		if(width > height){
			// division entiere
			height = (height*640 - ((height*640)%width)) /width;
			width = 640;
		}else{
			// division entiere
			width = (width*640 - ((width*640)%height)) /height;
			height = 640;
		}
	}
	
	attributes = "height='"+height+"' width='"+width+"'";*/
	
	width = 640;
	
	htmlContent = "<div class='lightboxImage' style='float:left;padding:20px;background-color:white;min-height:100px;width:640px;text-align:center;'>";
	htmlContent += "<h1 style='position:relative; top:-20px;'>"+title+"</h1>"
	htmlContent += "<img id='lightboxImageTag' src='"+imgUrl+"?m=640' alt='"+alttext+"'/>";
	//htmlContent += "<img id='lightboxImageTag' src='"+imgUrl+"?m=640' alt='"+alttext+"' height= />";
	
	/*if(description != null && description != ''){
		htmlContent += "<p>"+description;
		if(credit != null && credit != ''){
			htmlContent += "&nbsp;&nbsp;["+credit+"]";
		}
		htmlContent += "</p>";
	}*/
	htmlContent += "<p>"+description+"</p>";
	htmlContent += "</div>";
	
	//alert(htmlContent);
	
	jQuery('#dynamicLayerContent').html(htmlContent)
	jQuery('#dynamicLayer').modal({onOpen: modalOpen,onClose: modalClose});
	
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	
	//alert("onenDynamicLayer !");
	jQuery("#modalContainer").css("position","absolute");
	jQuery("#modalContainer").css("width", width+40+"px");
	jQuery("#modalContainer .layer_top, #modalContainer .layer_bottom").css("width", width+40+"px");
	jQuery("#modalContainer .layer_top .repeat, #modalContainer .layer_bottom .repeat").css("width", width+6+"px");
	
	if(isIE6){
		setTimeout("showTheLightBoxImageIE6()", 200);
	}
	window.scrollTo(0,0);
	
	associateimages();
}
function showTheLightBoxImageIE6(imgUrl){
	jQuery("#lightboxImageTag").show();
	jQuery("#lightboxImageTag").css("z-index", "3200");
	jQuery("#lightboxImageTag").css("visibility", "visible");
	jQuery("#lightboxImageTag").css("display", "block");
	jQuery("#lightboxImageTag").css("float", "left");
	jQuery('#dynamicLayerContent').empty();
	jQuery('#dynamicLayerContent').html(htmlContent);
	jQuery('#lightboxImageTag').attr("src", imgUrl)
}

// Diaporama & video
function AJAXopenLayerAndDoAction(url, action){
	
	
	// define mode (video or not)
	var video = (action === 'carousel') ? false : true;
	
	jQuery('.basicModalContent2 .box_center_body').empty();
	jQuery('.basicModalContent2').modal({onOpen: modalOpen,onClose: modalClose});
		
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	var isIE7 = ((navigator.userAgent.indexOf("MSIE 7.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if(isIE6){
		jQuery("#modalOverlay").css("position","absolute");
		var pp =  document.body.offsetHeight + 'px';
		jQuery("#modalOverlay").css("height",pp);
		jQuery("#modalContainer").css("position","absolute");
	}
	
	if(video){
		jQuery("#modalContainer").css("width","521px");
		jQuery("#modalContainer .layer_top").css("width","521px");
		jQuery("#modalContainer .layer_top .repeat").css("width","487px");
		jQuery("#modalContainer .layer_bottom").css("width","521px");
		jQuery("#modalContainer .layer_bottom .repeat").css("width","487px");
		jQuery(".basicModalContent2 .table2Colonnes").css("width","481px");
		jQuery(".basicModalContent2 .box_droite_body").css("width","auto");
	}else{
		jQuery(".basicModalContent2 .box_droite_body").css("width","682px");
	}
	
	jQuery.ajax({
		type: "GET",
		url: url,
		success: function(msg){
			
			jQuery(".basicModalContent2 .box_droite_body").empty();
			jQuery(".basicModalContent2 .box_droite_body").html(msg);

			// videos
			if(video){
				RTS.player.init();
			}
			
			// diaporamas
			associateimages();
			if(!video) {
				CarouselIt();
			}

		}
	});
	jQuery("#modalContainer").css("position","absolute");
	jQuery("#modalContainer").addClass("diaporamaLightBox");
	window.scrollTo(0,0);
}
var theControl="";
function CarouselIt(){
	
	//init carousel
	jQuery(".basicModalContent2 #mycarousel").jcarousel({
		scroll: 1, 
		visible: 1,
		itemVisibleOutCallback: {
			// the "disabled" attribute is not enough reactive, so we use hide/show.
			onBeforeAnimation: function(){
				jQuery("div.jcarousel-next").hide();
				jQuery("div.jcarousel-prev").hide();
			},
			onAfterAnimation: function(){
				jQuery("div.jcarousel-next").show();
				jQuery("div.jcarousel-prev").show();
			}
		}
	});
	
	//set total num pictures
	var nbElt = 0;
	jQuery(".basicModalContent2 #mycarousel li").each(function(){ 
		nbElt ++;
	});
	jQuery(".totalElt").html(nbElt);
		
	var isChromeOrSafari = (navigator.userAgent.indexOf("Safari") != -1);
	if(!isChromeOrSafari){
		//change num current picture
		var currElt = 1;
		jQuery(".jcarousel-next").click(function(){
			currElt ++;
			jQuery(".currentElt").html(currElt);
		});
		jQuery(".jcarousel-prev").click(function(){ 
			currElt --;
			jQuery(".currentElt").html(currElt);
		});
	}
	else{// traitement different sur chrome et safari. Mise en place d'un pseudo-carousel custom
		jQuery(".basicModalContent2 #mycarousel").css("left","auto");
		
		//change num current picture
		var currElt = 1;
		jQuery(".jcarousel-next").click(function(){ 
			if(currElt!=nbElt){
				jQuery(".basicModalContent2 #mycarousel li").hide();
				currElt ++;
				jQuery(".currentElt").html(currElt);
				jQuery(".jcarousel-item-"+currElt).show();
			}
		});
		jQuery(".jcarousel-prev").click(function(){ 
			if(currElt!=1){
				jQuery(".basicModalContent2 #mycarousel li").hide();
				currElt --;
				jQuery(".currentElt").html(currElt);
				jQuery(".jcarousel-item-"+currElt).show();
			}
		});
	}
}
// FIN Layer Modal