/*////////////////////////////////////////////////////////////
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 = 'Ogonnelloe School';
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>');
}



// sHover for Navigation
sfHover = function() {
	if (document.getElementById("nav")) {
		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") {
			anchor.target = "_blank";
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "image") {
			anchor.onclick = function () {
				window.open('image.php?'+this.href+'&'+this.title, 'imagePopUp', 'resizable=1,width=20,height=20');
				return false;
			}
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "legacy") {
			anchor.onclick = function () {
				window.open(this.href, 'legacy', 'resizable=1,scrollbars=1,width=825,height=600,top=20,left=20');
				return false;
			}
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "video") {
			anchor.onclick = function () {
				window.open(this.href, 'video', 'resizable=0,scrollbars=0,width=320,height=305,top=20,left=20');
				return false;
			}
		}
	}
}



// Toggle Function
function toggle(obj) {
	obj = document.getElementById(obj);
	if (obj.style.display == "none") {
		obj.style.display = "";
	} else {
		obj.style.display = "none";
	}
}



// Pop Up Window
function popUp(picURL, picTitle, w, h) {
	window.open(sPicURL+'?'+picTitle, 'popUp', 'resizable=1,width='+w+',height='+h+', scrollbars=no');
}



// Blockquote Cite Extraction
function extractBlockquoteCitations() {
	if (!document.getElementsByTagName) return;
	quotes = document.getElementsByTagName('blockquote');
	for (i = 0; i < quotes.length; i++) {
		title = quotes[i].getAttribute('title');
		cite = quotes[i].getAttribute('cite');

		if (title!='') {
			newdiv = document.createElement('div');
			newdiv.className = 'blockquote-cite';
			quotes[i].appendChild(newdiv);
			if (cite!='') {
				newlink = document.createElement('a');
				newlink.setAttribute('href', cite);
				newlink.setAttribute('title', cite);
				newlink.appendChild(document.createTextNode(title));
				newdiv.appendChild(newlink);
			} else {
				newdiv.appendChild(document.createTextNode(title));
			}
		}
	}
}



// Rotate images
function rotateImages(start) {
	if (!document.getElementById('centre-image')) return;
	holder 			= '';
	imageSrc 		= '';

	holder 			= document.getElementById('centre-image');
	holderClass 	= holder.className;
	holderClass 	= holderClass.split("setOf");
	holderClass 	= (holderClass.length>1)?holderClass[1]:'';
	parseFloat(holderClass);

	imageSrc 		= holder.getElementsByTagName('img')[0].src;
	imageName 	= imageSrc.split(".jpg");
	imageName 	= (imageName.length>1)?imageName[0]:'';
	imageName 	= imageName.substring(imageName.length-2);

	if (imageName.substring(1)=='0') { imageName = imageName.substring(1); }
	parseFloat(imageName);

	if (imageName <= (holderClass-1)) {
		imageName = parseFloat(imageName) + 1;
		if (imageName<'10') { imageName = '0'+ imageName; }
	} else {
		imageName = '00';
	}

	newSrc 		= imageSrc.substring(0,imageSrc.length-6);
	newSrc 		= newSrc + imageName + '.jpg';

	holder.getElementsByTagName('img')[0].src = newSrc;
	setTimeout(rotateImages, 3000);
}



// Activities
function minimiseCentreDetails() {
	if (!document.getElementById('related')) return;
	var related 	= document.getElementById('related');
	var list 			= related.getElementsByTagName('ul');

	for (var i=0; i<list.length; i++) {
		// Activities List Items
		if (list[i].className == 'activities') {

			toggleStatus = 'hidden';

			list[i].previousSibling.className = "showAll";
			list[i].previousSibling.title = "This toggles the display for all activities";
			list[i].previousSibling.onclick = function () {

				var listItems = this.nextSibling.childNodes;
				if(toggleStatus=='hidden') {
					for (var j=0; j<listItems.length; j++) {
						// cssClass('swap',listItems[j],'activities-hidden','activities-visible');
						cssClass('remove',listItems[j],'activities-hidden');
						cssClass('remove',listItems[j],'activities-visible');
						cssClass('add',listItems[j],'activities-visible');
						// cssClass('swap',listItems[j].firstChild.firstChild,'show','hide');
						cssClass('remove',listItems[j].firstChild.firstChild,'hide');
						cssClass('remove',listItems[j].firstChild.firstChild,'show');
						cssClass('add',listItems[j].firstChild.firstChild,'hide');
						toggleStatus='visible';
					}
				} else {
					for (var j=0; j<listItems.length; j++) {
						// cssClass('swap',listItems[j],'activities-visible','activities-hidden');
						cssClass('remove',listItems[j],'activities-hidden');
						cssClass('remove',listItems[j],'activities-visible');
						cssClass('add',listItems[j],'activities-hidden');
						// cssClass('swap',listItems[j].firstChild.firstChild,'hide','show');
						cssClass('remove',listItems[j].firstChild.firstChild,'hide');
						cssClass('remove',listItems[j].firstChild.firstChild,'show');
						cssClass('add',listItems[j].firstChild.firstChild,'show');
						toggleStatus='hidden';
					}
				}
				
			}


			var listItems = list[i].childNodes;
			for (var j=0; j<listItems.length; j++) {
				
				// For FioreFox's DOM
				if (listItems[j].nodeName=='LI') {
				
					// Maximise Activity
					listItems[j].id = "activity"+j;
					cssClass('add',listItems[j],'activities-visible');
					// listItems[j].setAttribute('className', 'activities-visible');

					// Create the Toggle Button
					newButton = document.createElement("span");
					listItems[j].insertBefore(newButton,listItems[j].firstChild);
					cssClass('add',newButton,'activities-toggle')

					newLink = document.createElement("a");
					newLink.href = "#";
					// newLink.setAttribute('class', 'show');
					newLink.className = "show";
					newLink.title = "This toggles the display for this activity";
					newLink.onclick = function () {
						listLinkParent = this.parentNode.parentNode;
						if(cssClass('check',listLinkParent,'activities-hidden')) {
							cssClass('swap',listLinkParent,'activities-visible','activities-hidden');
							this.innerHTML = '[ Minimise Activity ]';
							cssClass('swap',this,'hide','show');
						} else {
							cssClass('swap',listLinkParent,'activities-hidden','activities-visible');
							cssClass('swap',this,'show','hide');
							this.innerHTML = '[ Maximise Activity ]';
						}
						return false;
					}
					newButton.appendChild(newLink);

					linkText = document.createTextNode("[ Minimise Activity ]");
					newLink.appendChild(linkText);
					
				}
				
			}
		}
	}
}


// Actions for CSS 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;
	}
}



// Focus on Input Fields
// inputFocus = function () {
function inputFocus() {
	var focusEl = document.getElementsByTagName("INPUT");
	for (var i=0; i<focusEl.length; i++) {
		if ((focusEl[i].getAttribute("type") == "text")||(focusEl[i].getAttribute("type") == "password")) {
			focusEl[i].onfocus=function() {
				this.className+="sffocus";
			}
			focusEl[i].onblur=function() {
				this.className=this.className.replace(new RegExp("sffocus\\b"), "");
			}
		}
	}
	var focusTx = document.getElementsByTagName("TEXTAREA");
	for (var i=0; i<focusTx.length; i++) {
		focusTx[i].onfocus=function() {
			this.className+="sffocus";
		}
		focusTx[i].onblur=function() {
			this.className=this.className.replace(new RegExp("sffocus\\b"), "");
		}
	}
}
// if (window.attachEvent) window.attachEvent("onload", inputFocus);



// 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', minimiseCentreDetails);
EventManager.Add(window,'load', extractBlockquoteCitations);
// EventManager.Add(window,'load', setTimeout(rotateImages, 3000));
