/////////////////////////////////////////////////////////////////////////////
// Version 2.1 Signed by Sid Paral @ WEDNESDAY on 2008/12/06 09:24:22 (GMT-0800)
// Authentication (do not edit) $G7QQAe$
// Copyright (c) 1999-2008 by Sid Paral. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
//
// image loading support JavaScript
// gallery support for American Journal
//

// load image into cache

function loadcache (picFile, width, height)
{
	if (document.images)
	{
		var	img = new Image (width, height);
		img.src = picFile;
		return true;
	}
	else return false;
}

// load one image into referenced position

function loadpic (picRef, picFile)
{
	if (document.images)
	{
		document[picRef].src = picFile;
	}
}

// open new "_blank" window with a picture and of given size

function openPixWnd(url, width, height)
{
	width	+=	36;
	height	+=	36;
	var	aW	=	screen.availWidth - 16;
	var aH	=	screen.availHeight - 48;
	if (width > aW)
		width = aW;
	if (height > aH)
		height = aH;
	window.open (url, "_blank",
				"toolbar=no,location=no,directories=no,status=no," +
				"menubar=no,scrollbars=yes,resizable=yes," +
				"screenX=0,screenY=0," +
				"width=" + width + ",height=" + height);
	return false;
}

/////////////////////////////////////////////////////////////////////////////

var	g_Gallery					=	new Array ();
var g_iGalleryIndex				=	-1;
var	g_sGalButtonPath			=	"../obrsky/";

function GalleryEntry (sLargeURL, iLargeWidth, iLargeHeight,
			  		   sThumbURL, sTitleText, sCaption,
			  		   bCSS)
{
	this.m_sLargeURL			=	sLargeURL;
	this.m_iLargeWidth			=	iLargeWidth;
	this.m_iLargeHeight			=	iLargeHeight;
	this.m_sThumbURL			=	sThumbURL;
	this.m_sTitleText			=	sTitleText;
	this.m_sCaption				=	sCaption;
	this.m_bCSS					=	bCSS;
}

function indexOfGalleryURL (sURL)
{
	var	i;
	for (i = 0; i < g_Gallery.length; ++i)
	{
		if (g_Gallery[i].m_sLargeURL == sURL)
			return i;
	}
	return -1;
}

function galButtonHTML(iIndex, sType, sAlt, bCSS)
{
	var		sRes			=	"";
	if (iIndex >= 0)
		sRes				+=	"<A HREF=\"" + g_Gallery[iIndex] + 
								"\" ONCLICK=\"return onGalleryPix(" + 
								iIndex + ");\">";
	sRes					+=	"<IMG " + 
								(bCSS ? "CLASS=\"pix_link\"" : "BORDER=0") +
								" ALT=\"" + sAlt + "\" SRC=\"" + 
								g_sGalButtonPath + sType + ".png\">";
	if (iIndex >= 0)
		sRes				+=	"</A>";
	sRes					+=	"\r\n";
	return sRes;
}

function galleryBody (iIndex)
{
	var		iLen			=	g_Gallery.length;
	var		bCSS			=	g_Gallery[iIndex].m_bCSS;
//	alert ("Gallery of " + iLen);
	var		sPrev			=	(iIndex > 0)
							?	(galButtonHTML(0, "le", "|&lt;", bCSS) +
								 galButtonHTML(iIndex - 1, "ln", "&lt;", bCSS))
							:	(galButtonHTML(-1, "lex", "|x", bCSS) +
								 galButtonHTML(-1, "lnx", "x", bCSS));
//	alert (sPrev);
	var		sNext			=	(iIndex < (iLen - 1))
							?	(galButtonHTML(iIndex + 1, "rn", "&gt;", bCSS) +
								 galButtonHTML(iLen - 1, "re", "&gt;|", bCSS))
							:	(galButtonHTML(-1, "rnx", "x", bCSS) +
								 galButtonHTML(-1, "rex", "x|", bCSS));
//	alert (sNext);
	var		sBody			=	"<CENTER>\r\n"
							+	sPrev
							+	(bCSS ? "<SPAN CLASS=\"gal_count\">" : "<FONT SIZE=\"+3\" COLOR=\"gray\">")
							+	"&nbsp;&nbsp;" + (iIndex+1) + "&nbsp;/&nbsp;" + iLen + "&nbsp;&nbsp;"
							+	(bCSS ? "</SPAN>" : "</FONT>")
							+	sNext
							+	"<BR><BR>\r\n"
							+	"<IMG SRC=\""
							+	g_Gallery[iIndex].m_sLargeURL
							+	"\" WIDTH=\""
							+	g_Gallery[iIndex].m_iLargeWidth
							+	"\" HEIGHT=\""
							+	g_Gallery[iIndex].m_iLargeHeight
							+	"\" ALT=\""
							+	g_Gallery[iIndex].m_sTitleText
							+	"\" TITLE=\""
							+	g_Gallery[iIndex].m_sTitleText
							+	"\"><BR><BR>\r\n"
							+	(bCSS ? "<SPAN CLASS=\"gal_caption\">" : "<FONT SIZE=\"+1\">")
							+	g_Gallery[iIndex].m_sCaption
							+	(bCSS ? "</SPAN>" : "</FONT>")
							+	"\r\n</CENTER>\r\n";
//	alert (sBody);
	return sBody;
}

function isValidGalleryIndex (iIndex, sWhere)
{
	if ((iIndex < 0) || (iIndex >= g_Gallery.length))
	{
		alert ("Error in " + sWhere + "(iIndex=" + iIndex + 
			   "): gallery index out of bounds (0 ... " + 
			   (g_Gallery.length-1) + ")");
		return false;
	}
	return true;
}

function onGalleryInvoke (iIndex)
{
	if (!isValidGalleryIndex (iIndex, "onGalleryInvoke"))
		return true;
	var		sLink				=	window.location.pathname;
	var		iSlashPos			=	window.location.pathname.lastIndexOf("/");
	if (iSlashPos >= 0)
		sLink					=	sLink.substr (iSlashPos + 1);
	window.open (sLink + "?" + iIndex, "_blank");
	return false;
}

function onGalleryPix (iIndex)
{
	if (!isValidGalleryIndex (iIndex, "onGalleryPix"))
		return true;
	document.body.innerHTML	=	galleryBody (iIndex);
	g_iGalleryIndex			=	iIndex;
	return false;
}

function onGalleryKey (e)
{
	if ((g_iGalleryIndex < 0) || (g_iGalleryIndex >= g_Gallery.length))
		return;
	var	iLast				=	g_Gallery.length - 1;
	var	iIndex				=	g_iGalleryIndex;
	// distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var objEvent			=	window.event ? event : e;
	var wcCode				=	objEvent.charCode ? objEvent.charCode : objEvent.keyCode;
	switch (wcCode)
	{
	case 33:	// pg-up
		iIndex				-=	10;
		break;
	case 34:
		iIndex				+=	10;
		break;
	case 35:	// end
		iIndex				=	iLast;
		break;
	case 36:	// home
		iIndex				=	0;
		break;
	case 37:	// up
	case 38:	// left
		--iIndex;
		break;
	case 39:	// right
	case 40:	// down
		++iIndex;
		break;
	case 27:
		window.close ();
		return;
	default:
//		alert ("event:onkeypress keyCode=" + wcCode);
		return;
	}
	if (iIndex < 0)
		iIndex				=	0;
	else if (iIndex > iLast)
		iIndex				=	iLast;
	if (iIndex == g_iGalleryIndex)
		return;
	onGalleryPix (iIndex);
}

function onGalleryLoaded ()
{
	g_iGalleryIndex			=	-1;
	if (window.location.search == "")
		return;
	var	sParam				=	window.location.search.substr(1);
	var	iIndex				=	isNaN(parseInt(sParam,10))
							?	indexOfGalleryURL (sParam)
							:	parseInt(sParam,10);
	if (iIndex < 0)
		return;
	var		sHTML			=	galleryBody (iIndex);
//	alert (sHTML);
	document.body.innerHTML	=	sHTML;
	g_iGalleryIndex			=	iIndex;
//	document.onkeypress		=	onGalleryKey;
	document.onkeyup		=	onGalleryKey;
}

/////////////////////////////////////////////////////////////////////////////

//var	g_iDeferrals		=	0;
//var	g_iLinks			=	-1;

function jo_verbalizeElement (e)
{
	var Attr			=	e.attributes;
	var	sVerb			=	e.nodeName;
	var i				=	0;
	for (i = 0; i < Attr.length; ++i)
	{
		sVerb			+=	" " + Attr[i].nodeName
						+	"=\"" + Attr[i].nodeValue + "\"";
	}
	return sVerb;
}

function jo_deQuote (s)
{
	if (typeof s != "string")
		return s;
	var	iLen			=	s.length;
	if (iLen < 2)
		return s;
	var	f				=	s.substr(0, 1);
	var	t				=	s.substr(iLen - 1, 1);
	return ((f == t) && ((f == "\"") || (f == "\'"))) ? s.substr(1, iLen - 2) : s;
}

function jo_deTagifyNode (n)
{
	if (n.nodeName == "#text")		// element
		return " " + n.nodeValue;
//	return "element (" + typeof n + " " + n.nodeName + ")";
	return jo_deTagify (n.childNodes);
}

function jo_deTagify (nodes)
{
	if (nodes.length == 0)
		return "";
	var	sRes		=	"";
	var	sNode		=	"";
	var	l			=	nodes.length;
	var i			=	0;
	for (i = 0; i < l; ++i)
	{
		if ((sNode	=	jo_deTagifyNode (nodes[i])) != "")
		{
			if (sRes != "")
				sRes+=	" ";
			sRes	+=	sNode;
		}
	}
	sRes			=	sRes.replace (/[ \r\n\t]+/g, " ");
	if (sRes.substr(0, 1) == " ")
		sRes		=	sRes.substr (1);
	if (sRes.substr(-1, 1) == " ")
		sRes		=	sRes.substr (0, sRes.length - 1);
	return sRes;
}

function jo_interpretImage (img)
{
	var	Res				=	new Array("","",false);
	if (img.nodeName != "IMG")
		return Res;
	var attrs			=	img.attributes;
	var	i				=	0;
	for (i = 0; i < attrs.length; ++i)
	{
		var	attr		=	attrs[i];
		if (attr.nodeName == "src")
			Res[0]		=	attr.nodeValue;
		else if (attr.nodeName == "alt")
			Res[1]		=	attr.nodeValue;
		else if (attr.nodeName == "class")
			Res[2]		=	(attr.nodeValue == "pix_pix");
	}
	return Res;
}

function jo_interpretCaption (o)
{
	// first find the parent that is TABLE (good) or BODY (bad)
	while (o.nodeName != "TABLE")
	{
		if (o.nodeName == "BODY")
			return "(*BODY*)";
		o				=	o.parentNode;
	}
	var	tds				=	o.getElementsByTagName ("td");
	if (tds.length == 0)
		return "(*no TABLE TDs*)";
	o					=	tds[tds.length-1];
	if (o.nodeName != "TD")
		return "(no TD)";
	var	c				=	o.childNodes;
	if (c.length == 0)
		return "(no TD children)";
	return jo_deTagify (c);
//	return o.innerHTML;
}

function jo_interpretAnchor (a)
{
	var attrs			=	a.attributes;
	var	sOnClick		=	"";
	var sTarget			=	"";
	var sHREF			=	"";
	var i				=	0;
	for (i = 0; i < attrs.length; ++i)
	{
	    var attr        =   attrs[i];
	    if (attr.nodeValue == null)
	        continue;
		if (attr.nodeName == "onclick")
			sOnClick	=	attr.nodeValue;
		else if (attr.nodeName == "target")
			sTarget		=	attr.nodeValue;
		else if (attr.nodeName == "href")
			sHREF		=	attr.nodeValue;
	}
	if ((sOnClick.substr(0, 18) != "return openPixWnd(") ||
		(sTarget != "_blank") ||
		(sHREF == ""))
		return null;
	sOnClick			=	sOnClick.substring (18, sOnClick.length - 1);
	var	opw				=	sOnClick.split(",");
	if (opw.length != 3)
		return null;
	opw[0]				=	jo_deQuote(opw[0]);
	var	Imgs			=	a.getElementsByTagName ("img");
	if (Imgs.length != 1)
		return null;
	var	imd				=	jo_interpretImage (Imgs[0]);
	// special case excluding the old journal map
	if ((imd[1] == "map") || (imd[1] == "mapa"))
		return null;
	return new GalleryEntry (opw[0], opw[1], opw[2],
				  			 imd[0], imd[1], 
				  			 jo_interpretCaption (a),
				  			 imd[2]);
}

function jo_onJournalLoaded ()
{
	g_iGalleryIndex			=	-1;
	var		ancs		=	document.getElementsByTagName("a");
	var		iAncs		=	ancs.length;
//	if ((iAncs != g_iLinks) &&
//		(++g_iDeferrals < 1000))
//	{		
//		g_iLinks		=	iAncs;
//		setTimeout("jo_onJournalLoaded();", 1);
//		return;
//	}
//	alert ("jo_onJournalLoaded: " + iAncs + " link(s) after " + g_iDeferrals + " deferral(s)");
	var		iGalIndex	=	0;
	var		iIndex		=	0;
	var		entry		=	null;
	for (iIndex = 0; iIndex < iAncs; ++iIndex)
	{
		if ((entry		=	jo_interpretAnchor (ancs[iIndex])) != null)
		{
			iGalIndex	=	g_Gallery.length;
			g_Gallery.push (entry);
			ancs[iIndex].galleryIndex
						=	iGalIndex;
			ancs[iIndex].onclick	
						=	function () { return onGalleryInvoke(this.galleryIndex); };
		}
	}
//	alert ("Created gallery with " + g_Gallery.length + " entries");
	if (window.location.search == "")
		return;
	var	sParam				=	window.location.search.substr(1);
	iIndex					=	isNaN(parseInt(sParam,10))
							?	indexOfGalleryURL (sParam)
							:	parseInt(sParam,10);
	if (iIndex < 0)
		return;
//	alert ("Gallery " + iIndex);
	var		sHTML			=	galleryBody (iIndex);
//	alert (sHTML);
	document.body.innerHTML	=	sHTML;
	g_iGalleryIndex			=	iIndex;
//	document.onkeypress		=	onGalleryKey;
	document.onkeyup		=	onGalleryKey;
}

/////////////////////////////////////////////////////////////////////////////
// deferred script invocation
// Dean Edwards/Matthias Miller/John Resig
// http://dean.edwards.name/weblog/2006/06/again/

function jo_init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
  jo_onJournalLoaded ();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", jo_init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      jo_init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      jo_init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = jo_init;

