﻿//On load page, init the timer which check if the there are anchor changes each 300 ms
//http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/



$(document).ready(function(){
	checkAnchor();
	$(".arrow_icon").hide();
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		var load_page="";
		currentAnchor = document.location.hash;
		//if there is not anchor, the loads the default section
		if(!currentAnchor){
			load_page = "";
		}else{
			load_page = currentAnchor.substring(1);
		}
		
		$.ajax({						   
			   type: "POST",						   
			   url: base_url+load_page,
			   data: {ajax_call:"true"},
			   dataType: "json",
			   beforeSend: function() {
				   $("#center1").html("");
				   set_menu_selected(load_page);
				   $("#content").hide();
				   $("#loader_icon").show();
				},
			   success: function(msg){
				   $("#loader_icon").hide();
					document.title=msg.title_text;
					$("#header_picture").html("<img src='"+msg.header_picture+"' />");
					$("#header_picture").pngFix();
					$("#content").show();
				    $("#content").html(msg.page);
					$("#content").pngFix();
					$("#logo_text_container").html("<img src='"+msg.header_text+"' />");
					$("#logo_text_container").pngFix();
					
					
				}
		}); 
	}
}
function set_menu_selected(load_page){
	
	var aux_old=$("a.selected");
	aux_old.removeClass("selected").children(".arrow_icon").hide();
	if(load_page==""){
		load_page="home";
	}
	
	if($("a.menu_item[href='#"+load_page+"']").length>0){
    	$("a.menu_item[href='#"+load_page+"']").addClass("selected");
		setTimeout(function(){
							$("a.menu_item[href='#"+load_page+"']").children(".arrow_icon").show();},0);
	}
	
}