jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true,
        find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
        replace: '$1@$2'
    }, settings);
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            $(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
            var is_link = true;
        }
        $(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
    });
};

$.fn.slide = function(options){
	return this.each(function(){   
		$.slide(this, options);
	});
};

$.slide = function(container, options){
	var elements = $(container).children();
	if(elements.length > 1){
		elements.each(function(i){
			$(this).css({'left':i * options.width});
		});
		setTimeout((function(){
			$.slide.next(elements, options, 0);
		}), options.timeout);
	}
};

$.slide.next = function(elements, options, current){
	elements.animate({'left':'-='+options.width}, options.speed);
	
	if(current == elements.length - 1) next = 0;
	else next =  current + 1;
	
	setTimeout((function(){
		$(elements[current]).css({'left':(elements.length - 1) * options.width});
		$.slide.next(elements, options, next);
	}), options.timeout);
};

$(document).ready(function(){
	
	if ($('html').attr('lang') == 'fr') {
		var langue = 'fr';
		var print = 'Imprimer l\'article';
		var ancre = 'imprimer-l-article';
	} else if ($('html').attr('lang') == 'it') {
		var langue = 'it';
		var print = 'Stampare l\'articolo';
		var ancre = 'stampare-l-articolo';
	} else {
		var langue = 'en';
		var print = 'Print the article';
		var ancre = 'print-the-article';
	}
	
	// Galerie de type "slide"
	$('#diaporama').slide({
		width: 310,
		speed: 2000,
		timeout: 10000
	});
	
	// Bouton print
	$('#contenu.fiche .reseaux').append('<li><a class="print" href="#'+ancre+'" title="'+print+'">'+print+'</a></li>');
	$('a.print').click(function(){
		window.print();
	});
	
	// Liste archives et catégories events
	var deplie = -1;
	$(".slideToogle").children('ul').hide();
	$('.slideToogle').children('h4').each(function(i){
		$(this).attr('class', 'h4-'+i);
	}).css({'cursor':'pointer'}).click(function(){
		$(this).parent().children('ul').slideUp('normal');
			
		if(deplie != $(this).attr('class')){
			$(this).next().slideDown('normal');
			deplie = $(this).attr('class');
		}
		else{
			deplie = -1;
		}
	});
	
	$('#email').val('Your email address').blur(function(){
		if ($(this).val() == '') {
			$(this).val('Your email address').css({'color':'#888'});
		};
	}).focus(function(){
		if ($(this).val() == 'Your email address') {
			$(this).val('').css({'color':'#000'});
		};
	}).css({'color':'#888'});
	
	// Select categories
	$('#categoriesForm input').hide();
	$('#categoriesForm select').change(function(){
		$('#categoriesForm').submit();
	});
	
	$('a.fancybox').fancybox({
		//'titlePosition':'over'
	});
	
	$('.adresse').defuscate();

	$.gaTracker('UA-19798971-1');
});

