//OnClick hide
var menuIsOpening = false;
var lastBodyOnClick = "";
//OnClick hide
function Menu(menuWidth,menuHeight)
{
var docBody = document.body;
var menuDiv;
    //docBody.insertAdjacentHTML("BeforeEnd","<div></div>");
    docBody.insertAdjacentHTML("AfterBegin","<div style='position:absolute;display:none'></div>");
    menuDiv = docBody.children(0);
    this.mDiv       = menuDiv;
    this.AddItem    = MenuAddItem;
    this.AddMenu	= MenuAddMenu;
    this.DimmItem   = MenuDimmItem;
    this.EnableItem = MenuEnableItem;
    this.Show       = MenuShow;
    this.Hide       = MenuHide;
    this.AddBreak   = MenuAddBreak;
    this.Clear      = MenuClear;
    this.Resize     = MenuResize;
    this.menuWidth  = menuWidth;
    this.menuHeight = menuHeight;
	//this.ChangeBackgroundColor = ChangeMenuBackgroundColor;
	this.lastHighlighted = null;
	
	//Style
	/*
	this.Color = "black";
	this.BgColor = "menu";
	this.HighlightedColor = "white";
	this.HighlightedBgColor = "#0a246a";
	this.BorderStyle = "outset 3px";	
	this.FontFamily = "Tahoma";	
	this.FontSize = "13px";
	this.FontWeight = "";
	this.DimmedColor = "gray";
	this.ItemPadding = "14px";
    */
	this.Color = "#000080";
	this.BgColor = "white";
	this.HighlightedColor = "#000080";
	this.HighlightedBgColor = "#e4ecf5";
	this.BorderStyle = "outset 1px";	
	this.FontFamily = "Arial, Helvetica, sans-serif";	
	this.FontSize = "9pt";
	this.FontWeight = "";
	this.DimmedColor = "gray";
	this.ItemPadding = "5px";
	
	
    with (this.mDiv.style)
    {
          display         = "none";
          position        = "absolute";
          paddingTop      = "5px";
          cursor          = "default";
		  //direction       = "rtl";
		  zIndex		  = 101;
    }
    
    menuDiv.onmouseover = MenuHandleOver;
    menuDiv.onmouseout  = MenuHandleOver;
}
//OnClick hide
var fl_click=false

//OnClick hide
/*
function ChangeMenuBackgroundColor(prmColor)
{

	var menuDiv = this.mDiv;
	var itemDiv;
	var intCountDivs;
	menuDiv.style.backgroundColor=prmColor;
	
	for(intCountDivs=0;intCountDivs<menuDiv.children.length;intCountDivs++)
	{
		itemDiv = menuDiv.children(intCountDivs);
		itemDiv.style.backgroundColor=prmColor;
		
	}
	
}
*/
function MenuAddItem(caption,handler)
{
var menuDiv = this.mDiv;
var itemDiv;

    menuDiv.insertAdjacentHTML("BeforeEnd","<div>" + caption + "</div>");
    itemDiv = menuDiv.children(menuDiv.children.length - 1);

    with (itemDiv.style)
    {
          fontFamily      = this.FontFamily;
          fontSize        = this.FontSize;
          fontWeight      = this.FontWeight;
          width           = "100%";
          paddingRight    = this.ItemPadding;
          paddingLeft	  = this.ItemPadding;
          backgroundColor = this.BgColor;
          color           = this.Color;
          lineHeight      = "18px";
          overflow        = "hidden";
          cursor		  = "hand";
    }
	itemDiv.menuobj = this;
	itemDiv.menu = null;
	itemDiv.onclick     = handler;
	if (caption.indexOf("X.gif")>-1)
		itemDiv.onclick     = HideFromMenu;
	itemDiv.swapClick   = null;
    itemDiv.dimmed      = false;
    itemDiv.topMenu	    = false;
    itemDiv.menuItem    = true;
    itemDiv.highlighted = false;
    
}


function MenuAddMenu(caption,menuWidth,menuHeight)
{
var menuDiv = this.mDiv;
var itemDiv;

    menuDiv.insertAdjacentHTML("BeforeEnd","<div nowrap>" + caption + "&nbsp;&nbsp;&nbsp;<img src='../img/icons/arrow.gif' align='absmiddle' style='position: absolute; left: 5px; top: 4px;'></div>");
    itemDiv = menuDiv.children(menuDiv.children.length - 1);

    with (itemDiv.style)
    {
          fontFamily      = this.FontFamily;
          fontSize        = this.FontSize;
          fontWeight      = this.FontWeight;
          width           = "100%";
          paddingRight    = this.ItemPadding;
          backgroundColor = this.BgColor;
          color           = this.Color;
          lineHeight      = "14px";
          overflow        = "hidden";
    }

	itemDiv.menuobj = this;
	itemDiv.menu = new Menu(menuWidth,menuHeight);
    itemDiv.swapClick   = null;
    itemDiv.dimmed      = false;
    itemDiv.topMenu	    = true;
    itemDiv.menuItem    = true;
    itemDiv.highlighted = false;

    return itemDiv.menu;
}


function MenuAddBreak()
{
    this.mDiv.insertAdjacentHTML("BeforeEnd","<hr align=center style='width: 95%; line-height: 14px; overflow: hidden;'>");
}


function MenuShow(x,y)
{
    //OnClick hide
	menuIsOpening = true;
	//OnClick hide
	var menuDiv = this.mDiv;
	
	x = x + document.body.scrollLeft - this.menuWidth + 16;	
	y = y + document.body.scrollTop;
	
    /*if (x < 0)
		x = 0;

    if (y + this.menuHeight > document.body.clientHeight + document.body.scrollTop)
		y -= ((y + this.menuHeight + 30) - (document.body.clientHeight + document.body.scrollTop));*/
	this.Hide();

    with (menuDiv.style)
    {
          width           = this.menuWidth + "px";
          height          = this.menuHeight + "px";
          backgroundColor = this.BgColor;
          border          = this.BorderStyle;
          display = "";
    }
    
	if (x < 10)
		x = 10;
	
	if (y + menuDiv.clientHeight > document.body.scrollTop + document.body.clientHeight)
		y = document.body.scrollTop + document.body.clientHeight - menuDiv.clientHeight - 20;

    with (menuDiv.style)
    {
          posLeft = x;
          posTop  = y;
    }
    
}


function MenuHide()
{	
	
		
	var counter;
	var childs = this.mDiv.children;
	for (counter = 0; counter < childs.length; counter++)
	{
		if (childs[counter].menu != null)
			childs[counter].menu.Hide();

		if (childs[counter].menuItem)
		{
			childs[counter].style.backgroundColor = this.BgColor;

			if (childs[counter].dimmed)
				childs[counter].style.color = this.DimmedColor;
			else
				childs[counter].style.color = this.Color;

			childs[counter].highlighted = false;
		}
	}

    this.mDiv.style.display = "none";
    
}


function MenuClear()
{
    this.mDiv.innerHTML = "";
}


function MenuHandleOver()
{
var itemDiv = window.event.srcElement;
var menuObj = itemDiv.parentElement;

var counter;

    if (!itemDiv.menuItem)
        return;
    else
    {
		menuObj = itemDiv.menuobj;		
    }

	if (menuObj.lastHighlighted != null)
	{
		if (menuObj.lastHighlighted.menu != null)
			menuObj.lastHighlighted.menu.Hide();

		menuObj.lastHighlighted.style.backgroundColor = menuObj.BgColor;
        
		if (menuObj.lastHighlighted.dimmed)
			menuObj.lastHighlighted.style.color = menuObj.DimmedColor;
		else
			menuObj.lastHighlighted.style.color = menuObj.Color;
				
		menuObj.lastHighlighted.highlighted = false;
	}

    menuObj.lastHighlighted = itemDiv;
    itemDiv.style.backgroundColor = menuObj.HighlightedBgColor;

    if (itemDiv.dimmed)
        itemDiv.style.color = menuObj.DimmedColor;
    else
    {
        itemDiv.style.color = menuObj.HighlightedColor;

        if (itemDiv.topMenu)
        {
			for (counter = 0; counter < itemDiv.parentElement.children.length; counter++)
				if (itemDiv.parentElement.children[counter] == itemDiv)
					break;

			itemDiv.menu.Show(itemDiv.parentElement.style.pixelLeft - 16,
							  itemDiv.parentElement.style.pixelTop + (counter * 14) + 9 - document.body.scrollTop);
        }
    }

    itemDiv.highlighted  = true;
}


function MenuResize(menuWidth,menuHeight)
{
    this.menuWidth  = menuWidth;
    this.menuHeight = menuHeight;

    with (this.mDiv.style)
    {
          width  = menuWidth + "px";
          height = menuHeight + "px";
    }
}


function MenuDimmItem(optNum)
{
var curItem = this.mDiv.children(optNum);


    if (!curItem.dimmed && curItem.tagName == "DIV")
    {
        curItem.swapClick   = curItem.onclick;
        curItem.onclick     = "";
        curItem.style.color = this.DimmedColor;
        curItem.dimmed      = true;
    }    
}


function MenuEnableItem(optNum)
{
var curItem = this.mDiv.children(optNum);


    if (curItem.dimmed && curItem.tagName == "DIV")
    {
        curItem.onclick     = curItem.swapClick;
        curItem.style.color = this.Color;
        curItem.dimmed      = false;
    }   
}
function HideFromMenu()
{
	fl_click=true;
	MenuHideObj(this.parentElement);
	
}

function MenuHideObj(obj)
{	
			
	var counter;
	var childs = obj.children;
	for (counter = 0; counter < childs.length; counter++)
	{
		if (childs[counter].menu != null)
			childs[counter].menu.Hide();

		if (childs[counter].menuItem)
		{
			childs[counter].style.backgroundColor = obj.BgColor;

			if (childs[counter].dimmed)
				childs[counter].style.color = obj.DimmedColor;
			else
				childs[counter].style.color = obj.Color;

			childs[counter].highlighted = false;
		}
	}

    obj.style.display = "none";
    
}
