function SliderDots(){
  this.selectedDotIndex = 0;
}
SliderDots.prototype.selectByIndex = function(index){
  $($(".slideshow_navigation .dots img")[this.selectedDotIndex]).removeClass('current');
  $($(".slideshow_navigation .dots img")[index]).addClass('current');
  $(".slideshow_navigation .dots img")[this.selectedDotIndex].src = '/img/slide_dot_off.gif';
  $(".slideshow_navigation .dots img")[index].src = '/img/slide_dot_on.gif';
  this.selectedDotIndex = index;
}
SliderDots.prototype.createDots = function(){
  $('.slideshow_navigation .dots').empty();
  for (var i=0; i < $('.slides .slide').size(); i++) {
    if (i == 0) $('<a href="#"><img class="rollover current" src="/img/slide_dot_on.gif" alt="" /></a>').appendTo('.slideshow_navigation .dots');
    else $('<a href="#"><img class="rollover" src="/img/slide_dot_off.gif" alt="" /></a>').appendTo('.slideshow_navigation .dots');
  }
}

/* Begin ImageSlider Object */
function ImageSlider(numberOfImages, photoContainer){
  this.selectedIndex = 0;
  this.imageWidth = 439;
  this.numberOfImages = numberOfImages;
  this.photoContainer = photoContainer;
}
ImageSlider.prototype.selectByIndex = function(index){
  var currentX = this.selectedIndex * this.imageWidth;
  var destinationX = (index * this.imageWidth) - currentX;
  var distance = 0;  
  if(destinationX < 0){
    distance = "+=" + Math.abs(destinationX);
  } else {
    distance = "-=" + Math.abs(destinationX)
  }
  this.selectedIndex = index;
  this.photoContainer.animate({
    left: distance, easing: 'easeOutBounce'
  }, 500);
}
ImageSlider.prototype.gotoIndex = function(index){
  var currentX = this.selectedIndex * this.imageWidth;
  var destinationX = (index * this.imageWidth) - currentX;
  var distance = 0;  
  if(destinationX < 0){
    distance = "+=" + Math.abs(destinationX);
  } else {
    distance = "-=" + Math.abs(destinationX)
  }
  this.selectedIndex = index;
   this.photoContainer.animate({
    left: distance, easing: 'easeOutBounce'
  }, 1);
}
ImageSlider.prototype.gotoNextSlide = function(){
  var nextId = this.selectedIndex + 1;
  if(nextId <= this.numberOfImages - 1) {
    this.selectByIndex(nextId);
    return nextId;
  }  else return this.numberOfImages - 1;
}
ImageSlider.prototype.gotoPreviousSlide = function(){
  var nextId = this.selectedIndex - 1;
  if(nextId >= 0){
    this.selectByIndex(nextId);
    return nextId;
  } else return 0;
}

$(function() {
  if ($('#slideshow_items').length) {
    num_slides = $('#slideshow_items .slideshow_item').size();
    if (num_slides > 5) num_slides = 5;
    buildSlideButtons(num_slides);
    $($('#slideshow_items .slideshow_item')[0]).show();
    slideShowInterval = setInterval("slideShow()", 7000);
  }

  $('span.email').each(function() {
    $(this).wrap("<a></a>");
    var address = $(this).text().replace(" at ", "@");
    $(this).parent().attr('href', 'mailto:' + address);
    $(this).text(address);
  });
  
  if ($('.slideshow').length) {
    var sliderDots = new SliderDots();
    var sliderDots = new SliderDots();
    sliderDots.createDots();
    $('.slides').width($('.slides .slide').size()*439);
    var imageSlider = new ImageSlider($('.slides .slide').size(), $('.slides'));
            
    $('.dots a').click(function(event){
      var index = $(".dots a").index(this);
      sliderDots.selectByIndex(index);
      imageSlider.selectByIndex(index);
      return false;
    });
    
    $('.prev_link').click(function(event){
      sliderDots.selectByIndex(imageSlider.gotoPreviousSlide());
      return false;
    });

    $('.next_link').click(function(event){
      sliderDots.selectByIndex(imageSlider.gotoNextSlide());
      return false;
    });
  }

  $('.rollover').hover(function() {
    if (!$(this).hasClass('current')) {
      this.src = this.src.replace("_off","_on");
    }
  }, function() {
    if (!$(this).hasClass('current')) {
      this.src = this.src.replace("_on","_off");
    }
  });

  // Preload rollover images (from page)
  $('.rollover').each(function(){
    var img = new Image();
    img.src = this.src.replace("_off","_on");
  });
});

$(document).ready(function() {
  if ($('.work_filters').length) filterWork('', location.hash.substring(1));
});

function filterWork(elm, filter) {
  $('.work_filters .current').removeClass('current');
  if (filter && filter != ' ' && $('#work_container .'+filter).length) {
    $('#work_container div').hide();
    $('#work_container .'+filter).show();
    location.hash = '#'+filter;
    if (elm) $(elm).addClass('current');
    else $('.'+filter+'_link').addClass('current');
  } else {
    $('#work_container div').show();
    if (location.hash.substring(1) != '')location.hash = ' ';
    $('.all_link').addClass('current');
  }
}
