var menuArray = new Array(

// Specify the menu item names. Each corresponding text file should have the same name, in lowercase. Eg: home.txt

'HOME','PLAYERS','ARCHIVE','SUPPORTERS','AYSB','HIRE','CONTACT'

//
);

window.onload = load;
window.onresize = changeHeight;
var numItems = menuArray.length;
var itemWidth = Math.ceil(950/numItems);
var currentPage = null;
function displayMenu() {
	var contents = "";
	for (i=0; i<numItems; i++) {
		var newDiv = document.createElement("div");
		newDiv.className = 'menuItem';
		newDiv.style.width = itemWidth + 'px';
		newDiv.style.left = i * itemWidth + 'px';
		newDiv.innerHTML = menuArray[i];
		newDiv.onclick = showPage;
		newDiv.onmouseover = opacUp;
		newDiv.onmouseout = opacDown;
		newDiv.setAttribute("id",menuArray[i].toLowerCase())
		document.getElementById('menubar').appendChild(newDiv);
		changeOpac(85, menuArray[i].toLowerCase());
	}
}

function load() {
	displayMenu();
//	loadPage('home');
	
	// highlight the appropriate menu item
	pathArray = window.location.pathname.split('/');
	pageArray = pathArray[pathArray.length-1].split('.');
	if (pageArray.length<2) pageArray[0] = 'home';
	pageName = pageArray[0].toLowerCase();
	if (pageName == "index") pageName = "home";
	currentPage = pageName;
	document.getElementById(pageName).onmouseover = doNothing;
	document.getElementById(pageName).onmouseout = doNothing;
	changeOpac(100, pageName);

	changeHeight();
	
	if (currentPage == 'archive') displayThumbs(null);
}

var windowHeight = 0;
function changeHeight() {
	if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
		document.getElementById('maindiv').style.minHeight = window.innerHeight + 'px';
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
		var browserVer = parseFloat(navigator.appVersion.split("MSIE")[1]);
		if (browserVer >= 7) {
			document.getElementById('maindiv').style.minHeight = document.documentElement.clientHeight + 'px';
		} else {
			document.getElementById('maindiv').style.height = document.documentElement.clientHeight + 'px';
		}
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
		document.getElementById('maindiv').style.height = document.body.clientHeight + 'px';
	}
}

// For menu items
function showPage(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	var page = (targ.id);

	loadPage(page);
}

// Loads a page in the 'content' div
function loadPage(page) {
//alert(page);
	currentPage = page;
	setFront(page);
	if (page.toLowerCase()=='home')
		page = 'index';
	loadUrl(page.toLowerCase() + '.html?' + Math.random());
	window.scrollTo(0,0);
}

function setFront(page) {
	for (i=0; i<menuArray.length; i++) {
		document.getElementById(menuArray[i].toLowerCase()).onmouseover = opacUp;
		document.getElementById(menuArray[i].toLowerCase()).onmouseout = opacDown;
		changeOpac(85, menuArray[i].toLowerCase());
	}
	document.getElementById(page).onmouseover = doNothing;
	document.getElementById(page).onmouseout = doNothing;
	changeOpac(100, page);
}

function doNothing() {
	return;
}

function loadUrl(dest) {
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }
	
/* new..
  // Native XMLHttpRequest object
  if( window.XMLHttpRequest ){
    xmlhttp = new XMLHttpRequest();
  // ActiveX
  } else if( window.ActiveXObject ){
    try{
      xmlhttp = new ActiveXObject( 'Msxml2.XMLHTTP' );
    }catch( e ){
      try{
        xmlhttp = new ActiveXObject( iMicrosoft.XMLHTTP' );
      }catch( e ){}
    }
  }
*/

	xmlhttp.onreadystatechange = triggered;
	xmlhttp.open("GET", dest);
	xmlhttp.send(null);

}

function triggered() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var inStr = xmlhttp.responseText;//.substring(indexOf('<!--BEGINCONTENT-->'),indexOf('<!--ENDCONTENT-->') - 1);
		document.getElementById("content").innerHTML = inStr.substring(inStr.indexOf('<!--BEGINCONTENT-->'),inStr.indexOf('<!--ENDCONTENT-->') - 1);
	}
	if (currentPage == 'archive') {
		setTimeout("displayThumbs(null);",300);
	} else if (currentPage == 'gallery') {
		setTimeout("loadGallery();",300);
	}
}

// The following section deals with images and transparency.

// Catches Internet Explorer, to disable menu images
// This is because IE does not support applying opacity to a transparent png, among others
var ie = false;
if (navigator.appName == 'Microsoft Internet Explorer') {
	ie=true;
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;

	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

function opacUp () {
	this.style.opacity=0.95;
	this.style.filter = "alpha(opacity=95)";
}

function opacDown () {
	var toOpac = 80;
	var downTime = 1000;
	if (this.parentNode.id == 'menubar') {
		toOpac = 85;
		downTime = 600;
	}
	currentOpac(this.id, toOpac, downTime);
	//opacity(this.id, 100, toOpac, downTime);
}

// This is for the Google Maps application:
var WINDOW_HTML_1 = '<img src="images/gmap1.png" alt="" />';
var WINDOW_HTML_2 = '<img src="images/gmap2.png" alt="" />';

function loadMaps(a) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
//		map.addControl(new GSmallMapControl());
//		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(0,-60)));
		if (a==1) {
			map.setCenter(new GLatLng(-36.8525, 174.7512), 16);
			var marker = new GMarker(new GLatLng(-36.8531, 174.7511));
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(WINDOW_HTML_1);
			});
	    		marker.openInfoWindowHtml(WINDOW_HTML_1);
		} else if (a==2) {
			map.setCenter(new GLatLng(-36.8693, 174.7686), 16);
			var marker = new GMarker(new GLatLng(-36.8693, 174.7686));
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(WINDOW_HTML_2);
			});
			marker.openInfoWindowHtml(WINDOW_HTML_2);
		}
  	}
}

function pollC(id, load){
	if (!load&&document.getElementById(id)){
		document.getElementById(id).id='';
		return;
	}
	else if (load&&document.getElementById(id)){
		//if (id=='')  //optional
		loadMap();  //required
		return;
	}
	else if (load&&!document.getElementById(id))
	setTimeout("pollC('"+id+"', 'load')", 60);
}
