/**********************************************************
				  --- ANIMSLIDE Menu --- 
	Author : Felipe Fürst
	eMail  : fefurst@gmail.com
	v.	   : 2.1.1
	
	AO UTILIZAR ESTE SCRIPT NÃO RESMOVA ESTAS LINHAS !
	              O AUTOR AGRADECE.
				  
	Dúvidas? Bug? Sugestões? Mande um eMail .
	
	Desc.: Menu slide com possibilidade de "n" filhos
		+ Menu principal
		|-+ teste
		| |- Links
		| |- Scripts
		| |-+ Teste
		| | |-+ lalala
		| | | |-+ ...
		...
		Ex.:
		<script language="javascript1.4" type="text/javascript" src="menu.js"></script>
		<script language="JavaScript1.4">
			content('<a href="index.html">Página prioncipal</a>', "menu");
			startMenu("Menu principal", "menu");
				startMenu("teste", "submenu");
					content("Links", "opt");
					content("Scripts", "opt");
					startMenu("Teste", "submenu");
						startMenu("lalala", "submenu");
							startMenu("...", "submenu");
							endMenu();
						endMenu();
					endMenu();
				endMenu();
			endMenu();
		</script>
		
		<script language="JavaScript1.4">
			writeMenu();
		</script>
		
		Usage:
			startMenu("<menu name>", "<css class>");
			endMenu();
			-- Inicia e termina uma seção de menu e seus sub-ítens ou sub-menus.
			
			content("<item name>", "<css class>");
			-- Define um ítem do menu.
			
			writeMenu();
			-- Desenha o menu onde é chamado.
***********************************************************/


// --- inicio configuração ---
var intMenuWidth = 183; //lagura do menu ...
var booAnimMenu = false;

// --- fim    configuração ---

var arrMenuAnt = new Array();
var intMenuCountItens = 0;

var tmrTimer1 = null;
var tmrTimer2 = null;

var booMenuOpening = false;
var booMenuClosing = false;

var strMenuCode = "";

var intMenuDoNivel = 1;
var intMenuDoId = 1;

var isMozilla=document.getElementById && !document.all;
var isIe=document.all;

function startMenu(caption, css){
	
	tam = intMenuWidth;
	if(intMenuDoNivel>1)tam2 = 5+Math.pow(intMenuDoNivel, 2.2);
	else tam2 = 0;

	strMenuCode += "<div style=\"text-indent: "+(tam2)+"px; cursor:pointer; Width:"+tam+"px;\" onClick=\"faz(document.getElementById('menu"+intMenuDoId+"'), "+intMenuDoNivel+")\">";
	
	if(css == null) strMenuCode +="<div>"+caption+"</div>";
	else strMenuCode +="<div class=\""+css+"\">"+caption+"</div>";
	
	strMenuCode += "</div>\n";
	
	strMenuCode += "<div id=\"menu"+intMenuDoId+"\" style=\"display:none; overflow:hidden; Width:"+(intMenuWidth+2)+"px;\">\n";
	intMenuDoNivel++;
	intMenuDoId++;
}
function endMenu(){
	strMenuCode += "</div>\n"
	intMenuDoNivel--;
}
function content(conteudo, css){
	if(intMenuDoNivel>1)tam = 5+Math.pow(intMenuDoNivel, 2.2);
	else tam = 0;
	strMenuCode += "<div style=\"Width:"+intMenuWidth+"px; text-indent: "+(tam)+"px;\">";
	
	if(css == null) strMenuCode +="<div>"+conteudo+"</div>";
	else strMenuCode +="<div class=\""+css+"\">"+conteudo+"</div>";
	
	strMenuCode +="</div>"+"\n";
}

function space(){
	strMenuCode +="<br />\n";
}

function writeMenu(){
	document.write('<div id="medediv1" style="visibility: hidden; position: absolute"></div>');
	document.write('<div id="medediv2" style="visibility: hidden; position: absolute"></div>');
	
	strMenuCode2 = "";
	strMenuCode2 += "<div style=\"Width:"+(intMenuWidth-1)+"px;\">";
	strMenuCode2 += strMenuCode;
	strMenuCode2 +="</div>"+"\n";
	document.write(strMenuCode);
	strMenuCode = "";
}

	


function animclose(id, from, to){
	if (isIe) // Internet Explorer
		var sec = 0.32;
	else if(isMozilla) // Nestcape & FF
		var sec = 0.22;
	
	if(from > to){
		//clearTimeout (tmrTimer1);
		document.getElementById(id).style.height = from+'px';
		document.getElementById(id).style.overflow = 'hidden';
		document.getElementById(id).style.width = intMenuWidth;
		tmrTimer1 = setTimeout ("animclose('"+id+"',"+(from-(1+parseInt(from*sec)))+", "+to+")", 1);
		booMenuClosing = true;
	} else {
		document.getElementById(id).style.display = 'none';
		clearTimeout (tmrTimer1);
		document.getElementById(id).style.height = to+'px';
		booMenuClosing = false;
	}
}

function animopen(id, from, to){
	if (isIe) // Internet Explorer
		var sec = 0.32;
	else if(isMozilla) // Nestcape & FF
		var sec = 0.22;
	
	//alert("FROMo: "+fromO+"\nTOo: "+toO);
	
	if(from < to){
		//clearTimeout (tmrTimer2);
		// ie eh uma bosta mesmo ... não aceita height = 0 ...
		document.getElementById(id).style.height = from+1+'px';
		document.getElementById(id).style.display = 'block';
		document.getElementById(id).style.width = intMenuWidth;
		tmrTimer2 = setTimeout ("animopen('"+id+"',"+(from+(1+parseInt((from)*sec)))+", "+to+")", 1);
		
		
		booMenuOpening = true;
	} else {
		clearTimeout (tmrTimer2);
		document.getElementById(id).style.height = to+'px';
		if (isIe) {// Internet Explorer
			document.getElementById(id).removeAttribute('style');
		}
		else if(isMozilla) {// Nestcape & FF
			// flick bug DONE !!!
			document.getElementById(id).style.display = '';
			//document.getElementById(id).style.width = '';
			document.getElementById(id).style.height = '';
		}
		booMenuOpening = false;
	}
}

function fecha(obj, nivel){
	document.getElementById("medediv1").innerHTML=document.getElementById(obj.id).innerHTML;
	altura=document.getElementById("medediv1").offsetHeight;
	document.getElementById("medediv1").innerHTML="";
	
	if(booAnimMenu)
		animclose(obj.id, altura, 0);
	else
		obj.style.display = "none";
		 
	//if(arrMenuAnt[nivel] != null && arrMenuAnt[nivel] != obj) arrMenuAnt[nivel].style.display = "block";
	
	if(arrMenuAnt[nivel] != null && arrMenuAnt[nivel].id != obj.id) {
		document.getElementById("medediv2").innerHTML=document.getElementById(arrMenuAnt[nivel].id).innerHTML;
		altura2=document.getElementById("medediv2").offsetHeight;
		document.getElementById("medediv2").innerHTML="";
		if(booAnimMenu)
			animopen(arrMenuAnt[nivel].id, 0, altura2);
		else
			arrMenuAnt[nivel].style.display = "";
	}
	SetCookie ("cookiemenu"+nivel, null);
	deleteCookie("cookiemenu"+nivel);
}
	
function abre(obj, nivel){
	document.getElementById("medediv1").innerHTML=document.getElementById(obj.id).innerHTML;
	altura=document.getElementById("medediv1").offsetHeight;
	
	if(booAnimMenu)
		animopen(obj.id, 0, altura);
	else
		obj.style.display = '';
	
	
	//obj.style.display = "block";
	 
	//if(arrMenuAnt[nivel] != null && arrMenuAnt[nivel] != obj) arrMenuAnt[nivel].style.display = "none";
	
	if(arrMenuAnt[nivel] != null && arrMenuAnt[nivel].id != obj.id) {
		document.getElementById("medediv2").innerHTML=document.getElementById(arrMenuAnt[nivel].id).innerHTML;
		altura2=document.getElementById("medediv2").offsetHeight;
		document.getElementById("medediv2").innerHTML="";
		if(booAnimMenu)
			animclose(arrMenuAnt[nivel].id, altura2, 0);
		else
			arrMenuAnt[nivel].style.display = 'none';
	}
	SetCookie ("cookiemenu"+nivel, obj.id);
}


function faz(obj, nivel){
	if(!booMenuOpening && !booMenuClosing){
		if(obj.style.display == "none") abre(obj, nivel);
		else fecha(obj, nivel);
		
		arrMenuAnt[nivel] = obj;
		
		if(nivel > GetCookie("hlevel")) SetCookie("hlevel",nivel);
	}
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			break; 
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
			break; 
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "; path=/" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function deleteCookie(name, path, domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

onload=function (){
			qtd = GetCookie ("hlevel");
			//alert(qtd);
			if(qtd != null && qtd > 0)
				for(i=1; i<= qtd; i++){
					//alert("cookiemenu"+i+": "+GetCookie("cookiemenu"+i))
					if(GetCookie("cookiemenu"+i) != null && GetCookie("cookiemenu"+i) != "null"){
						//faz(document.getElementById(GetCookie("cookiemenu"+i)), i);
						obj = document.getElementById(GetCookie("cookiemenu"+i));
						if(obj) obj.style.display = "";
						
						if(arrMenuAnt[i] != null && arrMenuAnt[i] != obj) arrMenuAnt[i].style.display = "none";
						arrMenuAnt[i] = obj;
						if(i > GetCookie("hlevel")) SetCookie("hlevel",i);
					} else if(document.getElementById("menu1")) {
						document.getElementById("menu1").style.display = "none"; //ffx slow slide BUG ?
					}
				}
			else if(document.getElementById("menu1")) {
				document.getElementById("menu1").style.display = "none"; //ffx slow slide BUG ??
			}
	   }