var VgrJS = function() {

	this.onDocReadyCallbacks = new Array();
	
	this.config = {
		name: 'vgr',
		overDivName: 'lock_screen',
		infoDivName: 'info',
		popupInfoDivName: 'popup_info',
		popupAlerteDivName: 'popup_alerte',
		imageDir : 'web/images',
		ajaxLoader : '<img class="ajaxLoader" src="web/images/gif/ajax-loader.gif" alt="ajax" />'};


}


/**
 * Créér la popup
 */

VgrJS.prototype.showPopup= function(url,data,pos) {
	
	$.ajax({
		type: "POST",
		url: url,
		dataType: 'html',
		data: data,
		async: false, 
		success:function(data){
			vgrJS.showOverDiv(true);
			$('body').append( data );
			if (jQuery.browser.msie){
				if (jQuery.browser.version == '6.0'){
					$('#popup').css('top',pos.top-200);
					$('#popup').css('left',$(document).width()/2);
					var posPopup = $('#popup').offset();
					if (posPopup.top < 50){
						$('#popup').css('top',400);
					}
				}
			}
			$('#popup .fermer').click( function(e) {
            	$('#popup').remove();
                vgrJS.showOverDiv(false);
                $('body').css('overflow','auto');
            });
		}
    });
}


/**
 * fermer la popup
 */

VgrJS.prototype.closePopup= function() {
	
	$('#popup').remove();
	vgrJS.showOverDiv(false);
	$('body').css('overflow','auto');

}



/**
 * Afficher / Retirer la div de popup
 */
VgrJS.prototype.showOverDiv = function( show ) {
	if( show ) {
    	var overDiv = $( '#' + this.config.overDivName )
        overDiv.css( 'display', 'block' );
        overDiv.width( $(document).width() );
        overDiv.height( $(document).height() );
        overDiv.fadeTo( "slow", 0.60 );
        $('body select').each (function (e){
            if (!($(this).attr('disabled'))){
                    $(this).addClass('disabledByOverLay');
                    $(this).attr('disabled','disabled');
                    $(this).hide();
            }
        });
                
    }else {
        var overDiv = $( '#' + this.config.overDivName )
        overDiv.css( 'display', 'none' );
        $('.disabledByOverLay').show();
        $('.disabledByOverLay').removeAttr('disabled');
    }
}

       
        

/**
 * Montrer / Cacher le box Info
 */
VgrJS.prototype.showInfoDiv = function( show ) {
	
	if ($("#popup").size()){
		var info = this.config.popupInfoDivName;
	}else{
		var info = this.config.infoDivName;
	}	

	if( show ) {
    	var infoDiv = $( '#' + info )
        infoDiv.removeClass();
		infoDiv.addClass("display");

    }else {
        var infoDiv = $( '#' + info )
        infoDiv.empty();
		infoDiv.removeClass();
		infoDiv.addClass("hidden");
    }
}


/**
 * Ajouter une ligne dans le box info
 */
VgrJS.prototype.addInfoMessage = function( message ) {
	if ($("#popup").size()){
		var info = this.config.popupInfoDivName;
	}else{
		var info = this.config.infoDivName;
	}	
	$('#' + info).append( message + '<br />' );
}


/**
 * Montrer / Fermer le popup alerte
 */
VgrJS.prototype.showAlerteDiv = function( show ) {
	vgrJS.showOverDiv(show);
	if( show ) {
        $('#' + this.config.popupAlerteDivName).show();
    }else {
        $('#' + this.config.popupAlerteDivName + ' .texte').empty();
        $('#' + this.config.popupAlerteDivName).hide();
    }
}


/**
 * Ajouter une ligne dans le popup alerte
 */
VgrJS.prototype.addAlerteMessage = function( message ) {
	$('#' + this.config.popupAlerteDivName + ' .texte').append( message + '<br />' );
}


/**
 * Compléter un champ de type inout avec des 0 selon son maxlength
 */ 
VgrJS.prototype.completionZero = function (value,maxlength,type){

	var l = value.length;
	var zero = '';
	for (var i = l;i<maxlength;i++){
		zero += '0';
	}
	if (type == 'right'){
		return value+zero;
	}else{
		return zero+value;
	}		
}


VgrJS.prototype.verifChamp = function verifChamp(table,colonne,value){

	var result;
	$.ajax({
		type: "POST",
		url: "index.php5?c=ajax&a=_verif_champ",
		dataType: 'json',
		data: "table="+table+"&colonne="+colonne+"&value="+value,
		async: false, 
		error:function(msg, string){
			alert( "Error !: " + string );
		}, 
		success:function(data){
			result = data;
       	}
	});
	return result;
}	





