/**
* Contains functions for navigating ingame (Loading content etc)
* @package KDGLib
*/


/**
* Loads the content of a given URL into the #screen
* @param url URL
*/
function load(url) {
	$("#screen").empty().html('<div id="load" class="loadingIndicator">Laden...</div>');
	setTimeout(function(){ $("#load").fadeIn('slow'); }, 500);
	$("#screen").load(url, function(response, status, xhr) {
	  if (status == "error") {
	    alert("Internal Game Error: " + xhr.status);
	    setTimeout(function(){ $("#load").fadeOut('fast'); }, 750);
	  }
	});
}

/**
* Loads the given data into the #screen
* @param data data
*/
function loadData(data) {
	$("#screen").empty().html('<div id="load" class="loadingIndicator">Laden...</div>');
	setTimeout(function(){ $("#load").fadeIn('slow'); }, 500);
	$("#screen").empty().append(data);
}

/**
* Loads the content of a given URL into a DIV
* @param url URL
* @param target jQuery target string (like #screen)
*/
function loadInto(url, target) {
	$(target).empty().html('<div id="load" class="loadingIndicator">Laden...</div>');
	setTimeout(function(){ $("#load").fadeIn('slow'); }, 500);
	$(target).load(url, function(response, status, xhr) {
	  if (status == "error") {
	    alert("Internal Game Error: " + xhr.status);
	    setTimeout(function(){ $("#load").fadeOut('fast'); }, 750);
	  }
	});
}

/**
* Displays an overlay with the content of url
* @param url URL
*/
function loadOverlay(url) {
	$("#overlayinner").empty().html('<div id="load" class="loadingIndicator">Laden...</div>');
	setTimeout(function(){ $("#load").fadeIn('slow'); }, 500);
	$("#overlayinner").load(url, function(response, status, xhr) {
	  if (status == "error") {
	    alert("Internal Game Error: " + xhr.status);
	    setTimeout(function(){ $("#load").fadeOut('fast'); }, 750);
	  }
	});
}

/**
* Updates the Fields given in the json result of jsonapi.php
* @param request Identifier to jsonapi.php
*/
function updateFields(request) {
	$.getJSON('jsonapi.php?rq='+request, function(data) {
		$.each(data, function(key, val) {
			$("#"+key).empty().html(val);
		});
	}, function(data, text, j) {});
}

/**
* Shows or hides an indicator for loading progress
* @param bool true (show) or false (hide)
*/
function showLoadingIndicator(bool) {
	if (bool) {
		$("#loadoverlay").empty().html('<div id="load" class="loadingIndicator">&nbsp;</div>');
		$("#load").fadeIn('slow');
		return false;
	} else {
		$("#load").fadeOut('slow');
		$("#loadoverlay").delay(150).empty();
		return false;
	}
	return true;
}

/**
* Posts a Form and loads result into the screen
* @param url Target-URL
* @param string jQuery identifier (#formid)
* @returns boolean always false to prevent browser from loading
*/
function postForm(targeturl, identifier) {
	setTimeout(function(){ $("#overlayload").fadeIn('slow'); }, 500);
	$.post(targeturl,
		$(identifier).serialize(),
		function(data) {
			loadData(data);
			setTimeout(function(){ $("#overlayload").fadeOut('fast'); }, 750);
		}
	);
	return false;
}

