/**
 * Ajax helper library. Contains wrappers for jQuery GET and POST functions
 */

/**
 * Wrapper for jQuery get. Works the same way as normal get except that the given callback function is called with the response object as parameter, rather than the response text.
 * The second difference is that the callback function is called only when the operation was a success. (See handleReturnData() for error handling)
 * 
 * @param uriSegments The URI segements where the call is made, in addition to site_url
 * @param callback The callback function
 */

function get(uriSegments, params, callback) {
	
	$.get(site_url + uriSegments, function(data) {

		var responseObject = handleReturnData(data);

		if (responseObject && typeof callback == 'function') {
			callback(responseObject);
		}
	});
}

/**
 * Wrapper for jQuery post. Works the same way as normal post except that the given callback function is called with the response object as parameter, rather than the response text.
 * The second difference is that the callback function is called only when the operation was a success. (See handleReturnData() for error handling).
 * The third diffrence is that all posts are appended with two additional parameters, authentication and current_url.
 * 
 * @param uriSegments The URI segements where the call is made, in addition to site_url
 * @param callback The callback function
 */
function post(uriSegments, params, callback) {
	//Add the authentication data to post
	if (typeof params == 'object') {
		params.authentication = authChecksum
		params.current_url = current_url
	}
	else {
		params = {authentication: authChecksum, current_url: current_url}
	}
	/*$.ajax({
		beforeSend: function(xhr) {
			xhr.setRequestHeader('Accept-Encoding', '');	
		},
		data : params,
		type : 'POST', 
		url : site_url + uriSegments,
		success : function(data) {
			var responseObject = handleReturnData(data);
			if (responseObject && typeof callback == 'function') {
				callback(responseObject);
			}	
		}
	});*/
	$.post(site_url + uriSegments, params, function(data) {
		var responseObject = handleReturnData(data);
		if (responseObject && typeof callback == 'function') {
			callback(responseObject);
		}
	});
}

/**
 * Handles ajax call return data.
 */
function handleReturnData(data) {
	var responseObject = eval("("+data+")");
	if(responseObject.additional == "SessExp") {
		location.reload();	
	}
	if (responseObject.result == 0) {
		//Generic error handling takes place here (showing error messages etc)
		addMessage('error', responseObject.errorMessage, 0, '#systemMessageContainer');
		//TODO it probably should be possible to override this error handling
		//console.log(data.errorMessage);
		return false;
	}
	return responseObject;	 
}

/**
 * Adds a system message on the current page
 */
function addMessage(type, text, delay, where) {
	var temp = new Date;
	var messageId = temp.getMilliseconds();

	var message = '<li id="message'+messageId+'" class="'+type+'""><span class="message">'+text+'<a href="#">x</a></span></li>';

	if($(where+' .systemMessages').length>0) {
		$(where+' .systemMessages').prepend(message);
	}
	else {
		message = '<ul id="systemMessages" class="systemMessages fadeIn">'+message+'</ul>';
		$(where).prepend(message);
	}
	toFade = $("#systemMessages");
	var cmd = "fadeNotice(toFade, "+messageId+")";
	window.setTimeout(cmd, delay);
	
	return messageId;
}

//Fading system message
function fadeNotice(element, msgId) {

	elemWidth = element.width()/2;
	elemHeight = element.height()/2;
	
	center = getWindowCenter();
	
	adjustX = center.x - elemWidth;
	adjustY = center.y + elemHeight;
	
	element.css('left', adjustX);
	element.css('top', 250);
	
	element.fadeIn(1500).delay(3000).fadeOut(1500, function() {
		if(typeof(msgId) !== "undefined") {
			$("#message"+msgId).remove();
		}
	});
	
	element.mouseover(function() {
		$(this).unbind('mouseout');
		$(this).stop(true);
		$(this).mouseout(function() {
			$(this).fadeOut(1000, function() {
				if(typeof(msgId) !== "undefined") {
					$("#message"+msgId).remove();
				}
			});
		});
		
	});

}

/* Get center of the browser window */

function getWindowCenter() {
	if(typeof (window.innerWidth) == 'number') {
		var xRight = window.innerWidth/2;
		var yTop = window.innerHeight/2;
	
	}
	else {
		var xRight = document.documentElement.clientWidth/2;
		var yTop = document.documentElement.clientHeight/2;	
	}
	
	result = {
		x : xRight,
		y : yTop
	}
	
		
	return result;
}


