var browser=navigator.userAgent.toLowerCase();
var modalReturnTo = '';							//modalReturnTo stores the id of the item to bring to focus when the modal pane closes

document.domain="maynoothbns.ie";				//stops permission denied errors between www and projects domains

function styleNav(){
	//tidy up navigation style
	try {
		//make navigation bar always touch masthead...
		if (browser.indexOf("msie")==-1) document.getElementById('menuNav').style.height = document.getElementById('menuNavBar').clientHeight + "px";
		//if (browser.indexOf("safari")!=-1) document.getElementById('menuNav').style.overflow = "hidden"; //safari seems to round differently
	} catch (e) {
	}
}

function showModal(id,returnTo,title) {
	//show modal pane at center of browser window
	
	try {	
		var width, height;
	
		modalReturnTo = returnTo;				//modalReturnTo is a global var to store the id of the item to bring to focus when the modal pane closes
		
		if (showPopups()) {
			var centerAt = getCenter();
			var x = centerAt[0];
			var y = centerAt[1];
			//alert(x + ", " + y);
		}

		//fix lame IE6 that renders form input above standard layer model
		if (browser.indexOf("msie 6")!=-1) {
			if (document.getElementById('mainBody')) {
				var formInput = document.getElementById('mainBody').getElementsByTagName("input");
				for (var i = 0; i < formInput.length; i++) {
					if (formInput[i].className == "fInput") {
						formInput[i].style.visibility = "hidden";
						//formInput[i].style.display = "none";
					}
				}
				var formSelect = document.getElementById('mainBody').getElementsByTagName("select");
				for (var i = 0; i < formSelect.length; i++) {
					if (formSelect[i].className == "fInput") {
						formSelect[i].style.visibility = "hidden";
						//formSelect[i].style.display = "none";
					}
				}
			}		
		}
		
		document.getElementById(id).style.display = "block";

		if (!showPopups()) throw(e);

		var windowSize = getWindowSize();
		width = parseInt((windowSize[0] / 100) * 80);
		height = parseInt((windowSize[1] / 100) * 80);

		if (height == 0 || height<520) height = 520;
		if (width == 0 || width<600) width = 600;
		//alert(width);
		//alert(height);
						
		// position menu at center of screen
		document.getElementById(id).style.left = parseInt(x - (width/2)) + "px";
		
		//Math explanation for line below!
		//1/10 height of window + scroll offset will place window 10% down from top. Max height window will be centered then
		//(windowSize[1] / 10) + (y - (windowSize[1] / 2))
		//(windowSize[1] * .1) + (y - (windowSize[1] * .5));
		//y - (windowSize[1] * .4)
		document.getElementById(id).style.top = parseInt(y - (windowSize[1] * .4)) + "px";

		document.getElementById(id).style.width = width + "px"; //need this to get IE to behave!
		document.getElementById(id+'Iframe').style.height = (height - (document.getElementById(id).clientHeight - 55)) + "px"; //need this to get IE to behave!
		
		toggleInactiveLayer('deactivatePage');
		document.getElementById(id+"Close").focus();
		document.getElementById(id+"Title").innerHTML = "";
		document.getElementById(id+"Title").innerHTML = title;
	} catch (e) {
	}
}

function toggleInactiveLayer(id) {
	//gray out the background behind the modal pane
	
	try {
		if (id == "deactivateFrame") {
			//deactiveFrame layer is relative to userModal div but must be offset to the page origin
			document.getElementById(id).style.top = "-" + document.getElementById('projectModal').style.top;
			document.getElementById(id).style.left = "-" + document.getElementById('projectModal').style.left;	
		}

		var windowSize = getWindowSize();
		var width = document.body.scrollWidth;
		var height = document.body.scrollHeight;
		var w_height = windowSize[1];
		if (w_height > height) height = w_height;			//if body is short compared to window (probably opened in new window!)
		if (window.parent != window.self) height += 20;		//add a little overlap to stop a white line appearing

		
		if(document.getElementById(id) && document.getElementById(id).style.display=="block") {
			document.getElementById(id).style.display = "none";
			document.getElementById(id).style.width = "0"
			document.getElementById(id).style.height = "0"
		} else {
			document.getElementById(id).style.display = "block";
			document.getElementById(id).style.width = width + "px"
			document.getElementById(id).style.height = height + "px"
		}

	} catch (e) {
	}
}

function hideModal(id,src) {
	//hide modal pane
	
	try {	
		if (id == "projectModal") {			
			if (typeof src != "undefined" && src != '') {
					window.frames[id+'Iframe'].location = src;
					//document.getElementById('userModalIframe').src = src;
			 }
			document.getElementById(id+'Iframe').style.height = "55px";
		}

		//fix lame IE6 that renders form input above standard layer model
		if (browser.indexOf("msie 6")!=-1) {
			if (document.getElementById('mainBody')) {
				var formInput = document.getElementById('mainBody').getElementsByTagName("input");
				for (var i = 0; i < formInput.length; i++) {
					if (formInput[i].className == "fInput") {
						formInput[i].style.visibility = "visible";
						//formInput[i].style.display = "inline";
					}
				}
				var formSelect = document.getElementById('mainBody').getElementsByTagName("select");
				for (var i = 0; i < formSelect.length; i++) {
					if (formSelect[i].className == "fInput") {
						formSelect[i].style.visibility = "visible";
						//formSelect[i].style.display = "inline";
					}
				}
			}		
		}

		document.getElementById(id).style.display = 'none';
		document.getElementById(id+'Title').innerHTML = "";
		document.getElementById(id+'Title').innerHTML = "Loading...";

		toggleInactiveLayer('deactivatePage');
	} catch (e) {
		//alert(e);
	}
}

function cleanupModal(id) {
	//run after a modal pane is closed. container for miscellaneous cleanup items
	
	if (typeof id == "undefined") {
		id = modalReturnTo;
	 }
	
	try {	
		document.getElementById(id).focus();	//reset tabindex back to item that opened menu
	} catch (e) {
	}
	
	return false;
}

function getCenter() {
	//return the browser window center location
	
	var scrollTop = 0;
	var scrollLeft = 0;

	var windowSize = getWindowSize();
	var width = windowSize[0];
	var height = windowSize[1];

	if (document.documentElement) {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
	} else  if (document.body) {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
	}

	return [parseInt((width/2)+scrollLeft), parseInt((height/2)+scrollTop)] 
}

function getWindowSize() {
	//return the browser window dimensions

	var width = 800;
	var height = 600;

	try {
		if (window.innerHeight) {
			width = window.innerWidth;
			height = window.innerHeight;
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
	} catch (e) {
	}

	//alert(width + ", " + height);
	return [width, height] 
}

function resizeModal() {
	//resize the modal iframe appropriately for its content
	try {
		var max_h = 0;	//maximum height for modal pane
		var max_w = 0;	//maximum width for modal pane

		if (window.parent != window.self) {
			var windowSize = parent.getWindowSize();
			max_w = parseInt((windowSize[0] / 100) * 80);
			max_h = parseInt((windowSize[1] / 100) * 80);
		}
		
		if (max_h == 0 || max_w == 0) return;
		if (max_h<520) max_h = 520;
		if (max_w<600) max_w = 600;

		//content height
		var h = (document.getElementById('mainBody').clientHeight + 20);
		
		//control height
		var pmh = 0;			//project modal height
		if (parent.document.getElementById('projectModal')) {
			pmh = parent.document.getElementById('projectModal').clientHeight;
				if (pmh == 0) pmh = parent.document.getElementById('projectModal').offsetHeight;
		}
		var ifh = 0; 			//iframe height
		if (parent.document.getElementById('projectModalIframe')) {
			ifh = parent.document.getElementById('projectModalIframe').clientHeight;
				if (ifh == 0) ifh = parent.document.getElementById('projectModalIframe').offsetHeight;
		}
		var ch = pmh - ifh;
				
		//set up modal pane width
		parent.document.getElementById('modalWrapper').style.width = max_w + "px";

		//set up modal pane height
		if (h>(max_h - ch)) h=(max_h - ch);					//don't let the modal pane grow too big (hard for small screens)
		parent.document.getElementById('projectModalIframe').style.height = h + "px";
	} catch (e) {
	}
}

function showPopups() {
	//should we show our modal panes (absolute positioned div)?
	if (browser.indexOf("msie")!=-1 && browser.indexOf("mac")!=-1) return false;
	if (!document.getElementById) return false;
	//anything else?
	
	return true;
}

function addModalFrame(id,bg,url,close,closeimg,noFrames) {
	//create modal pane structure for iframe

	try {	
		if (showPopups() && niftyOk) {
			ed=document.createElement("div");
			ed.id=id;
			ed.style.position = "absolute";
			ed.style.display = "none";
			ed.style.borderBottomStyle = "none";
		
			insertTopCorners(ed,bg);
			insertModalPanel(ed,bg,url,close,id,closeimg,noFrames); 
			insertBottomCorners(ed);
			
			df = document.createElement("div");
			df.id = "deactivateFrame";
			ed.appendChild(df);

			document.getElementById("modalWrapper").appendChild(ed);
		}
	} catch (e) {
	}
}

function insertModalPanel(el,bg,url,close,id,closeimg,noFrames){
	//create a structured modal pane with a close button
	
	dp=document.createElement("div");
	dp.className = "styledPanel";
	dp.innerHTML = "";
	dp.innerHTML = "<h2 id=\"projectModalTitle\" class=\"" + bg + "\">Loading...<\/h2>";	

	insertClose(dp,close,url,id,closeimg);

	dpf=document.createElement("div");
	dpf.id = id + "IframeWrapper";
	dpf.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	dpf.innerHTML = "<iframe id=\"" + id + "Iframe\" name=\"" + id + "Iframe\" scrolling=\"auto\" frameborder=\"0\" width=\"100%\" height =\"55\" marginheight=\"0\" marginwidth=\"0\"  src=\"" + url + "\" title=\"" + id + "\">" + noFrames + "</iframe>";
	dp.appendChild(dpf);

	insertCloseForm(dp,close,url,id)

	el.appendChild(dp);
}

function insertTopCorners(el,bg){
	//create rounded corner styles for top of modal pane

	tc=document.createElement("div");
	tc.className = "topCorners";
	tc.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	tc.innerHTML = "<div class=\"l1\"></div><div class=\"l2 " + bg + "\"></div><div class=\"l3 " + bg + "\"></div><div class=\"l4 " + bg + "\"></div>"
	el.appendChild(tc);
}

function insertBottomCorners(el){
	//create rounded corner styles for bottom of modal pane
	
	bc=document.createElement("div");
	bc.className = "bottomCorners";
	bc.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	bc.innerHTML = "<div class=\"l4\"></div><div class=\"l3\"></div><div class=\"l2\"></div><div class=\"l1\"></div>"
	el.appendChild(bc);
}

function insertClose(el,close,url,id,closeimg){
	//create a close icon in the top right of a modal pane
	
	c=document.createElement("div");
	c.className = "closeIcon";
	c.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	c.innerHTML = "<a href=\"#\" id=\"" + id + "Close\" onClick=\"hideModal('" + id + "', '" + url + "'); return(cleanupModal());\" tabindex=\"2\" title=\"" + close + "\"><img src=\"" +  closeimg + "\" alt=\"" + close + "\"><\/a>"
	el.appendChild(c);
}

function insertCloseForm(el,close,url,id){
	//create a close icon in the top right of a modal pane

	f=document.createElement("form");
	f.className = "text center";
	f.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	f.innerHTML = "<input tabindex=\"2\" class=\"fButt\" type=\"button\" onClick=\"hideModal('" + id + "', '" + url + "'); return(cleanupModal());\" value=\"" + close + "\">";
	el.appendChild(f);
}
