// User rating stuff
$(document).ready(function() {
  // Hover over star
  $('.user_rating img').hover(function() {
    $(this).prevAll('img').attr('src', 'images/star_full.gif');
    $(this).attr('src', 'images/star_full.gif');
    $(this).nextAll('img').attr('src', 'images/star_empty.gif');
    
  }, function() {
    var sel = $(this).parent().find('input[type=hidden]').val();
    var idx = 0;
    $(this).parent().find('img').each(function() {
      idx++;
      if (idx <= sel) {
        $(this).attr('src', 'images/star_full.gif');
      } else {
        $(this).attr('src', 'images/star_empty.gif');
      }
    });
  });
  
  // Click on star
  $('.user_rating img').click(function() {
    var idx = 0;
    var orig = this;
    
    $(this).parent().find('img').each(function() {
      idx++;
      if (orig == this) return false;
    });
    
    $(this).parent().find('input[type=hidden]').val(idx);
  });
  
  // Open review section
  $('#submit-review-box').hide();
  $('#submit-review-link').click(function() {
    $('#submit-review-box').slideDown();
    return false;
  });
  
  $('a.cancel-review').click(function() {
    $('#submit-review-box').slideUp();
    return false;
  });
  
  // Process iframe submit
  $('#submit-review-iframe').load(function() {
    var nfo = $('#submit-review-iframe').contents().find('div').text();
    if (nfo == '') return;
    nfo = eval('(' + nfo + ')');
    
    if (nfo.status == 0) {
      $('#submit-review-message').html('<p class="error">' + nfo.message + '</p>');
      
    } else {
      $('#submit-review-message').html('<p class="confirmation">' + nfo.message + '</p>');
      $('#submit-review-box form').animate({'opacity':1},500).slideUp();
    }
  });
});

