var OPEN, CLOSE, Index;
var CLEANCOOKIES;

OPEN 	=	0;
CLOSE 	=	1;

Index = 0 ;

var IndexMenu = 0;
var ArrayOfMenus = new Array();
var MenuData     = new Array();
var MenuDef      = new Array();

function _MenuDef(pMenuId, pWIDTH, pBGCOLOR_MAIN_ITEM, pBGCOLOR_SUB_ITEM, pBGCOLOR_SUB_SUB_ITEM, pLINE, pBGCOLOR_SELECTBAR)
{
    this.MenuId = pMenuId;
    this.WIDTH = 167;
    this.BGCOLOR_MAIN_ITEM = pBGCOLOR_MAIN_ITEM;
    this.BGCOLOR_SUB_ITEM = pBGCOLOR_SUB_ITEM;
    this.BGCOLOR_SUB_SUB_ITEM = pBGCOLOR_SUB_SUB_ITEM;
    this.LINE = pLINE;
    this.TOP = 0;
    this.BGCOLOR_SELECTBAR = pBGCOLOR_SELECTBAR;
}

function CreateMenu(pMenuId, pWIDTH, pBGCOLOR_MAIN_ITEM, pBGCOLOR_SUB_ITEM, pBGCOLOR_SUB_SUB_ITEM, pLINE, pBGCOLOR_SELECTBAR)
{  
    IndexMenu = 0;
    MenuDef[pMenuId] = new _MenuDef(pMenuId, pWIDTH, pBGCOLOR_MAIN_ITEM, pBGCOLOR_SUB_ITEM, pBGCOLOR_SUB_SUB_ITEM, pLINE, pBGCOLOR_SELECTBAR);
    MenuData[pMenuId] = new Array();
}

/**********************************************************************/
/* NA MEXE 																														*/
/**********************************************************************/

function _MenuData(pId, pCaption, pUrl, pParentId, pLevel, pCaptionEncode)
{
	this.Id = pId;
	this.Caption = pCaption;
	this.Url = pUrl;
	this.ParentId = pParentId;
	this.Level = pLevel;
	this.HaveSons = false;
	this.CaptionEncode = pCaptionEncode;
}


function AddMenuData(MenuId, pId, pCaption, pUrl, pParentId, pCaptionEncode)
{
	var lLevel;
	var i;	
	var CaptionParent;
	
	if(pParentId == -1) 
	    lLevel = 1;
	else
	{
			lLevel = 1;
			for(i = 0; i < MenuData[MenuId].length; i++)
			{					    
				if(MenuData[MenuId][i].Id == pParentId)
				{
					lLevel = MenuData[MenuId][i].Level + 1;
					MenuData[MenuId][i].HaveSons = true;
					CaptionParent = MenuData[MenuId][i].Caption;
				}
			}		
	}			
	if(lLevel != 1)
	    var lNovoMenu = new _MenuData(pId, pCaption, pUrl, pParentId, lLevel, CaptionParent + ' - ' + pCaptionEncode);
	else
	    var lNovoMenu = new _MenuData(pId, pCaption, pUrl, pParentId, lLevel, pCaptionEncode);

	MenuData[MenuId][IndexMenu] = lNovoMenu;
	IndexMenu ++;
}

function Menu(pPai, pStatus, pLayer, pImagem, pUrl, pLnk, pHaveSons, pTd, pCaptionEncode)
{
	this.Pai = pPai;
	this.Status = pStatus;
	this.Layer = pLayer;
	this.Imagem = pImagem;
	this.Url = pUrl;
	this.Lnk = pLnk;
	this.HaveSons = pHaveSons;
	this.Td = pTd;
	this.CaptionEncode = pCaptionEncode;
}
		 
function CreateArray(MenuId, pLen)
{
	ArrayOfMenus[MenuId] = new Array(pLen);
	MenuDef[MenuId].TOP = pLen;	
}

function AddMenu(MenuId, pPai, pStatus, pLayer, pImagem, pUrl, pLnk, pHaveSons, pTd, pCaptionEncode)
{
	lNovoMenu = new Menu(pPai, pStatus, pLayer, pImagem, pUrl, pLnk, pHaveSons, pTd, pCaptionEncode);
	ArrayOfMenus[MenuId][Index] = lNovoMenu;
	OpenOrCloseMenu(MenuId, pLayer, false, false);
	Index ++;
}


function OpenSelected(MenuId)
{
	var lIndex;
	
	if(CLEANCOOKIES == 1)
		ClearAllCookies(MenuId);
    
	for(lIndex=0; lIndex < ArrayOfMenus[MenuId].length; lIndex++)
	{
		if(ArrayOfMenus[MenuId][lIndex] != null)
		{
			Selected = readCookie("Selected" + MenuId);
					
			if(readCookie(ArrayOfMenus[MenuId][lIndex].Layer) == 1)
				OpenOrCloseMenu(MenuId, ArrayOfMenus[MenuId][lIndex].Layer, false, false)
			
//			if(Selected == ArrayOfMenus[MenuId][lIndex].Layer)
//				if(document.getElementById(ArrayOfMenus[MenuId][lIndex].Lnk) != null)
//				{
//				    alert(ArrayOfMenus[MenuId][lIndex].Lnk);
//				    if(ArrayOfMenus[MenuId][lIndex].Level==1)
//					    document.getElementById(ArrayOfMenus[MenuId][lIndex].Lnk).className = 'MouseOverColor1Level';
//					else
//					    document.getElementById(ArrayOfMenus[MenuId][lIndex].Lnk).className = 'MouseOverColor2Level';
//				}
		}
	}
}

function Show(Value)
{	
	Dummy = document.getElementById&&!document.all?document.getElementById(Value) : document.all[Value];
	Dummy.style.visibility = 'Visible'
	Dummy.style.display = ''
}

function Hide(Value)
{	
    try{
	    Dummy = document.getElementById&&!document.all?document.getElementById(Value) : document.all[Value];
	    Dummy.style.visibility = 'Hidden'
	    Dummy.style.display = 'None'
	}catch(E){};
}

function OpenOrCloseMenu(MenuId, Name, Cookies, GoToUrl, Caption)
{
	var lIndex, WriteCookies, GoTo;

	if(Cookies == undefined)
		WriteCookies = true;
	else
		WriteCookies = Cookies;
    			
	if(GoToUrl == undefined)
		GoTo = '';
	else
		GoTo = GoToUrl;

	for(lIndex=0; lIndex < ArrayOfMenus[MenuId].length; lIndex++)
	{
		if(ArrayOfMenus[MenuId][lIndex] != null)
		{
			if(ArrayOfMenus[MenuId][lIndex].Layer == Name)
			{
			    
				if(ArrayOfMenus[MenuId][lIndex].HaveSons)
				{
					switch(ArrayOfMenus[MenuId][lIndex].Status)
					{
						case CLOSE:
							Show(ArrayOfMenus[MenuId][lIndex].Layer);
							ArrayOfMenus[MenuId][lIndex].Status = OPEN;
						
													
							if(WriteCookies) writeCookie(Name, 1, 90000);

							if(ArrayOfMenus[MenuId][lIndex].Url != '' && GoTo) 
							    GoSide(MenuId, ArrayOfMenus[MenuId][lIndex].Url, Name, Caption)
								
							break;
						case OPEN:
							Hide(ArrayOfMenus[MenuId][lIndex].Layer);
							ArrayOfMenus[MenuId][lIndex].Status = CLOSE;
							
							if(WriteCookies)
								writeCookie(Name, -1, 90000);
						     
							if(WriteCookies)
								if(ArrayOfMenus[MenuId][lIndex].Url != '' && GoTo) 
								    GoSide(MenuId, ArrayOfMenus[MenuId][lIndex].Url, Name, Caption)
							
							break;
					}
				}
				else if(WriteCookies) GoSide(MenuId, ArrayOfMenus[MenuId][lIndex].Url, Name, Caption);
			}
		
		}
	}
}

function GoSide(MenuId, GoTo, Name, Zone)
{    
    if(GoTo != '')
    {    
	    deleteCookie("Selected" + MenuId);
	    writeCookie("Selected" + MenuId, Name, 90000);
    		
        if(GoTo.indexOf('ShowFile') != -1 || GoTo.indexOf('ShowPluginsFile') != -1)
        {
	        eval(GoTo);
        }
        else if(GoTo.indexOf('http://') == -1)
	    {
		    deleteCookie('Zone');
		    writeCookie('Zone', escape(Zone), 90);

		    window.location = GoTo;
	    }
	    else window.open(GoTo)
    }
}

function ClearAllCookies(MenuId)
{
	deleteCookie("Selected" + MenuId);
	var lIndex;

	for(lIndex=0; lIndex < MenuDef[MenuId].TOP; lIndex++)
		if(ArrayOfMenus[MenuId][lIndex] != null)
			deleteCookie(ArrayOfMenus[MenuId][lIndex].Layer);
}

function MakeDiv(MenuId, pId)
{
	document.write('<Span style="position:relative; left:0px; top:0px; bottom:0px; width:' + (MenuDef[MenuId].WIDTH - 2) + 'px;" id="' + pId + '">');
}

function CloseDiv()
{
	document.write('</Span>');
}	


function readCookie(name) {
	var namearg = name + "=";
	var nlen = namearg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + nlen;
		if (document.cookie.substring(i, j) == namearg) {
			var endpos = document.cookie.indexOf (";", j);
			if (endpos == -1) endpos = document.cookie.length;
			var f = unescape(document.cookie.substring(j, endpos));
			return unescape(document.cookie.substring(j, endpos));
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return 0;
}

function deleteCookie(name) {
   if (readCookie(name)) {
     document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

function writeCookie(name, value, expiry) {
	//alert('name: ' + name + ' value: ' + value);
	var expdate = new Date();	
	if(expiry) { //horas
		expdate.setTime(expdate.getTime() + 1000 * 60 * 60 * expiry);
		document.cookie = name + "=" + value + "; expires=" + expdate.toGMTString();
	}
	else {
		document.cookie = name + "=" + value;
	}
}

