var default_hash = null;
var error_timer = null;
var page_timer = null;

var error_timeout = 12000;
var timer_wait = 500;

function launch() {	
	load_page(window.location.hash);
}

function load_page(hash) {
		
	error_timer = setTimeout("declare_error()", error_timeout);
	
	default_hash = hash;
	hash = clean_hash(hash);
	if(hash.length > 0) hash = "?" + hash;
	if(hash.indexOf("loadpage")>0)
	{
	  var pagina = hash.split("page=")[1];
	  $("#mainContent").html('<iframe id="content" name="contento" marginwidth="0" marginheight="0" src="' + pagina + '" frameborder="0" width="680" scrolling="No" height="638"></iframe>');
		clearTimeout(error_timer);
		page_timer = setTimeout("verify_hash()", timer_wait);
		document.title = 'Colegio de Abogados de Necochea';
	  
	}
	else
	$.ajax({type: "GET", url: "resolve.php" + hash, success: load_page_success});
}

function clean_hash(hash) {
	
	new_hash = hash.replace(/^\#/, "");
	return new_hash;
}

function declare_error() {
	
	if(error_timer) clearTimeout(error_timer);
	if(page_timer) clearTimeout(page_timer);
}

function load_page_success(html) {
	
	clearTimeout(error_timer);
	$("#mainContent").html(html);
	page_timer = setTimeout("verify_hash()", timer_wait);
	document.title = 'Colegio de Abogados de Necochea';
}

function verify_hash() {

	
	page_timer = setTimeout("verify_hash()", timer_wait);
	
	if(default_hash == window.location.hash) return;
	load_page(window.location.hash);
}
