/*Variaveis do Tipo Array*/
var arr_itens = new Array();
var arr_menu_ref = new Array();
var arr_menu_id = new Array();
var arr_top = new Array();
var arr_cont = new Array();
var arr_h_brd = new Array();

/*Outras Variaveis*/
var str_menu_ref;
var mn_width;
var mn_height;
var mn_li_height;

/*Menu*/
var mn_selected = new Array();
var cont = 0;
var set_cont;
var menu_id;
var mn_top;
var int_cont;
var mn_h_brd;

function get(docName){return document.getElementById(docName);}

function montaMenu(div_ref, int_w, int_h, int_li_h, int_h_brd, int_font_size, str_back_color_roll, str_back_color, str_lines_color, str_itens_color, str_itens_over_color, str_img_path, int_space, str_color_seta_rolls, st_cursor, str_img_path_click, itens, onClickCallback,indexSet){
	mn_width = int_w; mn_height = int_h; mn_li_height = int_li_h; mn_h_brd=int_h_brd;
	/*Define tamanhos e posições iniciais*/
	get(div_ref).style.width=mn_width+"px";get(div_ref).style.height=(mn_height+1)+"px";//Menu
	/*Monta a borda do topo*/
	str_elem = "brd_top_"+div_ref;
	brdTop = document.createElement("span");
	brdTop.setAttribute("id",str_elem);
	brdTop.style.width = mn_width+"px";
	brdTop.style.borderTop = "1px solid "+str_lines_color;
	brdTop.className = "brd_top";
	get(div_ref).appendChild(brdTop);
	/*Cria variáveis de acordo com o nome dado ao id do menu*/
	arr_menu_ref.push(div_ref);
	str_menu_ref = "s_"+arr_menu_ref.length;
	/*Monta o Container que trará os valores do Menu*/
	arr_menu_id.push("ul_"+str_menu_ref);
	menuContainer = document.createElement("ul");
	menuContainer.setAttribute("id","ul_"+str_menu_ref);
	/*menuContainer.style.backgroundColor = str_back_color;*/
	menuContainer.style.width = (mn_width+5)+"px";
	get(div_ref).appendChild(menuContainer);

	/*Traz os valores*/
	var ini; var fin; var loc; var temp;
	temp = 0;
	ini = 0;
	fin = itens.length;
	str = itens.substring(ini,fin);
	loc = str.indexOf(";");

	for(i=0; i<=fin; i++){
		if(i==loc){
			arr_itens.push(itens.substring(ini,loc));
			temp += 1;
			ini = loc+1;
			str = itens.substring(ini,fin)
			if( loc <= fin && str.indexOf(";") != -1){
				loc = (str.indexOf(";")+ini); }
			else{
				loc = fin; }
		}
	}
	/*Define as LIs e seus valores*/
	var contInt = 0;
	for(i=0; i<temp; i++){
		liSet = document.createElement("li");
		liSet.setAttribute("id", "li_"+i+"_"+str_menu_ref);
		var defin = Math.round( arr_itens[i].length / Math.round(((mn_width)+5)/6));
		if( Math.round(((mn_width)+5)/6) < arr_itens[i].length){
			liSet.style.lineHeight = ((int_li_h*(defin+1))-5)+"px";
			liSet.style.height = ((mn_li_height*(defin+1))-5)+"px";
			if(defin>contInt){
				contInt = defin;
				}
			//contInt = contInt+1;
		}else{
			liSet.style.lineHeight = int_li_h-5+"px";
			liSet.style.height = mn_li_height-5+"px";
		}
		liSet.style.borderBottom = "none";
		liSet.className = "li_off";

		menuContainer.appendChild(liSet);
		menuItens = document.createElement("li");
		menuItens.setAttribute("title", arr_itens[i]);
		menuItens.setAttribute("id", i+"_"+str_menu_ref);
		if(  Math.round(((mn_width)+5)/6) < arr_itens[i].length){
			menuItens.style.height = (mn_li_height*(defin+1))-5+"px";
			menuItens.style.lineHeight = int_li_h-2+"px";
		}else{
			menuItens.style.height = mn_li_height-5+"px";
			menuItens.style.lineHeight = int_li_h-5+"px";
		}
		menuItens.style.color = str_itens_color;
		menuItens.style.borderLeft = "1px solid "+str_lines_color;
		menuItens.style.borderRight = "1px solid "+str_lines_color;
		menuItens.style.borderBottom = "1px solid "+str_lines_color;
		menuItens.style.background = "url('"+str_img_path+"') no-repeat "+int_space+"px center "+str_back_color;
		menuItens.style.padding = "2px 4px 2px "+(10+(int_space*3))+"px";/***/
		menuItens.style.fontSize = int_font_size+"px";
		menuItens.className = "itens";
		menuItens.onclick = function () {
			setCursor(this.id,str_itens_over_color,str_back_color,str_img_path,st_cursor,int_space,str_img_path_click);
			if (onClickCallback != null)
				onClickCallback(this.id.substring(0, this.id.indexOf('_s')));
			return false;
		};
		menuItens.onmouseover = function (){(mn_selected[this.id.substring(this.id.length-1)-1]!=this.id)?get(this.id).style.backgroundColor = str_itens_over_color:"";	}
		menuItens.onmouseout = function (){(mn_selected[this.id.substring(this.id.length-1)-1]!=this.id)? get(this.id).style.backgroundColor = str_back_color:""; }
		menuItens.innerHTML = arr_itens[i];
		menuContainer.appendChild(menuItens);
		cont_intens = (((i+1)*mn_li_height) - (mn_height)+mn_h_brd);
	}
	if(indexSet!=null){
		setCursor(indexSet+"_"+str_menu_ref,str_itens_over_color,str_back_color,str_img_path,st_cursor,int_space,str_img_path_click);
	}
	/*Habilita ou Desabilita as rolagens de cima e de baixo*/
	if( (arr_itens.length*mn_li_height) + (contInt*mn_li_height) > mn_height){
		menuContainer.style.top = (mn_h_brd+1)+"px";
		/*Monta o botão de rolagem para CIMA(UP)*/

		str_elem = "menu_top_"+div_ref;
		rollUp = document.createElement("span");
		rollUp.setAttribute("id",str_elem);
		rollUp.setAttribute("title","Mantenha o cursor aqui para subir o conteúdo");
		rollUp.style.width = (mn_width-2)+"px";
		rollUp.style.height = mn_h_brd+"px";
		rollUp.style.lineHeight = mn_h_brd+"px";
		rollUp.style.borderLeft = "1px solid "+str_lines_color;
		rollUp.style.borderRight = "1px solid "+str_lines_color;
		rollUp.style.borderBottom = "1px solid "+str_lines_color;
		(str_color_seta_rolls!=null)? rollUp.style.background = "url('../../img/barr_up_trans_"+str_color_seta_rolls+".gif') no-repeat center center "+str_back_color_roll : rollUp.style.background = "url('../../img/barr_up_trans.gif') no-repeat center center "+str_back_color_roll;
		rollUp.onmouseover = function(){move(this.id.substring(this.id.length-1)-1,1);}
		rollUp.onmouseout = function(){clearTimeout(set_cont);}
		get(div_ref).appendChild(rollUp);
		get(str_elem).className="menu_up";

		/*Monta o botão de rolagem para BAIXO(DOWN)*/
		str_elem = "menu_down_"+div_ref; rollDown = document.createElement("span"); rollDown.setAttribute("id",str_elem);
		rollDown.setAttribute("title","Mantenha o cursor aqui para descer o conteúdo");
		rollDown.style.top = (mn_height-mn_h_brd)+"px";
		rollDown.style.width = (mn_width-2)+"px";
		rollDown.style.height = mn_h_brd+"px";
		rollDown.style.lineHeight = mn_h_brd+"px";
		rollDown.style.borderLeft = "1px solid "+str_lines_color;
		rollDown.style.borderRight = "1px solid "+str_lines_color;
		rollDown.style.borderTop = "1px solid "+str_lines_color;
		(str_color_seta_rolls!=null)? rollDown.style.background = "url('../../img/barr_down_trans_"+str_color_seta_rolls+".gif') no-repeat center center "+str_back_color_roll : rollDown.style.background = "url('../../img/barr_down_trans.gif') no-repeat center center "+str_back_color_roll;
		rollDown.onmouseover = function(){move(this.id.substring(this.id.length-1)-1,0);}
		rollDown.onmouseout = function(){clearTimeout(set_cont);}
		get(div_ref).appendChild(rollDown);
		get(str_elem).className = "menu_down";

		/*Monta a borda do bottom*/
		str_elem = "brd_bot_"+div_ref; brdBot = document.createElement("span"); brdBot.setAttribute("id",str_elem); brdBot.className = "brd_bot";
		brdBot.style.width = mn_width+"px";
		brdBot.style.top = mn_height+"px";
		brdBot.style.borderTop = "1px solid "+str_lines_color;
		get(div_ref).appendChild(brdBot);
	}
	mn_height = eval(cont_intens-cont_intens-cont_intens);
	arr_top.push(mn_height-(contInt*mn_li_height));
	arr_cont.push(mn_h_brd);
	arr_h_brd.push(mn_h_brd);
	mn_selected.push("");
	contInt = 0;
	arr_itens = new Array();
}

var arr_slctd_item = new Array();
/*Posiciona a seta de selecionado*/
function setCursor(id_item,over_color,click_color,img_path,st_selector,i_space,img_path_click){
	left = eval(Number(id_item.length)-Number(id_item.length))+1;
	right = eval(Number(id_item.length)-3);

	mn_selected.splice((id_item.substring(id_item.length-1))-1,1,id_item);

	if((arr_slctd_item.length==0) || (String(arr_slctd_item).indexOf(id_item.substring(right))<0)) {
		arr_slctd_item.push(id_item); }
	else {
		var mn_id = eval(id_item.substring(right+2));
		var mn_pos;
		for(i=0; i<arr_slctd_item.length;i++){
			if((arr_slctd_item[i].substring(right+2))==mn_id){
				mn_pos = i; } }
		get(arr_slctd_item[mn_pos]).className = "itens";
		get(arr_slctd_item[mn_pos]).style.background = "url('"+img_path+"') no-repeat "+i_space+"px center "+click_color;
		get("li_"+arr_slctd_item[mn_pos]).className = "li_off";
		arr_slctd_item.splice(mn_pos,1,id_item);
	}
	get(id_item).className = get(id_item).className + " selected";
	get(id_item).style.background = over_color;
	(st_selector!=false)?get("li_"+id_item).className = "li_on":get(id_item).style.background="url('"+img_path_click+"') no-repeat "+i_space+"px center "+over_color;
}

/*Movimenta o Menu*/
function move(mn_id,st){
	menu_id = arr_menu_id[mn_id];
	mn_top = arr_top[mn_id];
	int_cont = mn_id;
	(st==0)?up():down();
}
function up(){
	tem_cont = arr_cont[int_cont];
	if(tem_cont > (mn_top+1)){
		tem_cont=tem_cont-1;
		arr_cont.splice(int_cont,1,tem_cont); }
	else{
		clearTimeout(set_cont);
		return; }
	get(menu_id).style.top=tem_cont+"px";
	set_cont = setTimeout("up()",10);
}
function down(){
	tem_cont = arr_cont[int_cont];
	if(tem_cont < (arr_h_brd[int_cont]+1)){
		tem_cont=tem_cont+1;
		arr_cont.splice(int_cont,1,tem_cont); }
	else{
		clearTimeout(set_cont);
		return; }
	get(menu_id).style.top=tem_cont+"px";
	set_cont = setTimeout("down()",10);
}