var ajax = {
	launch: function(url, type, data) {
		if (type == undefined) type = 'GET';
		if (data == undefined) { if (type == 'GET') data = 'control'; else data = ''; }
		if (type == 'POST') url += '?control';
		$('html, body').css('cursor','progress');
		$.ajax({
			url: url,
			type: type,
			data: data,
			success:function(jsonresponse){
				ajax.parseAjaxResponse(jsonresponse); },
			error: function () { if (window.console && window.console.error){ console.error(arguments); } }
		});
	},

	parseAjaxResponse: function(jsonresponse) {
		$('html, body').css('cursor','auto');
		try {
			var arrayResponse = eval('(' + jsonresponse + ')');
			if (arrayResponse['html'] != null && arrayResponse['html'] != '') {
				$('body').append(arrayResponse['html']);
			}
			if (arrayResponse['js'] != null && arrayResponse['js'] != '') {
				if(jQuery.isArray( arrayResponse['js'] )) {
					for (var i=0; i < arrayResponse['js'].length; i++) {
						eval('(' + arrayResponse['js'][i] + ')');
					}
				}  else eval('(' + arrayResponse['js'] + ')');
			}
		} catch(err) {
			alert(err);
		}
	},

	dialog: function( divError, fctOk) {
		var id = "dialog"+divError.html().length;
		var newDialog = $('#dialog').clone().appendTo('body').attr('id',id).removeClass('hide');
		var titleDialog = _VAR_SITE_NAME_;
		if($('div.dialogTitle', divError).length>0 && $('div.dialogTitle', divError).html()!='') {
			titleDialog = $('div.dialogTitle', divError).html();
		}
		if($('div.dialogMessage', divError).length>0) $('div.message',newDialog).html( $('div.dialogMessage', divError).html() );
		else  $('div.message',newDialog).html( divErrordivError.html() );

		if($('div.dialogType',divError).length>0 && $('div.dialogType',divError).html()!="") { //On cherche le type de la dialog
			var altDiv = $('div.dialogType',divError).html();
			var flag = false;
			$('div.btn',newDialog).each( function() {
				if( $(this).attr('alt') != altDiv ) {
					$(this).remove(); //On vire les boutons qui ne correspondent pas au bon type
				} else { flag = true; }
			});
			if(flag==false) {
				var newDivBtn = '<div class="btn"><div class="submit">'+altDiv+'</div></div>';
				newDialog.append( newDivBtn );
			}
		} else { $('div.btn', newDialog).remove(); }

		$('div.btn .cancel', newDialog).click(function() { newDialog.dialog("destroy").remove(); });
		$('div.btn .submit', newDialog).click(function() {
			if (fctOk != 'undefined' && fctOk != undefined && fctOk != '') {
				try { eval('(' + fctOk + ')'); }
				catch(err) { alert(err); }
			}
			newDialog.dialog("destroy").remove();
		});

		var tooltipZIndex = parseInt($('body').css('z-index'));
		if( isNaN(tooltipZIndex) || tooltipZIndex==0 ) tooltipZIndex = 1000;

		newDialog.dialog({
			title: titleDialog,//$('#'+id+'.dialogMessage .dialogTitle').html()
			modal: true,
			zIndex: tooltipZIndex,
			draggable: false,
			closeOnEscape: false,
			open: function () { 
				$('.ui-dialog-titlebar-close:last').remove();
			}
		});
	},
	
	/*dialog: function( divError, fct1, fct2) {
		var id = "dialog"+divError.html().length;
		var newDialog = $('#dialog').clone().appendTo('body').attr('id',id)
		.removeClass('hidden')
		.find('.message').html( $('div.dialogMessage', divError).html() ).end();

		alert(divError.html());


		$(newDialog).find('.btnOk').click(function() {
			if (fct1 != 'undefined' && fct1 != '') {
				try { eval('(' + fct1 + ')'); }
				catch(err) { alert(err); }
			}
			$('#'+id+'.dialog').dialog("destroy").remove();
		}).end();


		$(newDialog).find('.btnYesNo').click(function() {
			if (fct2 != 'undefined' && fct2 != '') {
				try { eval('(' + fct2 + ')'); }
				catch(err) { alert(err); }
			}
			$('#'+id+'.dialog').dialog("destroy").remove();
		}).end();

		newDialog.dialog({
			title: $('div.dialogTitle', divError).html(),//$('#'+id+'.dialogMessage .dialogTitle').html()
			modal: true,
			zIndex: 10000 + id,
			draggable: false,
			closeOnEscape: false,
			open: function () { $('.ui-dialog-titlebar-close:last').remove(); }
		});
	},*/
	
	error: function() {
		var id = $('.dialogError').length;
		$('#actionDialogPreloads .dialogError').clone().appendTo('body').attr('id',id)
		.find('.pButton').click(function() {
			$('#'+id+'.dialogError').dialog("destroy").remove();
		}).end()
		.dialog({
			title: '',
			modal: true,
			zIndex: 99999,
			draggable: false,
			closeOnEscape: false,
			open: function () { $('.ui-dialog-titlebar:last').remove();}
		});
	},

	confirm: function (message, fctYes, fctNo) {
		var id = $('.dialogConfirm').length;
		$('#actionDialogPreloads .dialogConfirm').clone().appendTo('body').attr('id',id)
		.find('.message').html(message).end()
		.find('.button.yes').click(function() {
			if (fctYes != 'undefined' && fctYes != '')
			{
				try { eval('(' + fctYes + ')'); }
				catch(err) { alert(err); }
			}
			$('#'+id+'.dialogConfirm').dialog("destroy").remove();
		}).end()
		.find('.button.no').click(function() {
			if (fctNo != 'undefined' && fctNo != '') {
				try { eval('(' + fctNo + ')'); }
				catch(err) { alert(err); }
			}
			$('#'+id+'.dialogConfirm').dialog("destroy").remove();
		}).end()
		.dialog({
            title:  $('#'+id+'.dialogConfirm .dialogTitle').html(),
			modal: true,
			zIndex: 10002,
			draggable: false,
			closeOnEscape: false
		});
	},

	forward: function(url) {
		if(url == 'undefined' || url == undefined ) window.refresh() ;
		else window.location.href = url;
	},

	replaceHTML: function(id,html) {
		$('#'+id).replaceWith(String(html));
	}
};

$(document).ready( function(){
	$('a.ajax').expire('click').livequery('click',function(e) {
		if (!$(this).is('.disabled')) ajax.launch($(this).attr('href'));
		( e && e.preventDefault ) ? e.preventDefault() : window.event.returnValue = false;
	});
});