/***	NavBar Object ***/
function NavBar(){
	this.width = this.height = this.left = this.top = 0;
	this.items = [];
	this.debug = (arguments[0]) ? arguments[0] : false;
}
NavBar.cTarget = null;
NavBar.monitor = function(e){
	if(e)NavBar.cTarget = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
}
NavBar.refresh = function(){
	if(!NavBar.cTarget)return;
	if(c.isDOM1){
		while(NavBar.cTarget.nodeType == 3 || NavBar.cTarget.tagName == "TD" || NavBar.cTarget.tagName == "TR" || NavBar.cTarget.tagName == "TBODY") NavBar.cTarget = NavBar.cTarget.parentNode;
		if(!NavBar.cTarget.id || (NavBar.cTarget.id.indexOf("Menu") < 0 && NavBar.cTarget.id.indexOf("Item") < 0)){
			NavItem.closeAllBut();
		}
		if(this.debug)window.status = NavBar.cTarget.nodeType + " : " + NavBar.cTarget.tagName  + " : " + NavBar.cTarget.id
	} else if (c.isDOM0) {
		if(!NavBar.cTarget.id || (NavBar.cTarget.id.indexOf("Menu") < 0 && NavBar.cTarget.id.indexOf("Item") < 0)){
			NavItem.closeAllBut();
			Menu.closeAllBut();
		}
	}
}
NavBar.prototype.addItem = function(x,y,src,href){
	var item = this.items[this.items.length] = new NavItem(src);
	item.x = x;
	item.y = y;
	item.ownerBar = this;
	if(arguments[2])item.setImage(arguments[2]);
	if(arguments[3])item.setHref(arguments[3]);
}
NavBar.prototype.moveTo = function(x,y){
	this.left = x; this.top = y;
	if(this.items.length) for(var i=0; i<this.items.length; i++)this.items[i].moveTo(parseInt(this.items[i].style.left) + x, parseInt(this.items[i].style.top) + y)
}
NavBar.prototype.load = function(){
	for(var i=0; i<this.items.length; i++)this.items[i].create();
	if(arguments.length)
		this.moveTo((arguments[0] ? arguments[0] : 0),(arguments[1] ? arguments[1] : 0))
}

/***	NavItem Object ***/
function NavItem(){
	this.id = "NavItem" + ++NavItem.count;
	this.x = this.y = 0;
	this.timer = 0;
	this.ownerBar = this.childMenu = null;
	this.obj = this.style = this.href = null;
	this.image = new Image();
	this.image.id = this.id + "Image";
	if(arguments[0])this.image.src = arguments[0];
	this.persist = false;
}

NavItem.count = 0;
NavItem.roster = [];
NavItem.closeAllBut = function(){
	if(arguments.length){
		for(var id in NavItem.roster) if(id != arguments[0].id && !NavItem.roster[id].persist) NavItem.roster[id].style.visibility = "hidden"
	} else {
		for(var id in NavItem.roster) if(!NavItem.roster[id].persist) NavItem.roster[id].style.visibility = "hidden";
	}
}

NavItem.prototype.create = function(){
	if(c.isDOM1){
		var e = document.createElement("div");
		e.setAttribute("id",this.id);
		document.body.appendChild(e);
		this.obj = e;
		this.obj.appendChild(this.image)
	} else if(c.isDOM0) {
		document.body.insertAdjacentHTML("beforeEnd",'<div id="' + this.id + '"></div>');
		this.obj = c.getObject(this.id);
		this.obj.innerHTML = '<img id="' + this.image.id + '" src="' + this.image.src + '" border="0">'
	}
	this.setHref(this.href)
	this.style = c.getStyle(this.obj);
	this.style.position = "absolute";
	this.style.visibility = "hidden";
	this.moveTo(this.x,this.y);
	NavItem.roster[this.id] = this;
	
	// Create Event Listeners	
	var oItem = this;
	this.obj.onmouseout = function(){ 
		if(oItem.childMenu){
			oItem.childMenu.close(10);
			 if(!oItem.persist)oItem.timer = setTimeout(function(){oItem.style.visibility = "hidden"},10)
		}
		else if(!oItem.persist)oItem.style.visibility = "hidden";
	}
	this.obj.onmouseover = function(){ 
		oItem.style.visibility = "visible";
		if(oItem.childMenu){
			clearTimeout(oItem.timer);
			clearTimeout(Menu.timer);
			oItem.childMenu.cancelClose();
			oItem.childMenu.open();
		} else Menu.closeAllBut();
	}
}
NavItem.prototype.setImage = function(o){
	if(typeof o == "string") this.image.src = o;
	else if(typeof o == "object"){
		this.image = o;	
		this.image.id = this.id + "Image";
	}
	if(this.obj)
		if(c.isDOM1) this.obj.replaceChild(this.obj.firstChild,this.image)
		else if(c.isDOM0) this.obj.innerHTML = '<img id="' + this.image.id + '" src="' + this.image.src + '" border="0">'
}
NavItem.prototype.setHref = function(x){
	this.href = x; 
	if(this.obj)this.obj.onclick = function(){location.href = x};
}
NavItem.prototype.moveTo = function(x,y){
	var un = (typeof this.style.left == "string") ? "px" : 0;
	this.style.left = x + un;
	this.style.top = y + un;
}
NavItem.prototype.on = function(){
	if(!c.isBaseline)return;
	NavItem.closeAllBut(this);
	this.style.visibility = "visible"; 
	if(this.childMenu){
		var menu = this.childMenu;
		var timer = setTimeout(function(){menu.cancelClose()},100);
	}
}
NavItem.prototype.assignMenu = function(menu){
	this.childMenu = menu;
	menu.navItem = this;
}
NavItem.prototype.setPersist = function(b){
	this.persist = b;
	if(this.style)
		if(b) this.style.visibility = "visible";
		else this.style.visibility = "hidden"
}	

/***	Menu Object ***/
function Menu(){
	this.id = "Menu" + ++Menu.count;
	this.obj = this.style = this.parentMenu = this.navItem = null;
	this.width = this.height = this.itemCount = this.spacerCount = this.timer = 0;
	this.items = [];
	this.create(); 
	var oMenu = this;
	this.obj.onmouseover = function(){
		oMenu.cancelClose();
		clearTimeout(Menu.timer);
		if(oMenu.parentMenu)oMenu.parentMenu.cancelClose();
		if(oMenu.navItem)clearTimeout(oMenu.navItem.timer)
	}
	this.obj.onmouseout = function(evt){
		if(oMenu.parentMenu)oMenu.parentMenu.cancelClose();
		oMenu.close(Menu.delay);
		Menu.timer = setTimeout(Menu.closeAllBut,Menu.delay);
		if(oMenu.navItem)oMenu.navItem.timer = setTimeout(function(){oMenu.navItem.style.visibility="hidden"},Menu.delay);
	}
}
Menu.count = Menu.timer = 0;
Menu.delay = 100;
Menu.roster = [];
Menu.closeAllBut = function(){
	if(!arguments[0]) for(var m in Menu.roster) Menu.roster[m].close();
	else if(!arguments[0].parentMenu) for(var m in Menu.roster) Menu.roster[m].close();
}
Menu.prototype.create = function(){
	if(c.isDOM1){
		var e = document.createElement("div");
		e.setAttribute("id",this.id);
		document.body.appendChild(e);
		this.obj = e;
	} else if(c.isDOM0) {
		document.body.insertAdjacentHTML("beforeEnd",'<div id="' + this.id + '"></div>');
		this.obj = c.getObject(this.id);
	}
	this.style = c.getStyle(this.obj);
	this.style.position = "absolute";
	this.style.visibility = "hidden";
	Menu.roster[this.id] = this;
}
Menu.prototype.addItem = function(w,h){
	if(!this.items.length) var isFirstItem = true;
	var item = this.items[this.items.length] = new Item(w,h,this,(arguments[4])?arguments[4]:null,(arguments[5])?arguments[5]:null);
	if(arguments[2])item.setText(arguments[2]);
	if(arguments[3])item.setHref(arguments[3]);
	item.moveTo(0,this.height);
	this.height = this.style.height = (h + this.height);
	if(w > this.width) this.width = this.style.width = w;
}
Menu.prototype.addSpacer = function(h,bgColor){
	var sp = null;
	var id = "MenuSpacer" + (++this.spacerCount) + "_" + Menu.count;
	if(c.isDOM1){
		sp = document.createElement("div");
		sp.setAttribute("id",id);
		this.obj.appendChild(sp);
	} else if (c.isDOM0){
		this.obj.insertAdjacentHTML("beforeEnd",'<div id="' + id + '"></div>');
		sp = c.getObject(this.id);
	}	
	with(sp.style){
		var un = (typeof left == "string") ? "px" : 0;
		position = "absolute";
		left = 0 + un;		top = this.height + un;
		height = h;			width = this.width;
		backgroundColor = bgColor;
		if(c.isDOM1) clip = "rect(0px "+this.width+"px "+h+"px 0px)"
	}
	this.height = this.style.height = (h + this.height);
}
Menu.prototype.moveTo = function(x,y){
	var un = (typeof this.style.left == "string") ? "px" : 0;
	this.style.left = x + un;
	this.style.top = y + un;
}
Menu.prototype.open = function(){
	clearTimeout(this.timer);
	Menu.closeAllBut(this);
	this.style.visibility = "visible";
}
Menu.prototype.close = function(){
	var oMenu = this;
	if(arguments[0]) this.timer = setTimeout(function(){oMenu.close()},arguments[0]);
	else this.style.visibility = "hidden";
}
Menu.prototype.cancelClose = function(){clearTimeout(this.timer);}

/***	Item Object ***/
function Item(w,h,o){
	this.id = "Item" + ++Item.count;
	this.width = (w) ? w : 0;
	this.height = (h) ? h : 0;
	this.ownerMenu = o;
	this.onClass = (arguments[3]) ? arguments[3] : "itemOn" 
	this.offClass = (arguments[4]) ? arguments[4] : "itemOff" 
	this.childMenu = null;
	this.obj = this.style = this.contentObj = this.contentStyle = this.href = null;
	this.create();
	var oItem = this;
	this.obj.onmouseover = function(){ 
		this.className = c.getObject(this.id + "Content").className = oItem.onClass;
		if(oItem.childMenu) oItem.childMenu.open();
	}
	this.obj.onmouseout = function(){ 
		this.className = c.getObject(this.id + "Content").className = oItem.offClass ;
		if(oItem.childMenu) oItem.childMenu.close(100);
	}
}
Item.count = 0;
Item.prototype.create = function(){
	var id=this.id + "Table"
	if(c.isDOM1){
		var e = document.createElement("div");
		e.setAttribute("id",this.id);
		if(this.ownerMenu) this.ownerMenu.obj.appendChild(e);
		else document.body.appendChild(e);
		var table = document.createElement("table");
		table.setAttribute("id",id)
		table.insertRow(0);
		table.width = this.width;
		table.height = this.height;
		e.appendChild(table);
		var spacer = table.rows[0].insertCell(0);
		spacer.width = 8;
		spacer.noWrap;
		var content = table.rows[0].insertCell(1);
		content.setAttribute("id",this.id + "Content");
		content.width = this.width - 8; 
		content.noWrap;
		table.cellpadding = table.cellspacing = table.border = spacer.border = content.border = 0;
		this.obj = e; this.contentObj = content;
	} else if(c.isDOM0) {
		if(!c.isMac) var text = '<div id="' + this.id + '"><table cellpadding="0" cellspacing="0" border="0" width="' + this.width + '"><tr><td height="' + this.height + '" width="8" nowrap></td><td height="' + this.height + '" width="' + (this.width - 8) + '" id="' + this.id + 'Content" nowrap></td></tr></table></div>';
		else var text = '<div id="' + this.id + '" style="padding:5px 0px 0px 10px"><a style="text-decoration:none;" href="#" id="' + this.id + 'Content">test</a></div>';
		if(this.ownerMenu) this.ownerMenu.obj.insertAdjacentHTML("beforeEnd",text);
		else document.body.insertAdjacentHTML("beforeEnd",text);
		this.obj = c.getObject(this.id); this.contentObj = c.getObject(this.id + "Content");	
	}
	this.style = c.getStyle(this.obj);
	this.contentStyle = c.getStyle(this.contentObj);
	this.style.position = "absolute";
	this.style.visibility = "inherit";
	this.style.height = this.height;
	this.style.width = this.width;
	this.obj.className = this.contentObj.className = this.offClass;	
}
Item.prototype.setText = function(x){
	if(c.isDOM1) this.contentObj.appendChild(document.createTextNode(x));
	else if(c.isDOM0) this.contentObj.innerHTML = x
}
Item.prototype.getText = function(){
	if(c.isDOM1) return this.contentObj.firstChild.nodeValue;
	else if(c.isDOM0) return this.contentObj.innerHTML;
	else return null;
}
Item.prototype.setHref = function(x){this.href = x; this.obj.onclick = function(){location.href = x};}
Item.prototype.getHref = function(){return this.href;}
Item.prototype.moveTo = function(x,y){
	var un = (typeof this.style.left == "string") ? "px" : 0;
	this.style.left = x + un;
	this.style.top = y + un;
}
Item.prototype.assignChild = function(menu){
	this.childMenu = menu;
	menu.parentMenu = this.ownerMenu;
}
