var historyManager = {
	
	analyticTrackCode:null,
	pageTracker:null,
	firstPage:"firstpage.php",
	loaderID:"content-loader",
	request:null,
	
	init:function () {
		try {this.pageTracker = _gat._getTracker(analyticTrackCode);} catch(err) {}
		$.ajaxSetup({error:this.onError});
		SWFAddress.onChange = this.onChange;
	},
	
	onChange:function () {
		historyManager.resetRequest();
		try {onContentClose()}catch(e){}
		var newPath = SWFAddress.getPathNames();
		if (newPath == "") newPath = historyManager.firstPage;
		
		var parameters = '';
		if (SWFAddress.getParameterNames()) {
			for (var i = 0; i < SWFAddress.getParameterNames().length; i++) {
				parameters += '&' + SWFAddress.getParameterNames()[i] + '=' +SWFAddress.getParameter(SWFAddress.getParameterNames()[i]);
			}
		}
		$('#'+historyManager.loaderID).stop();
		$('#'+historyManager.loaderID).animate({opacity:0},500,function () {			
			historyManager.request = $.get (newPath,parameters,function (data) {
																	$('#'+historyManager.loaderID).html (data);
																	try {onContentDisplay()}catch(e){}
																	$('#'+historyManager.loaderID).stop();
																	$('#'+historyManager.loaderID).animate({opacity:1},500);
																	historyManager.request = null;
																	});
		
		
		});													
	},
		
	openPage:function (newPage) {
		this.resetRequest();
		window.location.hash = newPage;
	},
	
	resetRequest:function () {
		if (this.request) {
			this.request.abort();
			request = null;
		}
	},
	
	onError:function (x,e) {
		if(x.status==0){
			$('#'+historyManager.loaderID).html ("Es konnte keine Verbindung hergestllt werden.");
		}else if(x.status==404){
			$('#'+historyManager.loaderID).html ("Die angeforderte Seite konnte nicht gefunden werden.");
		}else if(x.status==500){
			$('#'+historyManager.loaderID).html ("Interne Server Fehler. Bitte versuchen Sie es erneut.");
		}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
		}else if(e=='timeout'){
			$('#'+historyManager.loaderID).html ("Zeitüberschreitung für Ihre Anfrage.");
		}else {
			alert('Unknow Error.\n'+x.responseText);
		}
	}
	
}

$(document).ready (historyManager.init());

