// plugin jQuery
$.fn.initClass = function( sNewClass) {
	return this.each(function() {
		$(this).removeClass().addClass( sNewClass);
	});
};
$.fn.replaceClass = function( sOldClass, sNewClass) {
	return this.each(function() {
		$(this).removeClass( sOldClass).addClass( sNewClass);
	});
};
$.fn.inverseClass = function( sClass, sClassBis) {
	return this.each(function() {
		$(this).hasClass( sClass) ?
			$(this).replaceClass( sClass, sClassBis) :
				$(this).replaceClass( sClassBis, sClass);
	});
};
$.fn.check = function(mode) {
	return this.each(function() {
		this.checked = (mode == 'toggle') ? !this.checked : $is_true(mode, true);
	});
};
$.fn.visible = function() {
	return this.each(function() {
		$(this).css({ visibility: 'visible' });
	});
};
$.fn.hidden = function() {
	return this.each(function() {
		$(this).css({ visibility: 'hidden' });
	});
};

$.fn.overlabel = function( options) {
// pb avec la verif de form, si erreur ne pas réafficher l'overlabel
    var o = $.extend( {
        label_class: 'overlabel-defaut'
    }, options );

    return this.each(function(){
        var label = $(this),
			id = this.htmlFor || label.attr('for'),
			sId = id.replace(/\:/, '\\:');

		function overFocus( oL) {
			 oL.hide();
		}
		function overBlur(jqO) {
			if( jqO.val() == '') {
				label.css({ position: 'absolute'}).show();
			}
		}

		// si title on change le label
		if( $('#' + sId).attr('title') != '') {
			label.text( $('#' + sId).attr('title'));
		}
		label.addClass(o.label_class);

		$('#' + sId)
			.focus( function() {
				overFocus( label);
				$(this).stopTime( 'checkEmpty');
			})
			.blur( function() {
				overBlur( $(this));
				$(this).everyTime(500, 'checkEmpty', function() {
					if( this.value == '') {
						overBlur( $(this));
					} else {
						overFocus( label);
					}
				});
			})
			.trigger('focus').trigger('blur');
	});
};

$.fn.contenuAjax = function( options) {
	// Options
    var o = $.extend( {
		url: '',
		type: 'get',
		data: '',
		dataType: 'html',
		place: 'append',
		loadclass: 'ajaxloading_small',
		timeout : 0
    }, options );

	return this.each(function(){
		var t = $(this);

		$.ajax({
			url: o.url,
			type: o.type,
			data: o.data,
			dataType: o.dataType,
			beforeSend : function() {
				t.addClass(o.loadclass);
			},
			error: function(oXhr, sStatus, errorThrown) {
				//alert('Erreur');
			},
			success: function(data) {
				switch(o.place) {
					case 'after': t.after(data); break;
					case 'append': t.append(data); break;
					case 'appendTo': t.appendTo(data); break;
					case 'before': t.before(data); break;
					case 'insertAfter': t.insertAfter(data); break;
					case 'insertBefore': t.insertBefore(data); break;
					case 'prepend': t.prepend(data); break;
					case 'prependTo': t.prependTo(data); break;
					case 'html': t.html(data); break;
				}
			},
			complete: function() {
				t.removeClass(o.loadclass);
			}
		});
	});
};

// langue
function _lg(s) {
	return (!$defined(i18n) && i18n[s]) ? i18n[s] : s;
}
function _lgNombre(n) {
	var o = '';
	n += '';

	for ( i = n.length; i > 3; i -= 3) {
		o = i18n.thousands_sep + n.slice(i - 3, i) + o;
	}
	return ( n.slice(0, i) + o );
}
function sprintf(s) {
	var bits = s.split('%'),
		out = bits[0],
		re = /^([ds])(.*)$/;

	for (var i = 1; i < bits.length; i++) {
		var p = re.exec(bits[i]);
		if (!p || arguments[i] === null) continue;
		if (p[1] == 'd') {
			out += parseInt(arguments[i], 10);
		} else if (p[1] == 's') {
			out += arguments[i];
		}
		out += p[2];
	}
	return out;
}
function pluriel(s, p, n) {
	return (n != 1) ? p : s;
}
//ex: sprintf(pluriel( _lg("%s sungulier", _lg('%s pluriels"), iNb), _lgNombre(iNb))

// "Constante" prédéfinie
function constante( sCode, aParam) {
	switch( sCode) {
		case 'ajaxError' : return _lg( 'Une erreur est survenue :\n\nRafraichissez la page et essayer à nouveau.\nSi le problème persiste, contactez le webmaster.');
		case 'ajaxIdentError' : return _lg( 'Votre session est terminée :\n\nRafraichissez la page et essayer à nouveau.\nSi le problème persiste, contactez le webmaster.');
		case 'formError' : return sprintf(
				pluriel(
					_lg( 'Erreur de saisie : %d erreur rencontrée lors de la validation du formulaire.'),
					_lg( 'Erreur de saisie : %d erreurs rencontrées lors de la validation du formulaire.'),
					aParam
				),
				_lgNombre( aParam)
			);
		default: return '[Erreur code: "' + sCode + '" inconnu.]';
	}
}

// Encode
function js_encode( s) {
	return s.replace(/\\/g, '\\\\').replace(/"/g, '\&#34;').replace(/\'/g, '\&#39');
}
function url_encode( s) {
	return encodeURIComponent( s);
}
function html_encode( s) {
	return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
function html_decode( s) {
	return s.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
}
function isHtml_encode( s) {
	return (s.search(/&amp;/g) != -1 || s.search(/&lt;/g) != -1 || s.search(/&gt;/g) != -1);
}
function html_decode_if_encode() {
	return isHtml_encode(s) ? html_decode(s) : s;
}

// Tronque la chaine passée en paramètre
function tronquer( s, iMax) {
	s = $.trim( s);
	return ( s.length > iMax ) ? s.substr( 0, iMax / 2) + '[\u2026]' + Right(s, iMax / 2) : s;
}
// coupe
function couper( s, iMax) {
	s = $.trim( s);
	return ( s.length > iMax ) ? Left(s, iMax) + '[\u2026]' : s;
}

function Left( s, n){
	if (n <= 0) {
	    return '';
	} else if (n > String(s).length){
	    return s;
	} else {
	    return String(s).substring(0, n);
	}
}
function Right( s, n){
    if (n <= 0) {
       return '';
    } else if (n > String(s).length) {
       return s;
    } else {
       var iLen = String(s).length;
       return String(s).substring(iLen, iLen - n);
    }
}

// Date
function date2timestamp( sFormat, value){
	var sPosD = sFormat.indexOf('d'),
		sPosM = sFormat.indexOf('m'),
		sPosY = sFormat.indexOf('y');
	var jour = value.substring( sPosD, sPosD + 2),
		mois = value.substring( sPosM, sPosM + 2),
		annee = value.substring( sPosY, sPosY + 4);
	return new Date( annee, mois, jour).getTime();
}

// syncho avec PHP
function format_html_id( s, sReplace) {
	sReplace = $defined(sReplace) ? sReplace : '';
	return s.replace(/^[^a-z_]+/i, sReplace).replace(/\W+/, sReplace);
}

// @see d_box_alert en php
function d_box_alert( options) {
	var o = $.extend({
		code: 'attention',
		text: '',
		id: 'box_alert'
	}, options);

	switch( o.code) {
		case 'attention': 			sCode = ' help-attention'; 		break;
		case 'information': 		sCode = ' help-information'; 	break;
		case 'critique': 			sCode = ' help-critique'; 		break;
		case 'interrogation': 		sCode = ' help-interrogation'; 	break;
		case 'formulaire-echec':	sCode = ' help-form-echec'; 	break;
		case 'formulaire-ok': 		sCode = ' help-form-ok'; 		break;
		default: sCode = '';
	}

	return '<div id="' + o.id + '" class="helpBoxInfos-ON ' + sCode + '">' + o.text + '</div>';
}

function alert_box( options) {
	var o = $.extend({
		 titre: '',
		 text: ''
	}, options);

	$('#dialog_alertBox').remove();
	$('body').append('<div id="dialog_alertBox" title="' + o.titre + '">' + o.text + '</div>');

	$('#dialog_alertBox').dialog({
		buttons: { "Ok": function() { $(this).dialog('close'); } },
		resizable: true,
		height: $('#dialog_alertBox').width(500).height() + 100,
		width: 500,
		modal: true,
		overlay: {
			opacity: 0.5,
			background: '#000'
		}
	});
}

// Events
function getEvent(e) { return e || window.event; }
function getSrcElement(e) { return	e.target || e.srcElement; }

function $redirect(sUrl, oWindow) {
	if ( !$defined(oWindow)) oWindow = window;
	if ( !$defined(sUrl) || sUrl == '' ) sUrl = oWindow.location;
	oWindow.location = sUrl;
}

function $defined(o) { return (o != undefined); }
function $is_true(t, v) { return $defined(t) ? t : v; }

function $type(o){
	if (!$defined(o)) return false; // non defini
	if (o.htmlElement) return 'element';
	var type = typeof o; // 'object', 'string', 'number', 'boolean', 'function'
	if (type == 'object' && o.nodeName){ // noeud
		switch(o.nodeType){
			case 1: return 'element'; // element
			case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
		}
	}
	if (type == 'object' || type == 'function'){
		switch(o.constructor){
			case Array: return 'array'; 	// tableau
			case RegExp: return 'regexp';	// expression
			//case Class: return 'class';		// class
		}
		if (typeof o.length == 'number'){
			if (o.item) return 'collection'; // childNodes, getElementsByTagName...
			if (o.callee) return 'arguments';// object argument
		}
	}
	return type;
}

function $id( s) {
	return (s) ? document.getElementById(s) : null;
}

function $insertCSS(url, sMedia) {
	$('head').append('<link rel="stylesheet" href="' + url + '" type="text/css" media="' + (sMedia || 'screen') + '" />');
}
function $insertScript(url) {
	$('head').append('<script src="' + url + '" type="text/javascript"></script>');
}

// tableau
function ArrayDedoublonne(aOrigine) {
	var bDoublon, j, k = 0, aTab = [];
	for ( var i = 0, length = aOrigine.length; i < length; i++ ) {
		bDoublon = false;
		for(j = 0; j < k && !bDoublon; j++) {
			if(aOrigine[i] === aTab[j]) {
				bDoublon = true;
			}
		}
		if(!bDoublon) {
			aTab[k++] = aOrigine[i];
		}
	}
	return aTab;
}

// recherche dichotomique si déjà trié sinon -> $.inArray
// bType (true = entier)
function ArrayRecherche_dicho(aOrigine, valeur, bType) {
	var trouve = -1, valTab, milieu = 0,
		debut = 0, // debut du tableau
		fin = aOrigine.length; // fin du tableau

	valeur = bType ? parseFloat(valeur) : valeur.toString();
	if(fin > 0) {
		while (trouve == -1) {
			milieu = Math.floor((debut + fin ) / 2);
			valTab = bType ? parseFloat(aOrigine[milieu]) : aOrigine[milieu].toString();
			if (valTab === valeur) {
				trouve = milieu;
			} else  {
				if (fin - debut <= 1) break;
				else {
				  if (valTab > valeur) fin = milieu;
				  else debut = milieu;
				}
			}
		}
	}
	return trouve;
}
// pour trier des nombres (croissant) : tab.sort(compare_num)
// tab.sort() (croissant) pour des chaines
function compare_num(a, b) { return a - b; }

// popup
var WindowPopupReference = null;
function popup(sURL, sNom, sOption, iW, iH, isCenter) {
//if(WindowPopupReference == null || WindowPopupReference.closed) {
	if (sOption == "all=yes") {
		sOption = 'directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1';
	}
	sOption += ((sOption != '') ? ',' : '') + 'width=' + iW + ',height=' + iH;
	if (window.screen && isCenter) {
		sOption += ',left=' + ((screen.width - iW) / 2) + ',top=' + ((screen.height - iH) / 2);
	}
	WindowPopupReference = window.open(sURL, sNom, sOption);
	WindowPopupReference.focus();
/*}
else
	WindowPopupReference.focus();*/
}

// DEBUG
function debug(text) {
	if (!window.top.dW || window.top.dW.closed) {
		showDebug();
	}
	window.top.dW.document.write(text + '\n');
}
function showDebug() {
	window.top.dW = window.open('', 'Debug', 'left=0,top=0,width=300,height=700,scrollbars=yes,status=yes,resizable=yes');
	window.top.dW.opener = self;
	window.top.dW.document.open();
	window.top.dW.document.write('<html><head><title>Debug</title></head><body><div align="right" onclick="if(wp = window.opener){wp.hideDebug();wp.showDebug();}">[ Reset ]</div><pre>\n');
}
function hideDebug() {
	if (window.top.dW && ! window.top.dW.closed) {
		window.top.dW.close();
		window.top.dW = null;
	}
}