function hideflash(){
	/* hide all flash in the page */
	flash = document.getElementsByTagName('object');
	for (var i = 0; i < flash.length; i++)
		flash[i].style.visibility = 'hidden';
}
function showflash(){
	/* show all flash in the page */
	flash = document.getElementsByTagName('object');
	for (var i = 0; i < flash.length; i++)
		flash[i].style.visibility = 'visible';
}

function hideLightBox(){
	window.lightBoxObject.transpBox.style.display = "";
	window.lightBoxObject.box.style.display = "";
	window.lightBoxObject.deleteChilds();
	showflash();
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function blendimage(divid, imageid, imagefile, millisec) {
	if( !document.getElementById(divid) || !document.getElementById(imageid) )
		return;
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;
	
	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function getWinDim() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return [myWidth,myHeight];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function startScroll(){
	var block = window.lightBoxObject.icoContainer;
	var clipH = parseInt(window.lightBoxObject.box.offsetHeight)-(20);		// 20 e' l'altezza delle 2 frecce
	var blockH = parseInt(block.offsetHeight);
	var minTop = 0;
	var stop = false;
	var px = parseInt(block.style.top);
	
	if(blockH>clipH)
		minTop = -(blockH-clipH);
	
	if(window.icoLayerScrollUp && px>=window.icoLayerScrollUp && px>minTop)
		px -= 5;
	else if(window.icoLayerScrollDown && px<=window.icoLayerScrollDown && px<0)
		px += 5;
	else
		stop = true;
	
	if(stop)
		clearInterval(window.icoLayerScrollTimerID);
	else{
		block.style.top = px+"px";
		window.icoLayerScrollTimerID = setTimeout("startScroll();",1);
	}
}

function LightBox(_type){
	// caricamento immagini
	this.showTransp = function (){
						var arrPos = getScrollXY();		// da sistemare
						var arrDim = getWinDim();		// da sistemare
						var t = arrPos[1]+arrDim[1]/2;
						this.transpBox = document.getElementById("myTransparentDiv");
						if(this.transpBox){
							this.transpBox.style.width = document.body.clientWidth+"px";
							this.transpBox.style.height = document.body.clientHeight+"px";
							this.transpBox.firstChild.style.top = (t-30)+"px";
							
							changeOpac(80, "trasparentImage");
							
							hideflash();
							
							this.transpBox.style.display = "block";
							this.transpBox.onclick = function(){
														hideLightBox();// temporaneo
													}
						}
					}
	this.deleteChilds = function(oP){
							var o;
							oP = oP||this.box;
							while( oP.childNodes.length && (o = oP.childNodes[oP.childNodes.length-1]) ){
								if(o.childNodes && o.childNodes.length)
									this.deleteChilds(o)
								o.parentNode.removeChild(o);
							}
						}
	this.setBoxAttributes = function(){
								var w = this.W||this.maxW;
								if(w>this.maxW)
									w = this.maxW;
								w += this.icoW+2;					// aggiungo la dimensione delle miniature
																	// piu' il bordo
								var h = this.H||this.maxH;
								if(h>this.maxH)
									h = this.maxH;
								
								h += 2+15;								// piu' il bordo + top delle frecce
								
								var arrDim = getWinDim();			// da sistemare
								var arrPos = getScrollXY();			// da sistemare
								var l = arrDim[0]/2;
									l -= w/2;
								var t = arrPos[1]+arrDim[1]/2;
								t -= h/2;
								
								this.box.style.width = w+"px";
								this.box.style.height = h+"px";
								this.box.style.top = t+"px";
								this.box.style.left = l+"px";
								this.box.style.clip = "rect(auto,"+(w+2)+"px,"+(h+2)+"px,auto)";
								this.box.style.display = "block";
							}
	this.init = function(){
		
		this.showTransp();				// trasparenza a tutta pagina
		
		switch(this.type){
			case "gallery":
				this.setGallery();		// carico tutte le immagini nella galleria a coppie (miniature/ingrandimenti)
			break;
			default:
				this.setHTML();
			break;
		}
		
		var aClose = document.createElement("div");
			aClose.setAttribute("id","closeButton");
			aClose.innerHTML = "[ "+window.closeWindow+" ]";
			aClose.onclick = function(){
				hideLightBox();
			}
		this.box.appendChild(aClose);
	}
	
	function showImage(_index){
		var els = window.lightBoxObject.icoContainer.childNodes;// possono essere IMG o DIV
		for(var i=0;i<els.length;i++)
			els[i].className = "lighBoxIco";
		els[_index].className = "lighBoxIcoSelected";
		_index = _index>>0;
		_index = _index*2+1;
		blendimage("myLightBoxZoom", "myLightBoxZoomImage", window.lightBoxObject.imagesList[_index], 200);
	}
	this.showImage = showImage;
	
	this.addElement = function (type,attributes){
						attributes = attributes||{};
						var ob = document.createElement(type);
						for(var p in attributes)
							ob.setAttribute(p,attributes[p]);
						return ob;
					  }
					  
	this.setGallery = function(){
		var img = new Image();
			img.setAttribute("id","myLightBoxZoomImage");
		var zoomContainer = document.createElement("div");
			zoomContainer.setAttribute("id","myLightBoxZoom");
			zoomContainer.appendChild(img);
		this.box.appendChild(zoomContainer);
		
		this.icoContainer = document.createElement("div");
		this.icoContainer.setAttribute("id","myLightBoxIco");
		this.icoContainer.style.position = "absolute";	// se non lo faccio da codice lo scorrimento nn funge
		this.icoContainer.style.top = "0px";			// se non lo faccio da codice lo scorrimento nn funge
		this.icoContainer.style.left = "0px";			// se non lo faccio da codice lo scorrimento nn funge
		for(var i=0,tmp,c=0,tmpObj;i<this.imagesList.length;i++){
			img = new Image();
			// icone
			if(i%2==0){
				if(this.imagesList[i]=="no_image"){
					tmpObj = this.addElement("div");
					tmpObj.innerHTML = c+1;
					img = null;
				}else
					tmpObj = img;
				
				this.icoContainer.appendChild(tmpObj);
				tmpObj.className = "lighBoxIco";
				tmpObj.setAttribute("id","lighBoxIco_"+c);
				tmpObj.onclick = function(){showImage(this.id.replace(/lighBoxIco_/,""));}		
				if(img)
					img.onload = function(){
									if(this.width>100){
										this.height = (this.height/this.width)*100;
										this.width = 100;
									}
								}
			// zoom
			}else{
				img.index = c;
				img.style.display = "none";
				this.box.appendChild(img); // solo per la cache delle immagini e per ricavare le dimensioni
				img.onload = function(){
					if(!window.lightBoxW || this.width>window.lightBoxW)
						window.lightBoxW = this.width;
					if(!window.lightBoxH || this.height>window.lightBoxH)
						window.lightBoxH = this.height;
					
					var imagesLength = (window.lightBoxObject.imagesList.length/2)-1;
					if(this.index==imagesLength){
						window.lightBoxObject.W = window.lightBoxW;
						window.lightBoxObject.H = window.lightBoxH;
						window.lightBoxObject.setBoxAttributes();
					}
				}
				c++;
			}
			if(img)
				img.src = this.imagesList[i];
		}
		this.box.appendChild(this.icoContainer);
		
		this.addGalleryMover();
	}
	
	this.addGalleryMover = function(){
								var aTop = document.createElement("div");
									aTop.setAttribute("id","areaTop");
									//aTop.innerHTML = "&#8744;";
									aTop.onclick = function(){
										window.icoLayerScrollUp = parseInt(window.lightBoxObject.icoContainer.style.top)-((100+2+8)*2);
										window.icoLayerScrollDown = null;
										startScroll();
									}
								this.box.appendChild(aTop);
								
								var aDown = document.createElement("div");
									aDown.setAttribute("id","areaDown");
									//aDown.innerHTML = "&#8743;";
									aDown.onclick = function(){
										window.icoLayerScrollDown = parseInt(window.lightBoxObject.icoContainer.style.top)+((100+2+8)*2);
										window.icoLayerScrollUp = null;
										startScroll();
									}
								this.box.appendChild(aDown);
							}
	this.setHTML = function(){
						this.box.innerHTML = this.innerHTML;
						this.setBoxAttributes();
					}
	
	this.box = document.getElementById("myLightBox");
	this.transpBox = null;
	this.icoContainer = null;
	this.W = 0;
	this.H = 0;					// lasciamo altezza fissa
	this.icoW = 150;				// larghezza icone (comprende anche 50px di margin)
	var arrDim = getWinDim();
	this.maxW = arrDim[0];
	this.maxH = arrDim[1];
	this.imagesList = [];
	this.type = _type||"message";
	window.lightBoxObject = this;	// riferimento all'oggetto per eventi immagini
	return this;
}

function showLightBoxGallery(frm){
	//var frm = ob.form;
	var LB = new LightBox("gallery");
	for(var i=0,index=0;i<frm.elements.length;i++){
		if(frm.elements[i].type=="hidden")
			LB.imagesList.push(frm.elements[i].value);
	}
	
	LB.init();
	//LB.showImage(ob.value-1);
	LB.showImage(0);
	return false;
}

function showPageInLightBox(url){
	var LB = new LightBox();
	LB.W = 600;
	LB.H = 400;
	LB.innerHTML = '<iframe src="'+url+'" width="100%" height="400" frameborder="0"></iframe>';
	LB.init();
	return LB;
}
