

/** autore Gerardo Lombardo

    per Hotel Casa Di Meglio
    data 01/02/09

*/

var arrayImgLightboxSrc = new Array();
var curZoomImg;
var preZoomImg;
var nexZoomImg;
var origHeightContenitore;

function selezionaImmagini() {

	var img = document.getElementsByTagName('a');
        var objImg;
	for (var i = n = 0; i < img.length; i++)
	{
			if (img[i].rel != 'lightbox') continue;
				else {

				        objImg = document.getElementById(img[i].id);
				        arrayImgLightboxSrc[n] = objImg.href;
				        objImg.setAttribute("id","lightbox"+n);
				        objImg.href="javascript:;";
				        objImg.setAttribute("rel","lightbox["+n+"]");
			
					$(objImg).bind("mouseover",function(e) { 
						$(this).animate({ opacity: 0.8 }, 100, function(e) {
							 $(this).animate({opacity: 1.0 },100);
						});
					});

				
					$(objImg).bind("click",function(e) { 
						if ( $("#bordoZoomImg").length==1 && $("#bordoZoomImg").css("display")!="none" ) {
							nascondiZoomImg();
						} else zoomImmagine(this);
					});
					n++;
				
				}

	}

}

function getNumeroImg(obj) {

  var num = obj.rel.replace("lightbox[", ""); 
  num = parseInt(num.replace("]",""));
  
  return num;

}

function zoomImmagine(obj) {

  var num = getNumeroImg(obj);
  
  var length = arrayImgLightboxSrc.length;
  var numPrecedente = num - 1;
  var numSuccessivo = num + 1;
  
  if ( numSuccessivo>length-1) numSuccessivo=0;
  if ( numPrecedente<0 ) numPrecedente = length-1;
  
  if ( !document.getElementById("zoomImg"+num) ) creaZoomImg(num);
  if ( !document.getElementById("zoomImg"+numPrecedente) ) creaZoomImg(numPrecedente);
  if ( !document.getElementById("zoomImg"+numSuccessivo) ) creaZoomImg(numSuccessivo);

  var contenitore = document.getElementById("body");  
  var center = document.getElementById("center");
  var contentHide = document.getElementById("center");
  var bordoZoomImg = document.getElementById("bordoZoomImg");
  var zoomImg = document.getElementById("zoomImg"+num);
  var img = document.getElementById("img"+num);
  var contenitoreTasti = document.getElementById("contenitoreTasti");
  var preloadingZoomImg = document.getElementById("preloadingZoomImg");
  curZoomImg = num;
  preZoomImg = numPrecedente;
  nexZoomImg = numSuccessivo;
  
   
  $(contentHide).fadeOut(500,function(e) {
        
        preloadingZoomImg.style.display="inline";
	      var immagine = new Image();
	      $(immagine).load( function() {
	      	      
          $(img).attr('src',arrayImgLightboxSrc[num]);
          bordoZoomImg.style.display="inline";
          preloadingZoomImg.style.display="none";
          
          $(bordoZoomImg).animate({ width: immagine.width},500, function(e) {
                $(zoomImg).css({width: immagine.width,height: immagine.height});
		$(contenitore).css({height:immagine.height});
		$(center).css({height:immagine.height});
                $(zoomImg).slideDown(500);
                $(contenitoreTasti).fadeIn(500);
           });			            
            
           immagine.onload=function(){};
        }).attr('src', arrayImgLightboxSrc[num]);
	
	      		
 });
           
}

function creaZoomImg(num) {

  var contenitore = document.getElementById("body");
  if ( !contenitore ) return;
  if ( !arrayImgLightboxSrc[num] ) return;

  var div = document.createElement("div");
  div.setAttribute("id","zoomImg"+num);
  div.className="zoomImg";
  div.style.display="none";
  $(div).bind("click",function(e) { nascondiZoomImg(); });
  
  var img = document.createElement("img");
  img.setAttribute("id","img"+num);
  img.setAttribute("title",document.getElementById('lightbox'+num).title);
  img.setAttribute("alt","immagine");
  img.src=arrayImgLightboxSrc[num];
  img.style.border="0";
  
  
  if ( !document.getElementById("bordoZoomImg") ) {
    var divBordo = document.createElement("div");
    divBordo.setAttribute("id","bordoZoomImg");
    divBordo.className="bordoZoomImg";
    divBordo.style.display="none";
    contenitore.appendChild(divBordo);
  }
  
  /** ########  TASTI ############### */
  
  if ( !document.getElementById("contenitoreTasti") ) {
    var contenitoreTasti = document.createElement("div");
    contenitoreTasti.setAttribute("id","contenitoreTasti");
    contenitoreTasti.style.display="none";
    contenitore.appendChild(contenitoreTasti);
  }
  
  if ( !document.getElementById("tastoChiudi") ) {
    var tastoChiudi = document.createElement("div");
    tastoChiudi.setAttribute("id","tastoChiudi");
    tastoChiudi.style.display="inline";
    $(tastoChiudi).css({'opacity' : '0.6'});
    $(tastoChiudi).bind("click",function(e) { nascondiZoomImg(); });
    contenitoreTasti.appendChild(tastoChiudi);
  }
  
  if ( !document.getElementById("tastoIndietro") ) {
    var tastoIndietro = document.createElement("div");
    tastoIndietro.setAttribute("id","tastoIndietro");
    tastoIndietro.style.display="inline";
    $(tastoIndietro).css({'opacity' : '0.6'});
    $(tastoIndietro).bind("click",function(e) {
      preparaCambioZoomImg(preZoomImg);
    });
    if ( arrayImgLightboxSrc.length>1 ) contenitoreTasti.appendChild(tastoIndietro);
  }
  
  if ( !document.getElementById("tastoAvanti") ) {
    var tastoAvanti = document.createElement("div");
    tastoAvanti.setAttribute("id","tastoAvanti");
    tastoAvanti.style.display="inline";
    $(tastoAvanti).css({'opacity' : '0.6'});
    $(tastoAvanti).bind("click",function(e) {
      preparaCambioZoomImg(nexZoomImg);
    });
    if ( arrayImgLightboxSrc.length>1 ) contenitoreTasti.appendChild(tastoAvanti);
  }
  
  /** ############################## */
  
   if ( !document.getElementById("preloadingZoomImg") ) {
    var preloadingZoomImg = document.createElement("div");
    preloadingZoomImg.setAttribute("id","preloadingZoomImg");
    preloadingZoomImg.style.display="none";
    contenitore.appendChild(preloadingZoomImg);
  }
  
  /** ########## PRELOADING ####### */
  
  div.appendChild(img);
  contenitore.appendChild(div);
  
  return;

}

function preparaCambioZoomImg(num) {

    var contenitoreTasti=document.getElementById("contenitoreTasti");
    var zoomImg = document.getElementById("zoomImg"+curZoomImg);
    
    contenitoreTasti.style.display="none";
    
    $(zoomImg).slideUp(800,function(e) {
        zoomImmagine(document.getElementById('lightbox'+num));
      });
}

function nascondiZoomImg() {

  var contentHide = document.getElementById("center");
  var bordoZoomImg = document.getElementById("bordoZoomImg");
  var zoomImg = document.getElementById("zoomImg"+curZoomImg);
  var contenitoreTasti = document.getElementById("contenitoreTasti");
  var preloadingZoomImg = document.getElementById("preloadingZoomImg");
  
    $(contenitoreTasti).fadeOut(500);
    $(zoomImg).slideUp(800,function(e) {
  
    $(bordoZoomImg).animate({ width: '1'},500, function(e) {
      $(contentHide).fadeIn(500,function(e) {
	$("#body").css({height:origHeightContenitore+"px"});
	$("#center").css({height:origHeightContenitore+"px"});
      });
      bordoZoomImg.style.display="none";
      preloadingZoomImg.style.display="none";
    });
            
    });

}

function initZoomImg() {

	origHeightContenitore = $("#body").height();
	selezionaImmagini();

}


addLoadEvent(initZoomImg);