// JavaScript Document
var zoomImagesPath = '/images/zoom/';

window.onresize = AX_zoomClose;
function AX_zoomClose(){
	if(document.getElementById('zoomShadow')){
		document.getElementById('zoomShadow').style.display = 'none';
	}
}
function AX_zoom(lnk,img){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight, scrollWidth, scrollHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		scrollWidth = window.pageXOffset;
		scrollHeight = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		scrollWidth = document.documentElement.scrollLeft;
		scrollHeight = document.documentElement.scrollTop;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		scrollWidth = document.body.scrollLeft;
		scrollHeight = document.body.scrollTop;
	}	
	var shadowWidth, shadowHeight;
	if(xScroll < windowWidth) shadowWidth = windowWidth; else shadowWidth = xScroll;
	if(yScroll < windowHeight) shadowHeight = windowHeight; else shadowHeight = yScroll;
	var inBody = document.getElementsByTagName("body").item(0);

	var inShadow = document.createElement("div");
	inShadow.setAttribute('id', 'zoomShadow');
	inShadow.style.position = 'absolute';
	inShadow.style.zIndex = '600';
	inShadow.style.width = shadowWidth +'px';
	inShadow.style.height = shadowHeight +'px';
	inShadow.onclick = AX_zoomClose;
	inBody.insertBefore(inShadow, inBody.firstChild);

	var inPreloadCnt = document.createElement("div");
	inPreloadCnt.setAttribute('id', 'preloadContainer');
	inShadow.appendChild(inPreloadCnt);

	var inPreloadImg = document.createElement("img");
	inPreloadImg.setAttribute('id', 'preloadImg');
	inPreloadImg.setAttribute('src', zoomImagesPath+'loading.gif');
	inPreloadCnt.appendChild(inPreloadImg);

	var inZoomCnt = document.createElement("div");
	inZoomCnt.setAttribute('id', 'zoomContainer');
	inZoomCnt.style.visibility = 'hidden';
	inShadow.appendChild(inZoomCnt);

	var inZoomCloseCnt = document.createElement("div");
	inZoomCloseCnt.setAttribute('id', 'zoomCloseContainer');
	inZoomCnt.appendChild(inZoomCloseCnt);
	var inZoomCloseSpan = document.createElement("span");
	inZoomCloseCnt.appendChild(inZoomCloseSpan);
	var inZoomCloseImg = document.createElement("img");
	inZoomCloseImg.setAttribute('id', 'zoomCloseImg');
	inZoomCloseImg.onclick = AX_zoomClose;
	inZoomCloseImg.setAttribute('src', zoomImagesPath+'closebox.png');
	inZoomCloseSpan.appendChild(inZoomCloseImg);

	var inZoomImgCnt = document.createElement("div");
	inZoomImgCnt.setAttribute('id', 'zoomImgContainer');
	inZoomCnt.appendChild(inZoomImgCnt);

	var inZoomImg = document.createElement("img");
	inZoomImg.onload = function(){
		//NASCONDO IL PRELOAD
		inPreloadCnt.style.display = 'none';
		//MODIFICO LE IMPOSTAZIONI DEL CONTENITORE
		inZoomCnt.style.visibility = 'visible';
		var yPos = scrollHeight+((windowHeight-inZoomImg.height)/2);
		inZoomCnt.style.margin = yPos+'px 0px 0px -'+inZoomImg.width/2+'px';
		//CREO IL DIV PER IL TITOLO
		if(lnk.title!=''){
			var inZoomTitCnt = document.createElement("div");
			inZoomTitCnt.setAttribute('id', 'zoomTitleContainer');
			inZoomTitCnt.style.width = inZoomImg.width+'px';
			inZoomCnt.appendChild(inZoomTitCnt);
			var inCapTable = document.createElement("table");
			inCapTable.setAttribute('align', 'center');
			inCapTable.setAttribute('border', '0');
			inCapTable.setAttribute('cellPadding', '0');
			inCapTable.setAttribute('cellSpacing', '0');
			inZoomTitCnt.appendChild(inCapTable);
			var inTbody = document.createElement("tbody");
			inCapTable.appendChild(inTbody);
			var inCapRow = document.createElement("tr");
			inTbody.appendChild(inCapRow);

			var inCapCol1 = document.createElement("td");
			inCapRow.appendChild(inCapCol1);
			var inCapSpa1 = document.createElement("span");
			inCapSpa1.setAttribute('class', 'title_left');
			inCapCol1.appendChild(inCapSpa1);
			var inCapImg1 = document.createElement("img");
			inCapImg1.setAttribute('src', zoomImagesPath+'title_left.png');
			inCapSpa1.appendChild(inCapImg1);

			var inCapCol2 = document.createElement("td");
			inCapCol2.setAttribute('id', 'zoomTitle');
			inCapRow.appendChild(inCapCol2);

			var inCapCol3 = document.createElement("td");
			inCapRow.appendChild(inCapCol3);
			var inCapSpa3 = document.createElement("span");
			inCapSpa3.setAttribute('class', 'title_right');
			inCapCol3.appendChild(inCapSpa3);
			var inCapImg3 = document.createElement("img");
			inCapImg3.setAttribute('src', zoomImagesPath+'title_right.png');
			inCapSpa3.appendChild(inCapImg3);

			var zoomTitle = document.getElementById('zoomTitle');
			zoomTitle.innerHTML = lnk.title;
		}
	}
	inZoomImg.setAttribute('id', 'zoomImg');
	inZoomImg.setAttribute('src', img);
	inZoomImgCnt.appendChild(inZoomImg);
	return false;
}