var request_uri = '';
var posting = false;
var last_uri = '';
var timer = false;
var hash = '';

// IDs for page components...
var load_anim = '#link_load';
var default_load_into = '#data';
var homepage = 'home';


function strpos(haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

var hash = location.hash;

function start_timer()
{
	setInterval(function()
	{
		if (window.location.hash != hash && (!posting))
		{
			initialise_state_from_url();
		}
	}, 100);
}


function initialise_state_from_url()
{
	
	if(window.location.hash)
	{
		DEBUG_trace('initialising from hash');
		var url = window.location.hash;
		hash = url;
		url = url.split("#");
		url = url[1];
		// Define the 'global' request uri variable
		request_uri = url;
		//alert('initialising from url: '+url);
		if(!timer)
		{
			timer = true;
			start_timer();
		}
		post(url);
	}else if(window.location != 'http://'+http_host+"/"){
		//alert('no hash!');
		DEBUG_trace('initialising from url');
		var href = window.location;
		href = href.toString();
		href = href.replace('http://'+http_host,"");
		//alert('href: '+href);
		request_uri = href;
		window.location = "/#"+href;
		//post(window.location,'#content',true);	
	}else{
		DEBUG_trace('redirect to home');
		url = "/"+homepage+"/";
		
		window.location = "/#"+url;
		initialise_state_from_url();
		
	}
}

function post(href,load_into,absolute) {
	// Requests one at a time please...
	
	if(!posting){
		DEBUG_trace('getting new page: '+href);
		posting = true;
		hash = window.location.hash;
		start_page_load_request();
		
		_gaq.push(['_setAccount', 'UA-17204203-1']);
		_gaq.push(['_trackPageview', "/"+href]);/**/		 
		_gaq.push(['_trackEvent', 'Page', 'Click', '/'+href]);
		 
		request_uri = href;
		var url = 'http://'+http_host+"/"+href;
		if(!absolute)
		{
			var url = 'http://'+http_host+"/"+href;
		}else{
			var url = href;	
		}
		if(!load_into)
		{
			// User hasn't supplied a target to load the content into
			var target = jQuery(default_load_into);	
		}else{
			// User has requested a custom container for the HTML to be returned into
			var target = jQuery(load_into);		
		}
		
		
		
		target.fadeOut(function(){
			
			/*jQuery('#content_load').animate({        
			opacity: 1,
			height: "16px"
		  }, 1500 );*/
	
			target.load(url,{'ajax':'true'},function(){
				/*jQuery('#content_load').animate({        
			opacity: 0,
			height: 0
		  }, 1500 );*/
				jQuery(load_anim).fadeOut(function(){jQuery(load_anim).remove();});
				
				
				target.fadeIn(function(){
					//var new_height = jQuery('#main_content').innerHeight();
					//custom_js();
					has_completed_page_load();
					posting = false;
				});
			});
		});
	}	
	
}

// open external links in a new window
$(function() {
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

$(document).ready(function(){
	
	// fade twitter logo
	//$('#twitter_logo').hover(function() { alert('hover');	}, function () { alert('leave hover'); });
	
});
