var eternalCarrousel = {};

jQuery(document).ready(function($)
{
  eternalCarrousel.changeArtwork = function()
  {
    var imgThumb = $('li:nth-child('+this.focusIndex+') img', '#artwork')

    $('.thumbs li img', this.target).removeClass('active');
    $('.thumbs li:nth-child('+this.focusIndex+') img', this.target).addClass('active');
    $('.image img:not(:nth-child('+this.focusIndex+'))').animate({opacity: 0});
    $('.image img:nth-child('+this.focusIndex+')').animate({opacity: 1});
  };

  eternalCarrousel.init = function(options)
  {
    this.direction = options.direction;
    this.target = options.target;
    this.visibleThumbs = options.visibleThumbs;
    this.scrollIndex = 1;
    this.focusIndex = 1;
    this.scrollAmount = (this.direction == 'vertical')? $('.thumbs img:first', this.target).height() + options.thumbMargin : $('.thumbs img:first', this.target).width() + options.thumbMargin;

    this.totalThumbs = $('.thumbs img', this.target).length;

    this.changeArtwork();
    this.bindEvents();
  };

  eternalCarrousel.bindEvents = function()
  {
    var $this = this;

    $('.previous', $this.target).click(function(){
      if($this.focusIndex == 1)
      {
          if($this.direction == 'vertical')
          {
            $('li:first', $this.target).animate({'marginTop': - $this.scrollAmount * ($this.totalThumbs - $this.visibleThumbs) });
          }
          else
          {
            $('li:first', $this.target).animate({'marginLeft': - $this.scrollAmount * ($this.totalThumbs - $this.visibleThumbs) });
          }
          $this.scrollIndex = $this.totalThumbs - ($this.visibleThumbs - 1);
          $this.focusIndex = $this.totalThumbs;
      }
      else
      {
        if($this.focusIndex < $this.scrollIndex + 1)
        {
          if($this.direction == 'vertical')
          {
            $('li:first', $this.target).animate({'marginTop': '+='+$this.scrollAmount });
          }
          else
          {
            $('li:first', $this.target).animate({'marginLeft': '+='+$this.scrollAmount });
          }
          $this.scrollIndex--;
        }
        $this.focusIndex--;
      }

      eternalCarrousel.changeArtwork();
      return false;
    });
    
    $('.next', $this.target).click(function(){
      if($this.focusIndex == $this.totalThumbs)
      {
          if($this.direction == 'vertical')
          {
            $('li:first', $this.target).animate({'marginTop': 0 });
          }
          else
          {
            $('li:first', $this.target).animate({'marginLeft': 0 });
          }
          $this.scrollIndex = 1;
          $this.focusIndex = 1;
      }
      else
      {
        if($this.focusIndex > $this.scrollIndex + 1)
        {
          if($this.direction == 'vertical')
          {
            $('li:first', $this.target).animate({'marginTop': '-='+$this.scrollAmount });
          }
          else
          {
            $('li:first', $this.target).animate({'marginLeft': '-='+$this.scrollAmount });
          }
          $this.scrollIndex++;
        }
        $this.focusIndex++;
      }
      eternalCarrousel.changeArtwork();
      return false;
    });

    $('.thumbs img', $this.target).click(function(e){
      var imgTarget = e.target;
      var index = 0;
      $('.thumbs img', $this.target).each(function(i, img){
        if($(img).attr('src') == $(imgTarget).attr('src'))
        {
          index = i+1;
        }
      });

      $this.focusIndex = index;
      $this.changeArtwork();
    })
  };
});
