/**
	neoPop v.1.0 - http://www.marcodeltongo.com/neoPop
	Auto-resizing image popup script.

	Copyright (c) 2005 by Marco Del Tongo <info@marcodeltongo.com>
	Licensed under the Open Software License version 2.1
 */

var neoPopMsgs = {

	en: {
		title:		'Image PopUp',
		loading:	'Loading...',
		error:		'Sorry !<br />An error occured !',
		close:		'Close window'
	},
	it: {
		title:		'Ingrandimento',
		loading:	'Caricamento...',
		error:		'Spiacente !<br />Immagine non disponibile.',
		close:		'Chiudi'
	},
	fr: {
		title:		'Agrandissement',
		loading:	'Chargement...',
		error:		'Désolé!<br />Une erreur s\'est produite!',
		close:		'Fin'
	},
	de: {
		title:		'Vergrößerung',
		loading:	'Laden...',
		error:		'Traurig!<br />Eine Störung trat auf!',
		close:		'Ende'
	},
	es: {
		title:		'Ampliación',
		loading:	'Cargamento...',
		error:		'ˇApesadumbrado!<br />ˇUn error ocurrió!',
		close:		'Cierre'
	}
};

function neoPop ( $element, $degradeTo )
{
	var $width = 256, $height = 64;
	var hM = 24, vM	= 64;
	var $popUp = false;

	$degradeTo = (!$degradeTo) ? "_blank" : $degradeTo;	// _self || _blank || _top || _parent

	// Try to find out what's the browser preferred language
	var $sLang = (navigator.language) ? navigator.language.substring(0,2) : ((navigator.browserLanguage) ? navigator.browserLanguage.substring(0,2) : 'en');
	// See if we have messages in that language or revert to english
	var $navLang = (neoPopMsgs[$sLang]) ? neoPopMsgs[$sLang] : neoPopMsgs['en'];

	// Degrade function
	this.degrade = function () 
	{
		switch($degradeTo) {
			case "_blank": {
				open($element.href);
			} break;
			case "_self": {
				location = $element.href;
			} break;
			case "_top": {
				top.location = $element.href
			} break;
			case "_parent": {
				parent.location = $element.href;
			} break;
			default: {
				open($element.href, $degradeTo);
			};
		};
		return false;
	};

	// Prepare window title
	var $altxt = ($element.getElementsByTagName) ?
		(($element.getElementsByTagName('IMG')[0] && $element.getElementsByTagName('IMG')[0].alt) ? $element.getElementsByTagName('IMG')[0].alt : $navLang.title) : 
		(($element.all.tags('IMG')[0] && $element.all.tags('IMG')[0].alt) ? $element.all.tags('IMG')[0].alt : $navLang.title);
	var $title = ($element.title) ? $element.title : $altxt;

	// Degrade if DOM not available
	if(!document.getElementById || !document.images) { return degrade(); };

	// Open new window using the image URL to avoid problems with KHTML Engine
	$popUp = window.open($element.href, '', 'width='+$width+',height='+$height+',left='+((screen.availWidth-$width)/2)+',top='+((screen.availHeight-$height)/2)+',resizable=1,scrollbars=1');

	// if popups are disabled...
	if(!$popUp) {
		return degrade();
	};

	if (!$popUp.document.open()) {	// Probably it's Safari/Konqueror or it's Opera
		if (!window.opera) {
			$popUp.resizeTo(screen.availWidth - 32, screen.availHeight - 64);
			$popUp.moveTo(16, 32);
		} else {	// It's Opera
			hM = 48;
			vM = 160;
			$popUp.resizeTo($width, $height+128);
		};
	};

	// Insert HTML code
	var $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
	$html += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="' + $sLang + '">\n<head>\n<title>' + $title + '</title>\n';
	$html += '<meta http-equiv="Content-Type" content="text/$html; charset=iso-8859-1" />\n';
	$html += '<meta http-equiv="Content-Language" content="' + $sLang + '" />\n';
	$html += '<meta name="MSSmartTagsPreventParsing" content="true" />\n<meta http-equiv="imagetoolbar" content="no" />\n';
	$html += '<meta name="generator" content="neoPop v.1.0 - http://www.marcodeltongo.com/neoPop" />\n';
	$html += '<style type="text/css">\n';
	$html += '  html { height: 100%; overflow: hidden; }\n';
	$html += '  body { height: 100%; margin: 0; padding: 0; text-align: center; }\n';
	$html += '  input { margin-top: 8px; }\n';
	$html += '  #loading { cursor: progress; width: 80%; border: 1px dotted #999; margin: 8px auto 0 auto; font: italic 14px/72px Georgia, Times, "Times New Roman", serif; text-align: center; background: #ccc; }\n';
	$html += '  #error { cursor: help; display: none; width: 100%; height: 100%; margin: 0; font: italic 14px/28px Verdana, Helvetica, Arial, sans-serif; text-align: center; background: #d00; color: White; }\n';
	$html += '  #content { visibility: hidden; margin: 0; padding: 8px 0; text-align: center; }\n';
	$html += '  img { cursor: pointer; cursor: hand; display: block; margin: 0 auto; border: 1px solid black; }\n';
	$html += '</style>\n';
	$html += '<script type="text/javascript">\n';
	$html += 'var hM = '+hM+', vM = '+vM+';\n';
	$html += 'function resizeToFit ( img )\n{\n';
	$html += '  var width = img.width, height = img.height;\n';
	$html += '  var left = 0, top = 0;\n';
	$html += '  document.getElementById("loading").style.display = "none";\n';
	$html += '  document.getElementById("content").style.visibility = "visible";\n';
	$html += '  document.getElementsByTagName("HTML")[0].style.overflow = "auto";\n';
	$html += '  window.resizeTo(width, height);\n';	
	$html += '  if (document.all) {\n  window.moveTo(0, 0);\n  };\n';	
	if (document.all && document.documentElement.clientHeight) {	// For IE 6 in compat mode
	$html += '  if (screen.availWidth < img.width) { width = screen.availWidth; window.resizeTo(width, height); left = 0;\n';
	$html += '  } else { width = document.body.clientWidth + hM; left = (screen.availWidth - width) / 2; window.resizeTo(width, height); };\n';
	$html += '  if (screen.availHeight < img.height) { height = screen.availHeight; window.resizeTo(width, height); top = 0;\n';
	$html += '  } else { height = document.body.clientHeight + vM; top = (screen.availHeight - height) / 2; window.resizeTo(width, height); };\n';
	} else {
	$html += '  if (screen.availWidth < img.width) { width = screen.availWidth; window.resizeTo(width, height); left = 0;\n';
	$html += '  } else { width = (img.width - document.body.clientWidth) + hM; left = (screen.availWidth - (img.width+hM)) / 2; window.resizeBy(width, 0); };\n';
	$html += '  if (screen.availHeight < img.height) { height = screen.availHeight; window.resizeTo(width, height); top = 0;\n';
	$html += '  } else { height = (img.height - document.body.clientHeight) + vM; top = (screen.availHeight - (img.height+vM)) / 2; window.resizeBy(0, height); };\n';
	};
	$html += '  window.moveTo(left, top);\n';
	$html += '  window.focus();\n';
	$html += '};\n';
	$html += 'function errorMsg ()\n{\n';
	$html += '  document.getElementById("loading").style.display = "none";\n';
	$html += '  document.getElementById("content").style.display = "none";\n';
	$html += '  document.getElementById("error").style.display = "block";\n';
	$html += '};\n';
	$html += '</script>\n';
	$html += '</head>\n<body>\n<div id="loading">' + $navLang.loading + '</div>\n';
	$html += '<div id="error">' + $navLang.error + '<br /><input type="button" onclick="window.close();" value="' + $navLang.close + '" /></div>\n';
	$html += '<div id="content"><a onclick="window.close();">';
	$html += '<img id="popImage" src="' + $element.href + '" alt="' + $altxt + '" title="' + $title + '" onerror="errorMsg();" onload="resizeToFit(this);" /></a>';
	$html += '\n<input type="button" onclick="window.close();" value="' + $navLang.close + '" />\n</div>\n</body>\n</html>';

	$popUp.document.write($html);
	$popUp.document.close();

	return false;
};

/**
	Autoapply to all or $wrapperId contained <A><IMG></A> $elements.
 */
function neoPopAuto ( $wrapperId )
{
	var $wrapper = ($wrapperId) ? document.getElementById($wrapperId) : document;
	var $anchors = ($wrapper.getElementsByTagName) ? $wrapper.getElementsByTagName("A") : $wrapper.all.tags("A");

	for (var i = 0; i < $anchors.length; ++i) {
		if ($anchors[i].href.match(/\.(gif|jpg|png|jpeg)$/i)) {
			if (!$anchors[i].onclick) {
				$anchors[i].onclick = function () { return neoPop(this); };
			};
		};
	};
};

neoPopAuto();