
var tpl_event					= "onmouseover='javascript:showMenu(this, true);' onmouseout='javascript:showMenu(this, false);'"; 
var tpl_menu_item			= "<div class='menuItem' menuid='{ID}' imgindex='{IMGINDEX}' " + tpl_event + " style='background-image: {BGIMAGE};'>\n{MENUTEXT}{MENU}</div>\n";
var tpl_menu_header 	= "<td class='menuHeader'>\n" + tpl_menu_item + "\n</td>\n";
//url(style_png/images/menu/bg_menu_normal.png);
//var tpl_menu_header 	= "<td class='menuHeader'><div menuid='{ID}'>{MENUTEXT}{MENU}</div></td>";


var tpl_menu_text			= "<a class='{CLASS}' href='{HREF}' target='{TARGET}' tooltip='{TOOLTIP}'>{LABEL}</a>\n";
var tpl_menu_container= "<div class='menuContainer' id='{ID}' style='{STYLE}'>\n{MENU}</div>\n";

var status_text 			= "© Unforgiven Studios 2005-2007";

var baseurl = "/";
var idnum = 1000;


	//Set Menu Item Height
if (menuitem_height == undefined)
	var menuitem_height = 24;

	//Set Menu Item Width
if (menuitem_width == undefined)
	var menuitem_width = 156;




	/*	DEBUG	*/
var menu_debug_src = "";



//Validate menu_images
if (menu_images == undefined)
	var menu_images = new Array();
//Validate menu_images_roll
if (menu_images_roll == undefined)
	var menu_images_roll = new Array();


/*
	createMenu(...)
	 - menuid		: ID of the root node for the menu.
	 
	Creates the menu
*/
function createMenu(menuid)
{
//Validate Menu ID
	if (menuid == null || menuid == "")
		return;
		
//Get Root ID
	var root = document.getElementById(menuid);
	
//Parse Menu XML to Memory
	var menu = parseNode(root);
	
//Build Menu
	if (menu.menu != null)
		buildMenu(menu.menu);

//Change Status Text
	window.status = status_text;
	return;
}


/*
	outputMenu(...)
	 - menu		: handle to the menu
	 - tab		: tab indent
	 
	Debug function
*/
function outputMenu(menu, tab)
{
	var s = "";
	var i = 0;
	
	s += tab + "Label:    " + menu.label + "\n";
	if (menu.menu != null)
	{
		s += tab + "Children: " + menu.menu.length + "\n";
		for (i=0; i<menu.menu.length; i++)
			s += outputMenu(menu.menu[i], tab+"\t");		
	}
	
	return s;
}


/*
	parseNode(...)
	 - node - handle to the HTML node
	 
	Parse the Menu Node, build an object [Not a HTML Object] to store all the menus data
*/
function parseNode(pnode)
{
	if (pnode == null || pnode.nodeName != "MENU")
		return;
	
//Create the menu element
	var menu = new Object( {id:"", label:"", href:"", menu:null} );
	
//retrieve attributes
	if (pnode.attributes == null)
	{
	//retrieve the hard way ?? not sure if this works properly : It should
		var p = null;
		for (p in pnode)
		{
			if (p == "id")
				menu.id = pnode["id"];
			if (p == "label")
				menu.label = pnode["label"];
			if (p == "href")
				menu.href = pnode["href"];
			if (p == "target")
				menu.target = pnode["target"];	
		}
	}
	else
	{
	//retrieve the easy way.
		if (pnode.attributes["id"] != null)
			menu.id = pnode.attributes["id"].nodeValue;			
	
		if (pnode.attributes["label"] != null)
			menu.label = pnode.attributes["label"].nodeValue;
			
		if (pnode.attributes["href"] != null)
			menu.href = pnode.attributes["href"].nodeValue;	
			
		if (pnode.attributes["target"] != null)
			menu.target = pnode.attributes["target"].nodeValue;							
	}
	
//FORCE UNIQUE [ISH] MENU ID
	menu.id += "_" + (idnum++);		
	
//Get Child Nodes
	var children = null;

//if the object has the "children" property...
	if (pnode.children)
		children = pnode.children;
		
//if the object has the "childNodes" property...
	if (pnode.childNodes)
		children = pnode.childNodes;

//parse children
	if (children != null && children.length != 0)
	{
	//Create Menu Array		
		menu.menu = new Array()
			
		var i = 0;
		
		for (i=0; i<children.length; i++)
		{
			var item = parseNode(children[i]);
			if (item != null)
				menu.menu.push(item);
		}				
	}

	return menu;
}



/*
	buildSpanNode(...)
	 - pm		: handle to the menu object
	 - hdr	: is this a header
	 
	parse the menu structure into HTMLElements
	
	Builds the menu [HTML Objects] from the Menu Object that was created during the parseNode
*/
function buildSpanNode(pm, hdr)
{
//Determine Objects Class
	var cls = (hdr == true) ? "menuHeader" : (pm.menu == undefined || pm.menu.length == 0) ? "menuItem" : "menuSub";

//Setup template
	var tpl = tpl_menu_text.replace("{CLASS}", cls);
	
//Add HyperLink Attributes
		//::link
	if (pm.href != undefined && pm.href != "")
		tpl = tpl.replace("{HREF}", pm.href);
	else
		tpl = tpl.replace("{HREF}", baseurl);
	
//::tooltip
	tpl = tpl.replace("{TOOLTIP}", pm.tooltip != undefined ? pm.tooltip : "");
	
//::target	
	tpl = tpl.replace("{TARGET}", pm.target != undefined ? pm.target : "");
		
//::label
	tpl = tpl.replace("{LABEL}", pm.label != undefined ? pm.label : "??");
		
//::id
	tpl = tpl.replace("{ID}", pm.id != undefined ? pm.id : "0000");
	
	return tpl;
}

/*
	buildMenuItem(...)
	 - pm 		: handle to the menu
	 - index	: index of the item
	 - len		: length of this menu... [not this menus children...]
	 - hdr		: is this a header
	 - main		: is this a main menu, or a sub menu?
*/
function buildMenuItem(pm, index, len, hdr, main)
{
	var s = "";
	var tpl = "";
	
//Menu header
	if (hdr == true)
		tpl = tpl_menu_header;
	else
		tpl = tpl_menu_item;

//ID
	tpl = tpl.replace("{ID}", pm.id);




//::img index
		//::	::	determine image index
	var img_index = -1;
	var img = "";
	
	if (hdr == false)
	{
		if (index == 0)
			img_index = (main==true) ? 1 : 4;
		else if (index == len - 1)
			img_index = 3;
		else
			img_index = 2;		
	}	
	else
		img_index = 0;
	
		//::	::	set image index attribute
	tpl = tpl.replace("{IMGINDEX}", img_index);
	
//::style
	if (img_index < menu_images.length && img_index >= 0)
		img = menu_images[img_index];
	
	tpl = tpl.replace("{BGIMAGE}", img);

//Has Sub-Menu
	if (pm.menu == undefined || pm.menu.length == 0)
	{
		tpl = tpl.replace("{MENUTEXT}", buildSpanNode(pm, hdr));
		s = tpl.replace("{MENU}", "");
	}
	else
	{
		tpl = tpl.replace("{MENUTEXT}", buildSpanNode(pm, hdr));
		s = tpl.replace("{MENU}", buildMenuContainer(pm, hdr, index));
	}

	return s;
}


/*
	buildMenuContainer(...)
	 - pm			: handle to the menu
	 - hdr		: is this container attached to the header
	 - index	:	index of the item
	 
	Builds the container for the menu 
*/
function buildMenuContainer(pm, hdr, index)
{
	var tpl = tpl_menu_container.replace("{ID}", pm.id);
	var top = index* menuitem_height;
	var style = hdr==true? "" : "top: " + top + "px;";

	tpl = tpl.replace("{STYLE}", style);
	tpl = tpl.replace("{MENU}", populateMenu(pm.menu, hdr));
	
	return tpl;
}


/*
	buildMenu(...)
	 - menuArray		: Handle to the menu array
	 
	Build the Menu
*/
function buildMenu(menuArray)
{
	var i = 0;
	var s = 0;
	
//Add Menus Headers
	for (i=0; i<menuArray.length; i++)
	{
		var pm = menuArray[i];
		var left = (i * menuitem_width);
		
		s = buildMenuItem(pm, i, menuArray.length, true, false);

		menu_debug_src += s + "\n\n";

		document.write(s);
	}

	return;
}

/*
	populateMenu(...)
	 - menu		: handle to the menu
	 - hdr		:
	Populates the Menu
*/
function populateMenu(menu, hdr)
{
	var s = "";
	var i = 0;
	
	if (menu == undefined)
		return "";
	
//Loop through menu items
	for (i=0; i<menu.length; i++)
		s += buildMenuItem(menu[i], i, menu.length, false, hdr);
		
	return s;
}


/*
	SHOW HIDE MENUS
*/
function showMenu(p, b)
{
	var id = "";
	var img_index = -1;
	var e = null;
	
	if (p.attributes["menuid"])
		id = p.attributes["menuid"].nodeValue;
		
	if (p.attributes["imgindex"])
		img_index = parseInt(p.attributes["imgindex"].nodeValue);

	if (id == "")
		return;
		
//Get Handle to Menu HTML Element
	e = document.getElementById(id);
	if (e == null)
		return;

//Show/Hide Menu
	e.style.display = (b==true) ? "block" : "none";
	
//Select what image array to use...
	var imgarray = (b==true) ? menu_images_roll : menu_images;

	//p.style.backgroundImage = (b == true) ? "url(style_png/images/_test.png)" : "";
	if (img_index < menu_images.length && img_index >= 0)	
		p.style.backgroundImage = imgarray[img_index];
	else
		p.style.backgroundImage = "";
	
	
	return;
}
