/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

 
attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	
	initLinks();
	initTables();
	initTabs();
	initNavigation();
	//tidyPage();
}

function resizePage()
{
	
}

function unloadPage() 
{

}

function initTabs()
{
	var tabs = document.getElementById("tabs");
	if(tabs != null)
	{
		var tab1link = document.getElementById("tab1link");
		var tab2link = document.getElementById("tab2link");
	
		if(tab1link != null && tab2link != null) 
		{
				
	
			attachEventListener(tab1link, "click", activateTab1, false);
			
			attachEventListener(tab2link, "click", activateTab2, false);
		}
	}	
}


function activateTab1()
{
	var tab2link = document.getElementById("tab2link");
	var tab1 = document.getElementById("tab1");
	var tab2 = document.getElementById("tab2");
	
	setClass(this.parentNode.parentNode,"current");
	removeClass(tab2link.parentNode.parentNode,"current");
	
	setClass(tab2,"hidden");
	removeClass(tab1,"hidden");
	
	return false;
}

function activateTab2()
{
	var tab1link = document.getElementById("tab1link");
	var tab1 = document.getElementById("tab1");
	var tab2 = document.getElementById("tab2");
	
	setClass(this.parentNode.parentNode,"current");
	removeClass(tab1link.parentNode.parentNode,"current");
	
	setClass(tab1,"hidden");
	removeClass(tab2,"hidden");
	
	return false;
}

function initNavigation() 
{
	var nav = document.getElementById("navigation");
	if(nav != null)
	{
	var uls = getChildrenByTagName(nav,"ul");
		for (var k = 0; k < uls.length; k++)
		{
			var listItems = getChildrenByTagName(uls[k],"li");
		
		var first = 0;
			var last = listItems.length-1;

			
			for (var i=0; i<listItems.length; i++) 
			{
				if(i==first) setClass(listItems[i],"first");
				if(i==last) setClass(listItems[i],"last");
				
				colourclass = 'colour' + ((i%5)+1);
				setClass(listItems[i],colourclass);
				
				if(listItems[i].className.match("last") && listItems[i].className.match("current"))
					setClass(listItems[i],"current-last");
				
				//listItems[i].style.zIndex=1000-i*10;
				
				attachEventListener(listItems[i], "mouseover", navigationOn, false);
				attachEventListener(listItems[i], "mouseout", navigationOff, false);
				attachEventListener(listItems[i], "focus", navigationOn, false);
				attachEventListener(listItems[i], "blur", navigationOff, false);
			}
		}
	}
};

function navigationOn() 
{
	var newclass="";
	for (j=1; j<=5; j++) 
	{
		var str="colour"+j;
		if(this.className.match(str))
			newclass="col"+j+"-hover";
	}
	if(this.className == "")
		this.className="hover " + newclass;
	else
		this.className+=" hover " + newclass;
}

function navigationOff()
{
	var newclass="";
	for (j=1; j<=5; j++) 
	{
		var str="colour"+j;
		if(this.className.match(str))
			newclass="col"+j+"-hover";
	}
	
	this.className=this.className.replace(new RegExp(" "+newclass+"\\b"), "");
	this.className=this.className.replace(new RegExp(newclass+"\\b"), "");
	
	this.className=this.className.replace(new RegExp(" hover\\b"), "");
	this.className=this.className.replace(new RegExp("hover\\b"), "");
}

function getChildrenByTagName(target, tagName)
{
	var children = target.childNodes;
	var matching = new Array();
	
	if (children != null)
	{
		for (var i = 0; i < children.length; i++)
		{
			if (children[i].nodeName.toLowerCase() == tagName)
			{
				matching[matching.length] = children[i];
			}
		}
	}
	
	return matching;
};

function tidyPage()
{
	// Fix up the image caption widths
	// Alternatively add inline styles manually to every div with class 'image','image-left' or 'image-right' to add a width equal to the image width
	
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
		if(divs[i].className.match("image"))
		{
			var imagelist = divs[i].getElementsByTagName("img");
			if(imagelist.length>0)
			{
				divs[i].style.width = (imagelist[0].width)+"px";
			}
		}
	}
}

function popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=550,height=550');");
}

function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		
 	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	if(theBody.className.match("new-window"))
 	{
	 	is_popup=true;
 		 window.focus();
	}
 
 	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";
 		}	
     	else if (anchor.className.match("print")) 
     	{
        	anchor.onclick = function() 
        	{
          		printPage();
          		return false;
        	};
    	}
    	else if (anchor.className.match("new-window")) 
     	{
        	anchor.onclick = function() 
        	{
          		popUp(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("file")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.open(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("close")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.close();
          		return false;
        	};
    	}
 	}
}

function initTables()
{
	var tables = document.getElementsByTagName("table");
	
	for (var i = 0; i < tables.length; i++)
	{
		var tbody = tables[i].getElementsByTagName("tbody")[0];
		var trs = tbody.getElementsByTagName("tr");
		
		for (var j = 1; j < trs.length; j += 2)
		{
			if (trs[j].className == "")
			{
				trs[j].className = "alt";
			}
			else if(!trs[j].className.match("alt"))
			{
				trs[j].className += " alt";
			}
		}
	}
	
	return true;
};

/* The JavaScript Anthology - James Edwards & Cameron Adams */
function attachEventListener(target, eventType, functionRef, capture)
{


 	if (typeof target.addEventListener != "undefined")
	{ 
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event)
		{
			if(typeof event == "undefined")
			{
				event = window.event
			};

			target["e" + functionString](event);
        };
		target.attachEvent("on" + eventType, target[functionString]);
	}
	else
	{
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			}
		}
		else
		{
			target[eventType] = functionRef;
		}
	}

	return true;
};

function setClass(el, str)
{
	if(el)
	{
		if (el.className == "")
			el.className = str;
		else
			el.className += " "+str;	
	}	
	return true;
}

function removeClass(el, str)
{
	if(el)
	{
		el.className=el.className.replace(new RegExp(" "+str+"\\b"), "");
		el.className=el.className.replace(new RegExp(str+"\\b"), "");	
	}
}