/*********************************/
/* Set every global JS listeners */
/*********************************/
function getListeners(){
	/* Init: Unbind all existing listeners */
	$('a[rel=ajax]').unbind('click');
	$('a[rel$="confirm"]').unbind('click');
	$('form[rel=ajax]').unbind('submit');

	/* Link listener for facebox */
  $('a[rel=facebox]').facebox();	

	/* Link listener for ajax submission */
	$('a[rel=ajax]').bind('click', function(e){
		/* Feedback the user that we're loading his file */
		$('body').append('<div class="ajaxLoad" id="__loading__">'+C_LOADING+'</div>');
		e.preventDefault(); /* Cancel the default browser */

		/* Hide the dropdown menu */
		$('#more-menu').hide();

		/* Get some link's attributes */
		var linkUrl = $(this).attr('href');
		var linkTitle = $(this).attr('ajax');
		var linkTarget = $(this).attr('target');

		/* Init missing attributes */
		if (linkTarget == '' || linkTarget == null){ linkTarget = '#core'; } /* No special target div, include it in core */
		/* If there is a title attribute, set an anchor */
		if (linkTitle != '' && linkTitle != null){
			setAnchor(linkTitle);
			$.history.add(linkTitle); /* ... and set this page in the history */
		}
		/* Let's go, ajaxize me this link please ! */
		$.ajax({
			type: 'GET',
			url: linkUrl,
			success: function(app){
				/* Request successful, if the result is not empty, append it to the target div */
				if (app != ''){
					if (linkTitle != '' && linkTitle != null){
						disableTab(linkTitle);
					}
					$(linkTarget).empty().append(app);
				}
								
			},
			complete: function(){
				/* Request is complete (success or fail) */
				$('#__loading__').remove(); /* Remove the loading tag */
				getListeners();
			}
		});
	});

	$('a[rel$="confirm"]').bind('click', function(){
		var triggeredElement = $(this);
		$('#ajax-dialog').append('<div id="ajax-dialog-confirm" title="'+C_CONFIRMATION+'">'+$(triggeredElement).attr('confirm')+'</div>');

		var dialogConfirmButton = {};
		dialogConfirmButton[C_YES] = function(){
			$(triggeredElement).attr('rel', 'ajax');
			getListeners();
			$(triggeredElement).trigger('click');
			$(this).dialog('close');
		}

		dialogConfirmButton[C_NO] = function(){
			$(this).dialog('close');
		}

		$('#ajax-dialog-confirm').dialog({
			resizable: false,
			modal: true,
			buttons: dialogConfirmButton,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.1
			}
		});

		return false;
	});

	/* Form listener for ajax submission */
	$('form[rel=ajax]').bind('submit', function(e){
		var formObject = $(this);
    var formAction = $(this).attr('action');
    var target = '#core'; /* Default div to include the result */

     /* Check if an hidden optional parameter named __target__ exist */
    var fieldTarget = $(":hidden[name='__target__']", this).fieldValue();
    if (fieldTarget != null && fieldTarget != ''){ target = fieldTarget; }

    /* Options for preparing the ajax form submission */
    var submitOptions = {
      url: formAction,
      success: function(responseText) {
				$(formObject).clearForm();
				if (responseText != ''){
					/* If request is a success, add the response to the target div */
					$(''+target).empty().append(responseText).hide().fadeIn("normal");
					$('#ajax-apps').css('display', 'none');
				}
				getListeners();
      }
    };

    /* I'm ready, all is ok, submit the form now ! */
    $(this).ajaxSubmit(submitOptions);
    return false; /* Cancel the default browser comportement */
	});
}