clickDept = false;

// Adiciona um item do menu do Cookie
function addMenu(n){
	cookie = readCookie(CookieName);
	if(cookie=='')
		cookie = "|";
	
	if(cookie.indexOf("|"+ n +"|") == -1){
		cookie = cookie + n +"|";
		createCookie(CookieName, cookie, "7");	
	}

}

// Remove um item do menu do Cookie
function delMenu(n){
	cookie = readCookie(CookieName);
	cookie = cookie.replace("|"+ n +"|","|");
	createCookie(CookieName, cookie, "7");		
}

// Exibe / Esconde um menu
function mostraSub(n){
	retorno = false;
	var t = document.getElementById('t'+n);
	var a = document.getElementById('a'+n);

	if(t!=null && a!=null){
		if (t.style.display != 'inline'){
			t.style.display = 'inline';
			a.className = 'volta';
			
			var otherItems = readCookie(CookieName).split('|');
			
			for(var i = 0; i < otherItems.length; i++){
				if(otherItems[i] != n){
					var tt = document.getElementById('t'+ otherItems[i]);
					var aa = document.getElementById('a'+ otherItems[i]);
					
					if(tt!=null && aa!=null){
						tt.style.display = 'none';
						aa.className = 'mostra';;
						delMenu(otherItems[i]);
					}							
					
				}
			}
			
			addMenu(n);
		}else{
			if(clickDept != true){
				t.style.display = 'none';
				a.className = 'mostra';
				delMenu(n);
			}
		}
		
	}else{
		retorno = true;
	}
	return retorno;
}

// Carrega estado inicial do menu
function Carrega(){
	var menu = "";
	menu = readCookie(CookieName);

	if(menu!=''){
		menu = menu.split('|');

		for(i=0;i<menu.length; i++){
			
			if(menu[i]!=''){
				mostraSub(menu[i]);
			}

		}	
	}

}

// Criar Cookies
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	if(value=='|')
		value='';
	var ck = name+"="+value+expires+"; path=/";
	
	document.cookie = ck;
}

// Ler Cookies
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		//if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return '';
	
}

function EscondeSub(ParentID){
	// Esconde o botão caso não exista sub-departamentos
	a = document.getElementById('a' + ParentID);
	if(a != null)
		a.style.display = 'none';
	
	aP = document.getElementById('aP' + ParentID);			
	if(aP != null)
		aP.onclick = null;			
}

