/*! @header
	This JavaScript file provides support for mouseover for
	non-logged-in users.  It also provides some routines that
	are used in other scripts.

	@indexgroup Photo Gallery (JavaScript)
 */


/*! The last image moused over (used for the "Do not save this image" overlay). */
var lastelt = -1;

/*! A timer used for the "Do not save this image" overlay. */
var mytimer = null;

/*! QuickTime X security flaw workaround */
var quicktime_is_insecure = 1;

if (navigator.plugins && !quicktime_is_insecure) {
	for (i=0; i < navigator.plugins.length; i++ ) {
		if (navigator.plugins[i].name.indexOf("QuickTime") >= 0) {
			// alert(navigator.plugins[i].name);
			if (navigator.plugins[i].name == 'QuickTime Plug-in 7.6.6') {
				alert('QuickTime X in Mac OS X v.10.6 is incapable of playing back streams from a properly secured server.  Please use the VLC plug-in with FireFox to view this page.');
			}
		}
	}
}


/*! Finds the vertical position of the top edge of an element.  */
    function getAbsTop(cell) {
        var ret = cell.offsetTop;
        // alert("TAG: "+cell.tagName+" TOP: "+ret);
        if (cell.style.position != 'absolute') {
                tempnode = cell.offsetParent;
                while (tempnode && (tempnode.style.position != 'absolute')) {
                        // if (tempnode.tagName == "BODY") break;
                        if (tempnode.offsetTop === undefined) { break; }
                        ret += tempnode.offsetTop;
                        tempnode = tempnode.offsetParent;
                }
        }
        return ret;
    }   

/*! Finds the vertical position of the left edge of an element.  */
    function getAbsLeft(cell) {
        var ret = cell.offsetLeft;
        // alert("TAG: "+cell.tagName+" LEFT: "+ret);
        if (cell.style.position != 'absolute') {
                tempnode = cell.offsetParent;
                while (tempnode && (tempnode.style.position != 'absolute')) {
                        // if (tempnode.tagName == "BODY") break;
                        if (tempnode.offsetLeft === undefined) { break; }
                        ret += tempnode.offsetLeft;
                        tempnode = tempnode.offsetParent;
                }
        }
        return ret;
    }

/*! Draws the "Do not save these thumbnails" graphic over images when
    a non-logged-in user mouses over them.
 */
function imgmouseover(elt)
{
	var left = getAbsLeft(elt); // findPosX(elt);
	var top = getAbsTop(elt); // findPosY(elt);
	var height = elt.offsetHeight;
	var width = elt.offsetWidth;

	if (lastelt == elt) { return; }
	lastelt = elt;

	// alert('at '+left+','+top);

	var curlayer = document.getElementById('xlayer');
	if (!curlayer) {
		return; // browser doesn't suppory layers.
		// alert('layer issues....\n');
	}
	var curbglayer = document.getElementById('ylayer');
	if (!curbglayer) {
		return; // browser doesn't suppory layers.
		// alert('layer issues....\n');
	}

	var adjtop = top + (height / 2) - 75;
	var adjleft = left + (width / 2) - 75;

	var content = '<img style=\'position: absolute; top: ';
	content += adjtop;
	content += 'px; left: ';
	content += adjleft;
	content += 'px; \' src=\'/nosave.png\' onClick=\'clickthru(this);\' ';
	content += ' />\n';

	var bgcontent = '<img style=\'position: absolute; top: ';
	bgcontent += top;
	bgcontent += 'px; left: ';
	bgcontent += left;
	bgcontent += 'px; \' src=\'/nosavebg.png\' onClick=\'clickthru(this);\'  ';
	bgcontent += ' height='+height+' width='+width+' ';
	bgcontent += ' />\n';

	curbglayer.innerHTML = bgcontent;
	curlayer.innerHTML = content;
	if (mytimer) { clearTimeout(mytimer); }
	mytimer = setTimeout("clearlayer("+top+", "+left+")", 2000);
}

/*! Mouse handler for when the mouse leaves one of the regions with
    the "Do not save" graphic over them.  Currently unused.
 */
function imgmouseout(elt)
{
	return;
}

/*! Passes clicks through the "Do not save" image to the link of
    the image behind it.
 */
function clickthru(elt)
{
	var link = lastelt.parentNode;
	// alert('wait a sec.  (DAG:elt is '+lastelt+' link is '+link+')');
	
	document.location = link.href;
}

/*! Clears the "Do not save" graphic (called by a timeout from
    {@link imgmouseover imgmouseover} after a few second timeout).
 */
function clearlayer(top, left)
{
	// alert('cl: '+top+' '+left);
	var curlayer = document.getElementById('xlayer');
	if (!curlayer) {
		return; // browser doesn't suppory layers.
		// alert('layer issues....\n');
	}
	var curbglayer = document.getElementById('ylayer');
	if (!curbglayer) {
		return; // browser doesn't suppory layers.
		// alert('layer issues....\n');
	}
	curlayer.innerHTML = '';
	curbglayer.innerHTML = '';
}

// findPosX and findPosY liberally borrowed from QuirksMode.org.
/*! Finds the horizontal position of an element.  Code borrowed from
    QuirksMode.org.
 */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

/*! Finds the vertical position of an element.  Code borrowed from
    QuirksMode.org.
 */
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

/*! Logs something in the FireFox console.  */
function ff_consolelog(string)
{
var consoleService = Components.classes['@mozilla.org/consoleservice;1']
                               .getService(Components.interfaces.nsIConsoleService);
	consoleService.logStringMessage(string);

}

/*! Used by toggleFull to hide parts of the tree. */
function setHidden(elt, hide)
{
	if (!elt) { return; }
	var firstChild = elt.firstChild;
	var nextSibling = elt.nextSibling;
	if (elt.getAttribute && elt.getAttribute('showForFull') == 'true') {
		var myelt;
		if (hide) {
			myelt = document.createElement("div");
			myelt.id='cameFromHere';
			myelt.style.position = elt.style.position;
			myelt.style.left = elt.style.left;
			myelt.style.top = elt.style.top;
			myelt.style.display = 'none';
			elt.parentNode.insertBefore(myelt, elt);
			elt.style.position='absolute';
			elt.style.top='0px';
			elt.style.left='0px';

			document.body.appendChild(elt);
		} else {
			myelt = document.getElementById('cameFromHere');
			myelt.parentNode.insertBefore(elt, myelt);
			myelt.parentNode.removeChild(myelt);
			elt.style.position = myelt.style.position;
			elt.style.left = myelt.style.left;
			elt.style.top = myelt.style.top;
		}
	}
	if (elt.getAttribute && elt.getAttribute('hideForFull') == 'true') {
		// alert('elt is '+elt+' hide is: '+hide+' id is: '+elt.id);
		if (elt.id == 'myinfospan') {
			elt.setAttribute('ishidden', hide);
		} else {
			var display;
			if (hide) {
				display = elt.style.display;
				elt.setAttribute('ishidden', display);
				elt.style.display = 'none';
			} else {
				display = elt.getAttribute('ishidden');
				elt.style.display = display;
				elt.setAttribute('ishidden', '');
			}
		}
	}
	setHidden(firstChild, hide);
	setHidden(nextSibling, hide);
}

/*! Toggles full photo view. */
function toggleFull(elt)
{
	// alert('tf: '+elt);
	var infoelt = document.getElementById('myinfospan');
	if (infoelt) {
	    var state = infoelt.getAttribute('ishidden');
	    if (state == 1) {
		// Unhide everything.
		// alert('unhide: "'+elt.style.display+'"');
		setHidden(document, 0);
		elt.setAttribute('width', elt.getAttribute('oldwidth'));
	    } else {
		// Hide everything.
		// alert('hide: "'+elt.style.display+'"');
		setHidden(document, 1);
		// alert('width: '+elt.width);
		elt.setAttribute('oldwidth', elt.width);
		elt.setAttribute('width', 'auto');
	    }
	} else {
		alert('Could not find info element.');
	}
}


/*! Plays a movie */
function playMovie(event)
{
	// moviestill
	// movieurl

	// alert('event: '+event);

	var still = document.getElementById('moviestill');
	if (!still) return;

	var movie = document.getElementById('movie');

	event.cancelBubble = true;

	var width = still.offsetWidth;
	var height = still.offsetHeight + 16;

	movie.removeChild(still);
	var embed = tryload(width, height, 1);

	if (typeof(embed.video) == "undefined") {
		// alert('vlc is '+(typeof(embed.video) != "undefined" ? "enabled" : "disabled"));
		// alert('embed: '+embed);
	}
	movie.removeChild(embed);
	embed = tryload(width, height, 0);

}

function tryload(width, height, withvlc)
{

	var movie = document.getElementById('movie');
	var url = document.getElementById('movieurl');
	var auth = null;

	if (quicktime_is_insecure) {
		auth = document.getElementById('movieauth');
		document.cookie = "movieauth="+escape(auth.innerText)+"; path=/loadmovie.php; secure";
	}


	// alert('width: '+width+' height: '+height);

	var embed = document.createElement("embed");
	embed.height = height;
	embed.width = width;


	if (quicktime_is_insecure) {
		embed.src = '/loadmovie.php/'+url.innerText;
		// document.location = embed.src;
		// return;
	} else {
		embed.src = url.innerText;
	}
	// embed.setAttribute("TYPE", "application/x-shockwave-flash");


	if (withvlc) {
		// Load with VLC plug-in if available because QuickTime X
		// can't load authenticated HTTP streams at all.
		embed.setAttribute("TYPE", "application/x-vlc-plugin");
	}

	embed.setAttribute("pluginspage", "http://www.videolan.org");
	embed.setAttribute("version", "VideoLAN.VLCPlugin.2");

	embed.setAttribute("controller", "TRUE");
	embed.setAttribute("autoplay", "TRUE");
	// embed.setAttribute("QTSRCDONTUSEBROWSER", "TRUE");
	embed.style.marginTop="0px";
	embed.style.borderTop="0px";
	embed.style.paddingTop="0px";
	// embed.innerHTML = "<param name='QTSRCDONTUSEBROWSER' value='true'></param>";

	movie.appendChild(embed);

	var playbutton = document.getElementById('playbutton');
	playbutton.style.display='none';

	return embed;
}


function page_loaded()
{
	var playbutton = document.getElementById('playbutton');
	var still = document.getElementById('moviestill');

	// alert('pb: '+playbutton);

	if (playbutton && still) {
		var height = still.offsetHeight;
		var width = still.offsetWidth;

		playbutton.style.position = "absolute";
		playbutton.style.zIndex = "10000";

		var left = getAbsLeft(still);
		var top = getAbsTop(still);

		playbutton.style.left = left;
		playbutton.style.top = top;

		playbutton.style.display = "block";

		var height2 = playbutton.offsetHeight;
		var width2 = playbutton.offsetWidth;

		// alert('left: '+left+' top: '+top+' height: '+height+' width: '+width+' height2: '+height2+' width2: '+width2);

		playbutton.style.left = left + ((width - width2) / 2)
		playbutton.style.top = top + ((height - height2) / 2)
	}
}

