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

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

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

function resizePage()
{
}

function unloadPage() 
{
}
*/

window.addEvent('domready',tidyPage);
window.addEvent('domready',initLinks);

function tidyPage()
{
	// Fix up the image caption widths
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
	
		/* Add shadows to images */
		if(divs[i].className.match("box"))
		{
			var thecontent = divs[i].innerHTML;
				
			var innerDiv1 = document.createElement('div');
			innerDiv1.className="inner1";
			
			var innerDiv2 = document.createElement('div');
			innerDiv2.className="inner2";
				
				
			divs[i].innerHTML = '';
			innerDiv2.innerHTML = thecontent;
			innerDiv1.appendChild(innerDiv2);
			divs[i].appendChild(innerDiv1);
		}
		
	}
	
	
	// Make the page height at least as big as the browser to display the background image to the bottom of the browser.
	if(document.getElementById("wrapper2"))
	{
		pageHeight = document.getElementById("wrapper2").offsetHeight;
		browserHeight = getBrowserHeight();
		if(pageHeight < browserHeight)
			document.getElementById("wrapper2").style.height=browserHeight+'px';
	}
}


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

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 popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=550,height=550');");
}


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



/* Author: Rebecca Skeers */
function setClass(el, str)
{
	if(el)
	{
		if (el.className == "")
			el.className = str;
		else
			el.className += " "+str;	
	}	
	return true;
}

/* Author: Rebecca Skeers */
function removeClass(el, str)
{
	if(el)
	{
		el.className=el.className.replace(new RegExp(" "+str+"\\b"), "");
		el.className=el.className.replace(new RegExp(str+"\\b"), "");	
	}
}

/* Author: Rebecca Skeers */
function getPreviousSibling (elem)
{
	elem = elem.previousSibling;
	
	while (elem != null && (elem.tagName == null || elem.tagName == 'undefined')) 
	{
		elem = elem.previousSibling;
	}

	return elem;
}

/* Author: Rebecca Skeers */
function getNextSibling (elem)
{
	elem = elem.nextSibling;
	
	while (elem != null && (elem.tagName == null || elem.tagName == 'undefined')) 
	{
		elem = elem.nextSibling;
	}

	return elem;
}


function printPage() 
{
  	if (window.print)
		window.print()
	else
		alert("Sorry, your browser doesn't support the print feature. Use the File menu on your browser to select Print.");
}