var menuCtrl = {
    mainUL : null,
    submenuUL:null,
    submenu:null,
    currentSelectedMain:null,
    currIndex:null,
    currSubIndex:null,
    currSubItems:null,
    items:[
        {n:"Home",t:"Home"},
        {n:"FirstQuote",c:[{n:"Overview",t:"FirstQuoteOverview"},{n:"Details",t:"FQDetails"}/*,{n:"Server",t:"FirstQuoteServer"},{n:"Graph",t:"FirstQuoteGraph"},{n:"Lite",t:"FirstQuoteLite"},{n:"Extensions",t:"FQExtensions"}*/]},
        {n:"StatLink",c:[{n:"Overview",t:"StatLinkOverview"},{n:"Front-end Tools",t:"StatLinkDash"},{n:"Administration Tools",t:"StatLinkAdmin"},{n:"Data Sets",t:"StatLinkCS"},{n:"Transco Invoice Client",t:"TranscoInvoiceClient"}]},
        {n:"LNGLive",t:"LNGLive"},
        {n:"Digital",t:"Digital"}
    ],
    xtraLinks:["Clients","About","Contact"],
    
    jumpTo:function(target){
      for(var a=0;a<this.xtraLinks.length;a++){
          if(target==this.xtraLinks[a]){
              this._hideCurrent();//TODO SORT THIS OUT
              getData(target);
          }
      }
      for(var i=0;i<this.items.length;i++){
          if(this.items[i].t){
              if(this.items[i].t == target){
                  this.showMenu(i);
                  return;
              }
          }else if(this.items[i].c){
              var subItems = this.items[i].c;
              for(var x =0;x<subItems.length;x++){
                  if(subItems[x].t == target){
                      this.showMenu(i,true);
                      this.showSubmenu(x, this._getOffset(this.mainUL.childNodes[i]));
                      return;
                  }
              }
          }   
      }  
    },
    _hideCurrent:function(){//called when additional link is used
        if(this.currIndex != null)this.mainUL.childNodes[this.currIndex].className = "";   
        this.currIndex = null;
        this.submenuContainer.style.height="20px";
        this.submenu.style.display = "none";
        
    },
    init:function(){
        this.mainUL =$("mainMenuUL");
        this.submenuUL = $("submenuUL");        
        this.submenu = $("submenu");
        this.submenuContainer = $("submenuContainer");
        
        for(var i=0;i<this.items.length;i++){
            var li = $CE("li");            
            li.innerHTML =this.items[i].n;
            li.index = i;
            invz.Events.add(li, this.menu_click, "click");
            this.mainUL.appendChild(li);
        }
        this.submenu.style.display = "none";
        this.showMenu(0);
    },
    menu_click:function(event){
        if(event.currentTarget.index != null){
            menuCtrl.showMenu(event.currentTarget.index);
        }
    },
    _getOffset:function(item){
        return item.offsetLeft + (parseInt(item.offsetWidth)/2) -20;
    },
    //show main menu
    showMenu:function(index,ignoreSubmenu/*ignore for visual only*/){
        if(this.currIndex == index)return;
        if(this.currIndex != null)this.mainUL.childNodes[this.currIndex].className = "";    
        this.mainUL.childNodes[index].className = "selected";    
        
        this.currIndex = index;
        if(this.items[index].c ){//has sub menu - build list and show first one
            var offsetLeft = this._getOffset(this.mainUL.childNodes[index])
            
            this.submenuUL.innerHTML = "";     
            var subItems = this.items[index].c;
            this.currSubItems = subItems;
            var length = subItems.length;
            for(var i =0;i<length;i++){
                var li = $CE("li");            
                li.innerHTML =subItems[i].n;
                li.index = i;
                invz.Events.add(li, this.submenu_click, "click");
                this.submenuUL.appendChild(li);                
            }
            this.submenu.style.display = "block";
            if(!ignoreSubmenu)this.showSubmenu(0,offsetLeft);
            this.submenuContainer.style.height="43px";
            
        }else{
            this.submenuContainer.style.height="20px";
            this.submenu.style.display = "none";
            this.currSubItems = null;
            this.currSubIndex = null;
            getData(this.items[index].t);
        }
    },
    submenu_click:function(event){
      if(event.currentTarget.index != null){
          menuCtrl.showSubmenu(event.currentTarget.index,null);
      }  
    },
    showSubmenu:function(index,offsetLeft){
        if(this.currSubIndex != null ){//not the first time the submenu has been selected
            //if(this.currSubIndex == index)return;
            try{
                this.submenuUL.childNodes[this.currSubIndex].className="";
            }catch(e){}            
        }
        this.submenuUL.childNodes[index].className = "selected";
        if(offsetLeft){
            this.submenu.style.left = offsetLeft+"px";
        }        
        this.currSubIndex = index;
            
        getData(this.currSubItems[index].t);
        
    }
    
}


