
function qmenu(contId) {
	this.contId = 'qmenu-main';
	if(contId) 
		this.contId = contId;  
	
	this.items = new Object;
	this.iCount = 0;
	this.orientation = 0;
	this.ctimer = null;
	
	this.add = add;
	this.display = display;
	this.collapse = collapse;
	this.setStyle = setStyle;
	
	this.rootl = new qmenuItem();
	this.rootl.menu = this;
	this.dp = 1;
	
	var uagent = navigator.userAgent;
	var ie = uagent.indexOf("MSIE ");
	if(ie != -1) {
		if(parseFloat(uagent.substring(ie + 5, uagent.indexOf(";", ie))) < 6) {
			this.dp = -1;
		} else if (document.compatMode=='BackCompat'){
			this.dp = -1;
		}
	}
	
	function add(itemTitle, parentId, itemUrl, itemId) {
	
		var item = new qmenuItem();
		item.title = itemTitle;
		item.url = itemUrl;
		item.menu = this;
		
		if(itemId) item.id = itemId; else item.id = itemTitle;
		
		if(parentId) {
			var parent = this.items[parentId];
		} else {
			var parent = this.rootl;
		}
		parent.children[parent.children.length] = item.id;
		item.parentId = parent.id
		item.level = parent.level + 1;
		var def = this.defStyle
		if(typeof(def) == 'object') {
			item.bgColor= def.bgColor;
			item.mouseOverBgColor = def.mouseOverBgColor;
			item.borderColor = def.borderColor;
			item.textColor = def.textColor;
			item.width = def.width;
			item.height= def.height;
		}
		this.items[item.id] = item;
		this.iCount++;
		return item;
	}
	function display() {
		this.rootl.expand();
	}
	function collapse() {
		var container = document.getElementById(this.contId);
		var divs = container.getElementsByTagName('div'); 
		var RemDiv = new Array();
		for (var i = 0; i < divs.length; i++) { 
			if(divs[i].item.level > 1) {
				RemDiv[RemDiv.length] = divs[i];
			}
		}
		for(i in RemDiv) {
			container.removeChild(RemDiv[i]);
		}
	}
	function setStyle(style) {
		if(typeof(this.defStyle) != 'object') {
			this.defStyle = new qmenuItem();
		}
		if(style.width) this.defStyle.width = style.width;
		if(style.height) this.defStyle.height = style.height;
		if(style.textColor) this.defStyle.textColor = style.textColor;
		if(style.bgColor) this.defStyle.bgColor = style.bgColor;
		if(style.mouseOverBgColor) this.defStyle.mouseOverBgColor = style.mouseOverBgColor;
		if(style.borderColor) this.defStyle.borderColor = style.borderColor;
		
		if(style.orientation) this.orientation = style.orientation;
	}
}

function qmenuItem() {
	this.id = 0;
	this.parentId = '';
	this.children = new Array();
	this.top = 0;
	this.left = 0;
	this.width = 100;
	this.height = 26;
	this.level = 0;
	this.textColor = '#000';
	this.bgColor = '#e0e0e0';
	this.mouseOverBgColor = '#d0d0d0';
	this.borderColor = '#a0a0a0';
	this.expand = expand;
		
	function expand() {
		
		var ExpandedItems = new Array();
		
		ExpandedItems[this.level]=this.id;
		var o = this;
		var i = this.level-1;
		
		while(o.parentId) {
			ExpandedItems[i]=o.parentId;
			o = o.menu.items[o.parentId];
			i--;
		}
				
		var container = document.getElementById(this.menu.contId);
		var divs = container.getElementsByTagName('div'); 
		var RemDiv = new Array();
		
		for (i = 0; i < divs.length; i++) { 
			
			var rm = true;
			
			for(var ix in ExpandedItems) {
				if(divs[i].item.level==1 || (ExpandedItems[ix] == divs[i].item.parentId)) {
					rm = false;
					break;
				}
			}
			
			if(rm)
				RemDiv[RemDiv.length] = divs[i];
		}
		
		for(i in RemDiv) {
			container.removeChild(RemDiv[i]);
		}
		
		if(this.children.length == 0) 
			return;
		
		var p = 0;
		var mo = this.menu.orientation;		
		
		if(mo == 0 && this.level == 1) {
			//p = this.top + this.height +1;
			p = this.top + this.height + this.menu.dp;
		} else if((mo == 1 && this.level >= 1) || (mo == 0 && this.level > 1)) {
			p = this.top + 5;
		}
		
		for(i in this.children) {
			var ci = this.menu.items[this.children[i]];
			var newdiv = document.createElement('div');
			
			newdiv.item = ci;
			newdiv.setAttribute('id', ci.id);
			newdiv.onmouseover = function() {
				if(this.item.menu.ctimer) {
					clearTimeout(this.item.menu.ctimer);
					this.item.menu.ctimer = null;
				}
				this.style.backgroundColor = this.item.mouseOverBgColor;
				this.item.expand();
			}
			newdiv.onmouseout = function() {
				qmenu[this.item.menu.contId] = this.item.menu;
				this.style.backgroundColor = this.item.bgColor;
				this.item.menu.ctimer=setTimeout('qmenu[\''+this.item.menu.contId+'\'].collapse()', 300);
			}
			newdiv.onclick = function() {
				if(this.item.url) window.location =this.item.url;
				return false;
			}
			newdiv.style.backgroundColor =ci.bgColor;
			newdiv.style.color = ci.textColor;
			newdiv.style.border = '1px solid ' + ci.borderColor;
			newdiv.style.width = ci.width + 'px';
			newdiv.style.height = ci.height + 'px';
			newdiv.style.position = 'absolute';
			
			if(mo == 0) {
				if(this.level == 0) {
					ci.left = p;
					ci.top = 0;
					p += ci.width + this.menu.dp;
				} else if(this.level == 1) {
					ci.left = this.left;
					ci.top = p;
					p += ci.height + this.menu.dp;
				} else {
					ci.left = this.left + this.width - 10;
					ci.top = p;
					p += ci.height + this.menu.dp;
				}
			} else {
				if(this.level == 0) {
					ci.left = this.left;
					ci.top = p;
					p += ci.height + this.menu.dp;
				} else {
					ci.left = this.left + this.width +1 - 10;
					ci.top = p;
					p += ci.height + this.menu.dp;
				}
			}
			newdiv.style.left = ci.left + 'px';
			newdiv.style.top = ci.top + 'px';
			var inHtml ='<p class="qmenu-inside">';
			if(ci.url) inHtml += '<a style="color:'+ci.textColor+'" href="'+ci.url+'">'+ci.title+'</a>'; else inHtml +=  ci.title;
			inHtml += '</p>';
			newdiv.innerHTML = inHtml;
			
			container.appendChild(newdiv);
		}
		
	}
}
