/* Image Popup Script 
Author: David Schontzler (stilleye.com)

--- Description ---
Displays image popup and automatically sizes & centers
window according to image (DOM browsers only, degrades otherwise)

--- Sample use ---
With title:
<a href="myImage.jpg" onclick="return iPop(this.href, 'Image title')">View my image</a>

Without title:
<a href="myImage.jpg" onclick="return iPop(this.href)">View my image</a>

Or, to make the text description the title...
<a href="myImage.jpg" onclick="return iPop(this.href, this.innerHTML)">View my image</a>
*/

var LoadingDots = 5;
var LoadingCurrent = 0;
var LoadingDirection = "right";
var LoadingTimeout = null;

function LoadingAnimation() {
	if( document.getElementById("LoadingAnimation") ) {
		var html = '';
		for(var i = 0; i <= LoadingDots; i++) {
			if(i == LoadingCurrent) html += '<strong>.</strong> ';
			else html += '. ';
		}
		
		if(LoadingCurrent == LoadingDots) LoadingDirection = "left";
		else if(LoadingCurrent == 0) LoadingDirection = "right";
		if(LoadingDirection == "right") LoadingCurrent++;
		else LoadingCurrent--;
		
		document.getElementById("LoadingAnimation").innerHTML = html;
	}
	LoadingTimeout = window.setTimeout('LoadingAnimation()', 250);
}

function SetupImageEvents() {
	var img = document.images[0];
	if(img && img.id == "PopupImage") {
		img.onload = function(e) { ShowImage(this, ImgTitle); }
		img.onerror = function(e) { NoImage(); }
	}
}

function ShowImage(img, title) {
	window.clearTimeout(LoadingTimeout);
	window.resizeTo(img.width, img.height);
	window.moveTo( (screen.width - img.width)/2, (screen.height - img.height)/2 );
	document.title = title;
	with( document.getElementById("Image").style ) {
		visibility = "visible";
		left = 0;
		top = 0;
	}
}

function NoImage() {
	window.clearTimeout(LoadingTimeout);
	document.title = "Image not found"
	
	document.getElementById("LoadingText").innerHTML = "Image not found";
	with( document.getElementById("LoadingBox") ) {
		style.cursor = "hand";
		style.cursor = "pointer";
		onclick = function() { window.close() }
	}
	var html = '';
	for(var i = 0; i <= LoadingDots; i++) html += '. ';
	with( document.getElementById("LoadingAnimation") ) {
		className = "error";
		innerHTML = html;
	}
}

function Popup(url, name, w, h, attribs, returnWin) {
	var winXpos = (screen.width - w) / 2;
	var winYpos = (screen.height - h) / 2;
	attribs = !attribs ? "" : attribs = ", " + attribs;
	var win = window.open(url, name, "width="+w+",height="+h+",left="+winXpos+",top="+winYpos+attribs);
	win.focus();
	if(returnWin) return win;
	return false;
}

function iPop(img, title) {
	if(!document.getElementById) return true;
	
	title = title || 'Image';
	
	var win = Popup("about:blank", "ImagePopupWindow", 120, 50, "", true);
	win._isPopup = true;
	
	var html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n<html>\n';
	html += '<head>\n<title>Loading...</title>\n<link href="image-popup.css" rel="stylesheet" type="text/css">';
	html += '\n<script type="text/javascript">ImgTitle="' + title.replace('"', '\\"') + '";</script>\n</head>';
	html += '\n<script src="image-popup.js" type="text/javascript"></script>\n</head>';
	html += '\n<body>\n<div id="LoadingBox"><div id="LoadingText">Loading Image</div><div id="LoadingAnimation"></div></div>';
	html += '\n<div id="Image"><a href="javascript:window.close()"><img src="' + img + '" alt="Click image to close" border="0" id="PopupImage"></a></div>';
	html += '\n</body>\n</html>';
	
	win.document.open();
	win.document.write(html);
	win.document.close();
	
	return false;
}

// only run the popup stuff it it is an actual image popup window
if(window.name == "ImagePopupWindow") {
	LoadingAnimation();
	SetupImageEvents();
}