﻿$(document).ready(function(){
 checkURL();
	//References
	var sections = $("span a");
	var loading = $("#loading");
	var content = $("#section_recent"); 
      
	sections.click(function(event){
checkURL(this.hash); 
		
       //show the loading bar
	 showLoading();  
		//load selected section
		switch(this.id){
					
			case "recentc":
			 content.ajaxStart(function(){
             content.fadeOut();
 });
 
 
			 content.load("set.php?page=" + this.title + ' '  + "#section_recent" , hideLoading); 
  	         
  	         content.ajaxStop(function(){
             content.fadeIn();
 
                 });
             break;
						
			default:
				//hide loading bar if there is no selected section
				hideLoading();
				break;
		}
		
    }); 
	

	//show loading bar
	function showLoading(){
		loading
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(1000, 0)
		
		;
	};
	setInterval("checkURL()",250);

});
var lasturl="";	//here we store the current URL hash

function checkURL(hash)
{
	if(!hash) hash=window.location.hash;	//if no parameter is provided, use the hash value from the current address

	if(hash != lasturl)	// if the hash value has changed
	{
		lasturl=hash;	//update the current hash
		loadPage(hash);	// and load the new page
	}
}

function loadPage(url)	//the function that loads pages via AJAX
{
	url=url.replace('#','');	//strip the #page part of the hash and leave only the page number


	$.ajax({	//create an ajax request to load_page.php
		type: "POST",
		url: "set.php",
		data: 'page='+url,	//with the page number as a parameter
		dataType: "html",	//expect html to be returned
		success: function(msg){

			if(parseInt(msg)!=0)	//if no errors
			{
				$('#section_recent').html(msg);	//load the returned html into pageContet
			}
		}

	});

}
