$(document).ready(function(){
  
  runOnLoad(function(){
    initializeGallery();
    preload_big_images();
  });
});

  function initializeGallery(){
    var $thumbnails_box = $('div.thumbnails_list');
    var $image_name = $('#image_name');
    var $image_notes = $('#image_notes');
    var $prev = $('#gallery_prev');
    var $next = $('#gallery_next');
    var $thumbnails_box_dropdown = $('.thumbnails_listCover #drop_down_thumbnails a');
  
    var original_height = $thumbnails_box.height();
    var higher_image_size = 0;

    original_height = $thumbnails_box.height();
    //get higher image
    $.each($thumbnails_box.find('img'), function(){
      if($(this).height() > higher_image_size){
        higher_image_size = $(this).height();
      }
    });
    higher_image_size += 10;
    //make the thumbnails boxed
    $thumbnails_box.addClass('in_line').css('height', higher_image_size);
    //display boxes with links - they are hidden by default
    $('#big_image').fadeIn();
    $('.thumbnails_listCover #drop_down_thumbnails').fadeIn();
    $next.fadeIn();//.css('height', higher_image_size);
    $prev.fadeIn().addClass('disabled');//.css('height', higher_image_size);
    
    var is_there_invisible_image = false;
    $.each($thumbnails_box.find('img'), function(){
      if(((this).offsetLeft > $thumbnails_box.width()) && !is_there_invisible_image)
        is_there_invisible_image = true;
    });
    if(!is_there_invisible_image){
      $thumbnails_box_dropdown.click().remove();
    }
    
    //roll down or up the thumbnail box
    $thumbnails_box_dropdown.click(function(){
      if($thumbnails_box.hasClass('in_line')){
        $thumbnails_box.removeClass('in_line').animate({"height": original_height+"px"}, 'slow');
        $(this).html('Contract');
        $next.fadeOut();
        $prev.fadeOut();
      } else {
        $thumbnails_box.animate({"height": (higher_image_size)+"px"}, 'slow').addClass('in_line');
        $(this).html('Expand');
        $next.fadeIn();
        $prev.fadeIn();
      }
      return false;
    });
  
    $next.click(function(){
      $thumbnails_box.find('a.active').next().click();
      return false;
    });
    $prev.click(function(){
      $thumbnails_box.find('a.active').prev().click();
      return false;
    });
    
    // listens for any navigation keypress activity
  	$(document).keydown(function(e){
  		switch(e.which){
        //right arrow
  			case 39:	$next.click();
  						break;
        //left arrow
  			case 37:	$prev.click();
  						break;
  		}
  	});
    
    //thumbnail clicked
    $thumbnails_box.find('a').click(function(){
      if($(this).hasClass('active'))
        return false;
      var link = $(this);
      $('#big_image').fadeTo("slow", 0.25, function(){
        $thumbnails_box.scrollTo($(link), 1500, {'axis':'x'}).find('a.active').addClass('have_seen').removeClass('active');
        $(link).addClass('active');
        $(this).find('img').attr('src', $(link).attr('href'));
        $image_name.html($(link).find('img').attr('alt'));
        $image_notes.html($(link).attr('title'));
        $(this).fadeTo("slow", 1);
  
        $prev.removeClass('disabled');
        $next.removeClass('disabled');
        if($(link).hasClass('first'))
          $prev.addClass('disabled');
        if($(link).hasClass('last'))
          $next.addClass('disabled');
      });
      return false;
    });
  }
  
  function preload_big_images(){
    var result = false;
    var $big_images_preloader = $('#big_images_preloader');
    gallery_id = $('h1.gallery_name').attr('id');
    gallery_id = parseInt(gallery_id.replace('gallery_id_',''));
    if(gallery_id > 0){
      params = 'ajax=1&big_images_preload=1&gallery_id='+gallery_id;
      $.ajax({
        type: "GET",
        url: 'galleries.php',
        data: params,
        async: false,
        dataType: "text",
        success: function(response){
          $big_images_preloader.html(response);
          result = true;
        }
      });
    }
    return result;
  }