
function ajax(href) {

    if(href.indexOf("index.php") == -1) {
	window.location.href = href;
	return true;
    }
    var base  = 'index.php';
    var query = href.split('.php#')[1];
    var page  = query.split('=')[0];
    var url   = base+'?'+query+'&ajax';
    var load_img = '<br/><br/><br/>'+
                   '<center><img src="images/loading.gif" /></center>';

    //alert("Ajax: " + url);
    $('#content_wrap').fadeTo(1800, 0, function(){
	$('#content_wrapper').html(load_img).load(url, function(){
	    $('#content_wrap').fadeTo(1800, 1);
	});
    });
}

function ajax_init(href) {

    if(href.indexOf("index.php") == -1) {
	window.location.href = href;
	return true;
    }
    var base  = 'index.php';
    var query = href.split('?')[1];
    var page  = query.split('=')[0];
    var url   = base+'?'+query+'&ajax';
    var load_img = '<br/><br/><br/>'+
	           '<center><img src="images/loading.gif" /></center>';

    //alert("Ajax init: " + url);
    $('#content_wrapper').html(load_img).load(url, function(){
	$('#content_wrap').fadeTo(1800, 1);
    });
    
}

$(document).ready(function(){

    var curr_href = window.location.href;
    //alert("New page: " + curr_href);

    // redirect to index.php if no file is specified
    if(curr_href.indexOf('.php') == -1)
	window.location.href = curr_href + 'index.php';

    // handle navbar click events to trigger a hashchange event
    $('#navbar a').click(function(){
	var href = this.href;
	href = href.replace('?','#');
	window.location.href = href;
	return false;
    });

    // if a new page comes in with index.php#something then we need to 
    // call ajax_init
    if (curr_href.indexOf('.php#') != -1) {
    	curr_href = curr_href.replace(".php#",".php?");
    	ajax_init(curr_href);
    } else {
	// fade in the starting page
	$('#content_wrap').fadeTo(2000, 1);
    }

    // call ajax on the hashchange event 
    $(window).bind('hashchange', function(){
	var href = window.location.href;
	//alert("Hashchange: "+href);
	ajax(href);
    });

}); // document ready

