/* Lightbox */
jQuery(document).ready(function(){
  jQuery('.lightbox').lightbox();
});



/* Slider */
 jQuery(document).ready(function($) {

  $(".slider").slideshow({
    width      : 980,
    height     : 268,
    transition : 'squareRandom'
  });

});


/* accordéon FAQ */
			
			$.accordian = function(items, first, options) {

				var active = first;
				var running = 0;

				var titles = options && options.titles || '.title';
				var contents = options && options.contents || '.content';
				var onClick = options && options.onClick || function(){};
				var onShow = options && options.onShow || function(){};
				var onHide = options && options.onHide || function(){};
				var showSpeed = options && options.showSpeed || 'slow';
				var hideSpeed = options && options.hideSpeed || 'fast';

				$(items).not(active).children(contents).hide();
				$(items).not(active).each(onHide);
				$(active).each(onShow);

				$(items).children(titles).click(function(e){

					var p = $(contents, this.parentNode);
					$(this.parentNode).each(onClick);

					if (running || !p.is(":hidden")) return false;
					running = 2;

					$(active).children(contents).not(':hidden').slideUp(hideSpeed, function(){--running;});
					p.slideDown(showSpeed, function(){--running;});

					$(active).each(onHide);
					active = '#' + $(this.parentNode)[0].id;
					$(active).each(onShow);

					return false;
				});

			};

			function simpleLog(message) {
				$('<div>' + message + '</div>').appendTo('#log');
			}

			$(function(){

				$.accordian('#list1 > div', '#item11');

				$.accordian('#list2 > div', '#item21', {
					titles:'.mytitle',
					contents:'.mycontent',
					onClick:function(){simpleLog(this.id + ' clicked')},
					onShow:function(){simpleLog(this.id + ' shown'); $(this).removeClass('off').addClass('on');},
					onHide:function(){simpleLog(this.id + ' hidden'); $(this).removeClass('on').addClass('off');},
					showSpeed:250,
					hideSpeed:550
				});

			});

/* Nbr de clics faq */
var Jaime = {
	evenements: function() {
//		$('a.jaime').click(this.click);
		$('div.mytitle').click(Jaime.click);
	},
	precharger: function() {
		var jaimes = $('div.mytitle');
		for(var i = 0, l = jaimes.length; i < l; i++) {
			$.ajax({
				context: jaimes[i], // $(this)
				type: 'POST',
				url: 'scripts/jaime.php',
				data: 'cle=' + encodeURIComponent($(jaimes[i]).attr('title')),
				success: function(data) {
					var votes = parseInt(data);
					if(isNaN(votes)) {votes = '-1';}
					if(votes > 0) {
						$(this).html($(this).attr('title') + ' <em>(vue ' + votes + ' fois)</em>');
					} else {
						$(this).html($(this).attr('title') + ' <em>(vue ' + votes + ' fois)</em>');
					}
				}
			});
		} // for
	},
	click: function(event) {
		var element = event.currentTarget; // comme this ( et non: event.target )
		// $(element).unbind('click'); // stopper les multi-clicks
		 $(element).click(function() { return false; }); // blocage du lien
		
		$.ajax({
			context: element, // $(this)
			type: 'POST',
			url: 'scripts/jaime.php',
			data: 'vote=&cle=' + encodeURIComponent($(element).attr('title')),
			success: function(data) {
				$(this).css({'opacity':0});
				$(this).addClass('jaime-a-voter');
				var votes = parseInt(data);
				if(isNaN(votes)) {votes = '-1';}
				if(votes > 1) {
						$(this).html($(element).attr('title') + ' <em>(vue ' + votes + ' fois)</em>');
				} else {
						$(this).html($(element).attr('title') + ' <em>(vue ' + votes + ' fois)</em>');
				}
				$(this).animate({'opacity':1}, {'duration':'slow'});
			},
			error: function() {
				alert('Une erreur s\'est produite, merci de recharger votre page');
			}
		});
		return false;
	}
};

$(document).ready(function() {
	Jaime.evenements();
	Jaime.precharger();
});

