var SelfJS = function() {

	this.config = {
		name: 'self',
		dialogDivName: 'dialog',
		openDialog: 0,
		popupOptions : {modal: true,autoOpen: true,zIndex: 10,width: 500,width: 250},
		dialogOptions : {modal: true,autoOpen: false,zIndex: 1000},
		ajaxLoader : '<img class="ajaxLoader" src="web/images/anim_loader.gif" alt="ajax" />'};

}


/**
 * Montrer / Cacher le box message
 * type = info | confirm | error
 */
SelfJS.prototype.showDialogMessage = function( string, type ) {

	$("#dialog").empty();
	$("#dialog").append( string );
	selfJS.showDialog(type);
	
}


/**
 * Montrer / Cacher le box message
 * type = info | confirm | error
 */
SelfJS.prototype.showDialog = function( type ) {

	$("#dialog").dialog('option', 'title', type);
	$("#dialog").dialog('option', 'dialogClass', 'dialog_'+type);
	$("#dialog").dialog("open");
	
}


/**
 * Positionne l'ajax loader
 */
SelfJS.prototype.showAjaxLoader = function( show, id ) {
	var element = $("#" + id);
	if (show){
		element.before(this.config.ajaxLoader);
		element.hide();
	}else{
		element.prev(".ajaxLoader").remove();
		element.show();
	}
}


/**
 * Show popup
 * @param options
 */

SelfJS.prototype.showPopup = function(options) {
	
	$.ajax({
		type: 'POST',
		url: options.url,
		dataType: 'html',
		data: options.data,
		async: false, 
		success:function(data){
			$('body').append( data );
			$('#popup').dialog(jQuery.extend(selfJS.config.popupOptions, options));
			$('#popup').dialog('option', 'buttons', { "Ok": function() { $('#popup').dialog("close"); } });
			$('#popup').bind('dialogclose', function(event, ui) {
				$('#popup').remove();
			});
		}
    });
}


/**
 * Lance une requête ajax en POST,JSON et SYNC
 * @param URL
 * @param data 
 */

SelfJS.prototype.ajaxPost = function(url,data) {
	var result;
	$.ajax({
		type: 'POST',
		url: url,
		dataType: 'json',
		data: data,
		async : false,
		success:function(data){
			result = data;
		}
    });
    return result;
}


