/* ****************************************************

	@file:		   global.js
	@description:  Comportements globaux
	@author:       remi (ixmedia.com)
	@updated:	   20080715

***************************************************** */

$(document).ready(function() {

	/**
	 * Gestion des boutons radios/checkbox à développement
	 */

	$('ul.radio li div').hide();

	$('ul.radio li')
	.children('input')
	.click(function() {
		update_listes(this);
	}).each(function() {
		update_listes(this);
	});

	function update_listes(e) {
		if (!$(e).parent().parent().is('.checkbox')) {
			if ($(e).is(':checked')) {
				$(e).parent().siblings().children('div').hide();
				$(e).parent().children('div').show();
			} else {

			}
		} else {
			if ($(e).is(':checked')) {
				$(e).parent().children('div').show();
			} else {
				$(e).parent().children('div').hide();
			}
		}
	}

	$('input.texte').focus(function() {
		this.focus();
		return false;
	})

	/**
	 * Gestion du compteur de mots !
	 */
	function compter_mots(texte) {
		if (texte == "") { return "0"; }
		var texte = texte + " ";
		texte = texte.replace(/^[^A-Za-z0-9]+/gi, "");
		texte = texte.replace(/[^A-Za-z0-9à]+/gi, " "); // ajouter dans cette regex les caractères
		//                                                 spéciaux pouvant être un mot d'une lettre (comme "à")
		return texte.split(" ").length - 1;
	}
	var words_timeout = null;
	function update_words() {
		var val = $('p.words').prev().children('textarea').val();
		var data = compter_mots(val);
		$('p.words span').html(data).alt(parseInt(data) > 150, function() { $(this).addClass('alert'); }, function() { $(this).removeClass('alert'); })
	}
	update_words();

	$('p.words').prev().children('textarea').keydown(function() {
		clearTimeout(words_timeout);
		words_timeout = setTimeout(function() {
			update_words();
		}, 400);
	})

	$('a.tableau').fancybox({
		hideOnContentClick: true,
		frameWidth: 800,
		frameHeight: 440,
		zoomSpeedIn : 0,
		zoomSpeedOut : 0,
		overlayShow : true,
		overlayOpacity: 0.7
	})

});

jQuery.fn.extend({
	'alt' : function(bool, fn_true, fn_false) {
		(bool) ? fn_true.call(this) : fn_false.call(this);
		return this;
	},
	'iif' : function(bool) {
		return (bool) ? this : jQuery('');
	}
});
