///**************************
///******** MenuManager Class
///**************************
function MenuManager()
{
    this.menus = new Array();
    this.menuStatusManager = new MenuStatusManager();
}
MenuManager.prototype.menusCount = function()
{
    return this.menus.length;
}
MenuManager.prototype.getMenuById = function(menuId)
{
    for(var i=0;i<this.menus.length;i++)
    {
        if (this.menus[i].id == menuId)
            return this.menus[i];        
    }
    return null;
}
MenuManager.prototype.addMenu = function(menu)
{
    for (i=0; i<this.menus.length; i++)
    {
        if (this.menus[i].id == menu.id)
        {
            this.menus[i] = menu;
            return;
        }
    }
    this.menus.push(menu);
}
MenuManager.prototype.deleteMenu = function(menuId)
{
    for (i=0;i<this.menus.length;i++)
    {
        if (this.menus[i].id == menuId)
        {
            this.menus.Remove(i);
            return;
        }
    }
}
MenuManager.prototype.setMenusTarget = function ()
{
	for (var i=0; i< this.menus.length; i++)
	{
		var menu = this.menus[i];
		menu.setItemTargets();
	}
}
MenuManager.prototype.clear = function()
{
	this.menus.length = 0;
}
MenuManager.prototype.saveCurrentMenuStatus = function()
{
	this.menuStatusManager.reset();
	for (var j=0; j<this.menus.length; j++)
	{
		menu = this.menus[j];
		var bodyShowStatus = true;
		if (menu.bodyStatus != "Shown")
			bodyShowStatus = false;
		this.menuStatusManager.addMenu(menu.id,bodyShowStatus);
		for (var i=0; i<menu.items.length; i++)
		{
			var menuItem = menu.items[i];
			this.menuStatusManager.addMenuItem(menuItem.menuId, menuItem.id, menuItem.childsShowStatus);
		}
	}
}
MenuManager.prototype.restoreMenuStatus = function()
{
	msm = this.menuStatusManager;
	var i=0;
	for(i=0; i< msm.menuItemStatuses.length; i++)
	{
		try
		{
			menuItemStatus = msm.menuItemStatuses[i];
			var menuId = menuItemStatus.menuId;
			var menuItemId = menuItemStatus.menuItemId;
			var childsShowStatus = menuItemStatus.childsShowStatus;
			var menu = this.getMenuById(menuId);
			var menuItem = menu.getItemById(menuItemId);
			if (menuItem.hasChild)
			{
				iconDiv = document.getElementById("mi_icon_"+menuId+"_"+menuItemId);
				if (childsShowStatus)
					MenuItem_Expand.call(iconDiv);
				else
					MenuItem_Shrink.call(iconDiv);
			}
		}
		catch(e)
		{
		}
	}
	for(i=0; i< msm.menuStatuses.length; i++)
	{
		try
		{
			var menuStatus = msm.menuStatuses[i];
			var menuId = menuStatus.menuId;
			var bodyShowStatus = menuStatus.bodyShowStatus;
			var menu = this.getMenuById(menuId);
			if (bodyShowStatus)
				menu.showBody();
			else
				menu.hideBody();
		}
		catch(e)
		{
		}
	}
}

MenuManager.prototype.shrinkAll = function()
{
	for(var i=0; i < this.menus.length; i++)
	{
		var theMenu = this.menus[i];
		theMenu.hideBody();
	}
}

MenuManager.prototype.shrinkAllElse = function(elseMenuId)
{
	for(var i=0; i < this.menus.length; i++)
	{	
		var theMenu = this.menus[i];
		if (theMenu.id == elseMenuId)
			continue;
		else
			theMenu.hideBody();
	}
	
}
MenuManager.prototype.refreshChildShowStatus = function()
{
	for(var i=0; i < this.menus.length; i++)
	{	
		var theMenu = this.menus[i];
		theMenu.refreshChildShowStatus();
	}
}
///**************************
///*************** Menu Class
///**************************
function Menu()
{
    this.id = -1;
    this.title = "";
    this.showContent = true;
    this.items = new Array();
    this.fullBodyHeight = -1;
    //Show And Hide Effect Variables
    this.bodyStatus = "Shown"; // "Hidden","InProcess";
    this.speed = 5;
    this.showHideStep = 20;
    this.inShowHideProcess = false;
    this.inHideProcess = false;
    this.inShowProcess = false;
    this.hideTimerId = -1;
    this.showTimerId = -1;
}
Menu.prototype.initial = function()
{
    showHideBtn = document.getElementById("m_show_hide_btn_"+this.id);
    showHideBtn.onclick = Menu_hideClick;
    showHideBtn.className = "hide_menu_btn";
}
Menu.prototype.itemsCount = function()
{
    return this.items.length;
}
Menu.prototype.getItemById = function()
{
}
Menu.prototype.getItemPath = function(itemlist)
{
}
Menu.prototype.shrinkAll = function()
{
	for (var i=0; i<this.items.length; i++)
	{
		var menuItem = this.items[i];
		//var itemDiv = document.getElementById('mi_'+this.id+'_'+menuItem.id);
		if (menuItem.hasChild)
		{
			iconDiv = document.getElementById("mi_icon_"+this.id+"_"+menuItem.id);
			MenuItem_Shrink.call(iconDiv);
		}
	}		
}
Menu.prototype.setItemTargets = function()
{
	for (var i=0; i<this.items.length; i++)
	{
		var menuItem = this.items[i];
		if (menuItem.target != "")
		{
			var itemDiv = document.getElementById('mi_'+this.id+'_'+menuItem.id);
			var itemLinks = itemDiv.getElementsByTagName('a');
			var mainLink = itemLinks[0];
			mainLink.setAttribute("target",menuItem.target);
		}
	}
}
Menu.prototype.setHide = function()
{
	this.bodyStatus = "Hidden";
    var showHideBtn = document.getElementById("m_show_hide_btn_"+this.id);
    showHideBtn.onclick = Menu_showClick;	
}
// Hide the body
Menu.prototype.hideBody = function()
{
    if ((this.bodyStatus == "Hidden") || (this.inHideProcess))
    {
        return;
    }
    if (this.inShowProcess)
    {
        clearTimeout(this.showTimerId);
        this.inShowProcess = false;
    }
    else
    {
        toHideDiv = document.getElementById("mi_group_main_"+this.id);
        menuBodyDiv = document.getElementById("m_body_"+this.id);
        bodyCurrentHeight = menuBodyDiv.offsetHeight;
        this.fullBodyHeight = bodyCurrentHeight;
        menuBodyDiv.style.height = bodyCurrentHeight;
        toHideDiv.style.display = "none";
    }

	if (navigator.appName == "Microsoft Internet Explorer")
	{
		this.showHideStep = bodyCurrentHeight / 5;
	}    
	else
	{
		this.showHideStep = bodyCurrentHeight / 10;
	}
    this.inShowHideProcess = true;
    this.inHideProcess = true;  
    this.bodyStatus = "InProcess";
    var showHideBtn = document.getElementById("m_show_hide_btn_"+this.id);
    showHideBtn.onclick = Menu_showClick;
    showHideBtn.className = "show_menu_btn";
    this.hideTimerId = setTimeout("Menu_hideProcess("+this.id+")",this.speed);
}
function Menu_hideProcess(menuId)
{
    theMenu = menuManager.getMenuById(menuId);
    menuBodyDiv = document.getElementById("m_body_"+theMenu.id);
    
	var theUserAgent = navigator.userAgent;
	var isFirefox = false;
	if (theUserAgent.indexOf("Firefox") != -1)
		isFirefox = true;
	//if (isFirefox)
	{
	    menuBodyDiv.style.height = 0;
        theMenu.inShowHideProcess = false;
        theMenu.inHideProcess = false;  
        theMenu.hideTimerId = -1;
        theMenu.bodyStatus = "Hidden";
		return;
	}
    
    bodyCurrentHeight = menuBodyDiv.offsetHeight;
    this.fullBodyHeight = bodyCurrentHeight;
    heightToSet = bodyCurrentHeight - theMenu.showHideStep;
    if (heightToSet < 0)
    {
        heightToSet = 0;
    }
    menuBodyDiv.style.height = heightToSet;
    if (heightToSet > 0)
    {
        theMenu.hideTimerId = setTimeout("Menu_hideProcess("+theMenu.id+")",theMenu.speed);
    }
    else
    {
        theMenu.inShowHideProcess = false;
        theMenu.inHideProcess = false;  
        theMenu.hideTimerId = -1;
        theMenu.bodyStatus = "Hidden";
    }
}
//Show the body
Menu.prototype.showBody = function()
{
    if ((this.bodyStatus == "Shown") || (this.inShowProcess))
    {
        return;
    }
    if (this.inHideProcess)
    {     
        clearTimeout(this.hideTimerId);
        this.inHideProcess = false;
    }
    this.inShowHideProcess = true;
    this.inShowProcess = true;  
    this.bodyStatus = "InProcess";
    showHideBtn = document.getElementById("m_show_hide_btn_"+this.id);
    showHideBtn.onclick = Menu_hideClick;
    showHideBtn.className = "hide_menu_btn";
    this.showTimerId = setTimeout("Menu_showProcess("+this.id+")",this.speed);
    menuManager.shrinkAllElse(this.id);
}

function Menu_showProcess(menuId)
{
    theMenu = menuManager.getMenuById(menuId);
    menuBodyDiv = document.getElementById("m_body_"+theMenu.id);
    
	var theUserAgent = navigator.userAgent;
	var isFirefox = false;
	if (theUserAgent.indexOf("Firefox") != -1)
		isFirefox = true;
	//if (isFirefox)
	{
	    menuBodyDiv.style.height = theMenu.fullBodyHeight;
        toShowDiv = document.getElementById("mi_group_main_"+theMenu.id);
        toShowDiv.style.display = "block";
        theMenu.inShowHideProcess = false;
        theMenu.inShowProcess = false;  
        theMenu.showTimerId = -1;
        theMenu.bodyStatus = "Shown";
        menuBodyDiv.style.height = "";
		return;
	}
    
    bodyCurrentHeight = menuBodyDiv.offsetHeight;
    heightToSet = bodyCurrentHeight + theMenu.showHideStep;
    if (heightToSet > theMenu.fullBodyHeight)
    {
        heightToSet = theMenu.fullBodyHeight;
    }
    menuBodyDiv.style.height = heightToSet;
    if (heightToSet < theMenu.fullBodyHeight)
    {
        theMenu.showTimerId = setTimeout("Menu_showProcess("+theMenu.id+")",theMenu.speed);
    }
    else
    {
        toShowDiv = document.getElementById("mi_group_main_"+theMenu.id);
        toShowDiv.style.display = "block";
        theMenu.inShowHideProcess = false;
        theMenu.inShowProcess = false;  
        theMenu.showTimerId = -1;
        theMenu.bodyStatus = "Shown";
        menuBodyDiv.style.height = "";
    }
}

Menu.prototype.getItemById = function(itemId)
{
    for(i=0;i<this.items.length;i++)
    {
        if (this.items[i].id == itemId)
            return this.items[i];
    }
}
Menu.prototype.removeItem = function(itemId)
{
    //Get the menuItem Object
    var menuItem = this.getItemById(itemId);
    
    //return if it is null
    if (menuItem == null) return;

    //fetch the ids of all its childs
    var childIds = new Array();
    for (i=0;i<menuItem.childs.length;i++)
    {
        childIds.push(menuItem.childs[i].id);
    }
    
    //Remove All Childs
    for (i=0;i<childIds.length;i++)
    {
        this.removeItem(childIds[i]);
    }
    
    //Remove form Parent's child list
    itemParent = menuItem.parent;
    itemParent.removeChild(itemId);
    if (itemParent.childs.length == 0)
    {
        itemParent.hasChild = false;
        if (!itemParent.hasChild)
        {
            itemParent.hasChild = true;
            parentIconDiv = document.getElementById("mi_icon_"+this.id+"_"+itemParent.id);           
            parentIconDiv.className = "mi_expand_icon";
            parentIconDiv.onclick = function() {};
            parentLink = document.getElementById("mi_"+this.id+"_"+itemParent.id).getElementsByTagName('a')[0];
            parentLink.onclick = function() {};
        }
    }
    //Remove from menu items list
    for (i=0;this.items.length;i++)
    {
        if (this.items[i].id == itemId)
            this.items.Remove(i);
    }
}
Menu.prototype.addItem = function(menuItem, parentId)
{
    this.items.push(menuItem);
    if (parentId >= 0)
    {
        itemParent = this.getItemById(parentId);
        itemParent.addChild(menuItem);
        menuItem.parent = itemParent;
        if (!itemParent.hasChild)
        {
            itemParent.hasChild = true;
            parentIconDiv = document.getElementById("mi_icon_"+this.id+"_"+itemParent.id);
            parentIconDiv.className = "mi_shrink_icon";
            parentIconDiv.onclick = MenuItem_Shrink;
            parentLink = document.getElementById("mi_"+this.id+"_"+itemParent.id).getElementsByTagName('a')[0];
            parentLink.onclick = MenuItem_Link_Click;
        }
    }
}
Menu.prototype.refreshChildShowStatus = function()
{
	var mId = this.id;
	for (var i=0; i<this.items.length; i++)
	{
		var menuItem = this.items[i];
		var miId = menuItem.id;
		if (menuItem.hasChild)
		{
			var theIconDiv = document.getElementById('mi_icon_'+mId+'_'+miId);
			if (menuItem.childsShowStatus)
				MenuItem_Expand.call(theIconDiv);
			else
				MenuItem_Shrink.call(theIconDiv);	
		}
	}			
}

//
function Menu_hideClick()
{
    elementId = this.id;
    theMenuId = elementId.slice(16);
    theMenu = menuManager.getMenuById(theMenuId);
    theMenu.hideBody();   
}
function Menu_showClick(menuShareId)
{
    elementId = this.id;
    theMenuId = elementId.slice(16);
    theMenu = menuManager.getMenuById(theMenuId);
    theMenu.showBody();
}
function CreateMenu(id, title)
{
    menu = new Menu();
    menu.id = id;
    menu.title = title;
    menu.initial();
    return menu;
}

///**************************
///*********** MenuItem Class
///**************************
function MenuItem()
{
    this.id = -1;
    this.title = "";
    this.menuId = -1;
    this.parent = null;
    this.childs = new Array();
    this.hasChild = false;
    this.target = "";
    this.childsShowStatus = false;
    this.link = "";
    this.shortKey = "";
}
MenuItem.prototype.addChild = function(child)
{
    this.childs.push(child);
}
MenuItem.prototype.getChildById = function(childId)
{
    for (i=0;i<this.childs.length;i++)
    {
        if (this.childs[i].id == childId)    
            return this.childs[i];
    }
    return null;
}
MenuItem.prototype.removeChild = function(childId)
{
    for (i=0;i<this.childs.length;i++)
    {
        if (this.childs[i].id == childId)    
        {
            this.childs.Remove(i);
            return;
        }
    }
}

function MenuItem_Expand()
{
    miIconDiv = this.id;
    identifier = miIconDiv.slice(8);
    childsContainer = document.getElementById("mi_group_"+identifier);
    childsContainer.style.display = "";
    this.className = "mi_shrink_icon";
    this.onclick = MenuItem_Shrink;
    //change thr menuItem's status
	var spliterIndex = identifier.indexOf("_");
	var menuId = identifier.slice(0,spliterIndex);
	var menuItemId = identifier.slice(spliterIndex+1);
	var menu = menuManager.getMenuById(menuId);
	var menuItem = menu.getItemById(menuItemId);
	menuItem.childsShowStatus = true;
}
function MenuItem_Shrink()
{
    miIconDiv = this.id;
    identifier = miIconDiv.slice(8);
    childsContainer = document.getElementById("mi_group_"+identifier);
    childsContainer.style.display = "none";
    this.className = "mi_expand_icon";
    this.onclick = MenuItem_Expand;
    //change the menuItem's status
	var spliterIndex = identifier.indexOf("_");
	var menuId = identifier.slice(0,spliterIndex);
	var menuItemId = identifier.slice(spliterIndex+1);
	var menu = menuManager.getMenuById(menuId);
	var menuItem = menu.getItemById(menuItemId);
	menuItem.childsShowStatus = false;
}
function MenuItem_Link_Click()
{
	itemDiv = this.parentNode;
    identifier = itemDiv.id.slice(3);
    iconDivId = "mi_icon_"+identifier;
    iconDiv = document.getElementById(iconDivId);
    if ((this.getAttribute('href') == 'javascript:void(0);') ||
		(this.getAttribute('href') == 'javascript:void(0)'))
    {
	    subItemsGroup = document.getElementById("mi_group_"+identifier);
		if (subItemsGroup.style.display =="none")
			MenuItem_Expand.call(iconDiv);
		else
			MenuItem_Shrink.call(iconDiv);
    }
    else
		MenuItem_Expand.call(iconDiv);
}


function CreateMenuItem(miId, miTitle, miMenuId, target, link)
{
    newMI = new MenuItem();
    newMI.id = miId;
    newMI.title = miTitle;
    newMI.menuId = miMenuId;
    newMI.target = target;
    newMI.link = link;
    return newMI;
}

//MenuStatus Class
MenuStatus = function ()
{
	this.menuId = -1;
	this.bodyShowStatus = true;	
}
MenuItemStatus = function()
{
	this.menuId = -1;
	this.menuItemId = -1;
	this.childsShowStatus = true;
}
MenuStatusManager = function()
{
	this.menuStatuses = new Array();
	this.menuItemStatuses = new Array();
}
MenuStatusManager.prototype.reset = function()
{
	this.menuStatuses.length = 0;
	this.menuItemStatuses.length = 0;
}
MenuStatusManager.prototype.addMenu = function(menuId, bodyShowStatus)
{
	menuStatus = new MenuStatus();
	menuStatus.menuId = menuId;
	menuStatus.bodyShowStatus = bodyShowStatus;
	this.menuStatuses.push(menuStatus);
}
MenuStatusManager.prototype.addMenuItem = function(menuId , menuItemId, childsShowStatus)
{
	menuItemStatus = new MenuItemStatus();
	menuItemStatus.menuId = menuId;
	menuItemStatus.menuItemId = menuItemId;
	menuItemStatus.childsShowStatus = childsShowStatus;
	this.menuItemStatuses.push(menuItemStatus);
}

menuManager = new MenuManager();

