window.addEventListener?window.addEventListener("load",init,false):window.attachEvent("onload",init);

//---------------------------------------------------[ INITIALIZE ]
function init()
{
	var userAgent = navigator.userAgent;
	var msIE7 = userAgent.indexOf('MSIE 7.0') != -1;
	var msIE6 = userAgent.indexOf('MSIE 6.0') != -1;
	var msIE55 = userAgent.indexOf('MSIE 5.5') != -1;
	if (msIE6 || msIE55)
	{
		findPNGs('right');
		fix_navigation('UL', 'navi_links');
		fix_navigation('UL', 'dropdown1');
		fix_navigation('UL', 'dropdown2');
		fix_navigation('UL', 'dropdown3');
		fix_navigation('UL', 'dropdown4');
		fix_navigation('UL', 'dropdown5');
	}
	toggleStyle("check");
}

function fix_navigation(tagName, elementClass)
{
	if (document.all && document.getElementsByTagName)
	{
		var ulRoot = document.getElementsByTagName(tagName);
		var ulCount = ulRoot.length;
		for (var u = 0; u < ulCount; u ++)
		{
			var ulClassName = ulRoot[u].className;
			if (ulClassName == elementClass)
			{
				var ulChildNodes = ulRoot[u].childNodes.length;		
				for (var n = 0; n < ulChildNodes; n ++)
				{
					node = ulRoot[u].childNodes[n];
					if (node.nodeName == "LI" && node.className != "current")
					{
						node.onmouseover=function() { this.className += "over"; }
						node.onmouseout=function() {	this.className = this.className.replace("over", ""); }
   				}
   			}
   		}
   	}
  	}
}

//------------------------------------------------[ display: THIS (ELEMENT, MODE=On/Off) ]
var currentElement;
function displayThis(element, mode)
{	
	if (currentElement) currentElement.hide();
	var element = $(element);
	(mode) ? element.show() : element.hide();
	currentElement = element;
}

//------------------------------------------------[ FIND ALL PNGS ]
function findPNGs()
{
	if (!document.getElementsByTagName) return; 
	var images = document.getElementsByTagName("IMG"); 
	for (var i = 0; i < images.length; i ++)
	{ 
		var image = images[i];
		var imageSrc = image.src;
		var regex = /.png/
		var foundPNG = regex.test(imageSrc);
		if (foundPNG)
		{
			fix_PNG(image);
		}
	}
}

function fix_PNG(imageRef)
{
	var imgID = (imageRef.id) ? "id='" + imageRef.id + "' " : "";
	var imgClass = (imageRef.className) ? "class='" + imageRef.className + "' " : "";
	var imgTitle = (imageRef.title) ? "title='" + imageRef.title + "' " : "title='" + imageRef.alt + "' ";
	var logoMargin = (imageRef.id == 'fyne_logo') ? 'margin: 8px;' : '';
	var imgStyle = "display: block; float: left;" + logoMargin + imageRef.style.cssText;
	var strNewHTML = "<span " + imgID + imgClass + imgTitle;
	strNewHTML += " style=\"" + "width:" + imageRef.width + "px; height:" + imageRef.height + "px;" + imgStyle; // + ";";
	strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	strNewHTML += "(src=\'" + imageRef.src + "\', sizingMethod='image');\"></span>";
	imageRef.outerHTML = strNewHTML;
}

//-------------------------------------------[ FIX CSS DROPDOWNS ]
function fixDropdown()
{
	if (document.all && document.getElementById)
	{
		navRoot = document.getElementById("navi_links");
		for (var i = 0; i < navRoot.childNodes.length; i ++)
		{
			node = navRoot.childNodes[i];

			if (node.nodeName == 'LI' && node.className == 'dropdown')
			{
				node.onmouseover = function()
				{
					this.className += ' over';
				}
				node.onmouseout = function()
  				{
  					this.className = this.className.replace(' over', '');
				}
   		}
   	}   		
  	}
}

//-------------------------------------------[ SET & GET: COOKIES ]
function create_cookie( name, value, days )
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	};
	document.cookie = name+"="+value+expires+"; path=/";
}

function read_cookie( name )
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	};
	return null;
};

function erase_cookie(name)
{
	create_cookie(name,"",-1);
};


//---------------------------------------[ SET: page style sheet. ]
function toggleStyle(setStyle)
{
	var i, linkTag;
	var styleState = read_cookie("simple"); // read current style status.
	if (styleState == null) styleState = 0;
	
	// toggle style state if requested.
	if (setStyle == "toggle") styleState = (!styleState);
	
	var cookieEnabled=(navigator.cookieEnabled)? true : false

	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
	{ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	};
	
	if (!cookieEnabled && setStyle == "toggle")
	{
		document.getElementById("cookie_note").innerHTML = "<strong>Please note:</strong> in order for this page's colour state to be remembered, cookies need to be enabled for this browser.";	
	};

		// for every link tag in the document.
		for(i=0; linkTag = document.getElementsByTagName("link")[i]; i++)
		{
			if (linkTag.getAttribute("type") == "text/css")
			{ // if a css link...
			
				linkTag.disabled = true; // set all external style links to disabled.
			
				// reset 'simple' style link to enabled as required.
				
				if (linkTag.getAttribute("title").indexOf("simple style") != -1 && styleState == true)
				{
					linkTag.disabled = false;
				};
    		
    			// reset standard style link to enabled as required.
    			if (linkTag.getAttribute("title").indexOf("standard style") != -1 && styleState == false)
    			{
    				linkTag.disabled = false;
    			};
    				
				// Set cookie conditions.
				if (styleState) 
				{
					create_cookie("simple","1",1);
				} else {
					erase_cookie("simple");
				};
   			};
   		};
}; // end function toggleStyle()

//-------------------------[ SETUP: train connection from passed index value. ]
function mystery( indexValue, subjectText )
{
	var mystery ="109,97,105,108,116,111,58,"; // setup code.
	var train = "?subject=" + subjectText; // setup subject as string.
	var coming = "&body=";
	
	mystery += return2sender(indexValue * 1); // get required code.
	var decode = mystery.split(","); // setup array from encoded.
	mystery = ""; // setup source variable as string.
	for (var e = 0; e < decode.length; e++)
	{
		mystery += String.fromCharCode(decode[e]); // decode as string.
	}
	document.location = mystery + train + coming; // Write call.
}