var currentAnchor = "";
var anterior = "";

function aja(parametros, pagina) {
	if (anterior != parametros){
		$(".content").fadeOut("fast");
		$.ajax({
			type: "POST",
			url: pagina,
			data: parametros,
			//show loading just when link is clicked
			beforeSend: function(){$("#loading").fadeIn("fast");}, 
			//stop showing loading when the process is complete
			complete: function(){ $("#loading").fadeOut("fast");}, 
			//so, if data is retrieved, store it in html
			success: function(html){ 
				$(".content").fadeIn("slow"); //animation
				$(".content").html(html); //show the html inside .content div
				pageTracker._trackPageview('/' + parametros.substr(5));
			}
		}); //close $.ajax(
		anterior = parametros;
	}
}

function procesar() {
	$('.entry a').click(function() { 
		if ($(this).attr("href").substring(0,1)=="#"){
			var content_show = $(this).attr("href"); 
			
			aja("page="+content_show, "req.php");
		}
	}); //close click(
}

function buscar() {
	var que = $("#s").attr("value"); 
	if (que!=""){
		document.location.hash="buscar/"+que;
		aja("page=#!buscar/"+$("#s").attr("value"), "req.php");
	}
	return false;
}

function checkAnchor(){  
    if(currentAnchor != document.location.hash){  
        currentAnchor = document.location.hash;
        if(currentAnchor=="") aja("page=#!inicio", "req.php");
        else aja("page="+currentAnchor, "req.php");
    }  
}  

$(document).ready(function(){

	var myFile = document.location.toString();
	if (myFile.match('#')) {
		var myAnchor = '#' + myFile.split('#')[1];
		aja("page="+myAnchor, "req.php");
	//} else {
	//	aja("page=#!inicio", "req.php");
	}

	$('#nav a').click(function() { //start function when any link is clicked
		if ($(this).attr("href").substring(0,1)=="#"){
			//retrieve title of link so we can compare with php file
			var content_show = $(this).attr("href"); 
			currentAnchor=content_show;
			aja("page="+content_show, "req.php");
		}
	}); //close click(

	$('#link a').click(function() { 
		if ($(this).attr("href").substring(0,1)=="#"){
			var content_show = $(this).attr("href"); 
			currentAnchor=content_show;
			aja("page="+content_show, "req.php");
		}
	}); //close click(

	setInterval("checkAnchor()", 300);	
});  
