// -------------------------------------------
// Functions called by the menu system at load time
// -------------------------------------------

// Declarations
var subnavinfo = new Array();
var subnavcurr = -1;
var galleryinfo = new Array();
var galleryimage = null;
var gallerylink = null;

// Subroutines
function cc_start() {
	subnav__generate();  // Set up the sub-navigation
	gallery__generate();  // Set up image gallery if it exists
	footermenus__reposition();  // Move footer popups
	fix_ie_objects(); // Handle issue with IE and need to activate flash
}

function subnav__generate() {
	var nav = document.getElementById('contextnav'); // Get reference to div
	if (!nav) return; // Quit if not found
	var tables = nav.getElementsByTagName('TABLE'); // Get reference to tables in div 
	if (tables.length!=2) return; // Quit if not found
	var tablenav = tables[0]; // Get reference to the tables
	var tablesubnav = tables[1];
	var rows = tablenav.getElementsByTagName('TR'); // Get rows in nav table
	if (rows.length!=1) return; // Quit if not one row
	var lnks = rows[0].getElementsByTagName('A'); // Get links in row
	var rows = tablesubnav.getElementsByTagName('TR'); // Get rows in subnav table
	if (rows.length!=lnks.length) return;  // Quit if mismatched rows/links
	for (var i=0;i<lnks.length;i++) var sub=new subnav__setup(i,lnks[i],rows[i]);
	var metas=document.getElementsByName("subnavindex"); // Get index of subnav
	if (metas.length==1) subnav__show(parseInt(metas[0].content)-1);
	tablesubnav.style.display = "block"; // Finally show nav
}
	
function subnav__setup(i,lnk,row) {
	// Get image in link
	var imgs=lnk.getElementsByTagName('IMG');
	if (imgs.length!=1) return;
	this.img=imgs[0];
	// Save URL and row
	this.href = lnk.href;
	this.row = row;
	this.row.style.display = "none";
	// Prepare image swap
	var tmpimg = new Image();	
	this.offsrc = this.img.src;
	this.onsrc = this.offsrc.replace(/\.gif/,'_on.gif');		
	tmpimg.src = this.onsrc;
	// Set up state changes
	this.img.onmouseover = new Function("subnav__show("+i+")");		
	subnavinfo[subnavinfo.length] = this;
	return this;
}

function subnav__show(i) {
	// Clear the currently selected item
	if (subnavcurr!=-1 && subnavinfo[subnavcurr]) {
		subnavinfo[subnavcurr].img.src = subnavinfo[subnavcurr].offsrc;		
		subnavinfo[subnavcurr].row.style.display = "none";
	}
	// Activate item
	if (subnavinfo[i]) {
		subnavinfo[i].img.src = subnavinfo[i].onsrc;		
		subnavinfo[i].row.style.display = "";
		subnavcurr = i;
	}
}

function gallery__generate() {
	var nav = document.getElementById('gallerynav'); // Get reference to div
	galleryimage = document.getElementById('galleryimage'); // Get reference to div
	if (!nav || !galleryimage) return;
	var lnks = nav.getElementsByTagName('A'); // Get reference to links in div 
	for (var i=0;i<lnks.length;i++) var gal=new gallery__setup(i,lnks[i]);
}

function gallery__setup(i,lnk) {
	// Get image in link
	var imgs=lnk.getElementsByTagName('IMG');
	if (imgs.length!=1) return;
	this.img=imgs[0];
	// Check to see if this is the fullsize
	if (this.img==galleryimage) gallerylink=lnk
	else {
		// Save link
		this.href = lnk.href;
		// Prepare image swap
		var tmpimg = new Image();	
		this.offsrc = this.img.src;
		this.onsrc = this.offsrc.replace(/\_th\.jpg/,'.jpg');		
		tmpimg.src = this.onsrc;
		// Set up state changes
		lnk.onmouseover = new Function("gallery__show("+galleryinfo.length+")");		
		galleryinfo[galleryinfo.length] = this;
		return this;
	}
}

function gallery__show(i) {
	// Activate item
	if (galleryinfo[i]) {
		galleryimage.src = galleryinfo[i].onsrc;
		if (gallerylink) gallerylink.href = galleryinfo[i].href;
	}
}

function footermenus__reposition() {
	var tbl=document.getElementById("tblFooter");
	if (!tbl) return;
	var tbltop=q17(tbl).y;
	repositionmenu("11",tbltop); // Team
	repositionmenu("14",tbltop); // Map
	repositionmenu("16",tbltop); // Search
}

function repositionmenu(lbl,y) {
	var mnu=document.getElementById("qm"+lbl);
	if (!mnu) return;
	mnu.style.top = y-mnu.offsetHeight;
}

function fix_ie_objects() {
	var objects=document.getElementsByTagName("object");
	for (var i=0;i<objects.length;i++) objects[i].outerHTML = objects[i].outerHTML;
}

var popupwin=null;
function popup(href,options) {
	if (!options) options="width=500,height=500,resizable=no,scrollbars=no,status=no,toolbar=no";
	var popupwin=window.open(href,"popupwin",options);
	popupwin.focus();
	return false;
}	

	

