/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	setFromStyleCookies();
	initLinks();
	tidyPage();
}

function resizePage()
{
	tidyPage();
}

function unloadPage() 
{
  	setStyleCookies();
}





function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		
 	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	if(theBody.className.match("popup"))
 	{
	 	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("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 tidyPage()
{
	// Make the height of the 'subnavigation' and 'content-inner' box on the home page match.	
	if(document.body)
	{
		if(document.body.className=="homepage")
			var div1 = document.getElementById("introduction");
		else
			var div1 = document.getElementById("subnavigation");
		var div2 = document.getElementById("content-inner");
		var div3 = document.getElementById("additional-content");
	
		var height1 = div1.offsetHeight;
		var height2 = div2.offsetHeight;
		var height3 = div3.offsetHeight;
		
		var diff1 = height1 - height2;
		var diff2 = height3 - height2;
		
		if(diff1>0 && diff1>diff2)
			div2.style.height=(height1-100)+'px';
		else if(diff2>0)
			div2.style.height=(height3)+'px';
		
		if(document.body.className=="homepage" && height1 < height2)
			div1.style.height=(height2-100)+'px'
			
			
			
		
			
		
	}	
	
	// Set the stylesheet for smalls screens if necessary.
	var theWidth = getBrowserWidth();
	if (theWidth <= 960)
	{
		setActiveStyleSheet("Small screen");
		//var pagediv = document.getElementById("page");
		//if(pagediv.offsetWidth<770)
		//	pagediv.style.width="770px";
		
	}
	else
		unsetActiveStyleSheet("Small screen");
	
	var width2 = getBrowserWidth();
	if (width2 > 1280)
		setActiveStyleSheet("Large screen");
	else
		unsetActiveStyleSheet("Large screen");
		
	
}



/* 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;
};


/* Based upon http://alistapart.com/stories/alternate/ */
function setActiveStyleSheet(title) 
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
			a.disabled = true;
			if(a.getAttribute("title") == title) 
			{
				a.disabled = true;
				a.disabled = false;
			}
		}
	}
}

/* Based upon http://alistapart.com/stories/alternate/ */
function unsetActiveStyleSheet(title) 
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
			if(a.getAttribute("title") == title) 
				a.disabled = true;
		}
	}
}


/* Based upon http://alistapart.com/stories/alternate/ */
function setStyleCookies()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			if(a.disabled) 
				createCookie("style"+i, 'false', 365);
			else
				createCookie("style"+i, 'true', 365);
		}
	}
	return null;
}

/* Based upon http://alistapart.com/stories/alternate/ */
function setFromStyleCookies()
{
	var i, a, cookie;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			cookie = readCookie("style"+i);
			if(cookie=='true')
			{
				a.disabled = true;
				a.disabled = false;
				
				if(a.getAttribute("title")=='Text version')
					document.styleSheets[1].disabled=true;
			}
			else 
			{
				a.disabled = true;
			}
		}
	}
}

/* Based upon http://alistapart.com/stories/alternate/ */
function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
		expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


/* Based upon http://alistapart.com/stories/alternate/ */
function readCookie(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;
}







/* The JavaScript Anthology - James Edwards & Cameron Adams */
function getBrowserWidth()
{
	if (window.innerWidth)
		return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth != 0)
		return document.documentElement.clientWidth;
	else if (document.body)
		return document.body.clientWidth;
	
	return 0;
}

/* The JavaScript Anthology - James Edwards & Cameron Adams */
function getBrowserHeight()
{
	if (window.innerHeight)
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight != 0)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
	
	return 0;
}


/* The JavaScript Anthology - James Edwards & Cameron Adams */
function getRoughPosition(ele, dir)
{
	var pos = dir == 'x' ? ele.offsetLeft : ele.offsetTop;
	var tmp = ele.offsetParent;
	while (tmp != null)
	{
		pos += dir == 'x' ? tmp.offsetLeft : tmp.offsetTop;
		tmp = tmp.offsetParent;
	}
	return pos;
}


function popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=550,height=550');");
}