//FERRAMENTA QUE SERVE PARA CHAMAR DETERMINADO ITEM DO MENU EM AJAX FABRICANDO CAMINHO LINKAVEL

//LOCAL DE DIFERENCIACAO DE FONTE DE CHAMADO
//.formato padrao de chamado: 
//##busca_atual_hash({ 1 },{ 2 })##
//ex.:busca_atual_hash('st',local_url_hash_partes[2])
//1.fonte diferenciadora do item do menu
//2.existência de hash com id de conteudo especifico
function busca_atual_hash(fonte,hash_2,hash_3){
	if(fonte == 'not'){
		if(hash_2){
			var hashe_mostrado = 'not|'+hash_2;
		}else{
			var hashe_mostrado = 'not';
		}
		abre_menu('noticias','fora','noticias',hash_2,'','','conteudo','conteudo','','',hashe_mostrado)
	}
}


//LOCAL EM QUE SE FECHA MENUS QUENAO INTERESSAM
//menus cadastrados:
//1.trabalhos
//2.textos
//3.profissional
//4.contato
function fecha_todos(qual){
	/* 1 */if(qual == 'trabalhos'){
		document.getElementById('contato').innerHTML = '';
	}else{
		//coloca o link escondido
		document.getElementById("esconde_menu_trabalhos").innerHTML = '<a href="#" onClick="abre_menu(\'trabalhos\',\'dentro\',\'sites\',\'\',\'cont\',\'esconde_menu_trabalhos\',\'conteudo\',\'carregamento\',\'#\',\'menu-trabalhos\',\'#\')"><font color="#837650">Trabalhos</font></a>';
		//colocando o link visivel
		document.getElementById("menu-trabalhos").innerHTML = '<a href="#" onClick="abre_menu(\'trabalhos\',\'fora\',\'sites\',\'\',\'cont\',\'esconde_menu_trabalhos\',\'conteudo\',\'carregamento\',\'#\',\'menu-trabalhos\',\'#tb|st\')">Trabalhos</a>';
	}
	/* 2 */if(qual == 'textos'){
		document.getElementById('contato').innerHTML = '';
	}else{
		//coloca o link escondido
		document.getElementById("esconde_menu_textos").innerHTML = '<a href="#" onClick="abre_menu(\'textos\',\'dentro\',\'posts\',\'\',\'cont\',\'esconde_menu_textos\',\'conteudo\',\'carregamento\',\'#\',\'menu-textos\',\'#\')"><font color="#837650">Textos</font></a>';
		//colocando o link visivel
		document.getElementById("menu-textos").innerHTML = '<a href="#" onClick="abre_menu(\'textos\',\'fora\',\'posts\',\'\',\'cont\',\'esconde_menu_textos\',\'conteudo\',\'carregamento\',\'#\',\'menu-textos\',\'#tx|10\')">Textos</a>';
	}
	/* 3 */if(qual == 'profissional'){
		document.getElementById('contato').innerHTML = '';
	}else{
		//coloca o link escondido
		document.getElementById("esconde_menu_profissional").innerHTML = '<a href="#" onClick="abre_menu(\'profissional\',\'dentro\',\'basicos\',\'2\',\'cont\',\'esconde_menu_profissional\',\'conteudo\',\'carregamento\',\'#\',\'menu-profissional\',\'\')"><font color="#837650">Profissional</font></a>';
		//colocando o link visivel
		document.getElementById("menu-profissional").innerHTML = '<a href="#" onClick="abre_menu(\'profissional\',\'fora\',\'basicos\',\'2\',\'cont\',\'esconde_menu_profissional\',\'conteudo\',\'carregamento\',\'#\',\'menu-profissional\',\'pf\')">Profissional</a>';
	}
	/* 4 */if(qual == 'contato'){
		document.getElementById('conteudo').innerHTML = '';
		document.getElementById('menu2').innerHTML = '';
	}else{
		//coloca o link escondido
		document.getElementById("esconde_menu_contato").innerHTML = '<a href="#" onClick="abre_menu(\'contato\',\'dentro\',\'basicos\',\'1\',\'cont\',\'esconde_menu_contato\',\'contato\',\'carregamento\',\'#\',\'menu-contato\',\'\')"><font color="#837650">Contato</font></a>';
		//colocando o link visivel
		document.getElementById("menu-contato").innerHTML = '<a href="#" onClick="abre_menu(\'contato\',\'fora\',\'basicos\',\'1\',\'cont\',\'esconde_menu_contato\',\'contato\',\'carregamento\',\'#\',\'menu-contato\',\'ct\')">Contato</a>';
	}
}


//LOCAL DE CHAMADO PARA PRIMEIRA INSTÂNCIA
//.acontece o chamado de primeira instância,
//onde é possível efetuar chamados consecutivos para outras instâncias
//.formato padrao do chamado:
//abre_menu({ 1 },{ 2 },{ 3 },{ 4 },{ 5 },{ 6 },{ 7 },{ 8 },{ 9 },{ 10 },{ 11 })
//ex.:abre_menu('trabalhos','dentro','sites','','cont','esconde_menu_trabalhos','conteudo','carregamento','#pns','menu-trabalhos','tb|st')
//1.nome do link
//2.condicional para tirar ou colocar
//3.nome da tabela do banco de dados do conteudo
//4.conteudo de hash (id)
//5.se continua para próxima etapa
//6.local de substituição de link pra tirar e pra colocar
//7.local para insercao e conteudo
//8.local de carregamento
//9.altura para manter a pagina
//10.nome do local do link
//11.hash
function abre_menu( qual , origem , parte , hash_2 , cont , local_esconde , local_insercao , local_carregamento , altura_pagina , nome_local_link , hax ){
	//alert(hax);
	//fecha_todos(qual); //para menus
	//location.href = altura_pagina;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	
	}else {
	
		if (window.ActiveXObject) {
			
			try {
			
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			catch (e) { }
		}
	}
	
	if (xhr) {
		
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 1) {
				document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#ffffff'>Carregando...</font>";
			
			}if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					
					var outMsg = xhr.responseText;
				
				}else {
					
					var outMsg = "<font face='verdana' size='3' color='#ffffff'>There was a problem with the request " + xhr.status + "</font>";
					
				}
				document.getElementById(local_carregamento).innerHTML = "";
				if(origem == 'fora'){//no caso de ser para inserir conteudo
					//inserindo conteudo no local necessário
					document.getElementById(local_insercao).innerHTML = outMsg;
					if( (nome_local_link != '') && (local_esconde != '') ){
						//pegando link guardado
						var link_guardado = document.getElementById(local_esconde).innerHTML;
						//guardando o link atual
						document.getElementById(local_esconde).innerHTML = document.getElementById(nome_local_link).innerHTML;
						//colocando o link pego no local do link atual
						document.getElementById(nome_local_link).innerHTML = link_guardado;
					}
					//construindo hash para reconstrução
					window.location.hash = hax;
				}else if(origem == 'dentro'){//no caso de ser para retirar conteudo
					//tirando conteudo mostrado
					document.getElementById(local_insercao).innerHTML = "";
					//pegando link guardado
					var link_guardado = document.getElementById(local_esconde).innerHTML;
					//guardando o link atual
					if(local_esconde){
						document.getElementById(local_esconde).innerHTML = document.getElementById(nome_local_link).innerHTML;
					}
					//colocando o link pego no local do link atual
					document.getElementById(nome_local_link).innerHTML = link_guardado;
					//zerando hash
					window.location.hash = "#";
					//retirando item de outra instancia 1
					if(document.getElementById(local_esconde+'_2').innerHTML == ''){}else{
						
						document.getElementById(document.getElementById(local_esconde+'_2').innerHTML).innerHTML = '';
					}
				}
				if(cont == 'cont'){//sendo para continuar o conteudo
					abre_item_menu(hash_2,parte,qual,altura_pagina,local_carregamento,hax);
				}
			}
		};
		if(hash_2){
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_menu&qual="+qual+"&parte="+parte+"&hash="+hash_2, true);
		}else{
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_menu&qual="+qual+"&parte="+parte, true);
		}
		xhr.send(null);
		
	}else {
		
		document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#ffffff'>Sorry, but I couldn't create an XMLHttpRequest</font>";
		
	}
}

//LOCAL DE CHAMADA PARA SEGUNDA INSTANCIA
//.acontece o chamado de segunda instância
//.formato padrao do chamado:
//abre_item_menu({ 1 },{ 2 },{ 3 },{ 4 },{ 5 },{ 6 })
//ex.:abre_item_menu(hash_2,parte,qual,altura_pagina,local_carregamento,hax);
//1.item id especifica chamado (caso tenha)
//2.nome da tabela onde buscar conteudo
//3.tipo de item a ser diferenciado com o menu chamado
//4.altura da pagina desejada
//5.local de carregamento
//6.hash
function abre_item_menu(qual,origem,menu_chamado,altura_pagina,local_carregamento,hax){
	//alert(hax);
	//altura da pagina
	location.href = altura_pagina;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	
	}else {
	
		if (window.ActiveXObject) {
			
			try {
			
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			catch (e) { }
		}
	}
	
	if (xhr) {
		
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 1) {
	
				document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#4E4325'>Carregando...</font>";
			
			}if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					
					var outMsg = xhr.responseText;
				
				}else {
					
					var outMsg = "<font face='verdana' size='3' color='#ffffff'>There was a problem with the request " + xhr.status + "</font>";
					
				}
				document.getElementById(local_carregamento).innerHTML = "";
				//diferencia pelo menu_chamado
				if(menu_chamado == 'trabalhos'){
					document.getElementById('local_item_menu').innerHTML = outMsg;
					if(!qual){
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_trabalhos','menu2','cont','');
					}else{
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_trabalhos','menu2','cont',qual);
					}
				}else if(menu_chamado == 'textos'){
					document.getElementById('local_item_menu').innerHTML = outMsg;
					if(!qual){
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_textos','menu2','cont','');
					}else{
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_textos','menu2','cont',qual);
					}
				}else if(menu_chamado == 'profissional'){
					document.getElementById('local_item_menu').innerHTML = outMsg;
					if(!qual){
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_profissional','menu2','cont','');
					}else{
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_profissional','menu2','cont',qual);
					}
				}else if(menu_chamado == 'contato'){
					document.getElementById('local_item_menu_contato').innerHTML = outMsg;
					if(!qual){
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_contato','menu2','cont','');
					}else{
						abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_contato','menu2','cont',qual);
					}
				}
				location.hash = hax;
			}
		};
		if(!qual){
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_item_menu&origem="+origem+"&menu_chamado="+menu_chamado+"&hax="+hax, true);
		}else{
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_item_menu&qual="+qual+"&origem="+origem+"&menu_chamado="+menu_chamado+"&hax="+hax, true);
		}
		xhr.send(null);
		
	}else {
		
		document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#ffffff'>Sorry, but I couldn't create an XMLHttpRequest</font>";
		
	}
}


//LOCAL DE CHAMADA PARA TERCEIRA INSTANCIA
//.acontece o chamado de terceira instância
//.formato padrao do chamado:
//abre_item_menu({ 1 },{ 2 },{ 3 },{ 4 },{ 5 },{ 6 },{ 7 })
//ex.:abre_item_menu_sbd(altura_pagina,local_carregamento,hax,conteudo,local_conteudo,cont,qual);
//1.altura da pagina desejada
//2.local de carregamento
//3.hash
//4.conteudo buscado
//5.local conteudo
//6.se continua ou nao
//7.hash 2
function abre_item_menu_sbd(altura_pagina,local_carregamento,hax,conteudo,local_conteudo,cont,qual){
	//alert(conteudo);
	//item 1 do hash
	var local_url_hash = hax;
	var local_url_hash_partes = local_url_hash.split("|");
	var obs1 = local_url_hash_partes[1];
	//if(local_url_hash_partes[2]){
	//	var obs2 = local_url_hash_partes[2];
	//}
	var obs2 = qual;
	//altura da pagina
	location.href = altura_pagina;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	
	}else {
	
		if (window.ActiveXObject) {
			
			try {
			
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			catch (e) { }
		}
	}
	
	if (xhr) {
		
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 1) {
	
				document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#4E4325'>Carregando...</font>";
			
			}if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					
					var outMsg = xhr.responseText;
				
				}else {
					
					var outMsg = "<font face='verdana' size='3' color='#ffffff'>There was a problem with the request " + xhr.status + "</font>";
					
				}
				document.getElementById(local_carregamento).innerHTML = "";
				//diferencia pelo menu_chamado
				document.getElementById(local_conteudo).innerHTML = outMsg;
				if(qual != ''){
					location.hash = hax+'|'+qual;
				}else{
					location.hash = hax;
				}
				
				if(cont == 'cont'){
					if(conteudo == 'menu_inferior_trabalhos'){
						if(qual != ''){
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'explicacao_item_tb','explicacao_item_menu','',qual);
						}else{
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'explicacao_item_tb','explicacao_item_menu','','');
						}
					}else if(conteudo == 'menu_inferior_textos'){
						if(qual != ''){
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_textos','menu2','',qual);
						}else{
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_textos','menu2','','');
						}
					}else if(conteudo == 'menu_inferior_profissional'){
						if(qual != ''){
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_profissional','menu2','',qual);
						}else{
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_profissional','menu2','','');
						}
					}else if(conteudo == 'menu_inferior_contato'){
						if(qual != ''){
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_contato','menu2','',qual);
						}else{
							abre_item_menu_sbd(altura_pagina,local_carregamento,hax,'menu_inferior_contato','menu2','','');
						}
					}
				}
			}
		};
		if(obs2){
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_item_sbd&conteudo="+conteudo+"&obs1="+obs1+"&obs2="+obs2+"&qual="+qual, true);
		}else{
			xhr.open("GET", "ajax_link/resposta_script2.php?content=abre_item_sbd&conteudo="+conteudo+"&obs1="+obs1+"&qual="+qual, true);
		}
		xhr.send(null);
		
	}else {
		
		document.getElementById(local_carregamento).innerHTML = "<font face='times new roman' size='3' color='#ffffff'>Sorry, but I couldn't create an XMLHttpRequest</font>";
		
	}
}
