
var currentHash;
var isLoading = false;

jQuery(document).ready(function($) {

//	$(document.createElement("div")).attr('id','window_mask').appendTo(document.body);
//	$(document.createElement("div")).attr('id','popup').appendTo(document.body);
	$('#window_mask').click( function() {
		if ( ! isLoading )
		{
			$('#popup').hide();
			$('#window_mask').hide();
		}
	});

	loadUser();

    $.history.init(function(url) {
            getView(url == "" ? "wiki/home" : url);
        },{unescape:"/,:_"});

    $('a').live('click', function(e) {
    	var url = $(this).attr('href');
    	if ( url.match(/^#/) )
    	{
    		url = url.replace(/^#/, '');
    		$.history.load(url);
    		$('html,body').animate({scrollTop:0}, 500);

    		return false;
    	}
    });
    
	$.ajax({
		url: 'http://www.belteshazzar.com/cron.php5',
		dataType: 'text',
		timeout: 0.1,
		error: function(request,text,error) {
		},
		success: function(data,text,request) {
		}
	});
});

function getView(hash)
{
	if ( isLoading ) return;
	isLoading = true;

_gaq.push(['_trackEvent', 'ajax', 'getView', hash]);

//$('.hide-me').css('display','none');
	
	if ( ! $('#window_mask').is(":visible") )
	{
$('.hide-me').text('');

		$('#window_mask').show();
		$("#popup").html("Loading ...");
		$('#popup').fadeIn('slow');
		$('html,body').animate({scrollTop:0}, 500);
	}
	currentHash = hash;
	if ( hash.match(/^!/) )
	{
		hash = hash.replace(/^!/, '');
	}
	var params = hash.split("/");
	var url = '';
	if ( params.length == 2 )
	{
		url = 'engine.php5?view='+params[0]+'&id='+params[1];
	}
	else if ( params.length == 1 )
	{
		url = 'engine.php5?view='+params[0];
	}
	if ( url=='' )
	{
		$("#gb-main-layer").html(hash);
	}
	else
	{
		$.ajax({
			url: url,
			type: 'POST',
			dataType: 'html',
			error: function(request,text,error) {
				$("#gb-main-layer").html("Sorry, there has been an error loading the form.");
	   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'getView-error', hash]);
			},
			success: function(data,text,request) {
				$("#gb-main-layer").html(data);
				$('#window_mask').hide();
				$("#popup").hide();
	   			isLoading = false;
_gaq.push(['_trackPageview', hash]);
_gaq.push(['_trackEvent', 'ajax', 'getView-success', hash]);
			}
		});
	}
}

function loadUser()
{
	$.ajax({
		url: 'engine.php5?view=user',
		dataType: 'html',
		type: 'POST',
		error: function(request,text,error) {
			$("#gb-user-layer").html("Sorry, there has been an error loading the form.");
_gaq.push(['_trackEvent', 'ajax', 'loadUser-error']);
		},
		success: function(data,text,request) {
			$("#gb-user-layer").html(data);
_gaq.push(['_trackEvent', 'ajax', 'loadUser-success']);
		}
	});
}

function scrollTo( el )
{
	$('html,body').animate({scrollTop: $(el).offset().top}, 500);
}

function doAction( data )
{
	if ( isLoading ) return;
	isLoading = true;
	if ( ! $('#window_mask').is(":visible") )
	{
$('.hide-me').text('');

		$('#window_mask').show();
		$("#popup").html("Loading ...");
		$('#popup').fadeIn('slow');
		$('html,body').animate({scrollTop:0}, 500);
	}
   	$.ajax({
		url: 'engine.php5',
		type: 'POST',
		data: data,
		dataType: 'json',
		error: function(request,text,error) {
   			$("#gb-main-layer").html("error: "+text+", error: "+error);
   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'doAction-error']);
		},
		success: function(result,text,request) {
			try
			{
				if ( result.action=='refresh' )
				{
					// set isLoading so that the view will load
					isLoading = false;
					if (data.action=="userlogin" || data.action=="userlogout" || data.action=="fb_userlogin" || data.action=="gfc_userlogin" )
					{
_gaq.push(['_trackEvent', 'ajax', data.action]);
						loadUser();
					}
					getView(currentHash);
_gaq.push(['_trackEvent', 'ajax', 'doAction-refresh']);
				}
				else if ( result.action=="error" )
				{
					$('#popup').html("An error occured: "+result.msg);
		   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'doAction-error']);
				}
				else if ( result.action=="success" )
				{
					//history.back();
					$('#popup').html(result.msg);
		   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'doAction-success']);
				}
				else if ( result.action=="getView" )
				{
					getViewAction( result.data );
_gaq.push(['_trackEvent', 'ajax', 'doAction-getView']);
				}
				else if ( result.action=="forward" )
				{
					isLoading = false;
					$.history.load(result.data);
_gaq.push(['_trackEvent', 'ajax', 'doAction-forward']);
				}
				else
				{
					$('#popup').html("unknown action: "+result.action);
		   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'doAction-error-unknown']);
				}
			}
			catch ( ex )
			{
				$('#popup').html("Oops an error occured");
	   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'doAction-error-exception']);
			}
			
		}
   	});
}

function getViewAction( data )
{
   	$.ajax({
		url: 'engine.php5',
		type: 'POST',
		data: data,
		dataType: 'html',
		error: function(request,text,error) {
   			$("#popup").html("Sorry, there has been an error loading the view.");
   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'getViewAction-error']);
   		},
   		success: function(html,text,request) {
			$("#gb-main-layer").html(html);
			$('#popup').hide();
			$('#window_mask').hide();					
   			isLoading = false;
_gaq.push(['_trackEvent', 'ajax', 'getViewAction-success']);
		}
   	});
   	
}

