/* ****************************************************

	@file:			jquery.ix.js
	@description:	Comportements communs iXmédia
	@author:		remi (ixmedia.com)
	@version:		20081030

******* Méthodes **************************************

####################################################################

 $('').liensExternes(params)

	$('#contenu a').liensExternes({
		exclude : window.location.host
	});

####################################################################

 $('').toggleClassIf(classe, bool)

	$('body').toggleClassIf('internetexplorer, $.browser.msie);

####################################################################

 $('').addUniqueClass(classe)

	$('#nav li a').click(function() {
		$(this).parent().addUniqueClass('active');
	});

####################################################################

 $('').rendreCliquable(exclu)

	$('#sidebar input').rendreCliquable('.exclu');

####################################################################
*/

(function($) {
	$.fn.liensExternes =
		/**
		 * Liens externes automatiques
		 *
		 * @param {Object} params
		 * @return {jQuery}
		 */
		function(params){

			params = $.extend({
				exclude : /^$/
			}, params);

			return $(this)
			.filter(function() {
				var $this = $(this);
				if ($this.is('a[href$=\.pdf]')) { return true; } // Fichiers PDF
				if ($this.is("a[href^=http]") && !$this.attr('href').match(params.exclude)) { return true; } // Liens commencant par "http" mais non exclus
			})
			.addClass('externe')
			.attr('target', '_blank');
		};

	$.fn.toggleClassIf =
		/**
		 * Ajouter/enlève une classe à une condition
		 *
		 * @param {String}	classe	La classe à ajouter/enlever
		 * @param {Boolean}	bool	La condition
		 * @return {jQuery}
		 */
		function(classe, bool) {
			if (bool) {
				return $(this).addClass(classe)
			} else {
				return $(this).removeClass(classe)
			}
		};

	$.fn.addUniqueClass =
		/**
		 * Ajoute une classe à un élément et l'enlève à ses voisins
		 *
		 * @param {String} classe	La classe en question
		 * @return {jQuery}
		 */
		function(classe) {
			return $(this).addClass(classe).siblings().removeClass(classe);
		}

	$.fn.rendreCliquable =
		/**
		 * Rend un champ cliquable
		 *
		 * @param {String} exclu	Filtre pour exclure des éléments particuliers
		 * @return {jQuery}
		 */
		function(exclu) {
			var $this = $(this);
			$(this).not(exclu).focus(function() {
				$(this).addClass('focus');
				if ($(this).val() == this.defaultValue) {
					$(this).val("");
				}
			}).blur(function() {
				$(this).removeClass('focus');
				if ($(this).val() == "") {
					$(this).val(this.defaultValue);
				}
			})
		}

})(jQuery);
