var MenuCount = 0;
var open_timeout_id;
function menulist()	
{
   MenuCount++;
   var dom = document;
   this.menuname = "MenuList" + MenuCount;	
   this.parent = arguments[0];  
   this.selected = null;  
   this.openned = null;	 
   this.items = new Array();  
   this.closing = {closed:null, timeout:null};  
   this.openning = {opened:null, timeout:null};  
   this.dragname = "MenuDraging" + MenuCount;	
   this.obj = "MenuObject" + MenuCount;  
   eval(this.obj+"=this");
   with(preferences)
   {
   	 this.template = template;  
   	 this.buffer = buffer;  
   	 this.open_time_delay = open_time_delay;  
   	 this.close_time_delay = close_time_delay;  
     this.tolerance = new Object();
     this.tolerance.x = tolerance.x;	
     this.tolerance.y = tolerance.y;  
     this.style = new Object();
     this.style.visibility = "hidden";
     this.style.position = "absolute";
     this.style.top = 0;
     this.style.left = 0;
     this.style.zIndex = -100;
     this.zIndex = MenuCount*10;
     this.dIndex = 100;
     this.style.width = 180;
     this.style.filter = style.filter;
     this.style.text = new Object();
     this.style.text.normal = style.text.normal;
     this.style.text.hover = style.text.hover;
     this.node = new Array();
     this.node.normal = new Object();
     this.node.hover = new Object();
   	 this.node.normal = new Image(node.normal.width, node.normal.height);
   	 this.node.normal.src = node.normal.src;
   	 this.node.hover = new Image(node.hover.width, node.hover.height);
   	 this.node.hover.src = node.hover.src;
     with(preferences.icon)
     {
		 	 this.icon = new Object();	
     	 this.icon.normal = new Image(normal.width, normal.height);
       this.icon.normal.src = normal.src;
     	 this.icon.hover = new Image(hover.width, hover.height);
     	 this.icon.hover.src = hover.src;
     }
     this.cursor = new Object();
     this.cursor.normal = cursor.normal;
   	 this.cursor.hover = cursor.hover;
   }
   this.generate = function(data)
   {  
      for(var i = 0; i < data.length; i++)
      {  
         var obj = false;
         if(data[i][4])
         {
            obj = new menulist();
            obj.openning = this.openning;
            obj.closing = this.closing;
            obj.generate(data[i][4]);
         }
         this.add(data[i], obj);
      }
   }
   this.add = function(data, is_child)
   {
      j = this.items.length;
      this.items[j] = new Object();
      this.items[j].it_exist = is_child?true:false;
      this.items[j].is_child = is_child;
      this.items[j].anchor = new Object();
      this.items[j].anchor.text = data[0];
      this.items[j].anchor.alt = data[1];
      this.items[j].anchor.href = data[2];
      this.items[j].anchor.target = data[3];
      if( data[5] != null )
      {
      	 this.items[j].icon = new Image(data[6], data[7]);
      	 this.items[j].icon.src = data[5];
      }
      this.items[j].names = new Object();
      this.items[j].names.myself = this.menuname + "ItemMyself" + j;
      this.items[j].names.anchor = this.menuname + "ItemAnchor" + j;
      this.items[j].names.icon = this.menuname + "ItemIcon" + j;
      this.items[j].names.text = this.menuname + "ItemText" + j;
      this.items[j].names.node = this.menuname + "ItemNode" + j;
      return true;
   }
   this.build = function()
   {  
      var output = new String();
      var re1 = new RegExp('{object}', 'gi');
      this.buffer = this.buffer.replace(re1, this.menuname);
      var re1 = new RegExp('{item}.*\\{/item}', 'gi');
      var re2 = new RegExp('{content}', 'gi');
      var re3 = new RegExp("{drag}", "gi"); 
      var menulist = this.template.replace(re1, '{content}');
      var menulist = this.buffer.replace(re2, menulist);
      menulist = menulist.replace(re3, this.dragname);
      var re1 = new RegExp("{item}(.*)\\{/item}", "gi");
      var re2 = new RegExp("{text}", "gi");
      var re3 = new RegExp("{content}", "gi");
      var re4 = new RegExp("{block([\\s\\w=\"\'-:;]*)}", "gi");
      var re5 = new RegExp("{/block}", "gi");
      var re6 = new RegExp("{link([\\s\\w=\"\'-:;]*)}", "gi");
      var re7 = new RegExp("{/link}", "gi"); 
      var re8 = new RegExp("{node([\\s\\w=\"\'-:;]*)}", "gi"); 
      var re9 = new RegExp("{icon([\\s\\w=\"\'-:;]*)}", "gi"); 
      var re10 = new RegExp("{textarea([\\s\\w=\"\'-:;]*)}", "gi"); 
      var re11 = new RegExp("{/textarea([\\s\\w=\"\'-:;]*)}", "gi"); 
      var re12 = new RegExp("{condition}", "gi"); 
      var path = '/nvss/ikar/js/template10/';
      var menuitem = re1.exec(this.template);
      for(var i = 0; i < this.items.length; i++)
      {
         var layout = menuitem[1];
         with(this.items[i])
         {  
            if(i) layout = layout.replace(re12, '<TD><IMG SRC="'+ path+ 'separator.gif" WIDTH="1" HEIGHT="20" BORDER="0"></TD>');
            else layout = layout.replace(re12, '<TD><IMG SRC="'+ path+ 'transparent.gif" WIDTH="1" HEIGHT="20" BORDER="0"></TD>');

         	  layout = layout.replace(re2, anchor.text);
            layout = layout.replace(re4, '<DIV ID="'+ names.myself+ '" $1>');
            layout = layout.replace(re5, '</DIV>');
            layout = layout.replace(re6, '<A HREF="'+ anchor.href+ '" NAME="'+ names.anchor+ '" '+ 
            														 (anchor.target ? 'TARGET="_blank" ' : '')+
            														 '$1>');
            layout = layout.replace(re7, '</A>');
            layout = layout.replace(re10, '<DIV ID="'+ names.text+ '" $1>');
            layout = layout.replace(re11, '</DIV>');
            layout = layout.replace(re8, this.insertnode(i));
            layout = layout.replace(re9, this.inserticon(i));
            output += layout;

            if(it_exist)
            {
               is_child.build();
            }
         }
      }
    	menulist = menulist.replace(re3, output); 
      dom.write(menulist);
   }
   this.insertnode = function(j)
   {
      var pic = new String();
      if(this.items[j].it_exist)
      {
         with( this )
         {
         		pic = '<IMG SRC="'+ node.normal.src+'" WIDTH="'+ node.normal.width+'" HEIGHT="'+ node.normal.height+ '" NAME="'+ items[j].names.node+ '" BORDER="0">';
         }
      }
      return pic;
   }
   this.inserticon = function(j)
   {
      var output = new String();
      if( this.icon.normal.src )
      {
         with( this )
         {
            if( items[j].icon && items[j].icon.src )  output = '<IMG NAME="'+items[j].names.icon+'" SRC="'+items[j].icon.src+'" WIDTH="'+items[j].icon.width+'" HEIGHT="'+items[j].icon.height+'" BORDER="0">';
            else output = '<IMG NAME="'+items[j].names.icon+'" SRC="'+icon.normal.src+'" WIDTH="'+icon.normal.width+'" HEIGHT="'+icon.normal.height+'" BORDER="0">';
         }
      }
      return output;
   }
   this.css = function()
   {
      var output = setStyleSheet(this.menuname, this.style);
      for(var i=0; i < this.items.length; i++)
      {
         with( this.items[i] )
         {
            if(it_exist)
            {
               output += is_child.css(1);
            }
         }
      }
      if(!arguments.length)	 dom.write('<style type="text/css">'+ output+ '</style>');
      return output;
   }
   this.events = function(obj)
   {
      var oList = dom.getElementById(this.menuname);
      oList.onmouseover = new Function(obj+ '.closing.closed = 0; ');
      oList.onmouseout = new Function(obj+ '.outlist(); ');
      for(var i = 0; i < this.items.length; i++)
      {
         with(this.items[i])
         {
            var oItem = dom.getElementById(names.myself);
            oItem.onmouseover = new Function(this.obj+ '.over('+i+'); ');
            oItem.onclick = new Function(this.obj+ '.click('+i+'); ');
            if(it_exist)
            {
               is_child.events(obj);
            }
         }
      }
   }
   this.outclick = function()
   {
      document.onclick = new Function(this.obj+ '.destroy(); return true;');
   }
   this.over = function(i)
	 {
      if(this.selected != i)
      {
      	 this.pointing(i);
         this.extraover(i);
      	 this.openning(i);
      	 this.selected = i;
      }
      return true;
   }
   this.extraover = function(j){ /* empty function */ }
   this.click = function(i)
   {
      this.openbreak(i);
      this.actlink(i);
      return true;
   }
   this.actlink = function(i)
   {
      with(this.items[i])
      {
     	  if(document.links(names.anchor))
     	  {
            document.links(names.anchor).click();
     	  }
      }
   }
   this.pointing = function(i)
   {
      if(this.selected != null) this.deselect(this.selected);
      this.select(i);
      return true;
   }
   this.select = function(i)
   {
      this.setselection(i);
      this.settextcolor(i);
      this.overicon(i);
      this.overnode(i);
      return true;
   }
   this.setselection = preferences.setselection;
   this.settextcolor = function(i)
   {
      setTextColor( this.items[i].names.text, this.style.text.hover);
   }
   this.overicon = function(i)
   {
      with( this )
      {
        var oIcon = document.images[items[i].names.icon];
         if(oIcon && icon.hover.src)
         {
            oIcon.src = icon.hover.src;
         }
      }
   }
   this.overnode = function(i)
   {
      with( this )
      {
        var oNode = document.images[items[i].names.node];
         if(oNode && node.hover.src)
         {
            oNode.src = node.hover.src;
         }
      }
   }
   this.deselect = function(i)
   {
      this.unselection(i);
      this.unsettextcolor(i);
      this.outicon(i);
      this.outnode(i);
      return true;
   }
   this.unsettextcolor = function(i)
   {
      setTextColor( this.items[i].names.text, this.style.text.normal);
   }
   this.unselection = preferences.unselection;
   this.outicon = function(i)
   {
      with( this )
      {
         var oIcon = document.images[items[i].names.icon];
         if(oIcon && icon.normal.src)
         {
            oIcon.src = icon.normal.src;
         }
      }
   }
   this.outnode = function(i)
   {
      with( this )
      {
        var oNode = document.images[items[i].names.node];
         if(oNode && node.normal.src)
         {
            oNode.src = node.normal.src;
         }
      }
   }
   this.openning = function(i)
   {
      with(this.items[i])
      {
         if( (it_exist) || (this.openned != null) )
         {
            this.openning.opened = 1;
            if(open_timeout_id)  clearTimeout(open_timeout_id);
            open_timeout_id = setTimeout(this.obj + '.openprocess('+i+')', this.open_time_delay);
         }
      }
      return true;
   }
   this.openbreak = function(i)
   {
      with(this.items[i])
      {
         if( (it_exist) || (this.openned != null) )
         {
            clearTimeout(open_timeout_id);
            this.openprocess(i);
         }
      }
      return true;
   }
   this.openprocess = function(i)
   {
      if(!this.openning.opened) return false;
      if(this.openned!=null)  this.items[this.openned].is_child.close();
      if(this.items[i].it_exist)
      {
         this.open(i);
         this.openned = i;
      }
      return true;
   }
   this.outlist = function()
   {
      this.closeprocess();
   }
   this.closeprocess = function()
   {
      this.closing.closed = 1;
      if(this.closing.timeout) clearTimeout(this.closing.timeout);
      this.closing.timeout = setTimeout(this.obj+ '.destroy();', this.close_time_delay);
   }
   this.destroy = function()
   {
      if(this.closing.closed)
      {
         this.openning.opened = null;
         this.closing.closed = null;
         this.extraclose();
         this.close();
      }
      return true;
   }
   this.close = function()
   {
      var closed = this.openned;
      var unselect = this.selected;
      this.openned = null;
      this.selected = null
      if(unselect != null)	this.deselect(unselect);
      this.hide(this.menuname, this.style.visibility);
      if(closed != null)	this.items[closed].is_child.close();
      return true;
   }
   this.extraclose = function(){ /* empty function */ }
   this.open = function(i)
   {
      with(this.items[i])
      {
      	 res = this.position(names.myself, is_child);
      	 this.show(is_child.menuname, res[0], res[1], is_child.zIndex);
      }
      return true;
   }
   this.position = function(parent, is_child, own)
   {  
      var child = is_child.menuname;
      var x, y;
      var win_w = getWindowWidth(); 
      var win_h = getWindowHeight(); 
      var parent_top = own ? own.top : getLayerEmbedTop(parent); 
      var parent_left = own ? own.left : getLayerEmbedLeft(parent); 
      var parent_w = own ? own.width : getLayerWidth(parent); 
      var parent_h = own ? own.height : getLayerHeight(parent); 
      var child_top = getLayerEmbedTop(child);
      var child_left = getLayerEmbedLeft(child);
      var child_w = getLayerWidth(child);
      var child_h = getLayerHeight(child);
      if((parent_top+child_h)>win_h) 
      {
        if((parent_top-child_h+parent_h)<0)
        {
           y = 0;
        }
        else
        {
          y = parent_top - child_h + parent_h;
        }
      }
      else
      {
        y = parent_top;
        y += is_child.tolerance.y;
      }
      if( (parent_left + parent_w + child_w) > win_w)
      {
         x = parent_left - child_w;
         x += is_child.tolerance.x;
      }
      else
      {
         x = parent_left + parent_w;
         x -= is_child.tolerance.x;
      }
      return [x, y];
   }
   this.hide = function(name, visibility)
   {
      setLayerVisibility(name, visibility);
      setZindex(name, 0);
      return true;
   }
   this.show = show;
}
function show(name, left, top, zIndex)
{
   var zIndex = this.dIndex * zIndex;
   setPosition(name, top, left);
   setZindex(name, zIndex);
   setLayerShow(name);
   return true;
}