/**
* Image gallery
**/

/**
* Thumbs for opening the large picture
**/
function set_photo_links () {
  $('div.thumb img').click(function() {
    var imageid = $(this).attr('imageid');
    
    $('#pic-large img:first')
      .attr('src', $(this).attr('longdesc'))
      .attr('alt', $(this).attr('alt'))
      .attr('title', $(this).attr('title'))
      .data('thumb', this)
      .data('imageid', imageid)
      ;
    
    // Sort out the stars
    var rating = $(this).attr('rating');
    var numvotes = $(this).attr('numvotes');
    set_stars_to (rating, numvotes);
    
    // Store some info in the stars that is used later when rating
    $('.rating_stars img').each(function(index) {
      $(this).data('imageid', imageid);
      $(this).data('rating', index + 1);
    });
    
    $('.prev-image').stop(true, true);
    $('.next-image').stop(true, true);
    
    var $par = $(this).parent().parent();
    if (! $par.prev().is('div.thumbs')) {
      $('.prev-image').fadeOut();
    } else {
      $('.prev-image').fadeIn();
    }
    
    if (! $par.next().is('div.thumbs')) {
      $('.next-image').fadeOut();
    } else {
      $('.next-image').fadeIn();
    }
    
  }).css('cursor', 'pointer');
  
  $('div.thumb img:first').click();
}


/**
* Sets the stars to match the specified ratings value - provided as a percent
**/
function set_stars_to (rating, numvotes) {
  // Stars
  $('.rating_stars img').each(function() {
    if (rating >= 20) {
      $(this).attr('src', 'images/star_full.gif');
    } else if (rating >= 10) {
      $(this).attr('src', 'images/star_half.gif');
    } else {
      $(this).attr('src', 'images/star_empty.gif');
    }
    
    $(this).data('src', $(this).attr('src'));
    
    rating -= 20;
  });
  
  // Numvotes
  if (numvotes == 0) {
    $('#numvotes').html('No votes yet');
  } else if (numvotes == 1) {
    $('#numvotes').html('Only one vote');
  } else {
    $('#numvotes').html(numvotes + ' votes');
  }
}


/**
* Previous image
**/
function prev_click () {
  var id = $('#pic-large img:first').data('imageid');
  
  $('img[imageid=' + id + ']:first').parent().parent().prev().find('img').click();
}

/**
* Next image
**/
function next_click () {
  var id = $('#pic-large img:first').data('imageid');
  
  $('img[imageid=' + id + ']:first').parent().parent().next().find('img').click();
}



$(document).ready( function() {
  set_photo_links();
  
  // Show submit box if there was an error
  if (location.hash == '#submit') {
    var url = $('#submitlink').attr('href');
    
    jQuery.facebox(function() {
      jQuery.get(url, function(data) {
        jQuery.facebox(data)
      })
    });
    
  } else if (location.hash.substr(0, 4) == '#img') {
    var id = location.hash.substr(4);
    id = parseInt(id, 10);
    
    $('img[imageid=' + id + ']:first').click();
  }
  
  
  // Rating hover
  $('.rating_stars img').hover(function() {
    $(this).prevAll().attr('src', 'images/star_full.gif');
    $(this).attr('src', 'images/star_full.gif');
    $(this).nextAll().attr('src', 'images/star_empty.gif');
    
  }, function() {
    $('.rating_stars img').each(function() {
      $(this).attr('src', $(this).data('src'));
    })
  });
  
  // Rating click
  $('.rating_stars img').click(function() {
    // processing msg
    $('.rating_stars').slideUp('slow', function() {
      $('.rating_stars').after('<p class="confirmation">Processing, please wait.</p>');
      $('.rate .confirmation').hide();
      $('.rate .confirmation').slideDown('slow');
    });
    
    // json request
    var data = {
      rating: $(this).data('rating'),
      imageid:  $(this).data('imageid')
    };
    $.getJSON("ajax/rate_photo.php", data, function(json) {
      $('.rate .confirmation').slideUp('slow', function() {
      
        // Success
        if (json.result == 1) {
      
          $('.rate .confirmation')
            .html('<p class="confirmation">Thanks for your vote!</p>')
            .slideDown('slow');
            
          // Hides the entire ratings box after 5000 seconds
          // Once the box is hidden, shows all the sub elements again
          $('.rate .confirmation').animate({opacity: 1}, 2000, function() {
            $('.rate .confirmation').slideUp('slow', function() {
              $('.rate .confirmation').remove();
              $('.rating_stars').slideDown('slow');
            });
          });
          
          set_stars_to (json.newrating, json.newnumber);
          
          $($('#pic-large img:first').data('thumb'))
            .attr('rating', json.newrating)
            .attr('numvotes', json.newnumber)
            ;
          
        // Failure
        } else {
        
          $('.rate .confirmation')
            .html('<p class="error">Database error while submitting vote.</p>')
            .slideDown('slow')
            .animate({opacity: 1}, 2000, function() {
              $('.rate .confirmation').slideUp('slow', function() {
                $('.rate .confirmation').remove();
                $('.rating_stars').slideDown('slow');
              });
            });
            
        }
        
      });
    });
  });
  
  
  $('.prev-image').click(prev_click);
  $('.next-image').click(next_click);
  
  $(document).keypress(function(e) {
    switch(e.keyCode) {
      case 37:  // left 
        prev_click();
        break;
        
      case 39:  // right
        next_click();
        break;
    }
  });
});
