document.write("<SCRIPT LANGUAGE='JavaScript' TYPE='text/javascript' SRC='../../../corefiles/functions/main.js'></SCRIPT>");

var transparentGif = "http://" + location.host + "/corefiles/images/transparent.gif";

var preLoadedImage;

function ResizeImageZoomWindowEvent() {
	if(document.getElementById("imageZoomBackground"))
		if(document.getElementById("imageZoomBackground").style.visibility == "visible")
			ResizeImageZoomWindow();
}

function SetImageZoomTopPositionEvent() {
	if(document.getElementById("imageZoomBackground"))
		if(document.getElementById("imageZoomBackground").style.visibility == "visible")
			SetImageZoomTopPosition();
}

function SetImageZoomTopPosition() {

	var windowHeight;
	if(isExplorer()) windowHeight = document.documentElement.clientHeight;
	else windowHeight = window.innerHeight;

	var verticalScrollOffset;
	if(isExplorer()) verticalScrollOffset = document.documentElement.scrollTop;
	else verticalScrollOffset = window.pageYOffset;

	
	var imageZoomDiv = document.getElementById("imageZoomDiv");
	imageZoomDiv.style.top = (windowHeight/2 - imageZoomDiv.offsetHeight / 2 + verticalScrollOffset) + "px" ;

}

function setUpImage() {

	var windowHeight;
	if(isExplorer()) windowHeight = document.body.offsetHeight;
	else windowHeight = window.innerHeight;
	
	var windowWidth;
	if(isExplorer()) windowWidth = document.body.offsetWidth;
	else windowWidth = window.innerWidth;	
	

	var imageZoomDiv = document.getElementById("imageZoomDiv");
	imageZoomDiv.style.left = (windowWidth / 2 - imageZoomDiv.offsetWidth / 2) + "px";
	SetImageZoomTopPosition();
}

function displayImage() {
//	alert("a");
	
	var imageZoomDiv = document.getElementById("imageZoomDiv");
	var imageZoomImage = document.getElementById("imageZoomImage"); 
	
	//alert(imageZoomImage.offsetWidth);
	
	imageZoomDiv.style.width = preLoadedImage.width + 2 +  "px";
	imageZoomDiv.style.height = preLoadedImage.height + 32 + "px";
	
	setUpImage();
	
	imageZoomImage.src = preLoadedImage.src;
	
	
	
}

function ResizeImageZoomWindow() {

	var windowHeight;
	if(isExplorer()) windowHeight = document.body.offsetHeight;
	else windowHeight = window.innerHeight;
	
	var windowWidth;
	if(isExplorer()) windowWidth = document.body.offsetWidth;
	else windowWidth = window.innerWidth;	
	
	/* Impostazione background. */
	var imageZoomBackground = document.getElementById("imageZoomBackground");
	if(parseInt(windowWidth) <= parseInt(imageZoomBackground.style.width)) {
		imageZoomBackground.style.marginLeft = "0px";
		imageZoomBackground.style.left = "0px";
	} else {
		imageZoomBackground.style.marginLeft = (parseInt(imageZoomBackground.style.width)/2*-1) + "px";
		imageZoomBackground.style.left = "50%";
	}
	

	setUpImage();


	
}

/* grazie a 
http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
*/
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
	

function expandImageByPath(imagePath) {
	var imageZoomDiv = document.getElementById("imageZoomDiv");
	var imageZoomBackground = document.getElementById("imageZoomBackground");
	var imageZoomImage = document.getElementById("imageZoomImage"); 

	imageZoomBackground.style.width = document.getElementById("content").clientWidth + (parseInt(getStyle(document.body, "margin-left")) + parseInt(getStyle(document.body, "margin-right")) * 2) + "px";
	imageZoomBackground.style.height = (document.body.scrollHeight + 10) + "px";

	
	imageZoomDiv.style.width = "200px";
	imageZoomDiv.style.height = "200px";
	
	
	var extensionDotPos = imagePath.lastIndexOf(".");
	if(imagePath.substring(extensionDotPos) == ".png")
		var newSrc = imagePath.substring(0,extensionDotPos) + "_big.jpg";
	else
		var newSrc = imagePath.substring(0,extensionDotPos) + "_big" + imagePath.substring(extensionDotPos);
	
	

	
	preLoadedImage = new Image();
	preLoadedImage.onload = function(){displayImage()};
	preLoadedImage.src = newSrc;
	
	//alert(imageZoomDiv.style.width);
	//imageZoomImage.src = newSrc;
	//alert("b");
	
	ResizeImageZoomWindow();
	SetImageZoomTopPosition();
	
	imageZoomDiv.style.visibility="visible";
	imageZoomBackground.style.visibility= "visible";

}

function expandImage(image) {

	var imageZoomDiv = document.getElementById("imageZoomDiv");
	var imageZoomBackground = document.getElementById("imageZoomBackground");
	var imageZoomImage = document.getElementById("imageZoomImage"); 

	imageZoomBackground.style.width = document.getElementById("content").clientWidth + (parseInt(getStyle(document.body, "margin-left")) + parseInt(getStyle(document.body, "margin-right")) * 2) + "px";
	imageZoomBackground.style.height = (document.body.scrollHeight + 10) + "px";

	
	imageZoomDiv.style.width = "200px";
	imageZoomDiv.style.height = "200px";
	
	var src = image.src;
	
				
	/* Path per il maledetto explorer. Se a questa immagine è stato applicato il fix per la
	trasparenza png allora il src bisogna ricavarlo dal parametro della funzione filter. */
	if(image.style.filter != "" && image.src.indexOf("transparent.gif") > 0) {
		src = image.style.filter;
		var pos = src.indexOf("src='") + 5;
		src = src.substr(pos, src.length - pos);
		pos = src.indexOf("'") ;
		src = src.substr(0, pos);
	}
	
	var extensionDotPos = src.lastIndexOf(".");
	if(src.substring(extensionDotPos) == ".png")
		var newSrc = src.substring(0,extensionDotPos) + "_big.jpg";
	else
		var newSrc = src.substring(0,extensionDotPos) + "_big" + src.substring(extensionDotPos);
	
	

	
	preLoadedImage = new Image();
	preLoadedImage.onload = function(){displayImage()};
	preLoadedImage.src = newSrc;
	
	//alert(imageZoomDiv.style.width);
	//imageZoomImage.src = newSrc;
	//alert("b");
	
	ResizeImageZoomWindow();
	SetImageZoomTopPosition();
	
	imageZoomDiv.style.visibility="visible";
	imageZoomBackground.style.visibility= "visible";

}

function collapseImage() {
	document.getElementById("imageZoomDiv").style.visibility="hidden";
	document.getElementById("imageZoomBackground").style.visibility="hidden";
	//document.getElementById("imageZoomImage").src = "";
	document.getElementById("imageZoomImage").src =  transparentGif;
	
}

function isExplorer() {
	return (navigator.userAgent.indexOf("MSIE") > 0);
}

function addImageZoomElement() {

	/* Il div che contiene l'immagine e il pulsante di chiusura. */
	var imageZoomDiv = document.createElement("div");
	imageZoomDiv.setAttribute("id", "imageZoomDiv");
	
	/* La barra di chiusura. */
	var imageZoomCloseBar = document.createElement("div");
	imageZoomCloseBar.setAttribute("id", "imageZoomCloseBar");
	imageZoomCloseBar.onmousedown=function(){collapseImage()};
	imageZoomDiv.appendChild(imageZoomCloseBar);
	
	/* Il pulsante di chiusura. */
	var imageZoomCloseButton = document.createElement("div");
	imageZoomCloseButton.setAttribute("id", "imageZoomCloseButton");
	//setElementStyle(imageZoomCloseButton, "position:absolute;top:0px;left:0px;height:28px;width:28px;border:1px solid #ff0000;");

	if(getLanguage() == "it") imageZoomCloseBar.appendChild(document.createTextNode("Chiudi"));
	if(getLanguage() == "fr") imageZoomCloseBar.appendChild(document.createTextNode("Fermer"));
	if(getLanguage() == "de") imageZoomCloseBar.appendChild(document.createTextNode("Schliessen"));
	imageZoomCloseBar.appendChild(imageZoomCloseButton);
	
	/* L'immagine. */
	var imageZoomImage = document.createElement("img");
	imageZoomImage.setAttribute("id", "imageZoomImage");
	//imageZoomImage.setAttribute("src", transparentGif);
	imageZoomImage.onmouseup=function(){collapseImage()};
	//imageZoomImage.onload = function(){displayImage()};
	imageZoomDiv.appendChild(imageZoomImage);
	
	/* Il background di copertura della pagina web. */
	var imageZoomBackground = document.createElement("div");
	imageZoomBackground.setAttribute("id", "imageZoomBackground");
	document.body.appendChild(imageZoomBackground);
	
	document.body.appendChild(imageZoomDiv);
	
	document.getElementById("imageZoomImage").src = transparentGif;
	

}

function setElementStyle(el, style) {
	if(!isExplorer())
		el.setAttribute("style", style);
	else
		el.style.setAttribute('cssText', style, 0);
}


function setUpImages() {
	
	var images = document.getElementsByTagName("img");

	for (i = 0; i < images.length; i++) {
		//alert(images[i].src);
		if(images[i].className == "zoom" || images[i].className.indexOf("zoom_") == 0) {
			
			images[i].style.cursor = "pointer";
			if(getLanguage() == "it")
				images[i].title = "Clicca per ingrandire la fotografia.";
			if(getLanguage() == "fr")
				images[i].title = "Cliquez pour agrandir la photo";
			if(getLanguage() == "de")
				images[i].title = "Bild zur Vergrösserung anklicken";
			
			images[i].onclick = function(){expandImage(this)};
			
			//alert(images[i].src + " - " + images[i].onclick);
	
		}
	}
}



var fun = setUpImages;
if (window.addEventListener) window.addEventListener('load', fun, false); 
else if (window.attachEvent) window.attachEvent("onload", fun);  

var fun = addImageZoomElement;
if (window.addEventListener) window.addEventListener('load', fun, false); 
else if (window.attachEvent) window.attachEvent("onload", fun);  


var fun = ResizeImageZoomWindowEvent;
if (window.addEventListener) window.addEventListener('resize', fun, false); 
else if (window.attachEvent) window.attachEvent("onresize", fun);  

var fun = SetImageZoomTopPositionEvent;
if (window.addEventListener) window.addEventListener('scroll', fun, false); 
else if (window.attachEvent) window.attachEvent("onscroll", fun);  