

/* ************** AL BOX ************** */
/* ************** AL BOX ************** */
/* ************** AL BOX ************** */

var fileBoxload=false;
var isCollapseInfoBox=false;
var ALBoxParams=new Array();
var preloadImgs=new Array();
var listGal=new Array();
var currentChange=false;
var currentScroll=null;
var currentPhoto=null;
var isPhotoBox=false;
var isLoadBox=false;
var diapoTimer=null;
var isGalBox=false;
var preloadStart=0;
var diapoEnRoute=0;
var diapoTime=5000;



function in_array(val,array){ for(var i=0;i<array.length;i++){ if(array[i]==val){ return true; } } return false; };
function is_numeric(num){ if( num==Number(num) ){ return true; }else{ return false; } };

function startBox(){ $("a.albox").click(loadBox); };
function loadBox(){
   
  try{
    var myclass = $(this).parent().attr('class');
    if( myclass.indexOf('moving')!==-1 ) return false;
  }catch(e){ }
  
  var t = $(this); listGal=new Array();
	var rev = t.attr("rev"), Width=null, Height=null;
	if(rev!=null){
		rev = rev.split(';'); for(i=0;i<rev.length;i++){
			el2 = rev[i].split(':');
			if(el2[0].toLowerCase() == 'width') Width=el2[1].substring(0,el2[1].length-2);
			else if(el2[0].toLowerCase() == 'height') Height=el2[1].substring(0,el2[1].length-2);
		}
	}
	
	var url = t.attr("myhref") || t.attr("href");
	var group = t.attr("rel");
	var title = t.attr("title").replace("\n","<br />");
  
  if(Width==null) Width = $(window).width()-20;
  if(Height==null) Height = $(window).height()-40;
  

  openWin( Width, Height, url, group, title, t );
  return false;

};
function closeWin(){
  
  try{
   	$('#ALBoxDiv').remove();
  	isLoadBox=false; endDiapo();
  	$("#askBox").remove(); 
	$("embed").css('visibility','visible');
  }catch(e){ }  

};
function openWin(Width,Height,url,group,title,link){

  
  closeWin(); isLoadBox=true; isGalBox=false, isPhotoBox=false;
  if( url.indexOf('.jpg')>1 || url.indexOf('.gif')>1 || url.indexOf('.jpeg')>1 || url.indexOf('.png')>1 || url.indexOf('.bmp')>1 ) gallery=true; else gallery=false;
  $("embed").css('visibility','hidden');

  if(gallery){ Width=300; Height=250; }

  var html='<div id="ALBoxBack" style="position:absolute;top:0;left:0;width:100%;height:100%;background:#000;-moz-opacity:0.5;opacity:0.5;filter:Alpha(Opacity=50);z-index:9000"></div>';
  html+='<div id="ALBox" style="position:absolute;top:0;left:0;height:'+(Height-20)+'px;width:'+Width+'px;z-index:9100"><div style="position:relative;width:100%;margin-top:-20px;height:20px;line-height:20px;text-align:right;background:#333;"><a href="javascript:closeWin()" style="font-variant:small-caps;color:#fff;text-decoration:none;font-weight:bold;padding-right:5px;">Fermer</a>';
  html+='<div id="PreloadBoxInfo" style="position:absolute;top:0;left:5px;color:#fff;font-size:10px;font-weight:bold;font-variant:small-caps;">&nbsp;</div></div>';
  if( gallery ){
		isPhotoBox=true;
		// Pour les images
		title=title||'';
		html+='<img src="images/blanc.gif" alt="'+title+'" style="display:none" id="ALBoxImg" />';
		if(group.length>0){ // pour diaporama
			html+='<div id="ALBoxPrec" class="ALBoxPrec" style="background:#eee;position:absolute;width:20%;cursor:pointer;font-weight:bold;top:0;left:0;z-index:14000;" title="Photo précédente" alt="Photo précédente">&nbsp;</div><img style="position:absolute;top:50%;left:10px;margin-top:-12px;z-index:14100;cursor:pointer;" src="images/prec.png" alt="Photo Précédente" title="Photo Précédente" class="ALBoxPrec" />';
			html+='<div id="ALBoxSuiv" class="ALBoxSuiv" style="background:#eee;position:absolute;width:20%;cursor:pointer;font-weight:bold;top:0;right:0;z-index:14000;" title="Photo suivante" alt="Photo suivante">&nbsp;</div><img style="position:absolute;top:50%;right:10px;margin-top:-12px;z-index:14100;cursor:pointer;" src="images/suiv.png" alt="Photo Suivante" title="Photo Suivante" class="ALBoxSuiv" />';
			html+='<div id="ALBoxDiapo" style="position:absolute;top:5px;width:80px;left:50%;margin-left:-40px;text-align:center;"><img src="images/play.png" style="cursor:pointer" alt="Démarrer le diaporama" title="Démarrer le diaporama" /></div>';
			isGalBox=true;
		}
		html+='<div id="ALBoxInfo" style="display:none;position:absolute;bottom:0;left:0;width:100%;height:40px;background:#FFF;border-top:3px solid #333;z-index:15000;opacity:0.7;-moz-opacity:0.7;filter:Alpha(Opacity=70)"></div>';
		html+='<div id="ALBoxTitle" style="display:none;position:absolute;bottom:5px;right:5px;color:#333;text-align:right;font-weight:bold;font-size:12px;margin-left:140px;z-index:16000;">'+title+'</div>';
		html+='<div id="ALBoxNumber" style="display:none;position:absolute;bottom:5px;left:5px;width:130px;font-size:11px;color:#000;font-weight:bold;z-index:16000;text-align:left;">&nbsp;</div>';
		html+='<div id="loadBox"><img src="images/blanc.gif" alt="Chargement en cours" width="'+Width+'" height="'+Height+'" /><img src="images/load.gif" alt="Chargement en cours" style="position:absolute;top:50%;margin-top:-7px;left:50%;margin-left:-64px" /></div>';
  }else{
		// Pour iframe
		ALBoxParams['Width'] = Width; ALBoxParams['Height'] = Height;
		html+='<iframe style="z-index:10000;height:100%;width:100%;border:none;" frameborder="0" border="0" src="'+url+'"></iframe>';
  }
  html+='</div>';


  $("body").append('<div id="ALBoxDiv">'+html+'</div>');
	if(gallery){
		$("#ALBoxImg")[0].onload = imageBox;
    $("#ALBoxImg").attr('src',url).click(collapseInfoBox);
		var count = 0; currentPhoto=null; $("a.albox").each(function(){
      var el = $(this); if( el.attr('rel')=="myGallery" ){ count++; if(link.attr('href')==el.attr('href')) currentPhoto=count; listGal[listGal.length]=el; }
		}); $("#ALBoxNumber").html("Photo "+currentPhoto+" sur "+count);
		if( isGalBox ){ $("#ALBoxDiapo > img").click(askDiapo); }
	}

  var HeightDoc = $(document).height();
  var HeightWin = $(window).height();
  var Top = (Number(HeightWin)-Number(Height))/2 + 10;

  var WidthWin = $(window).width();
  var Left = (Number(WidthWin)-Number(Width))/2;

	$("#ALBoxBack").css({height:HeightDoc}).click(closeWin);
	$("#ALBox").css({height:Height, width:Width, background:'#fff', color:'#000', left:Left, top:Top});

	$("#ALBoxSuiv, #ALBoxPrec").css({opacity:'0'}).hover(function(){ $(this).css({opacity:'0.85'}); }, function(){ $(this).css({opacity:'0'}); });
	$(".ALBoxSuiv").click(nextPhoto);
	$(".ALBoxPrec").click(prevPhoto);

  scrollBox(true);

};
function collapseInfoBox(){

  if(isCollapseInfoBox){
    isCollapseInfoBox=false;
		$("#ALBoxInfo").height( $("#ALBoxTitle").height()+10 ).show();
		$("#ALBoxTitle,#ALBoxNumber").show();
  }else{
    isCollapseInfoBox=true;
    $("#ALBoxInfo").css({height:0,overflow:'hidden'});
    $("#ALBoxTitle,#ALBoxNumber").hide();
  }
  
};
function preloadImgBox(num){

	if(currentPhoto==null) return;
	currentPhoto=Number(currentPhoto);

	var el = document.getElementById("ALBoxPreload"); 
	if(el==null){ 
	  try{
  		var newEl = document.createElement('div'); newEl.id='ALBoxPreload';
  		var el = document.getElementById("ALBoxDiv"); el.appendChild(newEl);
  		var el = document.getElementById("ALBoxPreload"); el.style.display = 'none';
		}catch(e){ }
	}
	if( !is_numeric(num) ) var num = currentPhoto; else num++; if(num>=listGal.length) num=0;
	while( in_array(num,preloadImgs) ){ num++; if(num>=listGal.length){ $("#PreloadBoxInfo").html(''); return null; }  }
	$("#PreloadBoxInfo").html("Préchargement photo "+(num+1));
	preloadImgs[preloadImgs.length] = num;

	if( listGal[num] ){
	  try{
  		var newEl = document.createElement('img');
  		var rand = 'preload_'+(Math.round( Math.random()*10000000 ));
  		newEl.id = rand; el.appendChild(newEl);
  		var el = document.getElementById(rand);
  		el.onload = function(){ preloadImgBox( num ); };
  		el.src = listGal[num][0].href;
  	}catch(e){ }
	}

};    
function changePhoto(num){

	if(currentChange) return; currentChange=true;

	if(num>listGal.length-1) num=0;
	if(num<0) num=listGal.length-1;
	currentPhoto=num+1;

	$("#loadBox").show(); var left = $("#ALBox").css('left');
	$("#ALBox").animate({left:'-800px'},350).animate({left: left},350,function(){
		$("#ALBoxTitle").html( listGal[num][0].title.replace("\n","<br />") );
		$("#ALBoxNumber").html( 'Photo '+currentPhoto+' sur '+listGal.length );
		currentChange=false;
	});
	$("#ALBoxImg").hide().attr('src',listGal[num][0].href);

};
function nextPhoto(){

  endDiapo(); 
  changePhoto(currentPhoto); 
  
};
function prevPhoto(){

  endDiapo(); 
  changePhoto(currentPhoto-2); 
  
};
function startDiapo(){ 
  
	clearTimeout(diapoTimer);
	if( diapoEnRoute==0 ){
		var v = $($("#askBox input")[0]).val(); $("#askBox").remove(); diapoEnRoute=1;
		updateDiapo(); $("#ALBoxDiapo > img").attr('src','images/stop.png'); 
		diapoTime=Number(v)*1000; if(diapoTime<1000) diapoTime=5000;
	}else{
		endDiapo();
	}
	
};
function endDiapo(){ 

	diapoEnRoute=0; clearTimeout(diapoTimer); try{ $("#askBox").remove(); }catch(e){ }
	$("#ALBoxDiapo > img").attr('src','images/play.png');
	
};
function updateDiapo(){ 

	if(diapoEnRoute==1){
		diapoTimer=setTimeout(function(){ 

			currentChange=true;	num=currentPhoto;
			if(num>listGal.length-1){ 
				startDiapo();
			}else{
				currentPhoto=num+1;
				$("#loadBox").show();
				$("#ALBoxImg").hide().attr('src',listGal[num][0].href);
				$("#ALBoxTitle").html( listGal[num][0].title );
				$("#ALBoxNumber").html( 'Photo '+currentPhoto+' sur '+listGal.length );
				currentChange=false;
				if(currentPhoto>listGal.length-1) startDiapo();
			}
		}, diapoTime ); 
	}
	
};
function askDiapo(e){ 

	if( diapoEnRoute==0 ){
	    if( $("#askBox").length==0 ) $("body").append('<div id="askBox" style="position:absolute;width:250px">&nbsp;</div>');
	    var html = '';
	    html+='<div style="background:#666;color:#fff;font-weight:bold;font-size:12px;text-align:left;padding:3px 7px;">Configuration du diaporama</div>';
	    html+='<div style="margin:0 5px 5px;padding:5px;border:1px solid #666;border-top:0;color:#000">';
      html+='<form style="margin:0;padding:0;z-index:100001" onsubmit="startDiapo();return false;">';
	    html+='<input maxlengh="2" value="'+(diapoTime/1000)+'" style="border:1px solid #123456;background:#56789A;color:#fff;width:20px;height:15px;" /> secondes';
	    html+='<input type="submit" value="Démarrer" style="border:1px solid #123456;background:#56789A;color:#fff;margin-left:10px;" />';
	    html+='</form>';
	    html+='</div>';
	    $("#askBox").html(html);
	    var h = $(window).height(); var w = $(window).width();
	    var h2 = $("#askBox").height(); var w2 = $("#askBox").width();
	    var t = (h-h2)/2; var l = (w-w2)/2; 
	    $("#askBox").css({background:"#ccc",border:'1px solid black',top:t,left:l,zIndex:'100000'}).find('input')[0].select();
	    window.scrollTo(0,0);
	}else startDiapo();
	
};
function imageBox(){

	if(!isLoadBox) return; 

	if(isPhotoBox){
		var el=$('#ALBoxImg'); 
		if(isGalBox && preloadStart==0){ preloadImgBox(); preloadStart=1; }
	
	  MaxHeight= $(window).height()-40;
		MaxWidth = $(window).width()-20;
	
		el.css({width:'',height:''});
	
		var Height = el.height();
		Height = Math.min(Height,MaxHeight);
		var HeightWin = $(window).height();
	
		var Width = el.width();
		Width = Math.min(Width,MaxWidth);
	    var WidthWin = $(window).width();
	
		if(Width<10 || Height<10){ setTimeout(imageBox,500); return; }
		
		if(Height==MaxHeight){ el.css({height:Height}); Width=el.width(); }
		else if(Width==MaxWidth){ el.css({width:Width}); Height=el.height(); }
	
		var scrollTop = (document.documentElement) ? document.documentElement.scrollTop : window.pageYOffset;
	  var Top = (HeightWin-Height)/2 + 10 + scrollTop;
	  var Left = (WidthWin-Width)/2;
	
		if(isGalBox){ 
			$('#ALBoxSuiv,#ALBoxPrec').hide();
			$("#ALBoxTitle,#ALBoxInfo,#ALBoxNumber").hide()
		}
		
		setTimeout(function(){
		  	$("#ALBox").animate({width:Width,height:Height,top:Top,left:Left},500,function(){
			  var el=$('#ALBoxImg'); 
				el.fadeIn(1000,function(){ if(diapoEnRoute==1) updateDiapo(); $("#loadBox").hide(); }); scrollBox(true);
				if(isGalBox){ 
					$('#ALBoxSuiv, #ALBoxPrec').css({height:Height,'line-height':Height+'px'}).show();
					if(!isCollapseInfoBox){ isCollapseInfoBox=true; collapseInfoBox(); }
				}
			});
		},250);

	}else{

		el = $("#ALBox");

		MaxHeight= $(window).height()-40;
		MaxWidth = $(window).width()-20;

		var Height = ALBoxParams['Height'];
		Height = Math.min(Height,MaxHeight);
		var HeightWin = $(window).height();
	
		var Width = ALBoxParams['Width']
		Width = Math.min(Width,MaxWidth);
	  var WidthWin = $(window).width();
		
		var scrollTop = (document.documentElement) ? document.documentElement.scrollTop : window.pageYOffset;
	    var Top = (HeightWin-Height)/2 + 10 + scrollTop;
	    var Left = (WidthWin-Width)/2;
	
		setTimeout(function(){
			$("#ALBox > iframe").animate({width:Width, height: Height},500);
			$("#ALBox").animate({width:Width,height:Height,top:Top,left:Left},500,function(){ 
				scrollBox(true);
			});
		},250);

	}

};
function scrollBox(started){

	if(!isLoadBox) return;

	var myDat=new Date();
	currentScroll=myDat.getTime();
  
  $("#ALBoxBack").height( $(document).height() );
  
	if(started===true) var time=10; else var time=250;
	setTimeout(function(){
		if( currentScroll==myDat.getTime() || started===true ){
			var scrollTop = (document.documentElement) ? document.documentElement.scrollTop : window.pageYOffset;
		
			var Height = $("#ALBox").height();
		    var HeightWin = $(window).height();
		    var Top = (HeightWin-Height)/2 + 10 + scrollTop;
		
			var Width = $("#ALBox").width();
		    var WidthWin = $(window).width();
		    var Left = (WidthWin-Width)/2;
		
			if(started===true) $("#ALBox").css({top:Top,left:Left},500);
			else $("#ALBox").stop().animate({top:Top,left:Left},500);
		}
	},time);
	
};
function boxEvent(e){

	if(!isLoadBox) return;
	var kC = (window.event) ? event.keyCode : e.keyCode;
  var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
  var Left = (window.event) ? '37' : 37;
  var Right = (window.event) ? '39' : 39;

  if(kC==Esc) closeWin();
	if(isGalBox){
		if(kC==Right) nextPhoto();
		if(kC==Left) prevPhoto();
	}

};
window.onscroll = scrollBox;
document.onkeydown = boxEvent;
window.onresize = imageBox;

/* ************** AL BOX ************** */
/* ************** AL BOX ************** */
/* ************** AL BOX ************** */

fileBoxload=true;