
var gArkAllMenu=new nosContainerArray() ;
var gSubItemCount=0 ;

// Main menu

function FindArkMenu(id) {
	it=gArkAllMenu.GetIterator() ;
	for (it.Begin();!it.IsDone();it.Next()) {
		menu=it.GetCurrent() ;
		if (id==menu.cssid_) {
			return menu ;
		}
	}
}
function ArkMainMenu() {

	this.superClass=nosMenu  ;
	this.superClass() ;

	gArkAllMenu.Insert(this) ;
	
	this.OnSelect=function(mi) {
		if (mi.GetTarget()!="") {
			document.location=mi.GetTarget() ;
		}
	}
	this.onMouseOver=function(e) {
		menu=FindArkMenu(this.id) ;
		if (menu) {
			gArkHolder.KeepAlive(menu) ;
		}
	}
	this.onMouseOut=function() {
		menu=FindArkMenu(this.id) ;
		if (menu) {
			gArkHolder.LeaveMenu(menu) ;
		}
	}
	
	this.EnableMouseOver=function() {
		l=new nosLayer(this.cssid_) ;
		l.EnableMouseOver(this.onMouseOver,this.onMouseOut) ;
	}
}

// MenuItem

function ArkMenuItem() {
	
	this.superClass=MenuItem  ;
	this.superClass() ;

	this.subMenu_="" ;
	this.target_="" ;
	this.needMouseOver_=true ;
	this.SetCSSClass("MENUITEM") ;
	this.cssid_="MENUITEM-"+gSubItemCount++ ;

	this.SetSubMenu=function(menu) {
		this.subMenu_=menu ;
	}
	
	this.GetSubMenu=function() {
		return this.subMenu_ ;
	}

	this.SetTarget=function(target) {
		this.target_=target ;
	}

	this.GetTarget=function() {
		return this.target_ ;
	}
	
	this.MouseOver=function() {

		if (gArkHolder==0) {
			gArkHolder=new MenuHolder() ;
		}
		gArkHolder.EnterMenu(this.menu_) ;
		subMenu=this.subMenu_ ;
		if (subMenu!="") {
			l=new nosLayer(subMenu.GetCSSID()) ;
			gArkHolder.Add(subMenu) ;
			s=new nosLayer(this.GetCSSID()) ;
			x=s.GetX() ;
			y=s.GetY() ;
			w=s.GetWidth()*this.menu_.layout_.GetXOffset() ;
			h=s.GetHeight()*this.menu_.layout_.GetYOffset() ;
			l.SetPosition(x+w,y+h)
			l.Show() ;
			subMenu.EnableMouseOver() ;
		}
	}

	this.MouseOut=function() {
	}
}


function MenuHolder() {
	
	this.items_=new nosContainerArray() ;
	this.hideRequest_=null ;

	this.updateContent=function(item) {

		deb=new nosStringWriter() ;
		deb.write("processing "+item.cssid_+"<br>") ;
		items=new nosContainerArray() ;
		it=this.items_.GetIterator() ;
		found=false ;
		for (it.Begin();!it.IsDone();it.Next()) {
			i=it.GetCurrent() ;
			deb.write(i.cssid_+"...") ;
			if (i!=item) {
				if (found) {
					deb.write("hiding") ;
					menu=i.cssid_ ;
					l=new nosLayer(menu) ;
					l.Hide() ;
				} else {
					deb.write("going down") ;
					items.Insert(i) ;
				}
			} else {
				found=true ;
				deb.write("found") ;
				items.Insert(i) ;
			}
			deb.write("<br>") ;
		}
		if (!found) {
			deb.write("now adding "+item.cssid_) ;
			items.Insert(item) ;
		}
		deb.write("done.") ;
	//	Debug(deb.string_) ;
		this.items_=items ;
	}

	this.EnterMenu=function(menu) {
		this.hideRequest_=null ;
		this.updateContent(menu) ;
	}
	
	this.KeepAlive=function(menu) {
		this.hideRequest_=null ;
	}
	this.LeaveMenu=function(menu) {
		stamp=new Date() ;
		this.hideRequest_=stamp ;
		cb=new nosCallback("HideAll",this,stamp) ;
		cb.SetTimer(300) ;
	}
	
	this.Add=function(menu) {
		this.hideRequest_=false ;
		this.items_.Insert(menu) ;
	}
	
	this.HideAll=function(cb) {
		src=cb.GetSource() ;
		if (this.hideRequest_==src) {
			it=this.items_.GetIterator() ;
			it.Next() ; // first one always visible
			for (;!it.IsDone();it.Next()) {
				i=it.GetCurrent() ;
				menu=i.GetCSSID() ;
				l=new nosLayer(menu) ;
				l.Hide() ;
			}
		}
		this.hideAll_=false ;
	}
}

var gArkHolder=new MenuHolder() ;


function ArkMenuLayout() {
	this.Render=function (d,it) {
		first=true ;
		d.write("<table cellspacing=0 cellpadding=0 align=center><tr>") ;
		for (it.Begin();!it.IsDone();it.Next()) {
			if (!first) {
				d.write("<td>&nbsp;</td>") ;
			} else {
				first=false ;
			}
			d.write("<td>") ;
			it.GetCurrent().Render(d) ;
			d.write("</td>") ;
		}
		d.write("</tr></table>") ;
	}
	this.GetXOffset=function() {
		return 0 ;
	}
	this.GetYOffset=function() {
		return 1 ;
	}
}

function GetMenu(name) {
	  for (ii=0;ii<gAllMenus.length;ii++) {
		mm=gAllMenus[ii] ;
	  	if (mm[0]==name) {
	  		return mm[mm.length-1] ;
	  	}
	  }
}

function BuildMenu() {
//EnableDebug() ;

d=document ;

// We first register all menus

 Debug("register menus") ;
  for (i=0;i<gAllMenus.length;i++) {

    m=gAllMenus[i] ;
	m[m.length]=new ArkMainMenu() ;
	menu=m[m.length-1] ;
	menu.SetCSSID(m[0]) ;
	 Debug("creating menu "+m[0]) ;
	if (i!=0) {
		menu.SetCSSClass("MENUPULLDOWN") ;
		menu.SetLayout(new nosVerticalMenuLayout()) ;
	} else {
		menu.SetLayout(new ArkMenuLayout()) ;
	}

  }

// Then we can create items and link menus together

 Debug("creating items") ;
  for (i=0;i<gAllMenus.length;i++) {
	m=gAllMenus[i] ;
	menu=m[m.length-1] ;
	for (j=1;j<m.length-1;j+=2) {
		mi=new ArkMenuItem() ;
		if (i==0) {
			mi.SetCSSClass("MAINMENUITEM") ;
		}
		mi.SetText(m[j]) ;
		link=m[j+1] ;
		if (link.substr(0,1)==">") {
			sub=GetMenu(link.substr(1)) ;
			mi.SetSubMenu(sub) ;
		}  else {
			mi.SetTarget(link) ;
		}
		menu.Insert(mi) ;
	}
  }
// Finally we render them all

 Debug("rendering items") ;
  for (i=0;i<gAllMenus.length;i++) {
	m=gAllMenus[i] ;
	menu=m[m.length-1] ;
	menu.Render(d) ;
  }
  m=gAllMenus[0] ;
  m[m.length-1].EnableMouseOver() ;
  Debug("done") ;
}




