// Cufon
Cufon.replace('div#left-nav li, ul#main-nav li, h1, h2, h3',{hover:true});

$(function() {
  
  // Portfolio sizing
  var total = 0;
  var margin = parseInt($('div#portfolio li').css('margin-right'));
  $('div#portfolio img').each(function() {
    total += $(this).attr('width') + margin;
  });
  $('div#portfolio ol').css('width',total);
  
  
  // Homepage fading images
  $('#portfolio-home ul').innerfade({ 
    containerheight: '505px',
    speed: 'slow',
    timeout: 3000
  });
  
  
  // Lazy loading portfolio images
  $("div#portfolio ol img").lazyload({ 
      placeholder : "/i/grey.gif",
      effect : "fadeIn",
      container: $("div#portfolio"),
      threshold: 300
  });
  
  // External links in new window
  $('a[rel=external]').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
  
  // Infield labels for forms
  $("form label").inFieldLabels();
  
  // Disable right click...
  $(document).bind("contextmenu",function(e){return false;});  
  
  // Product Page Thumbnail Stuff
  $('ul.product-thumbnails li a').click(function() {
    // Handle click on thumbnail
    var img = $(this).attr('href');
    var caption = $(this).find('img').attr('alt');
    var larger_img_link = $(this).parent().parent().parent().find('div.product-image-container a');
    larger_img_link.empty();
    
    var i = new Image();
    $(i).load(function () {
      $(this).hide();
      $(this).attr('title',caption);
      larger_img_link.append(this);
      $(this).fadeIn();
      larger_img_link.parent().animate({height: $(this).attr('height')});
    }).error(function () {
      
    }).attr('src', img);
    
    // Need to change the 'a' attribute of the larger image.....
    var matches = img.match(/physical_product_image_([0-9]+)_([0-9]+)/);
    var new_url = '/physical_product_image_'+matches[1]+'_600.jpg';
    larger_img_link.attr('href',new_url);
    
    return false;
  });
  $('div.product-image-container a').fancybox();
  //Set initial height of image containers 
  $('div.product-image-container img').load(function() {
    $(this).parent().parent().css('height',$(this).attr('height'));
  });
  
  
});