 var MAX_IMG_SIZE = 80; // max width/height in px
 
 $(document).ready(function(){
    $('.attrib select').each(function() {
		$(this).after('<div id="selected_attrib_'+$(this).attr('name')+'"></div>');
    });
 }); 
 
 $(document).ready(function(){
    $('.attrib select').change(function() {
       var c = $(this).attr('name');
       if ($('#img_'+c).length > 0)   $('#img_'+c).fadeOut();
       if ($('#img_'+c).length > 0)   $('#img_'+c).remove();
	  $.getJSON(
            '/product_alt_ajax.php',
            {what:'attribval_images',val_ids:$('option:selected', this).val()},
            function(json){
               for (id in json) {
                    var img = json[id];
					var h = img['h'];
					var w = img['w'];
					// scale down images if they are > MAX_IMG_SIZE tall or wide;
					if (w > MAX_IMG_SIZE) {
						h *= MAX_IMG_SIZE/w;
						w = MAX_IMG_SIZE;
					} else if (h > MAX_IMG_SIZE) {
						w *= MAX_IMG_SIZE/h;
						h = MAX_IMG_SIZE;
					}
                   var img_html = '<img src="'+img['src']+'" id="img_'+c+'" width="'+w+'px" height="'+h+'px" />';
				   $('#selected_attrib_'+c).append(img_html).fadeIn();         
              }
        });
    });
 });