var OpenMenu;
var CurrrentHandle;
var MaxHeight;
MaxHeight = 23;

var OpenMenu = new Array();
var CurrrentHandle = new Array();

function GetId(id)
{
    for(var t = 0 ; t < CurrrentHandle.length ; t++)
    {
         if(parseInt(CurrrentHandle[t].Id) == id)
          return t;
    }
    return -1;
}

function ShowWindows(id,ChildCount)
{
    var el;
    el = document.getElementById("menu_" + id);
    if(el.Height == null)
    {
        el.Height = (ChildCount * MaxHeight)+ 'px';
        el.style.height = '0px';
        el.style.overflow = 'hidden';
    }
    el.style.display = '';
    var i;
    i = parseInt(el.style.height);
    i = i + 4;

    el.style.height = i+'px';
    if(i >= parseInt(el.Height))
    {
        el.style.display = '';

        el.style.height = el.Height;
        var idd = GetId(id);
        if(idd != -1)
        {
            el.style.overflow = 'visible';
            clearInterval(CurrrentHandle[idd].Handle);
            CurrrentHandle.splice(idd,1);                      
        }
    }               
}

function HideWindows(id)
{
    var el = document.getElementById("menu_" + id);
    el.style.overflow = 'hidden';
    var i;
    i = parseInt(el.style.height);
    i = i - 2;

    if(i <= 0)
    {        
        el.style.display = 'none';
        el.style.height = '0px';
        var idd = GetId(id);
        if(idd != -1)
        {
            clearInterval(CurrrentHandle[idd].Handle);
            CurrrentHandle.splice(idd,1);                      
        }
        return;
    }
    else
        el.style.height = i+'px';
}


function Work(id,handle)
{
    this.Id = id;
    this.Handle = handle;
}

function WorkingOn(id)
{
    for(var t = 0 ; t < CurrrentHandle.length ; t++)
    {
     if(CurrrentHandle[t].Id == id)
         return CurrrentHandle[t];
    }
    return null;
}

function VisibleManager(id,show,childcount)
{
    var t;
    t = WorkingOn(id);
    if(t != null)
    {           
        clearInterval(t.Handle);                                        
        if(show == true)
            t.Handle = window.setInterval("ShowWindows("+id+"," + childcount + ");",5);
        else 
            t.Handle = window.setInterval("HideWindows("+id+");",5);
    }
    else
    {
        if(show == true)
            CurrrentHandle.push(new Work(id,window.setInterval("ShowWindows("+id+"," + childcount + ");",5)));
        else
            CurrrentHandle.push(new Work(id,window.setInterval("HideWindows("+id+");",5)));
    }
}

function IsChilds(parent,id,cname)
{
    var c,e;
    if(cname == null)
           return false;

    e = cname.FindParent(parent);
     if(e == null)return false;
     if(e.Childs == null)return false;
     for(var i = 0 ; i < e.Childs.length ; i++)
     {
       c = e.Childs[i];
          if(c.Id == id)
                return true;
     }
     return false;
}

function ShowMenu(id,cname,childcount)
{
    var opennew = true;

     for(var i = OpenMenu.length - 1  ; i >= 0 ; i--)
     {
          if(OpenMenu[i] == id)
          {
            opennew = false;            
              for(var t = OpenMenu.length - 1 ; t >= i ; t--)
              {
                 VisibleManager(OpenMenu[t],false,childcount);
                 OpenMenu.splice(t,1);
              }
            break;               
          }
     }

     if(opennew == true)
     {
       var found = false;
       
       for(var i = OpenMenu.length - 1 ; i >= 0 ; i--)
       {
         if(IsChilds(OpenMenu[i],id,cname) == true)
         {
           found = true;
              for(var t = OpenMenu.length - 1 ; t > i ; t--)
              {
                 VisibleManager(OpenMenu[t],false,childcount);
                 OpenMenu.splice(t,1);
              }
           break;
         }
       }
       
       if(found == false)
       {
            for(var t = OpenMenu.length - 1 ; t >= 0 ; t--)
            {
               VisibleManager(OpenMenu[t],false,childcount);
               OpenMenu.splice(t,1);
            } 
       }
       
       VisibleManager(id,true,childcount);
       OpenMenu.push(id);
     }

}

function MoveTo(url)
{
    window.location = url;
}

function ObjMenu(id,pid,title,link)
{
    this.Id = id;
    this.ParentId = id;
    this.Title = title;
    this.Link = link;
    this.Childs = null;
}

function MenuManager(partentid,classname)
{
    this.ParentId = partentid;
    this.ClassName = classname;
    this.Parent = new Array();
    this.Childs = new Array();

    this.AddMenu = function(id,pid,title,link)
    {
        t = new ObjMenu(id,pid,title,link);
        if(pid == this.ParentId)
            this.Parent.push(t);        
        else
        {
            pa = this.FindParent(pid);
            if(pa != null)
            {
                if(pa.Childs == null)
                    pa.Childs = new Array();
                pa.Childs.push(t);
            }
        }
        this.Childs.push(t);
    }

    this.FindParent = function(pid)
    {
        for(i = 0 ; i < this.Childs.length ; i++)
        {
          if(this.Childs[i].Id == pid)
              return this.Childs[i];
        }
        return null;
    }

    this.Write = function()
    {
        var level;
        level = 1;                 
        for(var j = 0 ; j < this.Parent.length ; j++)
        { 
          if(this.Parent[j].Childs == null)
            this.WriteItem(this.Parent[j],true,level);
          else
          {
            document.write("<div onclick=\"ShowMenu("+this.Parent[j].Id+ ",null," + this.Parent[j].Childs.length + ");\">");
            this.WriteItem(this.Parent[j],false,level);
            document.write('</div>');
            
            document.write('<div class="MenuPlace1" id="menu_'+this.Parent[j].Id+'" style="display:none; height:0px">');
            this.SubWrite(this.Parent[j].Childs,level + 1);
            document.write('</div>');
          }
        }
    }

    this.SubWrite = function(list,level)
    {              
        for( var j = 0 ; j < list.length ; j++)
        { 
          if(list[j].Childs == null)
            this.WriteItem(list[j],true,level);
          else
          {
            document.write("<div onclick=\"ShowMenu("+list[j].Id+ "," +this.ClassName+"," + list[j].Childs.length + ");\">");
            this.WriteItem(list[j],false,level);
            document.write('</div>');

            document.write('<div class="MenuPlace'+level+'" id="menu_'+list[j].Id+'" style="display:none; height:0px">');
            this.SubWrite(list[j].Childs,level + 1);
            document.write('</div>');
          }
        }
    }

    this.WriteItem = function(obj,empty,level)
    {
        if(empty == true)
          document.write('<DIV class=Menu'+level+' style="height:'+MaxHeight+'px"><img src=\"files\\Fa\\Images\\Template\\menu-default.gif\" style=\"float:right\" /><A href="'+obj.Link+'">');
        else 
        {
          document.write('<DIV class=Menu'+level+' style="height:'+MaxHeight+'px"><img src=\"files\\Fa\\Images\\Template\\menu-default.gif\" style=\"float:right\" /><A href="');
          document.write(obj.Link);
          document.write('">');
        }
        document.write(obj.Title);
        document.write('</A></DIV>');     
    }
}
