/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

 
attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	checkBrowserWidth();
	initLinks();
	initCSSExpressions();
	initTables();
	tidyPage();
}

function resizePage()
{
	checkBrowserWidth();
}

function unloadPage() 
{

}

function tidyPage()
{
	// Fix up the image caption widths
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
		if(divs[i].className.match("image-left") || divs[i].className.match("image-right"))
		{
			var imagelist = divs[i].getElementsByTagName("img");
			if(imagelist.length>0)
			{
				var captionlist = divs[i].getElementsByTagName("div");
				
				if(captionlist!=null && captionlist.length>0)
				{
					for (var j = 0; j < captionlist.length; j++)
					{
						if(captionlist[j].className.match("caption"))
						{
							captionlist[j].style.width = (imagelist[0].offsetWidth)+"px";
						}
					}
				}
			}
		}
	
	}
}

/* http://www.themaninblue.com/experiment/ResolutionLayout/ */
function checkBrowserWidth()
{
	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	if(!theBody.className.match("new-window"))
 	{ 	
		var theWidth = getBrowserWidth();
		
		if (theWidth == 0)
		{
			var resolutionCookie = document.cookie.match(/(^|;)res_layout[^;]*(;|$)/);
	
			if (resolutionCookie != null)
			{
				setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
			}
			
			addLoadListener(checkBrowserWidth);
			
			return false;
		}
	
		if (theWidth <= 800)
		{
			setStylesheet("800 x 600");
			document.cookie = "res_layout=" + escape("800 x 600");
		}
		else if (theWidth <= 950)
		{
			setStylesheet("Small screen");
			document.cookie = "res_layout=" + escape("Small screen");
		}
		else
		{
			setStylesheet("");
			document.cookie = "res_layout=";
		}
	}		
	return true;
};




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;
};




function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};



// Returns the height in pixels of the 'in-focus' box.
function inFocusHeight()
{
	return document.getElementById("focus-box").offsetHeight;
}

// Retruns the width in pixels of the 'in-focus' box.
function inFocusWidth()
{
	return document.getElementById("focus-box").offsetWidth;
}

// Initialise any CSS expressions for IE
function initCSSExpressions()
{
	if (navigator.appVersion.indexOf("MSIE")!=-1)
	{
		// Fix the shadow around the 'in-focus' box for IE.
		var infocusBackground = document.getElementById("focus-background");
		if(infocusBackground)
		{
try{
		infocusBackground.style.setExpression("height","inFocusHeight()");
	
		infocusBackground.style.setExpression("width","inFocusWidth()");
} catch(e){}

		}
 	}
}


function popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=700,height=700');");
}


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;
};





