var isIE = '\v' == 'v';

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 if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in 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;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}

function showBox (width, height, title, content) {
	if (arguments.length == 0) {
		width = tmpPUI.img.width;
		height = tmpPUI.img.height;
		title = tmpPUI.title;
		content = tmpPUI.content;
	}

	var wrapper = document.getElementById("popup-image-fsi-wrapper");
	if (!wrapper) {
		var wrapper = document.createElement("div");
		wrapper.id = "popup-image-fsi-wrapper";
		wrapper.style.display = "none";
		document.body.appendChild(wrapper);
	}

	var box = document.getElementById("popup-image-fsi");
	if (!box) {
		var box = document.createElement("div");
		box.id = "popup-image-fsi";
		document.body.appendChild(box);
	}
	
	var boxLoading = document.getElementById("popup-image-fsi-loading");
	if (!boxLoading) {
		boxLoading = document.createElement("div");
		boxLoading.id = "popup-image-fsi-loading";
		box.appendChild(boxLoading);
	}

	var boxContent = document.getElementById("popup-image-fsi-content");
	if (!boxContent) {
		var boxContent = document.createElement("div");
		boxContent.id = "popup-image-fsi-content";
		box.appendChild(boxContent);
	}
	
	var boxDescription = document.getElementById("popup-image-fsi-description");
	if (!boxDescription) {
		var boxDescription = document.createElement("div");
		boxDescription.id = "popup-image-fsi-description";
		box.appendChild(boxDescription);
	}
	
	//box.style.width = width + 20 + "px";
	//box.style.height = height + "px";

	var ps = getPageSize();
	box.style.top = (Math.ceil((Math.abs(ps[3] + ((isIE) ? document.documentElement.scrollTop * 2 : 0) - height)) / 2) - 20) + 'px';
	box.style.left = Math.ceil((ps[0] - width) / 2) + "px";
	
	wrapper.style.width = (isIE) ? ps[0] + "px" : "100%";
	wrapper.style.height = ps[1] + "px";
	
	wrapper.style.display = "block";
	box.style.display = "block";
	boxLoading.style.display = "none";
	
	if (title != "") {
		boxDescription.innerHTML =
			"<table style=\"width:" + width + "px;\">" +
				"<tr>" +
					"<td style=\"padding: 3px;\">" + title + "</td>" +
					"<td style=\"padding: 3px; text-align: right;\">" +
						"<a style=\"text-decoration: underline;\" onclick=\"closeBox();\">Закрыть</a> (Esc)" +
					"</td>" +
				"</tr>" +
			"</table>";
		boxDescription.style.display = "block";
	}
	if (content != "") {
		boxContent.innerHTML = content;
		boxContent.style.display = "block";
	}
	document.getElementsByTagName("body")[0].onkeypress = function (e) {
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		if (kC == Esc)
			closeBox();
		}
}

function showFullSizeImage(path, title) {
	tmpPUI = Object();
	tmpPUI.img = new Image();
	tmpPUI.img.src = path;
	
	tmpPUI.title = title;
	tmpPUI.content = 
		"<a onclick=\"document.getElementById('popup-image-fsi').style.display = 'none'; document.getElementById('popup-image-fsi-wrapper').style.display = 'none'\" title=\"Закрыть\">" +
			"<img src=\""+path+"\">" +
		"</a>";
	
	waitForImageLoading();
}

function waitForImageLoading() {
	if (tmpPUI.img.complete) {
		showBox();
		delete(tmpPUI);
	} else {
		setTimeout('waitForImageLoading();', 100);
	}
}

function closeBox() {
	document.getElementsByTagName("body")[0].onkeypress = null;
	document.getElementById("popup-image-fsi").style.display = "none";
	document.getElementById("popup-image-fsi-wrapper").style.display = "none";
}

window.onload = function() {
	var imgLinks = document.getElementsByTagName("a");
	var imgLinksCount = imgLinks.length;
	for (var x = (imgLinksCount - 1); x >= 0; x--) {
		if (imgLinks[x].className == "popup-image") {
			imgLinks[x].onclick = function () {
				showFullSizeImage(this.href, this.title);
				return false;
			}
		}
	}
}


