﻿/*
**	Using jQuery 1.2
*/
var useHist = true;
var loadURL = false;
var lastURL = "";
var baseURL = "index.php";

// ...the usual shit for making IE behave
if( $.browser.msie ) {
	/*
	**	We'll add a short delay to let IE
	**	sort out whatever IE does...
	*/
/*
	window.onload = function() {
		setTimeout( function() {
			Startup();
		}, 2000 );
	}
*/
} else {
	/*
	**	Safari doesn't handle the dhtml
	**	history, so skip the dhtml stuff
	*/
	if( !$.browser.safari ) {
		$(document).ready(function(){
			Startup();
		});
	}
}


/*
**	Set up everything we want to after
**	the page and DOM has finished loading
*/
function Startup() {

	/*
	**	Setup the dhtml history stuff
	*/
	if( useHist ) {
		window.historyStorage.init();
		window.dhtmlHistory.create();
		dhtmlHistory.initialize();
		dhtmlHistory.addListener( historyChange );
		if( dhtmlHistory.isFirstLoad() ) {
			entryURL = location.href;
			if( entryURL.indexOf( "#" ) != -1 ) {
				tmp = entryURL.split( "#", 2 )
				if( tmp[1] != '' ) {
					entryURL = tmp[0];
					loadURL = true;
					AjaxLoad( baseURL + "?tag="+tmp[1] );
				}
			}
		}
	}

	/*
	**	Unbind the click events and replace
	**	them with our own AJAX calls
	*/
	$("a.mainnav, a.pagelink")
		.unbind("click")
		.click(function(e) {
			AjaxLoad( this.href, e );
			return false;
		} );

	/*
	**	Run events for displaying AJAX
	**	page loads to the visitor
	*/
	$("#loading")
		.ajaxStart( function() {
			$(this).css("display","block");
		} )
		.ajaxStop( function() {
			if( $.browser.msie ) {
				$(this).css("display","none");
			} else {
				$(this).fadeOut();
			}
		} )
		.ajaxSuccess( function() {
			if( $.browser.msie ) {
				$(this).css("display","none");
			} else {
				$(this).fadeOut();
			}
		} )
		.append("Sidan laddas...");

	/*
	**	Make sure all links are not
	**	outlined when clicked
	*/
	$("a").focus( function() {
		this.blur();
	} );
}


/*
**	Take care of history navigation
*/
function historyChange( newLocation, historyData ) {
	var historyMsg = historyData;
	if( historyData != null ) {
		loadURL = true;
		AjaxLoad( baseURL + "?tag="+historyMsg );
	} else {
		loadURL = true;
		AjaxLoad( baseURL );
	}
}


/*
**	AJAX content loading routine
*/
function AjaxLoad( href, e ) {
	/*
	**	If we have a hash, strip it
	**	away from the URL
	*/
	if( href.indexOf( "#" ) != -1 ) {
		tmp = href.split( "#", 2 )
		href = tmp[0];
	}
	
	/*
	**	Load dynamic content into the #content div
	*/
	$("#content").load( href, {contents: true}, function() {

		/*
		**	Rebind all links to our AJAX calls
		*/
		$("a.mainnav, a.pagelink").unbind("click");
		$("a.mainnav, a.pagelink").click(function(e) {
			AjaxLoad( this.href, e );
			return false;
		} );

		/*
		**	If we're using the dhtml history, strip
		**	and store the GET variables from the URL
		*/
		if( useHist ) {
			tmp = href.split( "?tag=", 2 )
			if( loadURL == false && typeof( dhtmlHistory ) == "object" ) {
				/*
				**	Make sure we don't store same tag twice,
				**	since it's a bit redundant...
				*/
				if( tmp[1] != lastURL ) {
					/*
					**	Make sure it was a real user click, since IE
					**	has a nasty habit of doing things its own way
					*/
					if( typeof( e ) == 'object' && typeof( e.clientX ) == 'number' ) {
						if( e.clientX ) {
							dhtmlHistory.add( tmp[1], tmp[1] );
						}
					}
					lastURL = tmp[1];
				}
			} else {
				loadURL = false;
			}
		}
	} );
}

