Array.prototype.contains = function(value)
{
    var found = false;
    for (var i = 0; i < this.length; i++)
    {
        if (this[i] == value)
        {
            found = true;
            break;
        }
    }
    return found;
};
Array.prototype.findByTabId = function(tabid)
{
    for (var i = 0; i < this.length; i++)
    {
        if (this[i].TabId == tabid)
        {
            return this[i];
        }
    }
    return null;
};



Position.prototype.Top = 0;
Position.prototype.Left = 0;
function Position(top, left)
{
    this.Top = top;
    this.Left = left;
}



MenuItem.prototype.ParentId = null;
MenuItem.prototype.TabId = null;
MenuItem.prototype.IsInActiveTree = null;
MenuItem.prototype.Position = null;
function MenuItem(parentid, tabid, isinactivetree)
{
	this.ParentId = parentid;
	this.TabId = tabid;
	this.IsInActiveTree = isinactivetree;
}



MenuPart.prototype.Prefix = null;
MenuPart.prototype.MenuType = null;
MenuPart.prototype.Direction = null;
MenuPart.prototype.Tree = null;
MenuPart.prototype.TopHeight = null;
MenuPart.prototype.TopWidth = null;
MenuPart.prototype.TopNodes = null;
MenuPart.prototype.Nodes = null;
MenuPart.prototype.CurrentItem = null;
MenuPart.prototype.TopContent = null;
MenuPart.prototype.SubContent = null;
function MenuPart(prefix, menutype, direction)
{
	this.Prefix = prefix;
	this.MenuType = menutype;
	this.Direction = direction;
    //this.Tree = new Array();
	this.TopHeight = 0;
	this.TopWidth = 0;
    this.TopNodes = new Array();
    this.Nodes = new Array();
    this.Positions = new Array();
	this.CurrentItem = -1;
	this.TopContent = document.getElementById(prefix + "TopContent");
	this.SubContent = document.getElementById(prefix + "SubContent");
}
// Méthode appelé sur le body onload pour placer l'arbre en fonction de la direction du menu
MenuPart.prototype.OnLoad = function()
{
    if (this.Tree.length > 0)
    {
        // Construction des tableaux de nodes
        for (var i = 0; i < this.Tree.length; i++)
        {
            if (this.Tree[i].ParentId > 0)
            {
                if (!this.Nodes.contains(this.Tree[i].ParentId))
                {
                    this.Nodes.push(this.Tree[i].ParentId);
                }
            }
            else
            {
                this.TopNodes.push(this.Tree[i].TabId);
            }
        }
        // Variables pour le positionnement
        var item = document.getElementById(this.Prefix + "Item" + this.Tree[0].TabId);
        if (this.Direction == "Horizontal")
        {
            this.TopHeight = item.offsetHeight;
        }
        else
        {
            this.TopWidth = item.offsetWidth;
        }
        //
        if (this.MenuType == "Cartridge")
        {
            for (var i = 0; i < this.Tree.length; i++)
            {
                if (this.Tree[i].IsInActiveTree)
                {
                    if (this.Tree[i].ParentId < 0)
                    {
                        this.OverTopItem(this.Tree[i].TabId);
                    }
                    else
                    {
                        this.OverSubItem(this.Tree[i].TabId);
                    }
                }
            }
        }
    }
};
MenuPart.prototype.CheckLoad = function()
{
    if (!(((this.Direction == "Horizontal") && (this.TopHeight != 0)) || ((this.Direction == "Vertical") && (this.TopWidth != 0))))
    {
        this.OnLoad();
    }
};
// Positionnement des subcontents
MenuPart.prototype.GetItemPosition = function(id)
{
    var menuitem = this.Tree.findByTabId(id);
    if (menuitem != null)
    {
        // TODO : optimiser en regardant d'abord si la position du menu a changé,
        // si rien n'a changé et menuitem.Position != null, retourner menuitem.Position
        //
        var refitem = document.getElementById(this.Prefix + "Item" + id);
        var item = refitem;
        var left = 0;
        var top = 0;
        // Position du parent
        while ((item != null) && (item.id != this.Prefix + "SubContainer"))
        {
	        left += item.offsetLeft;
	        top += item.offsetTop;
	        item = item.offsetParent;
        }
        if (menuitem.ParentId < 0)
        {
            var sc = document.getElementById(this.Prefix + "SubContainer");
            left = left - sc.offsetLeft;
		    top = top - sc.offsetTop;
		}
		else
		{
		    var tc = document.getElementById(this.Prefix + "TopContent");
		    left = left - tc.offsetLeft;
		    top = top - tc.offsetTop;
		}
        // Offset par rapport au parent
        if (this.MenuType != "Cartridge")
        {
            if (this.TopNodes.contains(id))
            {
                if (this.Direction == "Horizontal")
                {
                    top += this.TopHeight;
                }
                else
                {
                    left += this.TopWidth;
                }
            }
            else
            {
                left += refitem.offsetWidth;
            }
        }
        else
        {
            top += refitem.offsetHeight;
            left -= refitem.offsetLeft;
        }
        //
        menuitem.Position = new Position(top, left);
        return menuitem.Position;
    }
    return null;
};
MenuPart.prototype.OverTopItem = function(id)
{
    this.CheckLoad();
    //
    if (this.MenuType == "Cartridge")
    {
        this.CurrentItem = -1;
        MenuPartClose(this.Prefix);
    }
    //
    var item = document.getElementById(this.Prefix + "Item" + id);
    var menuitem = this.Tree.findByTabId(id);
    if ((menuitem == null) || !menuitem.IsInActiveTree)
    {
        item.className = this.Prefix + "TopItem_Over";
    }
    var subcontent = document.getElementById(this.Prefix + "SubContent" + id);
    if (subcontent != null)
    {
        var pos = this.GetItemPosition(id);
        subcontent.style.top = pos.Top + "px";
        subcontent.style.left = pos.Left + "px";
        subcontent.style.visibility = "visible";
    }
    this.CurrentItem = id;
};
MenuPart.prototype.OutTopItem = function(id)
{
    var menuitem = this.Tree.findByTabId(id);
    if ((menuitem == null) || !menuitem.IsInActiveTree)
    {
        var item = document.getElementById(this.Prefix + "Item" + id);
        var subcontent = document.getElementById(this.Prefix + "SubContent" + id);
        if (subcontent != null)// && (subcontent.style.display == "block"))
        {
            item.className = this.Prefix + "TopItem_Over";
        }
        else
        {
            item.className = this.Prefix + "TopItem";
        }
    }
    this.OutItem();
};
MenuPart.prototype.OverSubItem = function(id)
{
    if (this.MenuType == "Cartridge")
    {
        this.CurrentItem = id;
        MenuPartClose(this.Prefix);
    }
    //
    var item = document.getElementById(this.Prefix + "Item" + id);
    var menuitem = this.Tree.findByTabId(id);
    if ((menuitem == null) || !menuitem.IsInActiveTree)
    {
        item.className = this.Prefix + "SubItem_Over";
    }
    var subcontent = document.getElementById(this.Prefix + "SubContent" + id);
    if (subcontent != null)
    {
        var pos = this.GetItemPosition(id);
        subcontent.style.top = pos.Top + "px";
        subcontent.style.left = pos.Left + "px";
        subcontent.style.visibility = "visible";
    }
    this.CurrentItem = id;
};
MenuPart.prototype.OutSubItem = function(id)
{
    var menuitem = this.Tree.findByTabId(id);
    if ((menuitem == null) || !menuitem.IsInActiveTree)
    {
        var item = document.getElementById(this.Prefix + "Item" + id);
        var subcontent = document.getElementById(this.Prefix + "SubContent" + id);
        //alert(subcontent);
        if (subcontent != null)// && (subcontent.style.display == "block"))
        {
            item.className = this.Prefix + "SubItem_Over";
        }
        else
        {
            item.className = this.Prefix + "SubItem";
        }
    }
    this.OutItem();
};
MenuPart.prototype.OutItem = function()
{
    if (this.MenuType != "Cartridge")
    {
        this.CurrentItem = -1;
        setTimeout('MenuPartClose(' + this.Prefix + ')', 100);
    }
};
function MenuPartClose(prefix)
{
    var menupart = eval(prefix);
    // On ferme tous les containers sauf si CurrentItem > 0, il faut alors garder ouverte toute la chaine menant à CurrentItem
    var keep = new Array();
    // Si CurrentItem > 0 on calcule la chaîne de CurrentItem, celle qu'il faudra garder
    if (menupart.CurrentItem > 0)
    {
        var curid = menupart.CurrentItem;
        // Si CurrentItem est une feuille il faut d'abord trouver son noeud
        for (var i = menupart.Tree.length - 1; i >= 0; i--)
        {
            if (menupart.Tree[i].TabId == curid)
            {
                keep.unshift(curid);
                curid = menupart.Tree[i].ParentId;
                if (menupart.Tree[i].ParentId <= 0)
                {
                    break;
                }
            }
        }
    }
    // Fermer tout sauf ce qu'il y a dans keep
    for (var i = 0; i < menupart.Nodes.length; i++)
    {
        if (!keep.contains(menupart.Nodes[i]))
        {
            var item = document.getElementById(menupart.Prefix + "Item" + menupart.Nodes[i]);
            var menuitem = menupart.Tree.findByTabId(menupart.Nodes[i]);
            if (menupart.TopNodes.contains(menupart.Nodes[i]))
            {
                if ((menuitem != null) && (menuitem.IsInActiveTree))
                {
                    item.className = menupart.Prefix + "TopItem_Active";
                }
                else
                {
                    item.className = menupart.Prefix + "TopItem";
                }
            }
            else
            {
                if ((menuitem != null) && (menuitem.IsInActiveTree))
                {
                    item.className = menupart.Prefix + "SubItem_Active";
                }
                else
                {
                    item.className = menupart.Prefix + "SubItem";
                }
            }
            var subcontent = document.getElementById(menupart.Prefix + "SubContent" + menupart.Nodes[i]);
            if (subcontent != null)
            {
                subcontent.style.visibility = "hidden";
            }
        }
    }
}
