﻿$(document).ready(function(){
	checkURL();
	//References
	var sections = $("a");
	var loading = $("#loading");
	var content = $("#content");

	$('a') 
    .livequery('click', function(event) { 
checkURL(this.hash); 
 
       //show the loading bar
	 showLoading();  
		//load selected section
		switch(this.className){
			case "splash2":
				
				content.ajaxStart(function(){
				content.slideUp();
				 });
				
				content.load("switch.php #section_splash2", hideLoading);
				
				content.ajaxStop(function(){
				content.slideDown();
					  });
					  
				break;
			
			case "species":
				content.ajaxStart(function(){
				content.slideUp();
				 });
				content.load("switch.php #section_species", hideLoading);
				content.ajaxStop(function(){
				content.slideDown();
					  });
				break;
			
			case "county":
				content.ajaxStart(function(){
				content.slideUp();
				 });
				
				content.load("switch.php #section_county", hideLoading);
				 
				content.ajaxStop(function(){
				content.slideDown();
					  });
				break;

			
			case "countylist":
			    content.ajaxStart(function(){
				content.slideUp();
				 });
                content.load("switch.php?cid=" + this.id + ' '  + "#section_countylist" , hideLoading); 
                content.ajaxStop(function(){
				content.slideDown();
					  });
				break;
							
				
				case "single":
				content.ajaxStart(function(){
				content.slideUp();
				 });
				content.load("switch.php?sid=" + this.id + ' '  + "#section_single", hideLoading);
				content.ajaxStop(function(){
				content.slideDown();
					  });
				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: "switch.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
			}
		}

	});

}

