/**
 * Copyright (c) 2006, Albert Dias
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 3) The name Albert Dias may not be used to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @author Albert Dias <albert.dias@gmail.com>
 * @version 1.0.x
 */

/// <summary>
///  Custom Global variables
/// </summary>

var GlobalBrowserVariablesSet;
var Opera;
var Opera6;
var Opera7;
var InternetExplorer;
var InternetExplorer6;
var NetscapeNavigator4;
var NetscapeNavigator6;
var W3CCompliant;
var DocumentBody;
var PixelSuffix;

/// <summary>
///  These functions set global browser identification variables and include commonly used routines
///  Significant portions of this code are based on Zorn, W. (2005), "wz_tooltip.js v. 3.37", www.walterzorn.com [http://www.walterzorn.com/tooltip/tooltip_e.htm] cited below
///
///  wz_tooltip.js    v. 3.37
///  The latest version is available at http://www.walterzorn.com or http://www.devira.com or http://www.walterzorn.de
///  Copyright (c) 2002-2005 Walter Zorn. All rights reserved.
///  Created 1.12.2002 by Walter Zorn (Web: http://www.walterzorn.com)
///  Last modified: 2.12.2005
///  Cross-browser tooltips working even in Opera 5 and 6, as well as in NN 4, Gecko-Browsers, IE4+, Opera 7 and Konqueror. No onmouseouts required. Appearance of tooltips can be individually configured via commands within the onmouseovers.
///  LICENSE: LGPL
///  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
///  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
///  For more details on the GNU Lesser General Public License, see http://www.gnu.org/copyleft/lesser.html
/// </summary>

function SetGlobalBrowserVariables()
{
	try
	{
		if (typeof(GlobalBrowserVariablesSet) == "undefined")
		{
			GlobalBrowserVariablesSet = true;
			Opera = !!(window.opera && document.getElementById);
			Opera6 = Opera && !document.defaultView;
			Opera7 = Opera && !Opera6;
			InternetExplorer = navigator.userAgent.toLowerCase().indexOf("msie") != -1 && document.all && ((document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body? document.body : null) && !Opera;
			InternetExplorer6 = InternetExplorer && parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE")+5)) >= 5.5;
			NetscapeNavigator4 = (document.layers && typeof document.classes != "undefined");
			NetscapeNavigator6 = (!Opera && document.defaultView && typeof document.defaultView.getComputedStyle != "undefined");
			W3CCompliant = !InternetExplorer && !NetscapeNavigator6 && !Opera && document.getElementById;
			DocumentBody = (((document.compatMode) && (document.compatMode != "BackCompat")) ? document.documentElement : ((document.body) ? document.body : null));
			PixelSuffix = (Opera6 || NetscapeNavigator4) ? "" : "px";
		}
	}
	catch (ExceptionObject)
	{
	}
}

function GetElement(Id)
{
	if (NetscapeNavigator4)
	{
		return((document.layers[Id] || null));
	}
	else
	{
		if (InternetExplorer)
		{
			return((document.all[Id] || null));
		}
		else
		{
			return((document.getElementById(Id) || null));
		}
	}
}

function ParseInteger(StringValue)
{
	var IntegerValue;
	IntegerValue = parseInt(StringValue);
	if (isNaN(IntegerValue))
	{
		return(0);
	}
	else
	{
		return(IntegerValue);
	}
}

function GetWidth(Tag)
{
	return(ParseInteger(NetscapeNavigator4 ? Tag.clip.width : (Tag.style.pixelWidth || Tag.offsetWidth)));
}

function GetHeight(Tag)
{
	return(ParseInteger(NetscapeNavigator4 ? Tag.clip.height : (Tag.style.pixelHeight || Tag.offsetHeight)));
}

/// <summary>
///  This function opens a centered popup
/// </summary>

function Popup(URL, Width, Height, Scrollable, Resizable)
{
	try
	{
		var Popup;
		if ((typeof(Width) == "number") && (typeof(Height) == "number"))
		{
			Popup = window.open(URL, "Popup", "width = " + Width + ", height = " + Height + ", top = " + ((screen.height - Height) / 2) + ", left = " + ((screen.width - Width) / 2) + ", resizable = " + (Resizable ? "yes" : "no") + ", scrollbars = " + (Scrollable ? "yes" : "no") + ", status = no");
		}
		else
		{
			Popup = window.open(URL, "Popup");
		}
	}
	catch (ExceptionObject)
	{
		alert("Your browser or a piece of software installed on your\ncomputer appears to be blocking popups on this page.\n\nPlease enable popups and refresh this page to continue.");
	}
	try
	{
		Popup.window.focus();
	}
	catch (ExceptionObject)
	{
	}
	return false;
}

/// <summary>
///  These functions parse the DOM and assign CSS selectors consistently across all user agents
/// </summary>

function AssignClassNameSuffix(Tag, ClassNameSuffix)
{
	try
	{
		if (typeof(Tag.className) == "undefined")
		{
			Tag.className = ClassNameSuffix;
		}
		else
		{
			Tag.className = Tag.className + " " + ClassNameSuffix;
		}
	}
	catch (ExceptionObject)
	{
	}
}

function AssignCssSelectors()
{
	try
	{
		SetGlobalBrowserVariables();
		var ListTag = document.getElementsByTagName("ul");
		var ListFirstChildTag = null;
		var ListLastChildTag = null;
		var ListItemTag = null;
		for (var i = 0; i < ListTag.length; i++)
		{
			ListFirstChildTag = null;
			ListLastChildTag = null;
			if (ListTag[i].hasChildNodes)
			{
				ListItemTag = ListTag[i].childNodes;
				for (var j = 0; j < ListItemTag.length; j++)
				{
					if (ListItemTag[j].tagName)
					{
						if (ListItemTag[j].tagName.toLowerCase() == "li")
						{
							if (ListFirstChildTag == null)
							{
								ListFirstChildTag = ListItemTag[j];
							}
							ListLastChildTag = ListItemTag[j];
						}
					}
				}
			}
			if (ListFirstChildTag != null)
			{
				AssignClassNameSuffix(ListFirstChildTag, "FirstChild");
				AssignClassNameSuffix(ListLastChildTag, "LastChild");
			}
		}
	}
	catch (ExceptionObject)
	{
		alert(ExceptionObject.message);
	}
}

/// <summary>
///  Functions used to Assign W3C Compatible Links
///  Portions of this code are based on Kokoska, T. (2003), "Proper way to append to a links onclick handler", thelist [http://lists.evolt.org/archive/Week-of-Mon-20031124/151971.html]
/// </summary>

function AppendOnClickFunction(AnchorTag, Commands)
{
	try
	{
		if (typeof(AnchorTag.onclick) == "function")
		{
			AnchorTag.OnClickFunction = AnchorTag.onclick;
			AnchorTag.onclick = new Function("event", ("this.OnClickFunction();" + Commands));
		}
		else
		{
			AnchorTag.onclick = new Function(Commands);
		}
	}
	catch (ExceptionObject)
	{
	}
}

function AssignAnchors()
{
	try
	{
		var LinkTag = document.getElementsByTagName("A");
		for (var i = 0; i < LinkTag.length; i++)
		{
			if (LinkTag[i].href.indexOf("#") == (LinkTag[i].href.length - 1))
			{
				AppendOnClickFunction(LinkTag[i], "return(false);");
			}
			else if ((typeof(LinkTag[i].rel) != "undefined") && (LinkTag[i].rel.toLowerCase() == "external"))
			{
				AppendOnClickFunction(LinkTag[i], "Popup(this.href);return(false);");
			}
		}
	}
	catch (ExceptionObject)
	{
	}
}

/// <summary>
///  These functions check for the existence of the Macromedia Flash Player and publish Flash content
///  Significant portions of this code are based on the javascript code generated by Macromedia Flash Player
///  and Stearns, G. (2005), "FlashObject v1.2.3: Flash detection and embed", [http://blog.deconcept.com/flashobject/] cited below
///
///  FlashObject v1.2.3: Flash detection and embed - http://blog.deconcept.com/flashobject/
///  FlashObject is (c) 2005 Geoff Stearns and is released under the MIT License:
///  http://www.opensource.org/licenses/mit-license.php
/// </summary>

function DetectFlashPlayer(RequiredVersionMajor)
{
	try
	{
		var DetectedVersionMajor;
		if ((navigator.mimeTypes) && (navigator.mimeTypes["application\/x-shockwave-flash"]))
		{
			if (navigator.mimeTypes["application\/x-shockwave-flash"].enabledPlugin)
			{
				DetectedVersionMajor = ParseInteger(navigator.plugins["Shockwave Flash"].description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".")[0]);
			}
		}
		else if (window.ActiveXObject)
		{
			var FlashPlayer = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			DetectedVersionMajor = ParseInteger(FlashPlayer.GetVariable("$version").split(" ")[1].split(",")[0]);
		}
		return((DetectedVersionMajor >= RequiredVersionMajor));
	}
	catch (ExceptionObject)
	{
		alert(ExceptionObject.description);
	}
	return(false);
}

function PublishFlashContent (VersionMajor, Id, Width, Height, Movie, ScriptAccess, Quality, BackgroundColor, ShowMenu, WindowMode, Scale, FlashVariables, HTMLAlternativeTagId)
{
	var FlashContentHTML = "";
	try
	{
		SetGlobalBrowserVariables();
		if (DetectFlashPlayer(VersionMajor))
		{
			FlashContentHTML += "\<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/fpdownload.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=" + VersionMajor + ",0,0,0\" id=\"" + Id + "\" width=\"" + Width + "\" height=\"" + Height + "\"\>\n";
			FlashContentHTML += " \<param name=\"movie\" value=\"" + Movie + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"allowScriptAccess\" value=\"" + ScriptAccess + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"quality\" value=\"" + Quality + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"bgcolor\" value=\"" + BackgroundColor + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"menu\" value=\"" + ShowMenu + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"wmode\" value=\"" + WindowMode + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"scale\" value=\"" + Scale + "\" \/\>\n";
			FlashContentHTML += " \<param name=\"FlashVars\" value=\"" + FlashVariables + "\" \/\>\n";
			FlashContentHTML += " \<embed type=\"application\/x-shockwave-flash\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" id=\"" + Id + "\" name=\"" + Id + "\" width=\"" + Width + "\" height=\"" + Height + "\" src=\"" + Movie + "\" allowScriptAccess=\"" + ScriptAccess + "\" swLiveConnect=\"true\" quality=\"" + Quality + "\" bgcolor=\"" + BackgroundColor + "\" menu=\"" + ShowMenu + "\" wmode=\"" + WindowMode + "\" scale=\"" + Scale + "\" flashvars=\"" + FlashVariables + "\" \/\>\n";
			FlashContentHTML += "\<\/object\>\n";
			if (typeof(HTMLAlternativeTagId) != "undefined")
			{
				if ((HTMLAlternativeTagId != null) && (HTMLAlternativeTagId != ""))
				{
					var HTMLAlternativeTag = GetElement(HTMLAlternativeTagId);
					if (HTMLAlternativeTag != null)
					{
						HTMLAlternativeTag.innerHTML = FlashContentHTML;
					}
					else
					{
						document.write(FlashContentHTML);
					}
				}
				else
				{
					document.write(FlashContentHTML);
				}
			}
			else
			{
				document.write(FlashContentHTML);
			}
		}
	}
	catch (ExceptionObject)
	{
		alert(ExceptionObject.description);
	}
}

/// <summary>
///  Automatically initialize the page
/// </summary>

window.onload = new Function("event", ("AssignCssSelectors();AssignAnchors();"));
