	var enderecoDoSite	= "link=";
	var container		= "conteudo";
	var paginaInicial	= "home.php";
	var ERcaminhoDoSite = new RegExp("^.*"+enderecoDoSite);
	var tituloDaJanela	= "";

	function scriptOnload(texto){
		var Emular = document.createElement('script');
		var encontrou = texto.indexOf('onload', "0");
		if (encontrou != -1) {
			dividir = texto.split('onload="');
			fim = dividir[1].split('"');
			Emular.setAttribute('language', 'Javascript');
			Emular.text = fim[0];
			document.body.appendChild(Emular);
		}
	}

	function aguarde(){
		var msg =	"<div style=\"padding-top: 150px\" id=\"carregando\" align=\"center\">\n\
						<img src=\"imagens/loader.gif\"><br>Aguarde...<br>\n\
					</div>\n\
					";
		$("#"+container).html(msg);
	}

	function erro(){
		var msg =	"<div style=\"padding-top: 50px\" id=\"carregando\" align=\"center\">\n\
						<br /><br /><img src=\"imagens/loader.gif\"><br><br>\n\
						<span style=\"font-weight: bold; color: #FF0000\">Houve um erro ao carregar a página.</span>\n\
					</div>\n\
					";
		$("#"+container).html(msg);
	}

	function carregaArquivo(nomeArquivo, parametros){
                var arq = nomeArquivo.split("?");
                
		if (arq[0] == "chacaras/index.php" || arq[0] == "profissionais/index.php" || arq[0] == "chacaras/vendas.php"){
			$("#lateralDireita").hide();
		}else{
			$("#lateralDireita").show();
		}


		if (!parametros){
			parametros = "";
		}

		$.ajax(
				{
					type: "POST",
					url: nomeArquivo,
					dataType: "html",
					data: parametros,

					success: function(msg){

						scriptOnload(msg);
						$("#"+container).html(msg);
						$('#carregando').remove();
						
						for (i = 0; i < $("#"+container+" a[@rel='ajax']").length; i++){
							urlAntiga	= $("#"+container+" a[@rel='ajax']")[i].href;
							hash		= urlAntiga.replace(ERcaminhoDoSite, '');
							hash		= hash.replace(".php&",".php?");
							$("#"+container+" a[@rel='ajax']")[i].href = "#"+hash;
						}

                        for (i = 0; i < $("#"+container+" form").length; i++){
                             $("#"+container+" form["+i+"]").attr({
                                action: $("#"+container+" form["+i+"]").attr("function")
                             });
                        }

						$("#"+container+" a[@rel='ajax']").click(function(){
							var hash = this.href;
							hash = hash.replace(/^.*#/, '');
							$.historyLoad(hash);
							return false;
						});

						document.title = tituloDaJanela;
					},

					beforeSend: function(){
						aguarde();
					},

					stop: function(){
						$('#carregando').remove();
					},

					error: function(){
						erro();
					}
				}
			)
	}

	function pageload(hash,parametros) {
		if(hash) {
			carregaArquivo(hash,parametros);
		} else {
			carregaArquivo(paginaInicial);
		}
	}

	$(document).ready(function(){

		tituloDaJanela = document.title.split("#")[0];
		
		for (i = 0; i < $("a[@rel='ajax']").length; i++){
			urlAntiga	= $("a[@rel='ajax']")[i].href;
			hash		= urlAntiga.replace(ERcaminhoDoSite, '');
			hash		= hash.replace(".php&",".php?");
            $("a[@rel='ajax']")[i].href = "#"+hash;
		}

        for (i = 0; i < $("form").length; i++){
             $("form["+i+"]").attr({
                action: $("form["+i+"]").attr("function")
             });
		}

		$.historyInit(pageload);
		$("a[@rel='ajax']").click(function(){
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});

		
	});