// JavaScript Document

var menuState;

function triggerMenu() {
	var sAcoes = document.getElementById("acoes");
	var sCanal = document.getElementById("canal");
	var sFeiras = document.getElementById("feiras");
	var dAcoes = document.getElementById("dropAcoes");
	var dCanal = document.getElementById("dropCanal");
	var dFeiras = document.getElementById("dropFeiras");
	
	menuState = getCookie('menuState');
	//alert(menuState);
	
	var acoesHeight;
	var feirasHeight;
	
	switch (menuState) {
		case "acoes":
			acoesHeight = 44;
			feirasHeight = 0;
			break;
		case "canal":
			acoesHeight = 110;
			feirasHeight = 0;
			dCanal.style.display = "block";
			break;
		case "feiras":
			acoesHeight = 0;
			feirasHeight = 44;
			break;
		default:
			acoesHeight = 0;
			feirasHeight = 0;
	}
	
	dAcoes.style.height = acoesHeight + "px";
	dFeiras.style.height = feirasHeight + "px";
	
	sAcoes.onclick = function() {
		if (parseInt(dFeiras.style.height) >0) {
			heightChange(dFeiras,0);
		}
		if (parseInt(dAcoes.style.height) <= 0) {
			heightChange(dAcoes,44);
			menuState = "acoes";
		} else {
			heightChange(dAcoes,0);
			menuState = "";
		}
	};
	
	sCanal.onclick = function() {
		if (parseInt(dFeiras.style.height) > 0) {
			heightChange(dFeiras,0);
		}
		if (dCanal.style.display == "none" || dCanal.style.display == "") {
			dCanal.style.display = "block";
		}
		if (parseInt(dAcoes.style.height) <= 44) {
			heightChange(dAcoes,110);
			menuState = "canal";
		} else {
			heightChange(dAcoes,44);
			menuState = "acoes";
		}
	};
	
	sFeiras.onclick = function() {
		if (parseInt(dAcoes.style.height) > 0) {
			heightChange(dAcoes,0);
		}
		if (parseInt(dFeiras.style.height) <= 0) {
			heightChange(dFeiras,44);
			menuState = "feiras";
		} else {
			heightChange(dFeiras,0);
			menuState = "";
		}
	};
}

window.onunload = function() {
	setCookie("menuState", menuState, 0);
};

//funções
function heightChange(elem, end) { 
    if (!elem.currentHeight) {
		elem.currentHeight = parseInt(elem.style.height);
	}
	//if no memory is set, set it first; 
    doHeightChangeMem(elem,elem.currentHeight,end,5,20,0.3); 
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
//Generic Animation Step Value Generator By www.hesido.com 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
}

function doHeightChangeMem(elem,startHeight,endHeight,steps,intervals,powr) { 
//Width changer with Memory by www.hesido.com
    if (elem.heightChangeMemInt) {
		window.clearInterval(elem.heightChangeMemInt);
	}
    var actStep = 0;
    elem.heightChangeMemInt = window.setInterval(
		function() { 
	  		elem.currentHeight = easeInOut(startHeight,endHeight,steps,actStep,powr);
	  		elem.style.height = elem.currentHeight + "px"; 
	  		actStep++;
	  		if (actStep > steps) window.clearInterval(elem.heightChangeMemInt);
		} 
		,intervals
	)
}

function setCookie(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 = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(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);
	}
	return null;
}

function delCookie(name) {
	setCookie(name,"",-1);
}