// This does the assorted preparations to the page. Add any general purpose housekeeping to this file.

function loadExtraStyles(){
// This determines if we are using IE6 and loads additional style sheets if we are

	// Split the string into part [0] and part [1]
	temp=navigator.appVersion.split('MSIE');
	// Parse the string for the "6" in 6.0
	ieVer=parseInt(temp[1]);
	// Is it greater than 6?
	var isIE6up=(ieVer <= 6) ? 1 : 0;
	
	if(isIE6up){

		var fileref=document.createElement("link");
  		fileref.setAttribute("rel", "stylesheet");
  		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", "/styles/dropdownmenu_ie6.css");
		document.getElementsByTagName("head")[0].appendChild(fileref)

		var fileref=document.createElement("link");
  		fileref.setAttribute("rel", "stylesheet");
  		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", "/styles/leftmenu_ie6.css");
		document.getElementsByTagName("head")[0].appendChild(fileref)
	}

} // end loadExtraStyles

// Menu Mouseovers. This just deals with the image replacement on the main menu

function MenuImage_Initialise(){

	// Find the menu items and assign event handlers to them all.
	var elementArray = new Array();
	elementArray = $('MainMenu').getElementsByTagName('a');
	for(var i = 0; i < elementArray.length; i++){
		/// ONLY DO THIS IF THE CHILD IS AN IMAGE.

		// Work out the two image states and store them now!
		var imageArray = elementArray[i].getElementsByTagName('img');

		if(imageArray[0]){
			// Off Image
			imageArray[0].imageOff = imageArray[0].src;
			// On Image
			imageArray[0].imageOn = MenuImage_getHoverImage(imageArray[0].src);
	
			// Add Event Handlers
			addEvent(elementArray[i],"mouseover",MenuImage_Hover,false);
			addEvent(elementArray[i],"mouseout",MenuImage_MouseOut,false);
		}

	} // end loop through items
	
} // end MenuImage_Initialise

function MenuImage_Hover(e){
	if(!e) var e = window.event;
	if(e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;

	// Get image state stored earlier and display
	targ.src = targ.imageOn;
} // end MenuImage_Hover

function MenuImage_MouseOut(e){
	if(!e) var e = window.event;
	if(e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;

	targ.src = targ.imageOff;
} // end MenuImage_MouseOut

function MenuImage_getHoverImage(myString){
//	alert(myString);
	var myBits = myString.split(".");
	var extension = myBits.pop();

	
	//alert(myBits[myBits.length - 1].substring(-3));
	// Check if this is already the hover version (for sites doing current page highlighting).
	if(myBits[myBits.length - 1].match(/_On$/)){
		return myString;
	}else{
		return myBits.join(".") + "_over." + extension;
	}
} // end MenuImage_getHoverImage


addEvent(window,"load", MenuImage_Initialise, false);
addEvent(window,"load", loadExtraStyles, false);

//// END MENU MOUSEOVERS

//// PDF Popups
function PdfPopup_Initialise(){

	// Find the menu items and assign event handlers to them all.
	var elementArray = new Array();
	elementArray = document.getElementsByTagName('a');
	for(var i = 0; i < elementArray.length; i++){

		// Check for class
		var pattern = new RegExp("(^| )" + "PdfPopup" + "( |$)");
		if(pattern.test(elementArray[i].className)){
			// Update link
			elementArray[i].theUrl = elementArray[i].href;
//			alert(elementArray[i].theUrl);
			elementArray[i].href = "javascript:void(0);";
			elementArray[i].target = "";
			
			// Add Event Handlers
			addEvent(elementArray[i],"click",doPdfPopup,false);
		}

	} // end loop through items

} // end PdfPopup_Initialise

function doPdfPopup(e){
if(!e) var e = window.event;
if(e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;

	// Get the link 
//	var theUrl = targ.parentNode.theUrl;
	var theUrl = targ.theUrl;
//	alert(theUrl);

//	return;	
//	alert(targ.theUrl);
	
	var win = window.open(theUrl, 'SitePopup', 'width=700,height=600,scrollbars=yes,resizeable=yes,status=yes,toolbar=no,menubar=yes,location=yes');
	return win;
	win.focus();
} // end doPdfPopup


addEvent(window,"load",PdfPopup_Initialise, false);