function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	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){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function zi(zi_src) {
	// alert("dfgdfgd")
	var objLoadingImage = d.getElementById('loadingImage');
	var ov = d.getElementById("overlay")
	var objImage = d.getElementById('lightboxImage');
	var objLightbox = d.getElementById('lightbox');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	if (objLoadingImage) {
		
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		// alert("top="+objLoadingImage.style.top + " left="+objLoadingImage.style.left)
		// objLoadingImage.style.display = 'block';
	}
	
	ov.style.height = arrayPageSize[1] + "px"
	ov.style.width = '100%'
	ov.style.display = "block"
	
	imgPreload = new Image();

	imgPreload.onload = function() {
		objImage.src = zi_src;

		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		if (navigator.appVersion.indexOf("MSIE")!=-1) {
			pause(250);
		} 

		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
		objLightbox.style.display = 'block';

		arrayPageSize = getPageSize();
		ov.style.height = (arrayPageSize[1] + 'px');
		
		return false;
	}
	// alert(zi_src)

	imgPreload.src = zi_src;
	
}

function h() {
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
}

function init_zi() {
	ov_div = d.createElement('DIV')
	ov_div.setAttribute("id", "overlay")

	ov_img_preloader = d.createElement('IMG')
	ov_img_preloader.setAttribute("id", "loadingImage")
	ov_img_preloader.src = "/css_maket/loading.gif"
	ov_div.appendChild(ov_img_preloader)

	lb_div = d.createElement('DIV')
	lb_div.setAttribute("id", "lightbox")

	z_img_a = d.createElement('A')
	z_img_a.setAttribute("href", "javascript:void(0);")
	z_img_a.setAttribute("title", "Click anywhere to close image")
	z_img_a.onclick = function () {h(); return false;}
	lb_div.appendChild(z_img_a)

	z_img = d.createElement('IMG')
	z_img.setAttribute("id", "lightboxImage")
	z_img_a.appendChild(z_img)

	b_obj = d.getElementsByTagName("body").item(0)
	b_obj.insertBefore(ov_div, b_obj.firstChild)
	b_obj.insertBefore(lb_div, ov_div.nextSibling)
}

function add_handl(el, ev, f) {
	if (el.addEventListener) {el.addEventListener(ev, f, false)}
	else if (el.attachEvent) {el.attachEvent('on'+ev, f)}
}

add_handl(window, "load", init_zi)
