// JavaScript Document

var appHash = "";
var appIndex = "";
var appDefault = "home.html";
var appFunction = function carregaPagina(url) {
	makeHistory(url);
	try {
		if(url.indexOf("{")!=-1) {			
			funcao = url.substring(url.indexOf("{")+1, url.length);
			if(funcao.indexOf("!")!=-1) {				
				par = funcao.substring(funcao.indexOf("!")+1, funcao.length);
				par = par.split("&");
				funcao = funcao.substring(0, funcao.indexOf("!"));
				temParametro = true;
			} else {
				temParametro = false;
			}
			url = url.substring(0, url.indexOf("{"));
			temFuncao = true;
		} else {
			temFuncao = false;			
		}
				
		if(url == "") {
			alert("?????");
			return;
		}
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			// Internet Explorer Windows
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(req) {
			req.onreadystatechange = function() {			
				lugarACarregar = document.getElementById("corpo");
				if(req.readyState == 4) {					
					tempHTML2 = req.responseText;
					if(lugarACarregar!=null) {
						lugarACarregar.innerHTML = req.responseText;
						
						if(temFuncao) {
							if(temParametro) {								
								funcao = funcao + "(";
								for(i=0;i<par.length-1;i++) {
									funcao = funcao + "'" + par[i] + "', ";
								}
								funcao = funcao + "'" + par[i] + "'";							
								funcao = funcao + ");";
							} else {
								funcao = funcao + "();";
							}							
							eval(funcao);
						}					
						switch(url) {							
							case "home.html": {
								desmarcarMenu(true);
								break;
							}							
							case "empresa.html": {
								itemMenuId = 1;
								marcarMenu(1);
								break;
							}
							case "produtos.html": {
								itemMenuId = 2;
								marcarMenu(2);
								break;
							}							
							case "cadastro.html": {
								if(!temFuncao) {									
									interesseId = 0;
								}								
								primeiroClique = true;
								itemMenuId = 3;
								marcarMenu(3);	
								break;
							}							
							case "representantes.html": {
								itemMenuId = 4;
								marcarMenu(4);
								break;
							}												
							case "contato.html": {
								marcarMenu(5);
								itemMenuId = 5;
								break;
							}
							case "artigosPublicacoes.html": {
								marcarMenu(6);
								itemMenuId = 6;
								break;
							}
							
							case "fonoView.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(1);
								break;	
							}							
							case "voxGames.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(7);
								break;	
							}							
							case "voxMetria.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}	
							case "earMix.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}	
							case "fisiMetrix.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
							case "fonoTools.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
							case "vocalgrama.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
						}						
						scrollTo(0,0);
						
					}
				} else if(req.readyState > 4) {
					alert("erro: " + req.readyState);
				} else if(req.readyState==1 && lugarACarregar!=null) {
					lugarACarregar.innerHTML = htmlCarregando;	
				} 				
			}
			req.open("GET", url, true);
			req.send(null);
		}
	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}
//});

var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;

function $(id) { return document.getElementById(id); }

function makeHistory(newHash) {
  if(isIe === true)
    $("control-iframe").setAttribute("src", "control.htm?id=" + newHash);
  else if(newHash != appIndex)
    window.location.hash = newHash;
  else
    go2index();
}

function handleHistory() {
  var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
  if(browserHash != appHash) {
    if(browserHash != "") {
      appFunction(browserHash);
      appHash = browserHash;
      makeHistory(browserHash);
	}
	else {
	  clearInterval(checkInterval);
	  window.location.hash = appHash;
	  makeHistory(appIndex);
	}
  }
}

function createIFrame() {
  var iFrame = document.createElement("iframe");
  iFrame.setAttribute("src", "control.htm?id=" + (hash = window.location.href.split("#")[1]) ? hash : appDefault);
  iFrame.setAttribute("id", "control-iframe");
  iFrame.style.display = "none";
  document.body.appendChild(iFrame);
}

function initialize() {
  if(isIe === true)
    createIFrame();
  checkBookmark();
  checkInterval = setInterval(handleHistory, 100);
}

function checkBookmark() {
  var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
  window.location.hash = (browserHash == "") ? appDefault : browserHash;
}

function go2index() {
  window.location = window.location.href.split("#")[0] + "#";
  window.location.reload();
}

function goTo(strUrl) {
	window.location.hash = strUrl;
	appFunction(strUrl);
}
