$(function() {

  var pictureIndex = 0;
  var slideshowTimer = null;
  var slideshowEnabled = false;
  
  $.pictureSlideshowReset = function() {
    pictureIndex = 0;
    if ($('.homepage').length > 0) {
      $.startSlideshow();
    }
  }
  
  function resetSlideshowTimer() {
    if (slideshowTimer != null) {
      clearInterval(slideshowTimer);
      slideshowTimer = null;
    } 
    if (slideshowEnabled) {
      slideshowTimer = setInterval(slideshowNext, 5000);
    }
  }
  
  $.startSlideshow = function() {
    if ($('.picture').length > 1) {
      slideshowEnabled = true;
      resetSlideshowTimer();
    }
  }
  
  $.stopSlideshow = function() {
    slideshowEnabled = false;
    resetSlideshowTimer();
  }
  
  function slideshowNext() {
    var slidesCount = $('.picture').length;
    var oldPict = $('#picture' + pictureIndex);
    oldPict.css({zIndex: 0}); //fadeOut();
    $('#legend' + pictureIndex).hide();
    if (pictureIndex == slidesCount - 1) {
      pictureIndex = 0;
    } else {
      pictureIndex++;
    }
    $('#legend' + pictureIndex).show();
    $('#picture' + pictureIndex).css({ zIndex: 1 }).fadeIn(function() {
      oldPict.hide();
    });
  }
  
  $('.previous').live('click', function() {
    var slidesCount = $('.picture').length;
    if (slidesCount <= 1) {
      return false;
    }
    var oldPict = $('#picture' + pictureIndex);
    oldPict.css({zIndex: 0}); //fadeOut();
    $('#legend' + pictureIndex).hide();
    if (pictureIndex == 0) {
      pictureIndex = slidesCount - 1;
    } else {
      pictureIndex--;
    }
    $('#legend' + pictureIndex).show();
    $('#picture' + pictureIndex).css({ zIndex: 1 }).fadeIn(function() {
      oldPict.hide();
    });
    resetSlideshowTimer();
    return false;
  });
  
  $('.next, .slideshow').live('click', function() {
    if ($('.homepage').length > 0) {
      window.location.href = "/communication.html";
      return;
    }
    var slidesCount = $('.picture').length;
    if (slidesCount <= 1) {
      return false;
    }
    var oldPict = $('#picture' + pictureIndex);
    oldPict.css({zIndex: 0}); //fadeOut();
    $('#legend' + pictureIndex).hide();
    if (pictureIndex == slidesCount - 1) {
      pictureIndex = 0;
    } else {
      pictureIndex++;
    }
    $('#legend' + pictureIndex).show();
    $('#picture' + pictureIndex).css({ zIndex: 1 }).fadeIn(function() {
      oldPict.hide();
    });
    resetSlideshowTimer();
    return false;
  });
  
  var currentIndex = -1;
  
  $.projectListReset = function() {
    currentIndex = -1;
  }
  
  $('.project_list li a').live('hover', function() {
    var pict;
    var i = $('.project_list li a').index(this);
    var oldPict = $('#picture' + currentIndex);
    if (currentIndex != i) {
      pict = $('#picture' + i);
      if (currentIndex >= 0 && pict.length > 0) {
        oldPict.css({zIndex: 0}); //fadeOut('slow');
        currentIndex = -1;
      }
      if (pict.length > 0) {
        pict.css({ zIndex: 1 }).fadeIn('slow', function() {
          if ($(oldPict).attr('id') != 'picture'+currentIndex) {
            oldPict.hide();
          }
        });
        currentIndex = i;
      }
    }
  });
  
  $.fn.scrollList = function (options) {
    var $this = $(this);
    if ($this.data('scrolllist')) {
      return;
    }
    $this.data('scrolllist', 'initialized');
    var maxHeight = $('.content').innerHeight() - 40 - $('.customer_name').height();
    var wrapper = $('<div class=\"list_wrapper\"></div>');
    var container = $('<div class=\"list_container\"></div>');
    function updateButtons() {
      $('.up_btn', wrapper).css({ visibility: (container.scrollTop() > 0) ? 'visible' : 'hidden' });
      $('.down_btn', wrapper).css({ visibility: (container.scrollTop() < $this.height() - container.height()) ? 'visible' : 'hidden' });
    }
    if ($this.height() > maxHeight) {
      $this.before(wrapper);
      wrapper.append(container).css({ position: 'relative', zIndex: '1020' });
      container.append($this).height(maxHeight - 20).css({ overflow: 'hidden' });
      wrapper.height(maxHeight);
      btns = $('<div class=\"btn\"></div>');
      btns.insertAfter(container);
      var downBtn = $('<img src=\"/images/arrow_down.png\" alt=\"plus bas\" class=\"down_btn\" />');
      downBtn.appendTo(btns).click(function() {
        container.animate({ scrollTop: Math.min($this.height() - container.height(), container.scrollTop() + container.height()) }, 1000, updateButtons);
      });
      var upBtn = $('<img src=\"/images/arrow_up.png\" alt=\"plus haut\" class=\"up_btn\" />');
      upBtn.appendTo(btns).click(function() {
        container.animate({ scrollTop: Math.max(container.scrollTop() - container.height(), 0) }, 1000, updateButtons);
      });
      updateButtons();
    }
  }
  
  $.pictureSlideshowReset();
  
});


