// JavaScript Document
//------------------BEGIN UNTILY FUNCTIONS (mostly HTML abstractions)
function DIV(id,style,acontents){
	var divString = "<div id=" + id + " style=";
	for(var st = 0; st < style.length; st++)
		divString = divString + style[st] + ";";
	return divString + "position:absolute;" + "z-index:2" + "'>" + acontents + "</div>";
}
function ALINK(href,aclass,thecontents){
	return "<a href='" + href + "' class='" + aclass + "'>" + thecontents + "</a>";
}
function IMG(src,width,height){
	return "<img src='" + src + "' width='" + width + "' height='" + height + "' border='0'>";
}


function pad(astring){
	var spacer = "";
	for(var r=astring.length; r < xMenu.BUFFEREDLENGTH; r++)  
		spacer +="&nbsp;";
	return "&nbsp;&nbsp;" + astring + spacer;
}

//-----------------------DOM INTERACTIVE , EVENT HANDLER HELPERS
function toggleLabel(aParent, show){ 
	theColor = (show == "show") ? xMenu.HIGHLIGHTCOLOR : xMenu.BACKGROUNDCOLOR;
	aParent.childNodes[0].style.background = theColor;
	aParent.childNodes[1].style.background = theColor;
}
function toggleItems(aParent, show){
	vis = (show == "show") ? "visible" : "hidden";
	aParent.style.visibility = vis;
	items = aParent.childNodes;
	for(i=0;i<items.length;i++)
		if(items[i].id == "menu"){	
			items[i].childNodes[0].style.visibility = vis;					
			items[i].childNodes[1].style.visibility = vis;						
		}
}
//------------------END UTILITY


//-----------------BEGIN OBJECT MODEL & STATIC VARIABLES
xMenuBar = new Object;	

function mNode(val,childs, id){
	this.childNodes = childs || null;
	this.value = val || "";
	this.top;
	this.left;
	this.id = id || "";
	this.setPosition = function(atop,aleft){ this.top = atop; this.left = aleft;};
}

xItem.prototype = new mNode;
function xItem(val, alink, id, width){
	this.base = mNode;
	this.base(val, null, id || "item");

	this.width = width || xMenuBar.CELLWIDTH;
	this.itemLink = alink || "";
	this.toString = 
		function(){
			return 	DIV(this.id, 
						["top:" + this.top + "px", 	
						 "left:" + this.left + "px",
						 "height:" + xMenuBar.CELLHEIGHT + "px",
						 "width:" + this.width + "px",
						 "background-color:" + xMenu.BACKGROUNDCOLOR], 
						  this.makeContents() );		
		};
		
	this.makeContents = function(){ return ALINK(this.itemLink,xItem.CSS_STYLE,pad(this.value)) }
}

xMenu.HORIZONTAL = 10;
xMenu.VERTICAL = 1;

xMenu.prototype = new mNode;
function xMenu(val, items, expand, top, left, flow){
	this.base = mNode;
	this.base(val || "",items);
	this.setPosition(top || 0, left || 2);

	this.expandDirection = expand;
	this.flow = flow || xMenu.VERTICAL;
	this.isLabeled = (this.flow == xMenu.VERTICAL);	
	this.toString = 
		function(){
			this.id = "menubar";
			if(this.isLabeled){
				this.id = "menu";
				var vis = "visibility:hidden";
			}
			return DIV(this.id, 					
					   ["top:" + this.top + "px",	"left:" + this.left + "px",  vis || ""], 
					   this.makeLabel() + this.getContents() ) ;
		}
		
	this.makeLabel =
		function(){
			var labelTop = 0;
			var labelLeft = 0;
			var headerLabelWidth = xMenuBar.CELLWIDTH - xMenu.ARROWWIDTH;
			imgSrc = (this.expandDirection == xMenu.HORIZONTAL) ? xMenu.RGTARROWSRC: xMenu.DWNARROWSRC;

			if(this.isLabeled){
				var header = new xItem(this.value, "#", "menulabel", headerLabelWidth);
				header.setPosition(labelTop, labelLeft);
				header += DIV("menulabel", 
							["top:" + labelTop + "px", 	
							 "left:" + (labelLeft + headerLabelWidth) + "px",
							 "height:" + xMenuBar.CELLHEIGHT + "px",
							 "width:" + xMenu.ARROWWIDTH + "px",
							 "background-color:" + xMenu.BACKGROUNDCOLOR], 
						 	 IMG(imgSrc,xMenu.ARROWWIDTH,xMenuBar.CELLHEIGHT));
			}
			return header || "";
		};
	this.getContents = 
		function(){
			var acurrentTop = 0;
			var acurrentLeft = 0; 

			//account for pre produced label!!  
			if(this.isLabeled)
				if(this.expandDirection == xMenu.HORIZONTAL)
					acurrentLeft += xMenuBar.CELLWIDTH;
				else
					acurrentTop += xMenuBar.CELLHEIGHT;

			//start to generate the items
			this.childNodes[0].setPosition(acurrentTop, acurrentLeft);
			var thecontents = this.childNodes[0].toString();			
			for(var z=1; z<this.childNodes.length;z++){
				if(this.flow == xMenu.HORIZONTAL)
					acurrentLeft += xMenuBar.CELLWIDTH;
				else
					acurrentTop += xMenuBar.CELLHEIGHT;
				this.childNodes[z].setPosition(acurrentTop,acurrentLeft);
				thecontents += this.childNodes[z]; 
			}		
			return thecontents;
		};
	this.show = function() { document.write(this); setTimeout("", 50); show();};
}
xItem.CSS_STYLE = "menu";
xMenu.DWNARROWSRC = "../images/spacer.gif";
xMenu.RGTARROWSRC = "../images/menu_arr_rgt.gif";
xMenu.BUFFEREDLENGTH = 10;
xMenu.ARROWWIDTH = 2;
xMenu.BACKGROUNDCOLOR = "#391474"; //"navy";
xMenu.HIGHLIGHTCOLOR = "red";
xMenuBar.CELLWIDTH = 120;
//xMenuBar.CELLWIDTH = 120;
xMenuBar.CELLHEIGHT = 16;
//-------------------END OBJECT MODEL


//-------------------BEGIN INTERACTIVE EVENT HANDLERS AND UNIVERSAL INITIALIZATION
function toggleMenu(anEvent){
	var anEvent = anEvent || window.event;	
	var action = (anEvent.type == "mouseover") ? "show": "hide";
	toggleLabel(this, action);
	toggleItems(this , action);					
}
function itemHighlight(anEvent){
	var anEvent = anEvent || window.event;
	var highlight = (anEvent.type == "mouseover") ? xMenu.HIGHLIGHTCOLOR: xMenu.BACKGROUNDCOLOR;
	this.style.background = highlight;
}

function show(){
	menuTop = document.getElementById("menubar");
	toggleItems(menuTop, "show");
	
	//event handler registration
	divList = document.getElementsByTagName("div");
	for(i = 0; i < divList.length; i++)
		if(divList[i].id == "menu"){
			divList[i].onmouseover = toggleMenu;
			divList[i].onmouseout = toggleMenu;
		} else if(divList[i].id == "item"){
			divList[i].onmouseover = itemHighlight;
			divList[i].onmouseout = itemHighlight;
		}
}
//-------------------------END INTERACTIVE





//-------------------------BEGIN CLIENT USE
// 	***********************************
//	make items, menus, and menubar here  
//	*********************************** 

//					****API*****
//xItem(label, link)
//xMenu(label, menu items, expand direction)
// 	      where expand direction = xMenu.HORIZONTAL OR xMenu.VERTICAL
//			and items = an array of items and menus, ex. [item1,menu1,item2,item3]
//TO CREATE TOP MOST MENU, use:
//xMenu(label, menu items, expand direction, top, left, menu flow direction)
// 	      where menu flow direction = xMenu.HORIZONTAL
//AFTER CREATING TOP MOST MENU, must use .show() to display

//Set any one of the following as appropriate to customize
//xItem.CSS_STYLE = "menu"; 				//matches style declaration for links
//xMenu.DWNARROWSRC = "spacer.gif";			//for down arrow for a vertically expanding menu
//xMenu.RGTARROWSRC = "menu_arr_rgt.gif";	//for right arrow for a horizontally expanding menu
//xMenu.BUFFEREDLENGTH = 25;				//for the target number of characters to pad a label to
//xMenu.ARROWWIDTH = 15;					//for width of arrow image, height of arrow image must be cell height
//xMenu.BACKGROUNDCOLOR = "gray";			//for background color of a menu item when not highlighted
//xMenu.HIGHLIGHTCOLOR = "red";			//for when highlighted
//xMenuBar.CELLWIDTH = 150;					//width of a menu item					
//xMenuBar.CELLHEIGHT = 20;					//height of a menu item


ffirstzero=new xItem("Place&nbsp;Ads","http://www.merxit.com/Classifieds/CPAddClass.asp");
ffirstitem=new xItem("Manage&nbsp;My&nbsp;Ads","http://www.merxit.com/Classifieds/CPManageAds.asp");
fseconditem=new xItem("My&nbsp;Memorized&nbsp;Ads","http://www.merxit.com/Classifieds/CPMemorizedAds.asp");
fthirditem=new xItem("Ad&nbsp;Watch","http://www.merxit.com/Classifieds/CPAdWatch.asp");
ffourthitem=new xItem("Browse&nbsp;Ad&nbsp;Watch","http://www.merxit.com/Classifieds/CPBrowseAdWatch.asp");
ffifthitem=new xItem ("Bulk&nbsp;Upload","http://www.merxit.com/Classifieds/cpbulkupload.asp");
classfees=new xItem("Classified&nbsp;Fees","http://www.merxit.com/Membership/XUDDisplayFees.asp");
firstmenu=new xMenu("CREATE&nbsp;ADS",[ffirstzero,ffirstitem,fseconditem,fthirditem,ffourthitem,ffifthitem,classfees],xMenu.VERTICAL);
sfirstitem=new xItem("Power&nbsp;Search","http://www.merxit.com/Classifieds/CPSearch.asp");
sseconditem=new xItem("New&nbsp;Ads","http://www.merxit.com/Classifieds/CPSearch.asp?cmd=NEW");
sthirditem=new xItem("Hot&nbsp;Ads","http://www.merxit.com/Classifieds/CPSearch.asp?cmd=HOT");
secondmenu=new xMenu("SEARCH",[sfirstitem,sseconditem,sthirditem],xMenu.VERTICAL);
tfirstitem=new xItem("Classifields&nbsp;Home","http://www.merxit.com/Classifieds/XcClassified.asp");
tseconditem=new xItem("Create&nbsp;an&nbsp;Ad","http://www.merxit.com/Classifieds/CPAddClass.asp");
tthirditem=new xItem("Manage&nbsp;My&nbsp;Ads","http://www.merxit.com/Classifieds/CPManageAds.asp");
tfourthitem=new xItem("My&nbsp;Memorized&nbsp;Ads","http://www.merxit.com/Classifieds/CPMemorizedAds.asp");
tfifthitem=new xItem("Ad&nbsp;Watch","http://www.merxit.com/Classifieds/CPAdWatch.asp");
tsixthitem=new xItem("Browse&nbsp;Ad&nbsp;Watch","http://www.merxit.com/Classifieds/CPBrowseAdWatch.asp");
tseventhitem=new xItem("View&nbsp;Messages","http://www.merxit.com/Membership/XUDMessageBase.asp");
t8thitem=new xItem("Classified&nbsp;Fees","http://www.merxit.com/Membership/XUDDisplayFees.asp");
//t9thitem=new xItem("Find&nbsp;Professional","http://www.merxit.com/Classifieds/CPViewInCat.asp?ID=596");
t9thitem=new xItem("Business Ads","http://www.merxit.com/Classifieds/CPbusinessadvertising.asp");
t10thitem=new xItem("My&nbsp;Account","http://www.merxit.com/Membership/XUD.asp ");
t11thitem=new xItem("Register","http://www.merxit.com/Membership/XUDRegister.asp");
t12thitem=new xItem("About&nbsp;Us","http://www.merxit.com/Classifieds/CPAboutus.asp");
t13thitem=new xItem("View&nbsp;Your&nbsp;Bill","http://www.merxit.com/Membership/XUDViewBill.asp");
t14thitem=new xItem("Forum","http://www.merxit.com/forum/Forum.asp");
fsixthitem=new xItem ("Bookmark&nbsp;Us","javascript: window.external.AddFavorite(location.href,document.title);");
thirdmenu=new xMenu("MENU&nbsp;OPTIONS",[fsixthitem,tfirstitem,tseconditem,tthirditem,tseventhitem,t8thitem,t9thitem,t10thitem,t11thitem,t12thitem,t13thitem,t14thitem],xMenu.VERTICAL);
ffffirstitem=new xItem("Logout","http://www.merxit.com/Membership/XUDLogout.asp");
fffzeroitem=new xItem("Login","http://www.merxit.com/Membership/XUDLogin.asp");
fourthmenu=new xMenu("LOGIN",[fffzeroitem,ffffirstitem],"http://www.merxit.com/Membership/XUDLogin.asp",xMenu.VERTICAL);
fffirstitem=new xItem("Buyers&nbsp;Guide","http://www.merxit.com/Classifieds/CPBuyerGuide.asp");
ffseconditem=new xItem("Sellers&nbsp;Guide","http://www.merxit.com/Classifieds/CPSellerGuide.asp");
ffthirditem=new xItem("Contact&nbsp;Us","http://www.merxit.com/Classifieds/cpcontactus.asp");
fffourthitem=new xItem("Privacy&nbsp;Policy","http://www.merxit.com/Membership/XUDPrivacy.asp");
fffifthitem=new xItem ("User's&nbsp;Agreement","http://www.merxit.com/Membership/XUDRegister.asp");
ffzeroitem=new xItem ("Help","http://www.merxit.com/Classifieds/CPhelp.asp");
helpclassfees=new xItem("Classified&nbsp;Fees","http://www.merxit.com/Membership/XUDDisplayFees.asp");
fifthmenu=new xMenu("HELP&nbsp;FAQ'S",[ffzeroitem,fffirstitem,ffseconditem,ffthirditem,fffourthitem,fffifthitem,helpclassfees],xMenu.VERTICAL);
//sixthmenu=new xMenu("BOOKMARK&nbsp;US",[fsixthitem],xMenu.VERTICAL);



aMenu = new xMenu("",[firstmenu,secondmenu,thirdmenu,fourthmenu, fifthmenu], xMenu.HORIZONTAL, 0, 0, xMenu.HORIZONTAL);
aMenu.show();
//-------------------------END CLIENT USE
