var toOpen;
var pageName; 
var xmlHttp; 

/* Main Navigation Structure IDs - specified in Main_Page */
var nav = new Array(); 
nav.push('when');
nav.push('about');
nav.push('attend');
nav.push('participate');
nav.push('sponsors');
nav.push('location');

/* 
** This function could be shortened with some looped array work
** essentiually need id and page title in an array
** this kind of set up would also help with extensibility and easy
** changes to the site navigations. 
** This function builds the navigation menu and adds in JavaScript function
** that is not permitted by the MediaWiki software.
*/
function buildNavigationMenu() {
	// check if there is any div with if of 'nav-menu' to build
	// i.e. check if we're on the main page.
	if(document.getElementById('nav-menu') != null) {
	// on main page, or there is a nav-menu to be built.
		var navMenu = "";	
		navMenu += "<table id=\"when\" onmouseout=\"unselect('when');\" onmouseover=\"select('when');\" class=\"menu\"><tr><td id=\"menu-directive-when\" class=\"menu-directive-when-idle\"><h1 class=\"directive\"><a href=\"/2009/mediawiki/index.php/WHENWHERE\">WHEN &#124; WHERE</a></h1></td><td class=\"menu-blurbs-when\"><p class=\"blurb-line\">23 - 27 NOVEMBER 2009</p><p class=\"blurb-line\">The University of Melbourne</p></td><td class=\"menu-art\"><div id=\"shadow-when\" class=\"border-art shadow-when\"><!-- --></div></td></tr></table>";
		navMenu += "<table id=\"about\" onmouseout=\"unselect('about');\" onmouseover=\"select('about');\" class=\"menu\"><tr><td id=\"menu-directive-about\" class=\"menu-directive-about-idle\"><h1 class=\"directive\"><a href=\"/2009/mediawiki/index.php/ABOUT_OZCHI\">ABOUT OZCHI</a></h1></td><td class=\"menu-blurbs-about\"><p class=\"blurb-line\">&nbsp;</p><p class=\"blurb-line\">&nbsp;</p></td><td class=\"menu-art\"><div id=\"shadow-about\" class=\"border-art shadow-about\"><!-- --></div></td></tr></table>";
		navMenu += "<table id=\"attend\" onmouseout=\"unselect('attend');\" onmouseover=\"select('attend');\" class=\"menu\"><tr><td id=\"menu-directive-attend\" class=\"menu-directive-attend-idle\"><h1 class=\"directive\"><a href=\"/2009/mediawiki/index.php/ATTEND\">ATTEND</a></h1></td><td class=\"menu-blurbs-attend\"><p class=\"blurb-line\">&nbsp;</p><p class=\"blurb-line\">&nbsp;</p></td><td class=\"menu-art\"><div id=\"shadow-attend\" class=\"border-art shadow-attend\"><!-- --></div></td></tr></table>";
		navMenu += "<table id=\"participate\" onmouseout=\"unselect('participate');\" onmouseover=\"select('participate');\" class=\"menu\"><tr><td id=\"menu-directive-participate\" class=\"menu-directive-participate-idle\"><h1 class=\"directive\"><a href=\"/2009/mediawiki/index.php/PARTICIPATE\">PARTICIPATE</a></h1></td><td class=\"menu-blurbs-participate\"><p class=\"blurb-line\">&nbsp;</p><p class=\"blurb-line\">&nbsp;</p></td><td class=\"menu-art\"><div id=\"shadow-participate\" class=\"border-art shadow-participate\"><!-- --></div></td></tr></table>";
		navMenu += "<table id=\"sponsors\" onmouseout=\"unselect('sponsors');\" onmouseover=\"select('sponsors');\"  class=\"menu\"><tr><td id=\"menu-directive-sponsors\" class=\"menu-directive-sponsors-idle\"><h1 class=\"directive\"><a href=\"/2009/mediawiki/index.php/SPONSORS\">SPONSORS</a></h1></td><td class=\"menu-blurbs-sponsors\"><p class=\"blurb-line\">&nbsp;</p><p class=\"blurb-line\">&nbsp;</p></td><td class=\"menu-art\"><div id=\"shadow-sponsors\" class=\"border-art shadow-sponsors\"><!-- --></div></td></tr></table>";
		document.getElementById('nav-menu').innerHTML=navMenu; 	
	}
	else if(document.getElementById('sub-page-menu') != null) {
		
		document.getElementById('when').onmouseover = function() { if(check('when')) { this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/when_sel.png) 0 0 no-repeat";}};
		document.getElementById('when').onmouseout = function() { if(check('when')) {this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/when_idle.png) 0 0 no-repeat";}};
		document.getElementById('about').onmouseover = function() {if(check('about')) {this.style.background="#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/about_sel.png) 0 0 no-repeat";}};
		document.getElementById('about').onmouseout = function() { if(check('about')) {this.style.background="#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/about_idle.png) 0 0 no-repeat";}};
		document.getElementById('attend').onmouseover = function() { if(check('attend')) {this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/attend_sel.png) 0 0 no-repeat";}};
		document.getElementById('attend').onmouseout = function() { if(check('attend')) {this.style.background="#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/attend_idle.png) 0 0 no-repeat";}};
		document.getElementById('participate').onmouseover = function() { if(check('participate')) {this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/participate_sel.png) 0 0 no-repeat";}};
		document.getElementById('participate').onmouseout = function() { if(check('participate')) {this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/participate_idle.png) 0 0 no-repeat";}};
		document.getElementById('sponsors').onmouseover = function() { if(check('sponsors')) {this.style.background= "#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/sponsors_sel.png) 0 0 no-repeat";}};
		document.getElementById('sponsors').onmouseout = function() { if(check('sponsors')) {this.style.background="#f5f5f5 url(/2009/mediawiki/skins/ozchi/images/sponsors_idle.png) 0 0 no-repeat";}}; 						
	}
	else {
		return;
	}	
	
	// ensure borders are correct height
		var i;
		var n = nav.length;
		for(i=0; i<n; i++) {
			if(document.getElementById(nav[i]) != null ) {
			var h = document.getElementById(nav[i]).offsetHeight;			
			document.getElementById("shadow-"+nav[i]).style.borderBottomWidth = (h-10)+"px";		
			}
		}
	
}; 

/* this function checks to see if the class name is one which is already selected
** in the case that it is, do not change its background icon.
*/
function check(objNode) {

	if(document.getElementById(objNode).className==("spm-"+objNode+"-sel") )
		return false;
	return true;	

}

/* Function uses an id to look up element an then change its class name */
function unselect(name) {				
	var anavcomp = document.getElementById('menu-directive-'+name); 		
	anavcomp.className = "menu-directive-"+name+"-idle";	
};

/* Function uses an id to look up element an then change its class name */
function select(name) {	
	var anavcomp = document.getElementById('menu-directive-'+name); 		
	anavcomp.className = "menu-directive-"+name+"-sel";	
};

function openPageInMainNav(ref,page)
{		
	toOpen=ref;
	pageName=page;
			
	if (window.XMLHttpRequest)
	{
		// Firefox, Safari, ...
		xmlHttp = new XMLHttpRequest();
	}    
	else if (window.ActiveXObject)// ActiveX version
	{
		// Internet Explorer
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
  
	//make a simple check of AJAX support
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url=wgServer +"/2009/mediawiki/index.php"; // wgServer declared in header
	var params="title="+pageName+"&action=render";

	 //Send the proper header information along with the request
	 xmlHttp.onreadystatechange=updateMainNav;
	 xmlHttp.open("POST", url, true);
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 xmlHttp.setRequestHeader("Content-length", params.length);
	 xmlHttp.setRequestHeader("Connection", "close");
	 xmlHttp.send(params);
};

function updateMainNav()
{
	 if (xmlHttp.readyState == 4) {		
		switch (xmlHttp.status) {
		   // Page-not-found error
		   case 404:
				alert('Page missing from WikiMedia. Missing Page: ' + pageName);
				break;
		   default:          									
				var content = xmlHttp.responseText;								
				try {
				
				document.getElementById(toOpen).innerHTML = content;	  

				} catch (ex) { alert(ex.message); }
				break;
		}
	 } 	 
};

function buildNewsMenu() {
	
	if(document.getElementById('news-content') == null)
		return;
	// make ajax call to retrieve the news page
	// this method will set the div with the news content
	openPageInMainNav('news-content','NEWS');	
};

function expandTools() {
	
	var tb = document.getElementById('tool-box');
	if(tb.className == "tool-box-hidden") {
		tb.className = "tool-box-open"; 
	}else { tb.className = "tool-box-hidden"; }
	
};
