/*////////////////////////////////////////////////////////////
JavaScript Code Library
Developed by GrafX Design Division Pty Ltd
http://www.grafx.com.au

Last Checked In By : Richard Czeiger
Public Revision No : 1.0

The software and related user documentation are
protected under copyright laws and remain the sole
property of GrafX Design Division. Full license is
available on the GrafX Design Division web site or
in the original download.

Technical support is available via the GrafX web site at
http://www.grafx.com.au
////////////////////////////////////////////////////////////*/



// Get the hacked stylesheet
document.write('<style type="text/css">@import url("styles/hacks.css");</style>');



// Suppress JavaScript Errors
window.onerror=new Function("return true")



// Set the Status Bar Message
var statusMsg = 'Jim Henson Productions presents "The Dark Crystal"';
window.defaultStatus=statusMsg;



// Fixes IE6 CSS Rendering Bug
if (document.all && window.attachEvent) window.attachEvent("onload", fixWinIE);
function fixWinIE() {
	if (document.body.scrollHeight < document.body.offsetHeight) { document.body.style.display = 'block'; }
}



// Spam Suppression for Emails
function stopSpam(name, domain, subject) {
	var symbol = '@';
	// Took out the Subject due to MS fault in Outlook Express (Source - http://support.microsoft.com/kb/q182985/ )
	// document.write('<a href="mailto:' + name + symbol + domain + '&subject=' + subject + '">' + name + symbol + domain + '</a>');
	document.write('<a href="mailto:' + name + symbol + domain + '">' + name + symbol + domain + '</a>');
	if (!document.getElementsByTagName) return;
	var spans = document.getElementsByTagName("span");
	for (var i=0; i<spans.length; i++) {
		if (spans[i].getAttribute("rel") == "noscript") {
			// spans[i].setAttribute("class", "hiddenNoscript");
			cssClass('add',spans[i],'hiddenNoscript','')
		}
	}
}



// sHover for Navigation
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("sfhover\\b"), "");
		}
	}
}



// XHTML External Link Script
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			// Opens Link in New Window
			anchor.target = "_blank";
			anchor.title = (anchor.title!='') ? anchor.title + " - opens in a new window" : "Opens in a new window" ;
			anchor.setAttribute("className", "external");
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "image") {
			anchor.target = "_blank";
			anchor.onclick = function () {
				window.open('image.php?'+this.href, 'imagePopUp', 'resizable=1,top=10,left=10,width=20,height=20');
				return false;
			}
		}
	}
}



// Toggle Function
function toggle(obj) {
	if (obj.style.display == "none") {
		obj.style.display = "";
	} else {
		obj.style.display = "none";
	}
}



// Pop Up Image Window
function PopupPic(sPicURL, sPicTitle) {
	window.open('image.html?'+sPicURL+'&'+sPicTitle, 'imagePopUp', 'resizable=1,width=20,height=20');
}



// Resposition the Content DIV and add two new ones
function repositionContent() {
    if (!document.getElementsByTagName) return;
	myContent = document.getElementById("content");
	cssClass('add',myContent,'contentJS','');

	mastheadJS = document.createElement("div");
	mastheadJS.onclick = function () { document.location.href = './'; return false; }
	myContent.insertBefore(mastheadJS,myContent.firstChild);
	cssClass('add',mastheadJS,'mastheadJS','');

	footerJS = document.createElement("div");
	// footerJS.onclick = function () { document.location.href = './'; return false; }
	// myContent.insertBefore(footerJS,myContent.lastChild);
	myContent.appendChild(footerJS);
	// secondLast = myContent.childNodes.length - 2;
	// myContent.insertBefore(footerJS,myContent.childNodes[secondLast]);
	cssClass('add',footerJS,'footerJS','');
}



// Get Elements By Class Name
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}



// Manage Element Classes
function cssClass(action,object,class1,class2) {
	// swap 		replaces class class1 with class class2 in object.
	// add 			adds class class1 to the object.
	// remove 	removes class class1 from the object.
	// check 		test if class class1 is already applied to object and returns true or false.
	switch (action) {
		case 'swap':
		object.className=!cssClass('check',object,class1)?object.className.replace(class2,class1): object.className.replace(class1,class2);
	break;
	case 'add':
		if(!cssClass('check',object,class1)){object.className+=object.className?' '+class1:class1;}
	break;
	case 'remove':
		var rep=object.className.match(' '+class1)?' '+class1:class1;
		object.className=object.className.replace(rep,'');
	break;
	case 'check':
		return new RegExp('\\b'+class1+'\\b').test(object.className)
	break;
	}
}



// Animate the urRu at the bottom of the screen
function scrollUrRu() {
	if (!document.getElementById('urru-march')) return;
	var marchDiv 	= document.getElementById('urru-march');
	var marchLink 	= marchDiv.getElementsByTagName('a')[0];

	marchLink.style.left = (marchLink.offsetLeft - 1)+'px';
	setTimeout(scrollUrRu, 1000);
}



// Font Detection
// Source: http://www.lalit.org/lab/fontdetect.php
// Notes: Acutal Fuction that does all the work. Returns an array with all the info.
// My Assumption is that most of the browsers will have arial set as their default san-serif font.
var Detector = function(){
	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");

	d.appendChild(s);
	d.style.fontFamily = "sans-serif";		//font for the parent element DIV.
	s.style.fontFamily = "sans-serif";		//arial font used as a comparator.
	s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
	s.innerHTML        = "mmmmmmmmmml";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
	h.appendChild(d);
	var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
	var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
	h.removeChild(d);

	/* test
	 * params:
	 * font - name of the font you wish to detect
	 * return:
	 * f[0] - Input font name.
	 * f[1] - Computed width.
	 * f[2] - Computed height.
	 * f[3] - Detected? (true/false).
	 */
	function test(font) {
		h.appendChild(d);
		var f = [];
		f[0] = s.style.fontFamily = font;	// Name of the font
		f[1] = s.offsetWidth;					// Width
		f[2] = s.offsetHeight;					// Height
		h.removeChild(d);

		font = font.toLowerCase();
		if (font == "arial" || font == "sans-serif") {
			f[3] = true;	// to set arial and sans-serif true
		} else {
			f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
		}
		return f;
	}
	this.test = test;
}
var fonts = [];

// Initialise Font Detector
function initialiseFontDetector() {
	fonts.push("Papyrus");
	d = new Detector();
	// compute height and width for all fonts
	for (i=0; i<fonts.length; i++) {
		fonts.push(d.test(fonts.shift()));
	}

	if (d.test('Papyrus')[3]) {
		// cssClass('add','content','papyrusFont');
		var content = document.getElementById('content');
		// content.setAttribute("class", "papyrusFont");
		content.style.fontFamily = "Papyrus";
		content.style.fontSize = "125%";
		content.style.lineHeight = "165%";
	}

}



// Global Cookie Function
function setCookie(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 getCookie(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 eraseCookie(name) {
	setCookie(name,'',-1);
}



// Event-Manager Script by Keith Gaughan

// For implementations that don't include the push() methods for arrays.
if (!Array.prototype.push) {
	Array.prototype.push = function(elem) {
		this[this.length] = elem;
	}
}

var EventManager = {
	_registry: null,

	Initialise: function() {
		if (this._registry == null) {
			this._registry = [];
			// Register the cleanup handler on page unload.
			EventManager.Add(window, "unload", this.CleanUp);
		}
	},

	// Registers an event and handler with the manager.
	Add: function(obj, type, fn, useCapture) {
		this.Initialise();
		// If a string was passed in, it's an id.
		if (typeof obj == "string")
			obj = document.getElementById(obj);
		if (obj == null || fn == null)
			return false;
		// Mozilla/W3C listeners?
		if (obj.addEventListener) {
			obj.addEventListener(type, fn, useCapture);
			this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
			return true;
		}
		// IE-style listeners?
		if (obj.attachEvent && obj.attachEvent("on" + type, fn)) {
			this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
			return true;
		}
		return false;
	},

	// Cleans up all the registered event handlers.
	CleanUp: function() {
		for (var i = 0; i < EventManager._registry.length; i++) {
			with (EventManager._registry[i]) {
				// Mozilla/W3C listeners?
				if (obj.removeEventListener)
					obj.removeEventListener(type, fn, useCapture);
				// IE-style listeners?
				else if (obj.detachEvent)
					obj.detachEvent("on" + type, fn);
			}
		}
		// Kill off the registry itself to get rid of the last remaining references.
		EventManager._registry = null;
	}
};



// Multiple Onload Functions to be called
// EventManager.Add(window,'load', sfHover);
EventManager.Add(window,'load', externalLinks);
// EventManager.Add(window,'load', initialiseFontDetector);
EventManager.Add(window,'load', repositionContent);
EventManager.Add(window,'load', setTimeout(scrollUrRu, 1000));
