var current_gallery_id = 0;
var change_in_progress = false;
var current_photo = 0;
var current_next = 2;
var current_prev = false;



$(document).ready(function() {
  /*$('.gallery-photo').click(function() {
      show_photo(current_gallery_id, 2);
    });*/

  $('.nav-right').click(function() {
      show_photo(current_gallery_id, 2);
    });
  //$(document).bind('keydown', 'Ctrl+right', function() { show_photo_by_keydown(current_gallery_id, 'next') } );
  //$(document).bind('keydown', 'Ctrl+left', function() { show_photo_by_keydown(current_gallery_id, 'prev') } );
  //$(document).bind('keydown', 'Ctrl+right', function() { show_photo(current_gallery_id, 2) } );

});


function show_photo (gallery_id, photo_num)
{
  if (change_in_progress) return false;
  
  $.post('/galleries/a/getphoto/', { gallery_id: gallery_id, photo_num: photo_num },
    function(data)
    {
      if (data.status == true)
      {
        change_in_progress = true;
        $('.gallery-photo').fadeTo('slow', 0.01, function() {
          
          //$('.gallery-photo').attr('src', '/images/galleries/'+data.photo_name);
          $('.gallery-photo').remove();
          $('#photo-wrapper').append('<img class="gallery-photo hidden nav-right" src="'+'/images/galleries/'+data.photo_name+'" alt=""/>');
          
          $('.gallery-photo').fadeTo('slow', 1, function() {
            change_in_progress = false;
            current_photo = photo_num;

            //$('.gallery-photo').unbind('click');
            
            /*$('.gallery-photo').click(function() {
              show_photo(gallery_id, data.next_photo);
            });*/
            
            $(document).unbind('keydown');
            
            if (data.next_photo)
            {
              $('.nav-right').removeClass('nav-disabled');
              $('.nav-right').unbind('click');
              $('.nav-right').click(function() {
                show_photo(gallery_id, data.next_photo);
                //$(document).bind('keydown', 'Ctrl+right', function() { show_photo(gallery_id, data.next_photo) } );
                current_next = data.next_photo;
              });
              $(document).bind('keydown', 'Ctrl+right', function() { show_photo(gallery_id, data.next_photo) } );
            }
            else
            {
              $('.nav-right').addClass('nav-disabled');
              $('.nav-right').unbind('click');
              current_next = false;
              //$(document).unbind('keydown', 'Ctrl+right');
            }

            if (data.prev_photo)
            {
              $('.nav-left').removeClass('nav-disabled');
              $('.nav-left').unbind('click');
              $('.nav-left').click(function() {
                show_photo(gallery_id, data.prev_photo);
              });
              current_prev = data.prev_photo;
              $(document).bind('keydown', 'Ctrl+left', function() { show_photo(gallery_id, data.prev_photo) } );
              //$(document).bind('keydown', 'Ctrl+left', function() { show_photo(gallery_id, data.prev_photo) } );
            }
            else
            {
              $('.nav-left').addClass('nav-disabled');
              $('.nav-left').unbind('click');
              current_prev = false;
            }
            $('#current-photo').html(current_photo);
          });
        });
      }
      else
      {
        alert('error');
      }
    }, "json"
  );
}
