$(document).ready(function(){
  
  // Toopltips for Product thumbnails
  $('ul.overview a').tooltip({
    showURL: false,
    showBody: "::",
    track: true, 
    delay: 0, 
    fade: 250
  });
  
  // Lightbox for Product photos
  $('a.lightbox').lightBox();

  // New inline popup orderform
  $('a.plus').click(function(){      
    $('body').append('<div id="overlay"></div>');
    $('#overlay').css({'height':$(document).height()}).hide().show();
    $('.order_form').hide();
    $('#prices tr').removeClass('active');   
    $(this).next('.order_form').toggle(); 
    var row = $(this).parents('tr');
    row.addClass('active');
    return false;
  });
  
  $('a.cancel').live('click', function(){
    $('.order_form').hide(); 
    $('#overlay').remove(); 
    $('#prices tr').removeClass('active'); 
    return false;
  });
  
  $('.nojs').hide();
  $('.order_trigger').show();
  
  $('.order_form form').submit(function(){ 
    $(this).ajaxSubmit({        
      url: $(this).attr('action') + '.js',
      success: function(result){
        add_to_cart(result);
      }
    });
    return false;
  });

  $(window).bind('resize', function() { 
    if(cart.length > 0){
      var windowHeight = $(window).height();
      var contentHeight = $('#container').height();
      if(windowHeight > contentHeight){
        cart.css({'bottom':((windowHeight-contentHeight) + 75) + 'px'});
      }
    }
  });

  $('#slideshow').cycle({
    timeout: 3000,
    speedIn: 1000,
    speedOut: 2000
  });
  var cart = $('#shoppingCart');
  if(cart.length > 0){
    var windowHeight = $(window).height();
    var contentHeight = $('#container').height();
    if(windowHeight > contentHeight){
      cart.css({'bottom':((windowHeight-contentHeight) + 75) + 'px'});
    }
  }

}); 

function add_to_cart(result){ 
  $('.order_form').hide(); 
  $('#overlay').remove();
  $('#prices tr').removeClass('active');
  var height = $('#shoppingCart').height() + 24;
  $('#shoppingCart').prepend('<div class="notice"></div>').hide().fadeIn('fast');
  $('#shoppingCart .notice').css({'height':height });
  setTimeout(function(){ 
    $('.notice').fadeOut('slow');
  }, 3000);           
}
