var image_item_height = 109; // width of list item in pixels
var imagegallery_position = 0; // left position of galley list element
var imagegallery_max; // max value gallery list element should be shifted to
var imagegallery_counter; // number of thumbnails loaded, used to know what item to load next
//var portfolio_type; // what type of portfolio are we dealing with? (eg. web, print, client etc.)
var imagegallery_additional_images = new Array(); // additional portfolio thumbnail items to load
var imagegallery_index = 0; // used to know which imagegallery_additional_images to load next


/*
 * Check to make sure next or previous should be disabled
 */
function imagegallery_check_controls() {

  var previous = $(".previous-image a"); // previous link
  var next = $(".next-image a"); // next link
  
  previous.unbind(); // get rid of old action
  // if all the way to the beginning, the previous button should be disabled

  if (imagegallery_position == 0) {
    previous.click(function(event){
      event.preventDefault();
    });
    $(".previous-image").removeClass('previous-hover');
    $(".previous-image").addClass('previous-disabled');
  } 
  else { // enable
    previous.hover(
      function () {
        $(".previous-image").addClass('previous-hover');
      }, 
      function () {
        $(".previous-image").removeClass('previous-hover');
      }
    );
    previous.click(function(event){
      event.preventDefault();
      imagegallery_previous_img(); // shift to see previous  
    });
    $(".previous-image").removeClass('previous-disabled');
  }
   
  next.unbind(); // get rid of old action
  // if all the way to the end, the next button should be disabled
  
  if ((Math.abs(imagegallery_position - imagegallery_max) <= image_item_height) || (imagegallery_max >= 0)) {
    next.click(function(event){
      event.preventDefault();
    });
    $(".next-image").removeClass('next-hover');
    $(".next-image").addClass('next-disabled');
  }
  else { // enable
    next.hover(
      function () {
        $(".next-image").addClass('next-hover');
      }, 
      function () {
        $(".next-image").removeClass('next-hover');
      }
    );
    next.click(function(event){
      event.preventDefault();
      imagegallery_next_img(); // shift to see next
    });
    $(".next-image").removeClass('next-disabled');
  }
}

/*
 * Previous button action
 */
function imagegallery_previous_img() {
  
  var gallery = $(".image-gallery ul"); // portfolio thumbnails

  // if not all the way to the beginning, shift
  if (imagegallery_position < 0) {
    $(".previous-image").addClass('previous-active');
    gallery.animate({"top": "+=" + image_item_height + "px"}, "slow", function() {
      $(".previous-image").removeClass('previous-active');
    });
    imagegallery_position += image_item_height;
  }

  imagegallery_check_controls(); // check the previous, next buttons
  
}

/*
 * Next button action
 */
function imagegallery_next_img() {
  
  var gallery = $(".image-gallery ul"); // portfolio thumbnails
  
  // if not all the way to the end, shift
  if (Math.abs(imagegallery_position - imagegallery_max) > image_item_height) {
    $(".next-image").addClass('next-active');
    gallery.animate({"top": "-=" + image_item_height + "px"}, "slow", function() {
      $(".next-image").removeClass('next-active');
    });
    imagegallery_position -= image_item_height;
  }
  
  imagegallery_check_controls(); // check the previous, more buttons, since there now may be more to see

  
}

/*
 * Get additional portfolio images
 */
function imagegallery_get_additional_images() {

  $.getJSON(baseURL + "farmer_to_farmer_extras/imagegallery/js",
    function(response){
      // on success
      if (response.status) {
        for (imagegallery_index; imagegallery_index < response.data.length; imagegallery_index++) {
          $(".image-gallery ul").append(response.data[imagegallery_index]);
          imagegallery_max -= image_item_height;
        }
        /*$(".image-gallery ul").prepend($(".image-gallery ul").html());
        imagegallery_max -= (image_item_height * response.data.length);
        $(".image-gallery ul").css({"top": "-" + (image_item_height * (response.data.length)) + "px"});
        imagegallery_position -= (image_item_height * (response.data.length));*/
        imagegallery_check_controls();
      }

    });
            
    imagegallery_max -= image_item_height;
          
}


/*
 * The page has loaded, let the fun begin
 */
$(document).ready(function(){
  
  var content_height = $('#content').height();
  
  if (content_height < 300) {
    $('#content').height(300);
    content_height = 370;
  }
  
  content_height -= 200;

  $('#block-views-photo_nav .image-gallery').css('height', (content_height - (content_height % image_item_height)) + 'px');
  
  // how much it should shift is the difference between it's total width and the amount that can be visible

  imagegallery_counter = $(".image-gallery ul li").length; // how many thumbnails have been loaded

  imagegallery_index = imagegallery_counter;

  imagegallery_max = -1 * ((image_item_height * imagegallery_counter) - $("#image-menu").outerHeight());
  
  imagegallery_get_additional_images();
  
  
  
});
