﻿var TimeoutID = null;
var LastSubMenuID = null;
function Menu() {
	this.URL = './Models.aspx?';
	this.Menu = {};
	this.SubMenu = [];
	this.Queries = {
		SelectedBrand : null,
		SelectedMenu : null,
		SelectedSubMenu : null
	}
	this.Show = ShowMenu;
}
function ShowMenu() {
	var strMenu = '<ul id="menu0">';
	var strSubMenu = '';
	strMenu += '<li><a ' + ((!this.Queries.SelectedMenu && !this.Queries.SelectedSubMenu)?'href="#" class="MenuSelected">':'href="' + this.URL + 'brand=' + this.Queries.SelectedBrand + '">') + 'همه گوشی ها</a></li>';

	for (var Item in this.Menu) {
		strMenu += '<li id="' + Item + '" >';
		strMenu += '<a ' + ((Item == 'menu_' + this.Queries.SelectedMenu)?'href="#" class="MenuSelected">':'href="' + this.URL + 'brand=' + this.Queries.SelectedBrand + '&m=' + Item.replace('menu_', '') + '">') + this.Menu[Item] + '</a>';
		for (var SubItem in this.SubMenu) {
				if (this.SubMenu[SubItem][0].indexOf(Item + '_') == -1) continue;
				strSubMenu += '<li id="' + this.SubMenu[SubItem][0] + '">\n';
				strSubMenu += '<a ' + ((this.SubMenu[SubItem][0] != Item + '_' + this.Queries.SelectedSubMenu)?'href="' + this.URL + 'brand=' + this.Queries.SelectedBrand + '&s=' + this.SubMenu[SubItem][0].replace(Item + '_', '') + '">':'href="#" class="SubMenuSelected">');
				strSubMenu += this.SubMenu[SubItem][1];
				strSubMenu += '</a>\n';
				strSubMenu += '</li>\n';
		}
		if (strSubMenu != '') {
			strSubMenu = '<ul id="' + 'sub' + Item + '">' + strSubMenu + '</ul>';
		}
		strMenu += strSubMenu;
		strMenu += '</li>\n';
		strSubMenu = '';
	}
	strMenu += '</ul>';
	document.writeln(strMenu);
}
function SetMenuAttributes(Nodes) {
	for (var i = 0; i < Nodes.length; i++) {
		if (Nodes[i].tagName != 'LI') continue;
		if (Nodes[i].id.split('_').length == 2) {
			var ULs = Nodes[i].getElementsByTagName('UL');
			if (ULs.length != 0) {
//				Nodes[i].getElementsByTagName('A')[0].style.backgroundImage = 'url(/Images/OSMenuArrow.gif)';
				Nodes[i].onmouseover = function() {
					clearTimeout(TimeoutID);
					TimeoutID = setTimeout("ShowSubMenu('" + this.id + "')", 200);
				};
				Nodes[i].onmouseout = function() {
					TimeoutID = setTimeout("HideSubMenu('" + this.id + "')", 200);
				};
				SetPosition(Nodes[i], ULs[0])
			}	
		}
	}
}
function SetPosition(LI, UL) {
	var Top, Left, Parent;
	Top = LI.offsetTop + LI.offsetHeight;
	Left = LI.offsetLeft + LI.offsetWidth - UL.offsetWidth;
	Parent = LI.offsetParent;
	while (Parent != null) {
		Top += Parent.offsetTop;
		Left += Parent.offsetLeft;
		Parent = Parent.offsetParent;
	}
	UL.style.top = Top + "px";
	UL.style.left = Left + "px";
}
function ShowSubMenu(ID) {
	if (LastSubMenuID != ID) HideSubMenu(LastSubMenuID);
	LastSubMenuID = ID;
	var SubMenu = document.getElementById('sub' + ID);
	SubMenu.style.visibility = 'visible';
}
function HideSubMenu(ID) {
	if (!ID) return;
	LastSubMenuID = null;
	var SubMenu = document.getElementById('sub' + ID);
	SubMenu.style.visibility = 'hidden';
}
window.onload = function() {
	var objMainMenu = document.getElementById('menu0');
	if (objMainMenu) SetMenuAttributes(objMainMenu.childNodes);
}