/*

v1.4.1

This script appends ajax functionality to links
template should have 
	if($_POST['ajax']) { content();exit; }
requires jquery

version notes:

1.1
	body's id is set according to the loaded filename.  ie 'path/page.php' yields body id 'page'.  css can use this to alter styles.  this should be done in template.php as well.
1.2
	init_ajax_nav_links() - separated initialization of a tags into a separate function so that new, dynamically created menus can be intialized
	load from hash now works with get vars
1.3
	enabled back button support  http://dev2dev.bea.com/pub/a/2006/01/ajax-back-button.html?page=2
1.4
	eval_html() - added, and called when ie<7 (jquery fails to do this on its own?)
1.4.1
	eval_html() - getscript added

*/

//configure these variables
	root = "";
	links = '.nav.ajax a';//css path to element(s) to be handled via ajax
	content = '#content'; //css path to element in which to put ajax response
	//root based on host
	switch(window.location.host)
	{	
		//development
		case "goedev.com":
		case "www.goedev.com":
		  	root = '/WhiteSands/site/';
		  	break;    
		//local
		case "localhost":
		  	root = '/GardenOfE/Goedev/WhiteSands/site/';
		  	break;
		//default / live
		default:
			root = "/";
			break;
	}
// end config

var ajax_cache = Array();

var expectedHash = "";

function makeHistory(newHash)
{
  window.location.hash = newHash;
  expectedHash = window.location.hash;
  return true;
}

function reportOptionValue()
{
  var myForm = document.make_history;
  var mySelect = myForm.change_year;
  return mySelect.options[mySelect.selectedIndex].value;
}

function setOptionValue(value)
{
  var myForm = document.make_history;
  var mySelect = myForm.change_year;
  mySelect.options[value-1].selected = true;
  return true;
}

function handleHistory()
{
  if ( window.location.hash != expectedHash )
  {
    expectedHash = window.location.hash;
	href = window.location.hash.toString().substring(1);//removes #
	href = (href == "")? "index.php": href;
	ajax_load(href);
    //var newoption = expectedHash.substring(6);
    //setOptionValue( newoption );
  }
  return true;
}

$(document).ready(function(){
	window.setInterval("handleHistory()", 500);
	//if hash, load it
	if(window.location.hash){
		href = window.location.hash.toString().substring(1);//removes #
		ajax_load(href);
	}
	init_ajax_nav_links();
});

function init_ajax_links(path){
	path = path? path: links;
	$(path).click(function(){
		ajax_load(this.href);
		return false;
	});	
}
function init_ajax_nav_links(new_links){ //depreciated. use init_ajax_links instead.
	return init_ajax_links(new_links);
}

function ajax_load(url){ 
	//avoid multiple loads
	if(document.loading) return false;
	document.loading=url;

	//body_id = 'index' from 'path/to/index.php?with=parameters'
	body_id = url.substring(url.lastIndexOf("/")+1).replace(".php", "");
	body_id = body_id.lastIndexOf("?")!=-1 ? body_id.substring(0, body_id.lastIndexOf("?")) : body_id;//filter out parameters "?get=rid&of=these"
	
	//relative href = 'index.php?with=parameters' from 'path/to/index.php?with=parameters'
	relative_href = url.substring(url.lastIndexOf("/")+1);

	//remove non-hash from url: redirect from domain.com/index.php#contact.php to domain.com/#contact.php
	file_start = window.location.toString().lastIndexOf("/")+1;//file starts after the last /	
	file_end = window.location.toString().lastIndexOf("#")>=file_start? window.location.toString().lastIndexOf("#"): window.location.toString().length;//file ends at # or end
	current_file = file_start < file_end? window.location.toString().substring(file_start, file_end): null;
	if(current_file)window.location=root+"#"+relative_href;
	
	//load from cache?
	if(it = ajax_cache[url]) {
		$(content).html(it);
		$('body').attr("id", body_id);
		document.loading=false;
		makeHistory(relative_href);
		return false;
	}
	
	//ajax it
	$('body').addClass('loading'); //body.loading * { cursor:wait !important; }
	$.ajax({
		body_id: body_id,
		type:'POST',
	   	dataType: "html",
		url:url,
		data:'ajax=true',
		success: function(response){
			//cache it
			ajax_cache[document.loading] = response;
			//display it
			$(content).html(response);
			//ie<7 eval
			if($.browser.msie)
				eval_html(response);
			//end loading
			document.loading=false;
			$('body').removeClass('loading');
			$('body').attr("id", this.body_id);
		},
		error: function(){
			$('body').removeClass('loading');
			document.loading = false;
			alert('An error ocurred while attempting to process your request.  Please try again.');
			return false;
		}
	})
	
	//update hash and history (for back button support)
	makeHistory(relative_href);
	return false;
}

function eval_html(html) {
	tags = /\<script.*?\>(.*?)\<\/script\>/gi;
	js = html.match(tags);
	if(js){
		for(i=0; i<js.length; i++) {
			if(js[i].match("src")){
				src = /.*src=\"(.*)\".*/;
				src = js[i].replace(src, "$1");
				$.getScript(src);
			};
		};
		js=js.join("");
		
		eval(js.replace(tags, "$1"));
	}
}