/*
 * Dynamic Menu
 * Written by Andy Peatling - http://www.cssdev.com/
 * April 1, 2006.
 */

addLoadEvent(collapseMenu);
addLoadEvent(prepareMenu);

// image to display when the sub-menu is closed...
//var open_img_path = "http://www.bestindiadeals.com/images/";
var open_img_path = "http://126.0.0.100/BGD3/images/";
var open_img = "menu_open.gif";

// image to display when the sub-menu is opened...
//var close_img_path = "http://www.bestindiadeals.com/images/";
var close_img_path = "http://126.0.0.100/BGD3/images/";
var close_img = "menu_close.gif";

function collapseMenu(node) 
{
	if (!document.getElementById) return false;
	if (!document.getElementById("menu")) return false;
	if (!node) node = document.getElementById("menu");

	if (node.childNodes.length > 0) 
	{
		for (var i=0; i<node.childNodes.length; i++) 
		{
			var child = node.childNodes[i];
			if (child.nodeName == "UL") 
					child.style.display = "none";

			collapseMenu(child);
		}		
	}
}

function prepareMenu() 
{
	if (!document.getElementById || !document.getElementsByTagName) return false;
	if (!document.getElementById("menu")) return false;

//	var links = document.getElementById("menu").getElementsByTagName("a");
	var images = document.getElementById("menu").getElementsByTagName("img");

	for (var i=0; i<images.length; i++) 
	{
		images[i].onclick = function() 
		{
			var image_src = toggleMenu(this.parentNode.getElementsByTagName("UL")[0], this.src);
			if(image_src)
				this.src = image_src;
				
			return false;
		}
	}
}

function toggleMenu(node, src)
{
	if (!document.getElementById) return false;

	if (!src) return false;
	if (!node) location.href = src;	

	var ret_src = "";
	// Collapse all nodes, and only show clicked node (when clicking top level of menu)
	if (node.parentNode.parentNode.id == "menu") 
		hideTopLevels();
	
	if (node.style.display == "") // is open => now closing
	{
		ret_src = open_img_path + close_img;
		Effect.BlindUp(node, {duration: 0.2});
	}
	else // is closed => opening up
	{
		ret_src = close_img_path + open_img;
		Effect.BlindDown(node, {duration: 0.2});
	}
	
	return ret_src;
}

function hideTopLevels() 
{
	var icon = "";
	var icon_pos  = "";
	if (!document.getElementById) return false;
	if (!(node = document.getElementById("menu"))) return false;	
	
	if (node.childNodes.length > 0) 
	{
		for (var i=0; i<node.childNodes.length; i++) 
		{
			var child = node.childNodes[i];
			for(var j=0; j<child.childNodes.length; j++) 
			{
				icon = "";
				var grandchild = child.childNodes[j];
				
				if(grandchild.nodeName == "IMG")
				{
					icon = grandchild;
					icon_txt = icon.src;

					icon_pos = icon_txt.lastIndexOf("/");
					
					if(icon_pos > -1)
					{
						icon_name = icon_txt.substr(icon_pos+1);
						if(icon_name == open_img)
							grandchild.src = close_img_path + close_img;
					}
				}

				if (grandchild.nodeName == "UL")
					if (grandchild.style.display == '')
						Effect.BlindUp(grandchild, {duration: 0.2});
			}
		}		
	}
}