
$(document).ready(function() {
  if ( $("div#hidden-image-container").length > 0 ) {
    initCategoryLinks();
  }
});

var fullPhoto       = null;
var fullPhotoDims   = { width : 600, height: 400 };
var thumbs          = [];
var currentPhoto    = 0;
var totalPhotos     = 0;
var defaultCategory = "portrait";
var scrollKFactor   = 7; // speed for thumbnail scrolling - smaller K = faster scroll

var initCategoryLinks = function() {
  var category = document.location.hash.substr(1);
  
  if ( ! category ) { category = defaultCategory; }
  
  if ( window[category] == undefined )
  {
    document.location.hash = "";
    window.location.reload();
    return false;
  }
  
  var images = window[category];
  
  $.each(images, function(i, item) {
    $("div#hidden-image-container").append('<img src="' + item + '" />');
  });
  
  $("div#hidden-image-container").append('<div class="clear"></div>');
  
  $("a.category").click(function() {
    document.location.hash = $(this).attr("href"); 
    window.location.reload();
  });

  initThumbs();
  initGallery();
};

var initThumbs = function() {
  $("div#right-column-thumbs").html($("div#hidden-image-container").html());
  
  $("div#right-column-thumbs img").each(function(i, item) {
    var currThumb = $(item);

    currThumb
      .wrap('<div class="thumbnail-container full" />')
      .addClass("thumb-image")
      .load(function() {
        if ( item.width > item.height ) {
          currThumb.css({ width : 100, visibility : "visible" });
        }
        else {
          currThumb.css({ height : 100, visibility : "visible" });
        }
      });
  });
};

var initGallery = function() {
  fullPhoto    = $("img#full-image"); 
  thumbs       = $("img.thumb-image");
  totalPhotos  = thumbs.length;
  currentPhoto = 0;
  changePhoto();
  
  fullPhoto.click(function() {
    ( currentPhoto < totalPhotos - 1 ) ? currentPhoto++ : currentPhoto = 0;
    changePhoto();
  });
  
  thumbs.click(function() {
    var clickedThumbSrc = $(this).attr("src");
    
    thumbs.each(function(i, item) {
      if ( $(item).attr("src") == clickedThumbSrc ) {
        currentPhoto = i; 
        changePhoto();
        return;
      }
    });
  });
  
  $(window).keypress(function(e){
    switch ( (e.which | e.keyCode) )
    {
      // n or N = next
      case 78:
      case 110: 
        ( currentPhoto < totalPhotos - 1 ) ? currentPhoto++ : currentPhoto = 0;
        changePhoto();
        break;
      // p or P = prev
      case 80:
      case 112:
        ( currentPhoto == 0 ) ? currentPhoto = totalPhotos - 1 : currentPhoto--;
        changePhoto();
        break;
      default:
        break;
    }
    
    return;
  });

  var rate = 1000;
  
  var thumbsContainer = $("div#right-column-thumbs");
  var fullTravel = thumbsContainer.height() - $("div#right-column-viewer").height();
  var currentThumbsTop = 0;
  var remainder = 0;
  var upController   = $("div#thumb-scroll-up span");
  var downController = $("div#thumb-scroll-down span");
  
  if ( fullTravel > 0 ) {
    downController.removeClass("inactive");
    
    fullRate = scrollKFactor * fullTravel;
    
    upController
      .mouseenter(function() {
        currentThumbsTop = Math.abs(parseInt(thumbsContainer.css("top")));
        remainder = currentThumbsTop;
        if ( remainder < 1 ) { return; }
        
        rate = Math.ceil(fullRate * remainder / fullTravel);
        thumbsContainer.animate({ "top" : "0px" }, rate, "linear");
      })
      .mouseleave(function() {
        thumbsContainer.stop();
        
        downController.removeClass("inactive");
        
        currentThumbsTop = Math.abs(parseInt(thumbsContainer.css("top")));
        
        if ( currentThumbsTop < 1 ) {
          upController.addClass("inactive");          
        }
      });

    downController
      .mouseenter(function() {
        currentThumbsTop = Math.abs(parseInt(thumbsContainer.css("top")));
        remainder = fullTravel - currentThumbsTop;
        
        if ( remainder < 1 ) { return; }
        
        rate = Math.ceil(fullRate * remainder / fullTravel);
        thumbsContainer.animate({ "top" : (0 - fullTravel) + "px" }, rate, "linear");
      })
      .mouseleave(function() {
        thumbsContainer.stop();
        
        upController.removeClass("inactive");
        
        remainder = fullTravel - Math.abs(parseInt(thumbsContainer.css("top")));
        
        if ( remainder < 1 ) {
          downController.addClass("inactive");
        }
      });
    
    // connect mouse wheel to scroll
    $("div#right-column-thumbs").mousewheel(function(event, delta, deltaX, deltaY) {
      // move up (down button must show) or move down (up button must show).
      ( deltaY > 0 ) ?
        downController.removeClass("inactive") : upController.removeClass("inactive");
      
      var top = parseInt(thumbsContainer.css("top")) + (20 * deltaY);

      // top must be inside our boundaries, also, must reset our controllers' css
      if ( top >= 0 ) {
        top = 0;  // max up
        
        // hide up controller
        upController.addClass("inactive");
      }
      
      if ( top <= (0 - fullTravel) ) {
        top = 0 - fullTravel;  // max down 
        
        // hide down controller
        downController.addClass("inactive");
      }
      
      thumbsContainer.css({ "top" : top + "px" });

      return false;
    });

  }
};

var changePhoto = function() {
  var newSrc = $(thumbs[currentPhoto]).attr("src").replace(/\/thumb\//, "/full/");
  fullPhoto.clearQueue();
  fullPhoto.animate({ opacity : 0 }, "fast", function() {
    fullPhoto.attr("src", newSrc);
    fullPhoto.animate({ opacity : 1 }, "slow", "linear");
  });
};

