var newDiv;
var newDivImg;
var loading;
var checkLoadedInterval;
var checkLoadedLoop;
var bLoading = false;
function zoom(obj)
{
	if(bLoading) {
		cancelLoading(false);
	}
	
	bLoading = true;
	
	if(newDiv) document.body.removeChild(newDiv);

	oldSrc = obj.src;
	fileNameSplited = obj.src.split('/');

	fileName = fileNameSplited[fileNameSplited.length-1];

	for (var i = 0; i < fileNameSplited.length; i++) {
		if (fileNameSplited[i] == 'min' || fileNameSplited[i] == 'mid') fileNameSplited[i] = 'pop';
	}

	filePath = fileNameSplited.join('/');

	newDivImg = document.createElement('img');
	newDivImg.src = filePath;

	loading = document.createElement('span');
	loading.style.color = '#000';
	loading.style.fontSize = '10px';
	loading.style.fontFamily = 'Verdana, Tahoma';
	loading.style.fontWeight = 'bold';
	loading.style.borderBottom = 'solid 1px #cecece';
	loading.margin = '0px';
	loading.padding = '0px';
	loading.innerHTML = 'Ładowanie ...';

	newDiv = document.createElement('div');
	newDiv.id = 'product_popUp';
	newDiv.style.position = 'absolute';
	newDiv.style.backgroundColor = '#eee';

    if(obj.offsetParent) {
		L = obj.offsetLeft;
		T = obj.offsetTop;
		pObj = obj;
		while (pObj = pObj.offsetParent) {
			L += pObj.offsetLeft;
			T += pObj.offsetTop;
		}
	}

	//newDiv.style.top = '265px';
	//newDiv.style.top = '2px';
	newDiv.style.top = T  + "px";//((obj.height/2) - 25) + "px"; //- 17
	newDiv.style.left = L + "px"; // - 7 
	
	newDiv.style.cursor = 'pointer';
	//newDiv.style.padding = '3px 20px';
	newDiv.style.padding = '2px 8px';
	//newDiv.style.backgroundColor = '#000';
	newDiv.onclick = new Function ("document.body.removeChild(newDiv); newDiv = null;");
	
	newDiv.appendChild(loading);
	//newDiv.innerHTML = 'Ładowanie ...';
	
	newDivImg.onLoad = new Function(
		checkLoadedInterval = setInterval('LoadImage();', 100)
	);

	document.body.appendChild(newDiv);
	
/*	popUp = window.open('', fileName.replace('.', '_'), 'left=20,top=20,width=400px,height=550px,status=no,toolbar=no,menubar=no,location=no');
	popUp.document.write('<html><body><div style="width: 100%; height: 100%; text-align: center; vertical-align: middle;"><img src="' + filePath + '" onclick="window.close()" style="cursor: pointer;" /></div></body></html>');
*/
}

var loopCount = 0;
function LoadImage() {
	scrollTop = document.body.scrollTop;
	if (typeof window.innerWidth!='undefined') {
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	} else if (document.documentElement && typeof document.documentElement.clientHeight!='undefined' && document.documentElement.clientHeight!=0) {
		windowHeight = document.documentElement.clientHeight;
		windowWidth = document.documentElement.clientWidth;
	} else if (document.body && typeof document.body.clientWidth!='undefined') {
		windowHeight = document.body.clientHeight;
		windowWidth = document.body.clientWidth;
	}
	
	windowHeight -= 10;
	windowWidth -= 10;
	
	if(newDivImg.width>1 && newDivImg.height != 30 && newDivImg.width != 28) {
		if(newDivImg.height > newDivImg.width && newDivImg.height > windowHeight) {
			scale = windowHeight / newDivImg.height;
			newDivImg.height = windowHeight;
			newDivImg.width = Math.ceil(scale * newDivImg.width);
		} else if(newDivImg.height < newDivImg.width && newDivImg.width > windowWidth) {
			scale = windowWidth / newDivImg.width;
			newDivImg.width = windowWidth;
			newDivImg.height = Math.ceil(scale * newDivImg.height);
			if(newDivImg.height > windowHeight) {
				scale = windowHeight / newDivImg.height;
				newDivImg.height = windowHeight;
				newDivImg.width = Math.ceil(scale * newDivImg.width);
			}
		}
		
		clearInterval(checkLoadedInterval);
		newDiv.removeChild(loading);
		loading = null;
		document.body.removeChild(newDiv);
		bLoading = false;
	} else {
		if(loopCount++ > 200) {
			cancelLoading(true);
			bLoading = false;
		}
		return false;
	}
	
	newDiv.style.top = Math.ceil(windowHeight-newDivImg.height)/2 + scrollTop + 'px';
	newDiv.style.padding = '3px';
	newDiv.style.backgroundColor = '#eee';
	newDiv.style.left = '50%';
	newDiv.style.marginLeft = -(newDivImg.width/2);

	newSpan = document.createElement('span');
	newSpan.style.top = '7px';
	newSpan.style.right = '7px';
	newSpan.style.padding = '0px 3px 1px 3px';
	newSpan.style.position = 'absolute';
	newSpan.style.fontSize = '10px';
	newSpan.style.fontWeight = 'bold';
	newSpan.style.color = '#F39900';
	newSpan.style.border = 'solid 1px #EFB859';
	newSpan.style.backgroundColor = '#EFEAE0';
	newSpan.innerHTML = 'X';

	newDiv.appendChild(newSpan);
	newDiv.appendChild(newDivImg);

	document.body.appendChild(newDiv);
}

function cancelLoading(showInfo)
{
	newDiv.removeChild(loading);
	if(showInfo) {
		loading.style.color = '#f00';
		loading.style.borderBottom = 'solid 1px #f00';
		loading.style.fontSize = '10px';
		loading.innerHTML = 'Brak obrazka';
		newDiv.appendChild(loading);
	}
	clearInterval(checkLoadedInterval);
	loopCount = 0;
}
