var historyManager = {
	
	analyticTrackCode:"UA-17953709-1",
	pageTracker:null,
	firstPage:"start.php",
	loaderID:"content-loader",
	request:null,
	isGalleryOpen:false,
	
	init:function () {
		try {historyManager.pageTracker = _gat._getTracker(historyManager.analyticTrackCode);} catch(e){};
		//$.ajaxSetup({error:this.onError});
		SWFAddress.onChange = this.onChange;
	},
	
	onChange:function () {
		
		historyManager.resetRequest();
		try {onContentClose()}catch(e){}
		var newPath = SWFAddress.getPathNames();
		
		if (newPath != booking.bookungPageID && newPath != booking.loungePageID) {
		
			/** letze Seite == booking? **/
			if (booking.isOpen) 
				booking.destroy();
		
			if (newPath == "") newPath = historyManager.firstPage;
			
			/*
			if (newPath != 'start.php') {
				$('#extra-bg').css('display','none');
				$('#c-frame').css('display','block');	
			}else{
				$('#extra-bg').css('display','block');
				$('#c-frame').css('display','none');	
			}*/
				
			
			try {historyManager.pageTracker._trackPageview("/"+newPath);}catch(e){};
			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).html ('<center><img style="margin-top:150px;margin-bottom:150px;" src="images/loader.gif" /></center>');
			historyManager.request = $.get (newPath,parameters,function (data) {
																	$('#'+historyManager.loaderID).html (data);
																	try {onContentDisplay()}catch(e){}
																	historyManager.request = null;
																	
																});	
		}else{
			
			if (newPath == booking.bookungPageID){
				booking.destroy();
				booking.init();
			}
			
			if (newPath == booking.loungePageID && booking.isOpen){
				booking.showLounge(SWFAddress.getParameter('lounge'),SWFAddress.getParameter('event'));
			}else{
				window.location.hash = "booking.php";
			}
			
				
		}
		
	},
		
	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);
		}
	},
	
	
	openGallery:function () {
		
		$('#c-frame').stop().animate({'left':'-800px','marginLeft':'0px'},2000,"easeInOutQuad");
		$('#image-bg').stop().fadeOut('fast');

		
		historyManager.isGalleryOpen = true;
		},
	
	
	closeGallery:function () {
		
		$('#c-frame').stop().animate({'left':'50%','marginLeft':'-550px'},2000,"easeInOutQuad");
		$('#image-bg').stop().fadeIn('fast');

		historyManager.isGalleryOpen = false;
		}
	}

$(document).ready (function () {	
		historyManager.init();
		SWFAddress.addEventListener(SWFAddressEvent.INIT, menuManager.init);
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, menuManager.onChange);
		$('#flow-warpp').stop().fadeOut();
		
		
		var preload = new Image();
		preload.onload = function () {
			$('#image-bg-main')
				.fadeTo(0,0)
				.css('display','block')
				.fadeTo(2000,1);
			
				scaleBgImage();
		}
		preload.src = $('#image-bg-main').attr('src');

		
	});

