var Portfolio = {
  galleries: [], 
  init: function(){
    Portfolio.Nav.init();
    $('.images').each(function(i, gallery) {
      Portfolio.galleries.push(new Portfolio.Gallery(gallery));
    });
  }
};

Portfolio.Gallery = function (container_selector) {
  var nav_items = [], current_index = 0, 
      container, items, navigation, 
      next_link, prev_link, current_item, next_item;

  container = $(container_selector).css({position: 'relative'});
  items = $('li', container);
  current_item = $(items[0]);

  prev_link = $("<a href='#' class='disabled'>&larr; Prev</a>").click(prev);
  next_link = $("<a href='#'>Next &rarr;</a>").click(next);

  navigation = $("<ul class='image_nav' />");
  navigation.append($("<li class='prev' />").append(prev_link));
  items.each(function(i, item) {
    $(item).css({position: 'absolute', top: 0, left: 0, zIndex: items.length - i + 100});
    var nav_item = $("<li><a href='#' /></li>").click(function(e) { e.preventDefault(); goTo(i); });
    navigation.append(nav_item);
    nav_items.push(nav_item);
  });
  nav_items[0].addClass('selected');

  navigation.append($("<li class='next' />").append(next_link));
  navigation.insertAfter(container);
  
  function end () { return current_index >= items.length - 1; }
  function beginning () { return current_index == 0; }
  function enable () { $.each($.makeArray(arguments), function(i, link) { link.removeClass('disabled'); }); }
  function disable (link) { link.addClass('disabled'); }
  
  function enable_links () {
    enable(next_link, prev_link);
    if (beginning()) { disable(prev_link); };
    if (end()) { disable(next_link); };
  }
  
  function select_nav () {
    $.each(nav_items, function(i, e) { e.removeClass('selected'); });
    nav_items[current_index].addClass('selected');
  }
  
  function next (e) {
    if (e) { e.preventDefault(); };
    end() ? current_index = 0 : current_index++;
    transition();
  }
  
  function prev (e) {
    if (e) { e.preventDefault(); };
    beginning() ? current_index = items.length - 1 : current_index--;
    transition();
  }
  
  function goTo (index) {
    current_index = index;
    transition();
  }
  
  function transition () {
    next_item = $(items[current_index]);
    container.queue(function() {
      current_item.animate({opacity: 0}, 400);
      next_item.animate({opacity: 1}, 400);      
      container.dequeue();
    });
    current_item = next_item;
    enable_links();
    select_nav();
  }
};

Portfolio.Nav = {
  init: function(){
    var pieces = $('.pieces'),
        starting_piece_pos = pieces.position().top,
        doc = $(document);
    
    pieces.css({position: "absolute", top: starting_piece_pos});
    
    $('#sidebar .nav a').click(function(e) {
      e.preventDefault();
      var piece = $($(this).attr("href")),
          newTop = -(piece.position().top - starting_piece_pos - doc.scrollTop());
      pieces.animate({top: newTop}, 500);
    });

    $('.back_to_top').click(function(e) {
      e.preventDefault();
      $('.pieces').animate({top: starting_piece_pos}, 500);
      doc.scrollTop(0);
    });
  }
}

$(document).ready(function() {
  Portfolio.init();
});