/*
	Contém os métodos necessários para a criação de um menu drop-down
	________________________________________________________________________
*/

var ids = Array();
var contIds = 0;
/*
	campos: array com os link e nomes dos campos. 
		(ex --> campos['nome'] = 'Teste'; campos['link'] = "http://...")
		
	parent: a tag que receberá o campo filho;
	x: eixo x onde começa a exibição do filho;
*/
function createDropDown(campos, parent, x){
	var ul = document.createElement("ul");
	
	for(var i = 0; i < campos.length; i++){
		var li = document.createElement("li");
		var a  = document.createElement("a");
		
		a.setAttribute("href", campos[i]['link']);
		a.appendChild(document.createTextNode(campos[i]['nome']));
		
		li.appendChild(a);
		ul.appendChild(li);
	}
	
	ul.style.position = "absolute";
	
	//gero um id único do campo
	var id = "drop" + Math.floor(Math.random() * 10 + 2);
	ul.setAttribute("id", id);
	
	ul.setAttribute("class", "dropdownmenu");
	ids[contIds] = id;
	
	contIds++;
	//...
	
	ul.style.left = "50%";
	ul.style.top = "172px";
	ul.style.marginLeft = (-378 + x) + "px";
	
	ul.onmouseover = function(){
		clearTimeout(timeout);
	}
	
	ul.onmouseout = function(){
		removeDropDown(ul.getAttribute("id"), delay)
	}
	
	parent.appendChild(ul);
	
	return id;
}

/*
	Faz a eliminação do Drop-Down passado por parâmetro.
	Dá um delay de acordo com o valor passado por parâmetro.
*/
var timeout;
var delay;
function removeDropDown(id, d){
	if(d > 0){
		delay = d;
		var time = "removeDropDown('"+id+"', 0)";
		timeout = setTimeout(time, delay);
	}
	else{
		var drop = document.getElementById(id);
		if(drop){
			var parent = drop.parentNode;	
			parent.removeChild(drop);
		}
		
		return null;
	}
}

/*
	Funções executadas quando dor clicado em qualquer parte do body.
*/
function getDropDownBodyOnClick(){
	//limpo todos os menus dropdown abertos.
	clearDropDowns();
}

/*
	Limpa todos os drop-down que por ventura possam estar abertos.
*/
function clearDropDowns(){
	clearTimeout(timeout);
	for(var i = 0; i < ids.length; i++){
		var drop = document.getElementById(ids[i]);
		
		if(drop){
			var parent = drop.parentNode;
			parent.removeChild(drop);
		}
	}
	
	ids = Array();
}