var listenOnHashChange = true;


$(document).ready(function() {
	
	var loc = window.location;
	
	listenOnHashChange = false;
	if( loc.hash != "" && loc.hash != "#/" ) {
		
		var path = loc.hash.substring( 1 );
		var target = loc.protocol + '//' +  loc.host + '/' + path;
		
		switch( loc.hash ){
		
			case '#/contact': 
			case '#/imprint': 
			case '#/press': 
			case '#/datenschutz': 
			case '#/disclaimer': 
			case '#/privacy':
			case '#/agb':
				showPopup( target, '', path );
				break;
			default:
				showContent( target, '', path );
				break;
		}
	}
	else if( loc.pathname != "" && loc.pathname != "/" && loc.pathname != "/frontend_dev.php" ){
		var pathname = loc.pathname; 
		loc.href = loc.protocol + '//' +  loc.host + '/#' + loc.pathname;
	}
	
	$.history.init( function( hash ){
		if( listenOnHashChange ) {
			showContent( hash, "", hash );
		}
    },
    { unescape: ",/" });
	
	listenOnHashChange = true;
	
} );


function showIFramePopup( url ){
	
	$.ajax( { 
		type: 'GET', 
		dataType: 'html', 
		success: function(data, textStatus){
			$('#popupContainer').html(data);
			$('#popupContainer').show();
			$('#iFramePopup').attr('src',url);
			
			s.pageName = "iFramePopup: Jobs";
	    	s.t();
			
		},
		error: function( data, textStatus ) {
			console.log( error ); //@TODO add error handling
		},
		url: '/home/iFramePopup' } );
}

function showContent( url, title, path ) {
	if( !flashLoaded ) {
		listenOnHashChange = false;
		window.location.hash = path;
		document.title = title;
		
		$.ajax( { 
			type: 'GET', 
			dataType: 'html', 
			success: function(data, textStatus){
				$('#content').html(data);
				$('#content').show();
				listenOnHashChange = true;
				
				s.pageName = title;
		    	s.t();
				
			},
			error: function( data, textStatus ) {
				console.log( error ); //@TODO add error handling
			},
			url: url } );
	}
}

function showPopup( url, title, path ){
	
	listenOnHashChange = false;
	window.location.hash = path;
	document.title = title;
	
	$.ajax( { 
		type: 'GET', 
		dataType: 'html', 
		success: function(data, textStatus){
			$('#popupContainer').html(data);
			$('#popupContainer').show();
			
			s.pageName = title;
	    	s.t();
			
			//workaround for scrollpane inside popups
			if( path == "/datenschutz" ) {
				$(".scrollPane").jScrollPane({
					hijackInternalLinks: true
				});
			}
			
			listenOnHashChange = true;
		},
		error: function( data, textStatus ) {
			console.log( error ); //@TODO add error handling
		},
		url: url } );
}

function closePopup() {
	$('#popupContainer').hide();
	$('.orangePopup').hide();
};
