$(document).ready(function(){
  
  // Handles the Downloads
  $('#fotocd').hide();
  $('#prices input[type=radio]').click(function(){
    if($(this).hasClass('files')){
      $('#fotocd').show();
    }else{
      $('#fotocd input[type=checkbox]').attr('checked','');
      $('#fotocd').hide();
    };
  })
  
  $('a.icon').live('click', function(){
    $('a.icon').removeClass('active');
    $('tr').removeClass('hot');
    $(this)
      .addClass('active')
      .parents('tr').addClass('hot');
  })
  
  // Generic link for addeing Ajax to a swift link
  $('a.swift').live('click', function(){ 
    var href =  $(this).attr('href');
    var trigger = $(this);
    var body = $.get(href + '.js', function(data) { 
      trigger.swift('', { 
        dir: 'north', 
        duration: 300,  
        body: data,
        buttons: {
          cancel: {
            label: 'Cancel',
            action: function(){
              $('a.icon').removeClass('active');
              $('tr').removeClass('hot');
            }
          }
        }
      });
    });
    return false;
  });  
      
  // Image Zoom  
  if($('#image').length > 0){  
    $('#image').simplezoom({
      height : 326,
      width : 326,
      target: '#info'  
    });
  }

  // Table row highlighter
  $('table.ruled').table_highlighter();
  // Thumbnail highlighter
  $('#thumbnails').thumbnail_highlighter();
  
  // Plugin?
  // Keybindings for browsing the gallery with arrow keys
  $(document).keyup(function(event){
    // Right arrow = next image
    if(event.which == 39 && $('#next').length > 0) { 
      var url = $('a#next').attr('href');
      document.location = url
    } 
    // Right arrow = next page
    if(event.which == 39 && $('a.next_page').length > 0) { 
      var url = $('a.next_page').attr('href');
      document.location = url
    }
    // Up arrow = back (up a level)
    if(event.which == 38 && $('.competition a').length > 0) { 
      var url = $('.competition a').attr('href');
      document.location = url
    }
    // Left arrow = previous
    if(event.which == 37 && $('#prev').length > 0) { 
      var url = $('a#prev').attr('href');
      document.location = url
    } 
    // Left arrow = previous_page
    if(event.which == 37 && $('a.previous_page').length > 0) { 
      var url = $('a.previous_page').attr('href');
      document.location = url
    }
  });
  
  var orderSearch = $('#order_search');
  $('input[type=submit]', orderSearch).hide();
  $('select', orderSearch).change(function(){
    var url = orderSearch.attr('action') + '.js';
    orderSearch.ajaxSubmit({
      url:url,
      target:'#user_orders'
    });
  });
  
});

