/************************************************
* Commons Functions
* Author: Francesco Donzello
* Special thanks to: JQuery
************************************************/

var bLogin;
var dLoading;
var dMessages;
var OK = 1;
var ERR = 0;


jQuery.parseJSON = function(a)
{
	return a;
}
$.ajaxSetup({
	dataType: 'json',
	timeout: 15000,
	global: true,
	beforeSend:function(){
		lockUI()
	},
	complete:function(msg){
		unlockUI()
	}
})
jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
	if (options.type === 'post' || options.type === 'POST')
	{
	    var prefix = '/';
		options.url = prefix + options.url;		
	}
});
function jNotify(type, message)
{
	alert(message);
}
function lockUI()
{
	$.blockUI({
		message: "Attendere...",
		fadeIn: 0,
		fadeOut: 0,
		showOverlay: true,
		bindEvents: false,
	    // styles for the overlay 
	    overlayCSS:  { 
	        backgroundColor: 'transparent'
	    },
	});
}
function unlockUI()
{
	$.unblockUI();
}
function checkJson(a)
{
	/** deprecated */
	return true;
}
function switchPage(id)
{
	window.location = id == 1 ? self.location : 'index.php';
}
function loginLock()
{
	bLogin.attr("disabled", "disabled");
	showLoading();
}
function loginUnlock()
{
	bLogin.attr("disabled", 0);
	alert(bLogin.attr('disabled'))
	hideLoading();
}
function showLoading()
{
	dLoading.show();
}
function hideLoading()
{
	dLoading.hide();
}
function showMessage(a)
{
	dMessage.html(a).show();
}
function hideMessage()
{
	dMessage.html('').hide();
}
function changeLeague(leagueid)
{
	if ( !leagueid ) return false;
	showLoading();
	hideMessage();
	$.ajax({
		type: 'POST',
		url: "auth/changeLeague",
		data: 'id=' + leagueid,
		success: function(msg)
		{
			var a = jQuery.parseJSON(msg);
			if ( a.result )
			{
				hideLoading();
				showMessage('Aggiornamento pagina...')
				window.location.reload()
			}
			else
			{
				showMessage(a.error);
				hideLoading();
			}
		},
		error: function()
		{
			alert('Problemi nell\'elaborazione della richiesta. Riprovare...');
			hideLoading();
		}
	});
}

$(document).ready(function(){

	$.blockUI.defaults.css = {};

	bLogin = $("div#login form#login input#submit");
	dLoading = $("div#login div#loading");
	dMessage = $("div#login div#messages");

	$("#logout").click(function(){
	
		showLoading();
		
		$.ajax({  
			type: "POST",
			url: "auth/logout",
			data: 'do=logout',
			success: function(msg)
			{
				var a = jQuery.parseJSON(msg);
				if ( a.result )
				{
					hideLoading();
					showMessage('Aggiornamento pagina..');
					window.location = self.location
				}
				else
				{
					alert('Errore. Riprovare');
					hideLoading();
				}
			}
		});
	
	});

	$("form#login").submit(function(){
		var str = $(this).serialize();

		$.ajax({
			type: "POST",
			url: "auth/login",
			data: str,
			success: function(msg)
			{
				var a = jQuery.parseJSON(msg);
				if( a.result )
				{
					hideLoading();
					showMessage('Attendere..')
					switchPage(1);
				}
				else
				{
					showMessage(a.error);
				}
			}
		});

		return false;

	});

});


