var currentAnchor = null;  
var currentSection = null;
var currentPage = null;
var content;


//Function which checks if there are anchor changes, if there are, sends the ajax petition  
function checkAnchor(){  
   //Check if it has changes  
	 if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
	 	//if there is not anchor, the loads the default section
		if(!currentAnchor)
			changePage('home')
		else
		//if(currentAnchor)
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			if(splits[0] != currentSection){
			   //change(splits[0]);
				 currentSection = splits[0];
				 changeNav(currentSection);
			}
			//delete splits[0];
			//Create the params string
			//var params = splits.join('&');
			//var query = "section=" + section + params;
			if(splits[1]){
			  currentPage = splits[1];
				changePage(currentPage);
			}
			else{
			  changePage('home');
			}			
			setSelected(currentPage);
			
		}	
	}

}  

function changeNav(section){
	 $.get("pageServer.php","section=" + section, function(data){
	    menu=data;
      $("#nav_menu").fadeOut('fast');
			setTimeout('$(\"#nav_menu\").html(menu);checkMenus()',200);
			setTimeout('$(\"#nav_menu\").fadeIn(2000)',500);			
	 });
}

function changePage(page){
    $.get("pageServer.php","page=" + page, function(data){		
      content=data;		
      $("#content").fadeOut('slow');
	    setTimeout('$(\"#content\").html(content)',200);
	    setTimeout('$(\"#content\").fadeIn(2000)',500);
    });
}

function checkMenus(){
    if($("#"+currentPage).parent().parent().is(":hidden")){
		   $("#"+currentPage).parent().parent().slideDown("slow");
			 setSelected(currentPage);
		}
}

function navClikd(section){
		//A timeout is required to allow the URL to update before we read it
		//This will obviously need to be checked across the different browsers 
		if(section){
		  document.location.hash=section;
		}
		//setSelected(e.target || e.srcElement);												 
}

function setSelected(id){
   $(".selected").removeClass('selected');
	 $("#"+id).addClass('selected');	 
}

function show_gal_menu(){
	if ($("#gal_menu").is(":hidden")) {
        $("#gal_menu").slideDown("slow");
  } else {
    $("#gal_menu").slideUp("slow");
  }
}

function show_info_menu(){
	if ($("#info_menu").is(":hidden")) {
        $("#info_menu").slideDown("slow");
  } else {
    $("#info_menu").slideUp("slow");
  }
}

function loadSlideshow(dir, buttons_yes_no, duration,wmark){
	 $('#slide_holder').agile_carousel({
  		first_last_buttons: buttons_yes_no,
      hover_next_prev_buttons: "no",
      next_prev_buttons: buttons_yes_no,
      pause_button: buttons_yes_no,
      slide_buttons: "no",
      slide_directory: dir,
   		slide_links: "",
      slide_number_display: "no",
			//slide_timer_length: duration,
      timer_length: duration,
    	transition_duration: 2300,
    	transition_easing: "easeInOutQuad",	
      transition_type: "fade",
  		water_mark: wmark
	 });
}

function login(){
  uname = document.getElementById('uname_box').value;
	pswrd = document.getElementById('pswrd_box').value;
	$.ajax({
     url: "login_checker.php",
     data: ('uname='+uname+'&pswrd='+pswrd),
		 complete: function(data){
					 			if(data.responseText=="valid"){
					 			   $("#login").fadeOut();
									 $("#page_mask").fadeOut();
							  }
							  else
							     $("#login div").html("<span>Invalid password</span>");
				 }		
  });

}