$(document).ready(function() {
  
  var search = $('#search-box');
  var join = $('#join-input');
  var searchinput = search.find('INPUT:first');
  var joininput = join.find('INPUT:first');
  var shopbutton = $('.add-to-basket');

  $('.tickets-varieties').change(function(){
    var form = this;
    var row = $(form).parents('.row');
    var values = new Array(2);
    values[0] = $(form).val();
    values[1] = row.find('INPUT[name="varieties[]"]').val();
    $('.catalogue-ticket-selection input[type="image"]').hide();
    $.getJSON('/catalogue/varieties', [{name: "varieties", value: values}], function(sku){
      $(form).parents('.row').find('.sku').val(sku.id);
      $(form).parents('.row').find('.price').html(sku.price);
      $('.catalogue-ticket-selection input[type="image"]').show();
    });
  });
    
  //Focus the input field when you click the div of the search or email box
  search.click(function() { searchinput.focus(); });
  join.click(function() { joininput.focus(); });  
  
  //Handle the search box focus and blur
  searchinput.val('Search');
  searchinput.focus(function() {
    $(this).val('');
  });
  searchinput.blur(function() {
    if ($(this).val() == '')
      $(this).val('Search');
  });
  
  //Handle the email box focus and blur
  joininput.val('enter your email address and click GO to find out more!');
  joininput.focus(function() {
    $(this).val('');
  });
  joininput.blur(function() {
    if ($(this).val() == '')
      $(this).val('enter your email address and click GO to find out more!');
  });
  
  // Select all links that contains lightbox in the attribute rel
  $('a[rel=lightbox]').lightbox();
  $('#images a').lightbox();
  
  //Hovering for main menu top level items
  $('#menu a:visible img').hover(function() {
    var img = $(this);
    var src = img.attr('src');
    img.attr('src',src.replace('.gif','-hover.gif')).css({"padding-left": "10px"}); 
  }, function() {
    var img = $(this);
    var src = img.attr('src');
    img.attr('src',src.replace('-hover.gif','.gif')).css({"padding-left": "0px"});    
  });
  
  
  // Hovering for go button
  $('.go-button').hover(function() {
    $(this).css({"right": "-10px"}); 
  }, function() { 
    $(this).css({"right": "0px"});    
  });
  
    
  // Hovering for other items
  $('.jump').hover(function() {
    $(this).css({"padding-left": "10px"}); 
  }, function() { 
    $(this).css({"padding-left": "0px"});    
  });
  
  // Hovering for other items
  $('.jump-right').hover(function() {
    $(this).css({"padding-right": "0px"}); 
  }, function() { 
    $(this).css({"padding-right": "10px"});    
  });  
  
    
  //Handle hovering on the add to basket buttons
  shopbutton.hover(function() {
    src = $(this).attr('src');
    $(this).attr('src',src.replace('.gif','-hover.gif'));
  }, function() {
    src = $(this).attr('src');
    $(this).attr('src',src.replace('-hover.gif','.gif'));    
  });
});