// plugin jQuery
jQuery.fn.replaceClass = function( sOldClass, sNewClass) {
	this.removeClass( sOldClass).addClass( sNewClass);
};

// 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;
}

// "Constante" prédéfinie
function constante( sCode) {
	var s = '';

	switch( sCode) {
		case 'ajaxError' : s = _lg(	"Une erreur est survenue :!\n\nRafraichissez la page et essayer à nouveau.\nSi le problème persiste, contactez le webmaster.");
			break;
		default: s = '[Erreur code: "' + sCode + '" inconnu.]';
	}
	return s;
}

function js_encode( s) {
	return s.replace(/\\/g, '\\\\').replace(/"/g, '\&#34;').replace(/\'/g, '\&#39');
}

// Tronque la chaine passée en paramètre
function tronquer( s, iTailleMax) {
	s = jQuery.trim( s);
	return ( s.length > iTailleMax ) ? s.substring( 0, iTailleMax - 3) + "..." : s;
}

// @see d_box_alert en php
function d_box_alert( options) {
	var settings = {
		 code: 'attention',
		 text: '',
		 id: 'box_alert'
	};
	if(options)
		 $.extend(settings, options);

	switch( settings.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="' + settings.id + '" class="helpBoxInfos-ON ' + sCode + '">' + settings.text + '</div>';
}

// effet (à voir si utile)
var affiche_fx = {
	settings : {
		 selecteur: '',
		 parent: '',
		 speed: 'fast',
		 text: ''
	},

	init: function(options) {
		if(options)
			$.extend(this.settings, options);
	},

	fadeIn: function(options) {
		this.init(options);
		var s = this.settings;

		if (s.text != '') {
			$(s.selecteur).remove();
			$(s.parent).prepend(s.text);
		}
		$(s.selecteur).hide().fadeIn(s.speed);
	},

	fadeOut: function(options) {
		this.init(options);
		var s = this.settings;

		if (s.text != '') {
			$(s.selecteur).remove();
			$(s.parent).prepend(s.text);
		}
		$(s.selecteur).show().fadeOut(s.speed);
	},

	write: function(options) {
		this.init(options);
		var s = this.settings;

		$(s.selecteur).remove();
		if (s.text != '') $(s.parent).prepend(s.text);
	}
};

function alert_box( options) {
	var settings = {
		 titre: '',
		 text: ''
	};
	if(options)
		 $.extend(settings, options);

	$('#dialog_alertBox').remove();
	$('body').prepend('<div id="dialog_alertBox" title="' + settings.titre + '">' + settings.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(element) {
	return (element) ? document.getElementById(element) : 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>');
}

var prechargement = {
	load : function(a) {
		this.chargement = a;
		for (var el in a) {
			var img = document.createElement('img');
			img.setAttribute('src', this.chargement[el]);
			this.chargement[el] = img;
		};
	},
	image : function(sNom) {
		return this.chargement[sNom];
	}
};

var WindowObjectReference = null;
function popup(sURL, sNom, sOption, iW, iH, isCenter) {
//if(WindowObjectReference == null || WindowObjectReference.closed) {
	if (sOption == "all=yes")
		sOption = 'directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1,';
	else
		sOption += (sOption != '') ? ',' : '';

	if (window.screen && isCenter)
		sOption += 'left=' + ((screen.width-iW)/2) + ',top=' + ((screen.height-iH)/2);

	WindowObjectReference = window.open(sURL,sNom,sOption + ((sOption!='') ? ',' : '') + 'width=' + iW + ',height=' + iH);
	WindowObjectReference.focus();
/*}
else
	WindowObjectReference.focus();*/
}

// DEBUG
// Author: Denis Howlett <feedback@isocra.com>
// WWW: http://www.isocra.com/
// Modification : ActenGo
function debug(text) {
	showDebug();
	if (window.top.dW && ! window.top.dW.closed)
		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><pre>\n');
}
function hideDebug() {
	if (window.top.dW && ! window.top.dW.closed) {
		window.top.dW.close();
		window.top.dW = null;
	}
}

// TODO : à virer ou à jqueryzer

// insertion d'un lien imprimer
// iPos = la position d'un LI a rajouter si le conteneur est un ul
function windowPrint(oElem, sText, iPos) {
	if (window.print && $id(oElem) && sText != '') {
		var oEl = $id(oElem),
			oA = document.createElement('a');
		oA.setAttribute('href', '#');
		oA.appendChild(document.createTextNode(sText));
		oA.onclick = function() { window.print(); return false; };

		var bInsert = false, cpt = 0; i = 0;
		if (oEl.tagName == 'UL' || oEl.tagName == 'OL') {
			if (oEl.childNodes || oEl.childNodes.length>0) {
				var oLi = document.createElement('li');
				oLi.appendChild(oA);
				while(i<oEl.childNodes.length && !bInsert) {
					if (oEl.childNodes[i].nodeName == 'LI') {
						if(++cpt==iPos)	{
							oEl.insertBefore(oLi,oEl.childNodes[i]);
							bInsert = true;
						}
					}
					i++;
				}
			}
			if(!bInsert) oEl.appendChild(oLi);
		}
		else
			oEl.appendChild(oA);
	}
}