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


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

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

function resizePage()
{

}

function unloadPage() 
{

}




/* http://blog.peter-ryan.co.uk/2007/12/08/suckerfish-menus-with-hide-delay/ */
initMenu = function() 
{
	var timeout = 600;
	var cssClass = "hover";

	var queue = [];
	var reCSS = new RegExp("\\b" + cssClass + "\\b");
	var nav = document.getElementById("navigation");
	if(nav != null)
	{
		var sfEls = nav.getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
	
			// mouseover and mouseout handlers for regular mouse based interface.
			sfEls[i].onmouseover = function() {
				queueFlush();
				this.className += " " + cssClass;
			}
			sfEls[i].onmouseout = function() {
				queue.push([setTimeout(queueTimeout, timeout), this]);
			}
	
			// focus and blur handlers for keyboard based navigation.
			sfEls[i].onfocus = function() {
				queueFlush();
				this.className += " " + cssClass;
			}
			sfEls[i].onblur = function() {
				queue.push([setTimeout(queueTimeout, timeout), this]);
			}
	
			// click event handler needed for tablet type interfaces (e.g. Apple iPhone).
			sfEls[i].onclick = function(e) {
				if (this.className.search(reCSS) == -1) {
					// CSS not set, so clear all sibling (and decendants) menus, and then set CSS on this menu...
					var elems = this.parentNode.getElementsByTagName("li");
					for (var i=0; i<elems.length; i++) {
						elems[i].className = elems[i].className.replace(reCSS, "");
					}
					this.className += " " + cssClass;
				} else {
					// CSS already set, so clear all decendant menus and then this menu...
					var elems = this.getElementsByTagName("li");
					for (var i=0; i<elems.length; i++) {
						elems[i].className = elems[i].className.replace(reCSS, "");
					}
					this.className = this.className.replace(reCSS, "");
				}
				if (e && e.stopPropagation)
					e.stopPropagation();
				else
					window.event.cancelBubble = true;
			}
		}
		
	}

	queueFlush = function () {
		while (queue.length) {
			clearTimeout(queue[0][0]);
			queueTimeout();
		}
	}

	queueTimeout = function() {
		if (queue.length) {
			var el = queue.shift()[1];
			el.className = el.className.replace(reCSS, "");
		}
	}
	
	
	
}

function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		

 	if(document.body.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.style.display = 'inline';
	     	anchor.style.visibility = 'visible';
        	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=600,height=600');");
}

/* Author: Rebecca Skeers rebecca@webmistress.com.au 2005 */
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.");
	return false;
}


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