// JScript Flyout Menu Object
// Written by Corné van Rensburg
// for http://www.gracefieldschool.co.uk
// Copyright (c) ThirdFloorDigital. 2003.
// version 1.0

//--> Menu Object prototype start <--------------------------------------------------------------------------
//--> Menu Constructor <-------------------------------------------------------------------------------------
function Menu( label, fnt, fcol, xpos, ypos ) {
	this.version = "Version 1.0";
	this.type = "Menu";
	
	if ( label == "root" ) {
		this.xpos = "0";
		this.ypos = "0"
		this.position = "relative";
	} else {
		this.xpos = xpos;
		this.ypos = ypos;
		this.position = "absolute";
	}
	this.items = new Array();
	this.actions = new Array();
	this.url = new Array();
	this.hasSub = new Array();
	this.icon = new Array();
	this.mOverIcon = new Array();
	
	this.label = label;
	this.key = this.label.replace(" ", "_");
	this.itemCount = 0;

	this.isRoot = this.label == "root" ? true : false;

	if (!window.menus) window.menus = new Array();
	window.menus[ this.label ] = this;
	window.menus[ window.menus.length ] = this;
	
	this.font = fnt;
	this.fontColor = fcol;
	
	this.addMenuItem = addMenuItem;
	this.getMenuItem = getMenuItem;
	this.drawMenu = drawMenu;
	
	this.startMenu = startMenu;
	this.drawMenuItem = drawMenuItem;
	this.endMenu = endMenu;
	
	this.menuStarted = false;
	this.menuEnded = false;
}
//--> addMenuItem Method <-----------------------------------------------------------------------------------
function addMenuItem( label, sub, url, act ) {
	this.items[ this.items.length ] = label;
	if ( !sub ) {
		this.url[ this.url.length ] = "navTo('" + url + "');";
		this.actions[ this.actions.legth ] = "";
		this.hasSub[ this.hasSub.length ] = false;
		if ( !this.isRoot ) {
			this.icon[ this.icon.length ] = "images/sub_shield_out.gif";
			this.mOverIcon[ this.mOverIcon.length ] = "images/sub_shield_over.gif";
		} else {
			this.icon[ this.icon.length ] = "images/shield_out.gif";
			this.mOverIcon[ this.mOverIcon.length ] = "images/shield_over.gif";
		}
	} else {
		this.actions[ this.actions.length ] = "openSub('" + act + "');";
		this.url[ this.url.length ] = "navTo('" + url + "')";
		this.hasSub[ this.hasSub.length ] = true;
		this.icon[ this.icon.length ] = "images/shield_out_sub.gif";
		this.mOverIcon[ this.mOverIcon.length ] = "images/shield_over_sub.gif";
	}

	this.itemCount++;
}
//--> getMenuItem Method <-----------------------------------------------------------------------------------
function getMenuItem( idx ) {
	return this.items[ idx ];
}
//--> drawMenu Method <--------------------------------------------------------------------------------------
function drawMenu() {
	var j = -1;
	this.startMenu();
	for ( i = 0; i < this.itemCount; i++ ) {
		if ( this.hasSub[ i ] ) j++;
		this.drawMenuItem( i, j );
	}
	this.endMenu();
}
//--> startMenu Method <-------------------------------------------------------------------------------------
function startMenu() {
	s = "<DIV id=\"" + this.key + "\" style=\"position:" + this.position + ";";
	if ( this.key != "root" ) s += "left:" + this.xpos + ";top:" + this.ypos + ";display:none";
	s += "\">";
	if ( this.key != "root" ) {
		s += "<IMG src=\"images/sub_top.gif\" border=0 onmouseover=\"openSub('" + this.key;
		s += "')\" onmouseout=\"closeSub()\">";
	}
	document.write( s );
	this.menuStarted = true;
}
//--> endMenu Method <---------------------------------------------------------------------------------------
function endMenu() {
	s = ""
	if ( this.key != "root" ) s += "<IMG src=\"images/sub_bottom.gif\" border=0>";
	s += "</DIV>";
	document.write( s );
	this.menuEnded = true;
}
//--> drawMenuItem Method <----------------------------------------------------------------------------------
function drawMenuItem( idx, sub_idx ) {
	if ( !this.menuStarted ) return false;
	if ( this.menuEnded ) return false;
	s = "";
	s += "<DIV onmouseover=\"mAction('" + this.items[ idx ].replace( " ", "_" ) + "_img" + "', '";
	s += this.mOverIcon[ idx ];
	s += "');";

		
	if ( this.hasSub[ idx ] ) s += this.actions[ sub_idx ];
	if ( this.key != "root" ) s += "openSub('" + this.key + "');";
	
	s += "window.status = '" + this.items[ idx ] + "';return true\" onmouseout=\"mAction('";
	s += this.items[ idx ].replace( " ", "_" ) + "_img" + "', '";
	s += this.icon[ idx ];
	s += "');";

	if ( this.hasSub[ idx ] ) s += "closeSub();";
	if ( this.key != "root" ) s += "closeSub();";

	s += "window.status = ''; return true\"";
	s += " onclick=\"" + this.url[ idx ] + "\"";
	s += " style=\"font:10px ";
	s += this.font;
	s += ";cursor:hand;color:";
	s += this.fontColor;
	s += ";width:182px\">";
	
	s += "<table width=100% cellpadding=0 cellspacing=0 border=0 style=\"font:bolder 10px ";
	s += this.font;
	s += ";color:";
	s += this.fontColor;

	s += "\"><tr>";
	if ( this.key != "root" ) {
		s += "<td align=\"left\" valign=\"middle\" width=\"2px\" style=\"background:#00009c;\">";
		s += "<img src=\"images/sub_left.gif\" border=0>";
		s += "</td>";
	}
	s += "<td align=\"right\" valign=\"middle\" style=\"background:#00009c;\" width=*>";
	
	s += this.items[ idx ];

	if ( !this.isRoot ) {
		s += "</td><td width=\"30px\" align=\"right\" style=\"background:#00009c;\">";
	} else {
		s += "</td><td width=\"24px\" align=\"right\" style=\"background:#00009c;\">";
	}

	
	s += "<img src=\"";
	s += this.icon[ idx ];
	s += "\" ";
	s += "id=\"";
	s += this.items[ idx ].replace( " ", "_" );
	s += "_img";
	s += "\"";
	s += ">";
	
	s += "</td></tr></table>"
	
	s += "</DIV>";
	
//	alert( s );
	document.write( s );
}
//--> Menu Object prototype end   <--------------------------------------------------------------------------



//--> external actions <-------------------------------------------------------------------------------------
var bPreloadStarted = false;
var tmr;
function navTo( url ) {
	window.location.href = url;
	closeAllSubs();
}
function mAction( id, img ) {
	document.getElementById( id ).src = img;
}
function openSub( sub ) {
	if ( tmr ) clearTimeout( tmr );
	closeAllSubs();
	document.getElementById( sub ).style.display = "inline";
}
function closeSub() {
	if ( tmr ) clearTimeout( tmr );
	tmr = setTimeout( "closeAllSubs()", 500 );
}
function closeAllSubs() {
	var i;
	for ( i = 0; i < window.menus.length; i++ ) {
		if ( window.menus[ i ].key != "root" ) {
			document.getElementById( window.menus[ i ].key ).style.display = "none";
		}
	}
}

//--> preload section <--------------------------------------------------------------------------------------
var imgRoot_NoSub_Out = new Image();
var imgRoot_NoSub_Over = new Image();
var imgRoot_Sub_Out = new Image();
var imgRoot_Sub_Over = new Image();
var imgSub_Out = new Image();
var imgSub_Over = new Image();

function preloadImages() {
	if ( bPreloadStarted ) return true;
	
	imgRoot_NoSub_Out.src = "images/shield_out.gif";
	imgRoot_NoSub_Over.src = "images/shield_over.gif";
	imgRoot_Sub_Out.src = "images/shield_out_sub.gif";
	imgRoot_Sub_Over.src = "images/shield_over_sub.gif";
	imgSub_Out.src = "images/sub_shield_out.gif";
	imgSub_Over.src = "images/sub_shield_over.gif";
	bPreloadStarted = true;
}