AWSMenu = function (id , redirectURL) { this.init(id , redirectURL); } 

AWSMenu.prototype.init = function(id , redirectURL) {
	this.items = new Array();
	this.mirror = new Array();
	this.id = id;
	this.redirectURL = redirectURL;
}

AWSMenu.prototype.build = function() {
	//var count = this.items.length;
	//for (var i = 0 ; i < count;i++) {
	if (this.id == "menu") {
		this.writeMenuHeader(this.getItem(26));
		this.writeMenuHeader(this.getItem(45));
		this.writeMenuHeader(this.getItem(27));
		this.writeMenuHeader(this.getItem(28));
		this.writeMenuHeader(this.getItem(29));
	}
	var count = this.mirror.length;
	for (var i = 0 ; i < count;i++) {
		var item = this.mirror[i];
		if (item ==null)
			continue;
		
		if(item.parentId > 0) {
			var parent = this.getItem(item.parentId);
			parent.add(item);
		} 
	}
	
	//alert (document.getElementById("menuContainer").innerHTML);
}

AWSMenu.prototype.writeMenuHeader = function (item) {
	if (!item) return;
	var container = document.getElementById("menuContainer");
	var html = "<div id=\"mi" + item.id + "\" class=\"menuHeader\" onclick=\"document.location.href = '../shop/MainCategory.aspx?cid=" + item.id + "'\" onmouseover=\"showSubMenu(" + item.id + ")\" onmouseout=\"hideSubMenu(" + item.id + ")\">"
	html += item.text;
	html += "<div id=\"mi" + item.id + "_children\" style=\"display:inline;z-index:5;visibility:hidden\"></div>";
	html += "</div>";
	
	container.innerHTML += html;
}

AWSMenu.prototype.writeLevelTBL = function(item) {
	if (!item.hasChildren()) return;
	var container = document.getElementById("mi" + item.id + "_children");
	if (container.innerHTML == "") {
		var mi = document.getElementById("mi" + item.id);
		var top = getElementsTop(mi) + 14;
		var left = getElementsLeft(mi);
		var uniqueId = this.id + "_" + item.id + "_tbl";
		var html = "<table  style='z-index:5;position:absolute;top:" + top + ";left:" + left + ";' cellpadding='0' cellspacing='0' border='0' class='menuTbl' id='" + uniqueId + "' >";
		html += "<tr><td class=miSpaceGray>&nbsp;</td></tr>";
		html += "<tr><td class=miSpace>&nbsp;</td></tr>";
		html += "<tr>";
		html += "<td class='menuItems'>"
		html += "<table  cellpadding='0' cellspacing='0' border='0' >";
		var count = item.items.length;
		for (var i = 0; i < count;i++) {
			var child = item.mirror[i];
			if (child == null) continue;
			html += "<tr>";
			html += "<td class='menuItem'>"
			html += "<a href='" + this.redirectURL + "?cid=" + child.id + "' target='_self'>&nbsp;- " + child.text + "</a>";
			html += "</td>";
			html += "</tr>";
		}
		html += "</table>";
		html += "</td>";
		html += "</tr>";
		html += "</table>";
		
		container.innerHTML = html;
	}
	container.style.visibility = "visible";
}

AWSMenu.prototype.add = function(item) {
	this.items[item.id] = item;
	this.mirror.push(item);
}

AWSMenu.prototype.getItem = function(id) {
	return this.items[id];
}

AWSMenuItem = function (id , parentId , text) { this.init(id , parentId , text); }

AWSMenuItem.prototype.init = function(id , parentId , text) {
	this.items = new Array();
	this.mirror = new Array();
	this.id = id;
	this.parentId = parentId;
	this.text = text;
}

AWSMenuItem.prototype.add = function(item) {
	this.items[item.id.toString()] = item;
	this.mirror.push(item);
}

AWSMenuItem.prototype.getItem = function(id) {
	return this.items[id.toString()];
}

AWSMenuItem.prototype.hasChildren = function() {
	return this.items.length > 0;
}

////////////////////////////////////////////////////////////////////////
//global function getElementsTop(obj)
//gets: Object obj
//return: int representing the top position of the obj.
//do: calculates the absolute top position of the obj.
////////////////////////////////////////////////////////////////////////
function getElementsTop(obj) {
	var top = 0 ; 
	while (obj.offsetParent)
	{
		top += obj.offsetTop;
		obj = obj.offsetParent;
	}
	
	return top;
}

////////////////////////////////////////////////////////////////////////
//global function getElementsTop(obj)
//gets: Object obj
//return: int representing the left position of the obj.
//do: calculates the absolute left position of the obj.
////////////////////////////////////////////////////////////////////////
function getElementsLeft(obj) {
	var left = 0 ;
	
	while (obj.offsetParent)
	{
		left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return left;
}
