jQuery(document).ready(function(){

	// Bind the even listenner to the colour super attribute dropdown
	jQuery("select.attr_Color").change(function() { updateProductImages(jQuery(this)); });
    
});

function updateProductImages(sel)
{
	// Find the simple product id for the current value
	var attrID = sel.attr("id").replace(/attribute/, "");
	var selected_val = sel.val();
	if (selected_val)
	{		
		var selector = "#stockInfoData tr td."+attrID+":contains('"+selected_val+"')";
		var product_id = jQuery(selector).siblings('td.simpleproductid:first').text();		
		if (product_id > 0)
		{				
			var params = { product:product_id };
			// Replace the main image	
			var media_url = '/ajaxproductimages/fetch/media/';	
			jQuery('.product-img-box').load(media_url, params);
			// replace the gallery
			var gallery_url = '/ajaxproductimages/fetch/gallery/';
			jQuery('.product-gallery-box').load(gallery_url, params);
			// replace the upsells
			var upsells_url = '/ajaxproductimages/fetch/upsell/';
			jQuery('#product_tabs_upsell_products_tabbed_contents').load(upsells_url, params);
		}
	}
}

