--- uc_option_image.js	2009-04-14 11:59:30.000000000 -0700
+++ uc_option_image.corrected.js	2010-09-07 14:22:00.000000000 -0700
@@ -1,6 +1,24 @@
 
 var UCOI = UCOI || {};
 
+jQuery.fn.uc_ucoi_attach = function() {
+  // Selects                                      
+  $('select.form-select', this).change(function(){
+    if (aid = UCOI.getAID(this)){
+      UCOI.switchImage(aid, this, Drupal.settings.UCOI.size);  
+    }
+  });
+  
+  // Radios                                      
+  $('.form-radios input', this).click(function(){
+    if (aid = UCOI.getAID(this)){
+      UCOI.switchImage(aid, this, Drupal.settings.UCOI.size);  
+    }
+  });
+  return this;
+}
+
+
 /**
  * Initialize.
  */
@@ -11,7 +29,10 @@ UCOI.init = function() {
   this.noimage = Drupal.settings.UCOI.noimage;
   this.attributes = Drupal.settings.UCOI.attributes;
   this.defaultSize = Drupal.settings.UCOI.default_size;
+  this.use_lightbox = Drupal.settings.UCOI.use_lightbox;
+  this.nodeid = Drupal.settings.UCOI.nodeid;
   
+/*
   // Selects                                      
   $('.add-to-cart select.form-select').change(function(){
     if (aid = UCOI.getAID(this)){
@@ -25,47 +46,91 @@ UCOI.init = function() {
       UCOI.switchImage(aid, this, size);  
     }
   });
+*/
+
+  //onload functions
+  //$('.add-to-cart .form-radios input').onload(function(){
+  //  if (aid = UCOI.getAID(this)){
+  //    UCOI.switchImage(aid, this, size);
+  //  }
+  //});
+  $('.add-to-cart').uc_ucoi_attach();
+
+
 };
 
 /**
  * Switch an option image.
  */
 UCOI.switchImage = function(aid, input, size) {
-  var pid = $(input).parents('.node').attr('id');
-  var nid = pid.replace('node-', '');             
+  /*var pid = $(input).parents('.node').attr('id');
+  var nid = pid.replace('node-', '');             */
+  var nid=this.nodeid;
   var oid = $(input).val(); 
-  var image = $(input).parents('.content').children('img.uc-option-image');       
-          
+  var image;
+  /*var lightbox = $(input).parents('.content').children('div.uc-option-image-block').children('a.lightbox-processed');  */
+  var lightbox = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed'); 
+  
+  if (this.use_lightbox)
+/*     image = $(input).parents('.content').children('div.uc-option-image-block').children('a.lightbox-processed').children('img.uc-option-image'); */
+        image = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed').children('img.uc-option-image'); 
+  else
+/*     image = $(input).parents('.content').children('div.uc-option-image-block').children('img.uc-option-image');*/
+     image =  $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('img.uc-option-image');
+
+
   // Make sure we have permission to switch this attribute
-  if (this.attributes[aid] === 0){
+  if (this.attributes[aid] == 0){
     return;
   }
           
   try {          
     var images = this.images[nid][aid];
-    
-    if (images[oid].derivative){ 
-      this.switchImageEffect(image, images[oid].derivative);
-    } 
+
+    if ((oid=="" || images[oid].derivative=="") && image){
+	parentImage = image[0].parentNode;
+	parentImage.removeChild(image[0]);    
+    }else if (image[0] && images[oid].derivative){ 
+      this.switchImageEffect(image, lightbox, images[oid]);
+    } else if (image[0] == null){
+       if (this.use_lightbox)
+/*         parentImage = $(input).parents('.content').children('div.uc-option-image-block').children('a.lightbox-processed');*/
+           parentImage =  $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed');	       
+       else
+/*         parentImage = $(input).parents('.content').children('div.uc-option-image-block');*/
+            parentImage = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block');
+
+       parentImage[0].innerHTML = "<img src=\""+images[oid].derivative+"\" class=\"uc-option-image\">";
+       // objectToAppend = document.createElement("img");
+       // objectToAppend.setAttribute("src",images[oid].derivative);
+       // objectToAppend.setAttribute("class","uc-option-image");
+       // parentImage[0].appendChild(objectToAppend);
+       if (this.use_lightbox)
+          $(lightbox).attr('href',images[oid].lightbox);
+    }
   }
   catch (e){   
-    this.switchImageEffect(image, this.noimage); 
+    this.switchImageEffect(image, lightbox, this.noimage); 
   }
 };
 
 /**
  * Switch the imagepath based on the selected effect.
  */
-UCOI.switchImageEffect = function(image, imagepath) {
-  switch(this.effect){
+UCOI.switchImageEffect = function(image, lightbox, imageproperty) {
+  
+   if (this.use_lightbox)
+      $(lightbox).attr('href',imageproperty.lightbox);
+
+   switch(this.effect){
     case 'fade':
-      $(image).fadeOut(200, function(){
-        $(this).attr('src', imagepath).fadeIn(200);
+      $(image).fadeOut(200, function(){		      
+        $(this).attr('src', imageproperty.derivative).fadeIn(200);
       });
       break;
       
     default:
-      $(image).attr('src', imagepath); 
+      $(image).attr('src', imageproperty.derivative); 
   }
 };
 
@@ -81,4 +146,4 @@ if (Drupal.jsEnabled) {
   $(function(){
     UCOI.init();
   });
-}
\ No newline at end of file
+}
