(function($)
{

	$.fn.carrousel = function(options)
  {

		// default configuration properties
		var defaults = 
    {
      delay:               5000,
      fadeDuration:        1000,
      zoomOpacity:         1,
      zoomBackgroundColor: '#000000',
      zoomHeight:          600,
      zoomWidth:           950,
      moveDescriptionIn:   false
    };

		var options     = $.extend(defaults, options);
    var items       = new Array();
    var currentItem = null;
    var timer       = null;
    var zoomIsBuild = false;
    var main        = this;
    var autoplay    = true;
    
    // each carrousel items
    var i = 0;
		$('li', this).each(function()
    {
      if ($(this).css('display') != 'none')
      {
        currentItem = i;
      }
      items[i] = $(this);

      /*
      $(this).css('cursor', 'pointer');
      $(this).click(function()
      {
        zoom(main);
      });
      */

      if (options.moveDescriptionIn)
      {
        $('.description', $(this)).css('display', 'none');
      }

      if (i > 0)
      {
        $(this).css('display', 'none');
      }

      ++ i;
		});

    $('#goNext', $(this)).click(function()
    {
      goNext();
      return false;
    });

    $('#goPrev', $(this)).click(function()
    {
      goPrev();
      return false;
    });

    $('#autoplay', $(this)).click(function()
    {
      if (autoplay)
      {
        clearTimeout(timer);
        autoplay = false;
        $(this).attr('id', 'pause');
        $('span', $(this)).html('Pause');
      }
      else
      {
        autoplay = true;
        $(this).attr('id', 'autoplay');
        $('span', $(this)).html('Play');
        timer = setTimeout(function() { defaultAnimate(); }, options.delay);
      }

      return false;
    });

    function defaultAnimate()
    {
      goNext();
    }

    function goNext()
    {
      clearTimeout(timer);

      var nextItem = currentItem + 1;
      if (nextItem > (items.length -1))
        nextItem = 0;

      changeItem(nextItem);

      if (autoplay)
      {
        timer = setTimeout(function() { defaultAnimate(); }, options.delay);
      }
    }

    function goPrev()
    {
      clearTimeout(timer);

      var nextItem = currentItem - 1;
      if (nextItem < 0)
        nextItem = (items.length -1);

      changeItem(nextItem);

      if (autoplay)
      {
        timer = setTimeout(function() { defaultAnimate(); }, options.delay);
      }
    }

    function changeItem(nextItem)
    {
      items[nextItem].fadeIn(options.fadeDuration);
      items[currentItem].fadeOut(options.fadeDuration);

      if (options.moveDescriptionIn)
      {
        options.moveDescriptionIn.html($('.description', items[nextItem]).html());
      }

      if($('#diaporamaActions #globalDescription').length != 0)
      {
        $('#diaporamaActions #globalDescription').fadeOut(1000, function(){
          var str = $('.description', items[nextItem]).html();
          //str = str.replace('<h2>Dernières réalisations</h2>', '');
          str = str.replace('<h3>', '<h1>');

          $('#diaporamaActions #globalDescription div').html(str.replace('</h3>', '</h1>'));
          $('#diaporamaActions #globalDescription').fadeIn(1000);
        });
      }

      currentItem = nextItem;

      return true;
    }
    
    timer = setTimeout(function() { defaultAnimate(); }, options.delay);
	};

})(jQuery);
