// JavaScript Document

function ajaxInit() {
	var req;
	try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(ex) {
			try {
				req = new XMLHttpRequest();
			} 
			catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				req = null;
			}
		}
	}
	return req;
}

function loadUrl(url){
	objAjax = ajaxInit();
	
	objAjax.open("GET", "html/"+url+".html", true);
	
	objAjax.onreadystatechange=function() {
		
		if (objAjax.readyState==1){
			$('#middle').append("<p>Carregando...</p>");
		}
		
		if (objAjax.readyState==4){
			$('#middle').empty();
			$('#middle').append(objAjax.responseText);
			
			if(url=="home"){
				
				loadScript("js/home.js");
				$('#middle').css('background-image','url(imagens/casca-de-pinus.png)')
			}
			else
				$('#middle').css('background-image','none')
				if(url=="localizacao"){
					//nada
				}
				else if(url=="orcamento"){
					loadScript("js/orcamento.js");
				}
				else{
					loadScript("js/eventos.js");
				}
		}
	}
	
	objAjax.send(null);
}

function loadScript(src){
	//Funcao generica para incluir js dinamicamente
	var scriptTag = document.createElement('script');
	scriptTag.setAttribute("type","text/javascript");
    
	if (scriptTag.readyState) {
		//IE
		scriptTag.onreadystatechange = function(){
			if (scriptTag.readyState == "loaded" || scriptTag.readyState == "complete") {
				scriptTag.onreadystatechange = null;
				setTimeout(function(){scriptTag.parentNode.removeChild(scriptTag)}, 1)
			}
		};
	}else{
		//Others
		scriptTag.onload = function(){
			setTimeout(function(){
				scriptTag.parentNode.removeChild(scriptTag)
			}, 1)
		};
	}
	scriptTag.src = src;
	document.getElementsByTagName("head")[0].appendChild(scriptTag);
}
