// Preload large images so no jumping occurs on mouse-overs - adapted from mattfarina.com/2007/02/01/preloading_images_with_jquery
$(window).bind('load', function() {
    var preload = new Array();
    $(".product-thumb").each(function() {
        s = $(this).attr("src").replace("small/", "");
        preload.push(s)
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});

// Handle mouse-overs - adapted from javascript/jquery written for AVF by Trevor Morris at Creation Design and Marketing in Stafford, UK
$(document).ready(
	function() {
		$.meta.setType('class');
				
		$('#product-images a').click(function(){			
			$('#product-images a').removeClass('active');
			$(this).addClass('active');
			new_image_src = $(this).find('img').attr('src').replace('small/','');
			new_image_alt = $(this).find('img').attr('alt');
			new_image_class = $(this).find('img').attr('class').replace('product-thumb','product-main');
			new_image_obj = $('<img>').attr({'src':new_image_src,'alt':new_image_alt,'class':new_image_class,'height':$(this).data().height,'width':$(this).data().width});
			$('#product-main').empty().append(new_image_obj);
			return false;
		}).hover(
			function(){
				new_image_src = $(this).find('img').attr('src').replace('small/','');
				new_image_alt = $(this).find('img').attr('alt');
				new_image_class = $(this).find('img').attr('class').replace('product-thumb','product-main');
				new_image_obj = $('<img>').attr({'src':new_image_src,'alt':new_image_alt,'class':new_image_class,'height':$(this).data().height,'width':$(this).data().width});
				$('#product-main').empty().append(new_image_obj);
			},
			function(){
				new_image_src = $('#product-images a.active img').attr('src').replace('small/','');
				new_image_alt = $('#product-images a.active img').attr('alt');
				new_image_class = $('#product-images a.active img').attr('class').replace('product-thumb','product-main');
				new_image_obj = $('<img>').attr({'src':new_image_src,'alt':new_image_alt,'class':new_image_class,'height':$('#product-images a.active').data().height,'width':$('#product-images a.active').data().width});
				$('#product-main').empty().append(new_image_obj);
			}
		);
	}
);