? LICENSE.txt
Index: uc_option_image.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_option_image/uc_option_image.info,v
retrieving revision 1.4
diff -u -p -r1.4 uc_option_image.info
--- uc_option_image.info	14 Apr 2009 18:59:30 -0000	1.4
+++ uc_option_image.info	13 Jan 2011 02:36:46 -0000
@@ -4,3 +4,10 @@ dependencies[] = uc_attribute
 dependencies[] = imagecache
 package = "Ubercart - core (optional)"
 core = 6.x
+
+; Information added by drupal.org packaging script on 2009-04-21
+version = "6.x-1.4"
+core = "6.x"
+project = "uc_option_image"
+datestamp = "1240286767"
+
Index: uc_option_image.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_option_image/uc_option_image.install,v
retrieving revision 1.5
diff -u -p -r1.5 uc_option_image.install
--- uc_option_image.install	21 Apr 2009 22:09:09 -0000	1.5
+++ uc_option_image.install	13 Jan 2011 02:36:46 -0000
@@ -1,6 +1,10 @@
 <?php
+//$Id$
 
-function uc_option_image_install($no_image=NULL) {
+/**
+ * Implmenets hook_install
+*/
+function uc_option_image_install($no_image = NULL) {
   if (empty($no_image)) {
     return uc_option_image_install(drupal_get_path('module', 'uc_option_image') .'/no_image.png');
   }
Index: uc_option_image.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_option_image/uc_option_image.js,v
retrieving revision 1.5
diff -u -p -r1.5 uc_option_image.js
--- uc_option_image.js	14 Apr 2009 18:59:30 -0000	1.5
+++ uc_option_image.js	13 Jan 2011 02:36:46 -0000
@@ -1,84 +1,94 @@
+// $Id$
+/*
+ * @file uc_option_image.js
+ * Provides javascript for uc_option_images
+ * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html
+ * 
+ */
 
-var UCOI = UCOI || {};
 
-/**
- * Initialize.
- */
-UCOI.init = function() {
-  var size = Drupal.settings.UCOI.size;   
-  this.images = Drupal.settings.UCOI.images; 
-  this.effect = Drupal.settings.UCOI.effect;
-  this.noimage = Drupal.settings.UCOI.noimage;
-  this.attributes = Drupal.settings.UCOI.attributes;
-  this.defaultSize = Drupal.settings.UCOI.default_size;
-  
+Drupal.behaviors.uc_option_images = function(context) {
+	var getAID = function(input) {
+		var name = $(input).attr('name');
+		return name.match(/attributes\[([0-9]+)\]/)[1]; 
+	};
+	
+	var switchImage = function(aid, input, size) {
+		var nid= settings.nodeid;
+		var oid = $(input).val(); 
+		var image;
+		var lightbox = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed'); 
+		
+		if (settings.use_lightbox) {
+			image = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed').children('img.uc-option-image'); 
+		}
+		else {
+			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 (settings.attributes[aid] == 0){
+			return;
+		}
+						
+		try {          
+			var images = settings.images[nid][aid];
+			if ((oid == "" || images[oid].derivative == "") && image) {
+				parentImage = image[0].parentNode;
+				parentImage.removeChild(image[0]);    
+			} else if (image[0] && images[oid].derivative) { 
+				switchImageEffect(image, lightbox, images[oid]);
+			} else if (image[0] == null) {
+				if (settings.use_lightbox) {
+					parentImage =  $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('a.lightbox-processed');	       
+				}
+				else {
+					parentImage = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block');
+				}
+	
+				parentImage[0].innerHTML = "<img src=\""+images[oid].derivative+"\" class=\"uc-option-image\">";
+				if (settings.use_lightbox) {
+					$(lightbox).attr('href',images[oid].lightbox);
+				}
+			}
+		}
+		catch (e){   
+			switchImageEffect(image, lightbox, settings.noimage); 
+		}
+	};
+	
+	var switchImageEffect = function(image, lightbox, imageproperty) {
+		if (settings.use_lightbox) {
+			$(lightbox).attr('href', imageproperty.lightbox);
+		}
+	
+		switch (settings.effect){
+			case 'fade':
+				$(image).fadeOut(200, function(){		      
+					$(this).attr('src', imageproperty.derivative).fadeIn(200);
+				});
+				break;
+				
+			default:
+				$(image).attr('src', imageproperty.derivative); 
+		}
+	};
+	
+  var settings = Drupal.settings.UCOI;
+	var size = settings.size;
+    
   // Selects                                      
-  $('.add-to-cart select.form-select').change(function(){
-    if (aid = UCOI.getAID(this)){
-      UCOI.switchImage(aid, this, size);  
+  $('.add-to-cart select.form-select:not(.uoi-processed)', context).addClass('uoi-processed').change(function(){
+    if ((aid = getAID(this))){
+      switchImage(aid, this, size);  
     }
   });
   
   // Radios                                      
-  $('.add-to-cart .form-radios input').click(function(){
-    if (aid = UCOI.getAID(this)){
-      UCOI.switchImage(aid, this, size);  
+  $('.add-to-cart .form-radios input:not(.uoi-processed)', context).addClass('uoi-processed').click(function(){
+    if ((aid = getAID(this))){
+      switchImage(aid, this, size);  
     }
   });
-};
-
-/**
- * Switch an option image.
- */
-UCOI.switchImage = function(aid, input, size) {
-  var pid = $(input).parents('.node').attr('id');
-  var nid = pid.replace('node-', '');             
-  var oid = $(input).val(); 
-  var image = $(input).parents('.content').children('img.uc-option-image');       
-          
-  // Make sure we have permission to switch this attribute
-  if (this.attributes[aid] === 0){
-    return;
-  }
-          
-  try {          
-    var images = this.images[nid][aid];
-    
-    if (images[oid].derivative){ 
-      this.switchImageEffect(image, images[oid].derivative);
-    } 
-  }
-  catch (e){   
-    this.switchImageEffect(image, this.noimage); 
-  }
-};
-
-/**
- * Switch the imagepath based on the selected effect.
- */
-UCOI.switchImageEffect = function(image, imagepath) {
-  switch(this.effect){
-    case 'fade':
-      $(image).fadeOut(200, function(){
-        $(this).attr('src', imagepath).fadeIn(200);
-      });
-      break;
-      
-    default:
-      $(image).attr('src', imagepath); 
-  }
-};
-
-/**
- * Get attribute AID from an input.
- */
-UCOI.getAID = function(input) {
-  var name = $(input).attr('name');
-  return name.match(/attributes\[([0-9]+)\]/)[1]; 
-};
-
-if (Drupal.jsEnabled) {
-  $(function(){
-    UCOI.init();
-  });
-}
\ No newline at end of file
+}
Index: uc_option_image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_option_image/uc_option_image.module,v
retrieving revision 1.22
diff -u -p -r1.22 uc_option_image.module
--- uc_option_image.module	16 Apr 2009 19:26:50 -0000	1.22
+++ uc_option_image.module	13 Jan 2011 02:36:47 -0000
@@ -13,9 +13,7 @@
 // using layers of PNG images
 
 /* -----------------------------------------------------------------
-
   Hook Implementations 
-
 ------------------------------------------------------------------ */  
 
 /**
@@ -36,13 +34,15 @@ function uc_option_image_nodeapi(&$node,
       $node->option_images = array();
       $node->option_images_cached = array();
       $node->attributes = uc_product_get_attributes($node->nid);
+      $attribute_same_image = variable_get('uc_option_image_same_image', '');
        
       if (count($node->attributes)) {
         $page_size = variable_get('uc_option_image_page_size', 'preview');     
-        foreach($node->attributes AS $attribute) {
+        foreach ($node->attributes as $attribute) {
           if (count($attribute->options)) {
-            foreach($attribute->options AS $option) {
-              $file = uc_option_image_load($node->nid, $attribute->aid, $option->oid);
+            foreach ($attribute->options as $option) {
+              $same = $attribute_same_image[$attribute->aid];
+              $file = uc_option_image_load($node->nid, $attribute->aid, $option->oid, $same);
               if ($file && $file->filepath) {
                 $node->option_images[] = $file;
                 // Load imagecached option images
@@ -50,7 +50,7 @@ function uc_option_image_nodeapi(&$node,
                   $node->option_images_cached[$option->oid] = imagecache_create_url($page_size, $file->filepath);
                 }
                 else {
-                  $node->option_images_cached[$option->oid] = $file->filepath;
+                  $node->option_images_cached[$option->oid] = '/'. $file->filepath;
                 }
               }
             }
@@ -64,12 +64,13 @@ function uc_option_image_nodeapi(&$node,
       // @todo refactor
       if (isset($node->content['add_to_cart'])){ 
         if (user_access('view option images')){
-          if (count($node->attributes)){ 
+          if (count($node->attributes)) { 
             $attributes = $node->attributes;          
-            $first_attribute = array_shift($node->attributes);  
+            $attribute_same_image = variable_get('uc_option_image_same_image', '');
+            $first_attribute = array_shift($node->attributes);               
             array_unshift($node->attributes, $first_attribute);
             $page_size = variable_get('uc_option_image_page_size', 'preview');  
-            $teaser_size = variable_get('uc_option_image_teaser_size', 'thumbnail'); 
+            $teaser_size = variable_get('uc_option_image_teaser_size', 'thumbnail');
             $size = $a4 ? $page_size : $teaser_size;
                    
             // Pass attributes to uc_option_image to populate JS settings
@@ -80,16 +81,17 @@ function uc_option_image_nodeapi(&$node,
             // Determine if we have a default option using
             // the first attribute's default option
             if ($first_attribute->default_option){
-              $default_option = $first_attribute->default_option;
+              $default_option = $first_attribute->default_option;              
             }
                                        
             // Load the default image file
-            $file = uc_option_image_load($node->nid, $first_attribute->aid, $default_option);
-                      
+            $same = $attribute_same_image[$first_attribute->aid];
+            
+            $file = uc_option_image_load($node->nid, $first_attribute->aid, $default_option, $same);                     
             // Display the image based on teaser/page view
             // Ensure that original file exists
             if ($file->filepath && file_exists($file->filepath)){
-              $image = theme('uc_option_image', $file, $size);  
+              $image = theme('uc_option_image', $first_attribute->aid, $file, $size);  
             }
             else {   
               $image = theme('uc_option_image_no_image', $node, $size);
@@ -97,14 +99,14 @@ function uc_option_image_nodeapi(&$node,
             
             // Preload images
             if ($a4){
-              $preloaded_images = theme('uc_option_image_preloaded', $node, $size); 
+              $preloaded_images = theme('uc_option_image_preloaded', $first_attribute->aid, $node, $size); 
             }
             
             $node->content['option_image'] = array(
                 '#value' => $image . $preloaded_images,
                 '#access' => user_access('view option images'),
                 '#weight' => (int) variable_get('uc_option_image_node_weight', '10'),
-              );      
+              );                             
           }
         }
       }
@@ -115,43 +117,51 @@ function uc_option_image_nodeapi(&$node,
 /**
  * Implementation of hook_form_alter();
  */
-function uc_option_image_form_alter(&$form, &$form_state, $form_id){ 
+function uc_option_image_form_alter(&$form, &$form_state, $form_id){   
+  //print $form_id;
   switch($form_id){
     // Attribute options form
     case 'uc_object_options_form': 
       // Make sure we are a node's options page
-      if (!is_numeric(arg(1)) && arg(0) != 'node'){
+      if (!is_numeric(arg(1)) && arg(0) != 'node') {
         return;
       }           
       // Add option image previews and browse fields
-      if ($aids = element_children($form['attributes'])){  
+      if ($aids = element_children($form['attributes'])) {  
         $node = menu_get_object();
         $nid = $node->nid;
         $attributes = variable_get('uc_option_image_attributes', '');
-                                       
-        foreach($aids AS $aid) { 
+        $attribute_same_image = variable_get('uc_option_image_same_image','');                               
+        foreach ($aids as $aid) {         
           // Make sure the attribute is switchable
-          if (isset($attributes[$aid]) && !$attributes[$aid]){
+          if (isset($attributes[$aid]) && !$attributes[$aid]) {
             continue;
           }
-          
           // Display fields
-          if ($oids = element_children($form['attributes'][$aid]['options'])){ 
-            foreach($oids AS $oid) { 
-              $file = uc_option_image_load($nid, $aid, $oid);
-              
+          if ($oids = element_children($form['attributes'][$aid]['options'])) {
+            $same = $attribute_same_image[$aid]; 
+            foreach($oids as $oid) { 
+              $file = uc_option_image_load($nid, $aid, $oid, $same);
               if (!empty($file)) {
                 $form['attributes'][$aid]['options'][$oid]['option_image_preview'] = array(
                   '#type' => 'markup',                       
-                  '#value' => theme('uc_option_image', $file, variable_get('uc_option_image_preview_size', '_original')),       
-                );
-                $form['attributes'][$aid]['options'][$oid][uc_option_image_id($nid, $aid, $oid)] = array(
-                  '#type' => 'file',
-                  '#title' => t('Image'),
-                  '#name' => 'files[' . uc_option_image_id($nid, $aid, $oid) . ']',
-                  '#size' => 8,
-                  '#default_value' => $file->filename,       
+                  '#value' => theme('uc_option_image', $aid, $file,
+                                    variable_get('uc_option_image_preview_size', '_original')),       
                 );
+                $field_id = uc_option_image_id($nid, $aid, $oid);
+                if ($file->filename != 'option_image_0_0_0') {
+                  //add a remove checkbox
+                  $form['attributes'][$aid]['options'][$oid]['remove'] = array(
+                    '#type' => 'checkbox',
+                  );
+                }
+                else {
+                  $form['attributes'][$aid]['options'][$oid][$field_id] = array(
+                    '#parents' => array($field_id), //no tree here
+                    '#type' => 'file',
+                    '#size' => 8,
+                  );
+                }
               }
             }
           } 
@@ -171,7 +181,8 @@ function uc_option_image_form_alter(&$fo
       $presets = imagecache_presets();
       $size_options = uc_option_image_get_size_options();
       $attribute_options = uc_option_image_get_attribute_options(); 
-      
+      $attribute_same_image = $attribute_options;
+
       // Ensure we have imagecache presets otherwise
       // display a message so they can create presets first.
       if (empty($presets)){
@@ -183,100 +194,140 @@ function uc_option_image_form_alter(&$fo
       
       $form['#validate'][] = 'uc_option_image_uc_attribute_admin_settings_validate';
       $form['uc_option_image'] = array(
-          '#type' => 'fieldset',
-          '#title' => t('Option Images'),
-        );    
+        '#type' => 'fieldset',
+        '#title' => t('Option Images'),
+      );    
       $form['uc_option_image']['uc_option_image_js'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Switch Images'),
+        '#description' => t('Use JavaScript to switch attribute option images when
+                            selected from a select field if it is available.'),
+        '#default_value' => variable_get('uc_option_image_js', TRUE),
+      );
+      $form['uc_option_image']['uc_option_image_show_noimage'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Show No Image Icon'),
+        '#description' => t('Show no image icon when there is no image associated with an attribute.'),
+        '#default_value' => variable_get('uc_option_image_show_noimage', FALSE),
+      );
+      if (module_exists('lightbox2')) {
+        $form['uc_option_image']['uc_option_image_use_lightbox'] = array(
           '#type' => 'checkbox',
-          '#title' => t('Switch Images'),
-          '#description' => t('Use JavaScript to switch attribute option images when selected from a select field if it is available.'),
-          '#default_value' => variable_get('uc_option_image_js', TRUE),
+          '#title' => t('Use Lightbox'),
+          '#description' => t('Use lightbox to show attribute images when image in front page is clicked.'),
+          '#default_value' => variable_get('uc_option_image_use_lightbox', TRUE),
         );
-      if (!empty($attribute_options)){
+      }
+
+      if (!empty($attribute_options)) {
         $form['uc_option_image']['uc_option_image_attributes'] = array(
-            '#type' => 'checkboxes',
-            '#title' => t('Switch Attributes'),
-            '#description' => t('Only checked attributes will attempt to be switched when changed. For example you would want to check "Shoe Style" but not "Shoe Size", as "Shoe Size" most likely does not have associated images.'),
-            '#options' => $attribute_options,
-            '#default_value' => variable_get('uc_option_image_attributes', ''),
-          );
+          '#type' => 'checkboxes',
+          '#title' => t('Enable option image for the following attributes'),
+          '#description' => t('Only checked attributes will attempt to be switched when changed. For example you would want to check "Shoe Style" but not "Shoe Size", as "Shoe Size" most likely does not have associated images.'),
+          '#options' => $attribute_options,
+          '#default_value' => variable_get('uc_option_image_attributes', array()),
+        );
+
+          
+        $form['uc_option_image']['uc_option_image_same_image'] = array(
+          '#type' => 'checkboxes',
+          '#title' => t('Set option images toi be the same for all prducts'),
+          '#options' => $attribute_same_image,
+          '#default_value' => variable_get('uc_option_image_same_image', array()),
+        );
+
       }
+      
       $form['uc_option_image']['uc_option_image_effect'] = array(
-          '#type' => 'select',
-          '#title' => t('Switch Effect'),
-          '#description' => t('Select one of the various image switching effects.'),
-          '#options' => array(
-              'none' => t('None'),
-              'fade' => t('Fade'),
-            ),
-          '#default_value' => variable_get('uc_option_image_effect', 'fade'),
-        );     
+        '#type' => 'select',
+        '#title' => t('Switch Effect'),
+        '#description' => t('Select one of the various image switching effects.'),
+        '#options' => array(
+            'none' => t('None'),
+            'fade' => t('Fade'),
+          ),
+        '#default_value' => variable_get('uc_option_image_effect', 'fade'),
+      );
+      
       $form['uc_option_image']['uc_option_image_preview_size'] = array(
-          '#type' => 'select',
-          '#title' => t('Preview Image Size'),
-          '#description' => t('Image size to display in the option table when editing a product.'),
-          '#options' => $size_options,
-          '#default_value' => variable_get('uc_option_image_preview_size', '_original') ,
-        );
+        '#type' => 'select',
+        '#title' => t('Preview Image Size'),
+        '#description' => t('Image size to display in the option table when editing a product.'),
+        '#options' => $size_options,
+        '#default_value' => variable_get('uc_option_image_preview_size', '_original') ,
+      );
+      
       $form['uc_option_image']['uc_option_image_teaser_size'] = array(
-          '#type' => 'select',
-          '#title' => t('Teaser Image Size'),
-          '#description' => t('Image size which will display in teaser listings.'),
-          '#options' => $size_options,
-          '#default_value' => variable_get('uc_option_image_teaser_size', '_original'),
-        );
+        '#type' => 'select',
+        '#title' => t('Teaser Image Size'),
+        '#description' => t('Image size which will display in teaser listings.'),
+        '#options' => $size_options,
+        '#default_value' => variable_get('uc_option_image_teaser_size', '_original'),
+      );
+   
       $form['uc_option_image']['uc_option_image_page_size'] = array(
+        '#type' => 'select',
+        '#title' => t('Page Image Size'),
+        '#description' => t('Image size which will display in a full page view.'),
+        '#options' => $size_options,
+        '#default_value' => variable_get('uc_option_image_page_size', '_original'),
+      );
+
+      if (module_exists('lightbox2')) {
+        $form['uc_option_image']['uc_option_image_lightbox_size'] = array(
           '#type' => 'select',
-          '#title' => t('Page Image Size'),
-          '#description' => t('Image size which will display in a full page view.'),
+          '#title' => t('Lightbox Image Size'),
+          '#description' => t('Image size which will display in lightbox when image in page is clicked.'),
           '#options' => $size_options,
-          '#default_value' => variable_get('uc_option_image_page_size', '_original'),
+          '#default_value' => variable_get('uc_option_image_lightbox_size', '_original'),
         );
+      }
+     
       $form['uc_option_image']['uc_option_image_node_weight'] = array(
-          '#type' => 'weight',
-          '#title' => t('Option Image Weight'),
-          '#description' => t('Weight used to determine where the option image will display.'),
-          '#default_value' => variable_get('uc_option_image_node_weight', '9'),
-        );
-        
+        '#type' => 'weight',
+        '#title' => t('Option Image Weight'),
+        '#description' => t('Weight used to determine where the option image will display.'),
+        '#default_value' => variable_get('uc_option_image_node_weight', '9'),
+      );
+      
       // Derivative sizes
       $presets = imagecache_presets();
-      if (count($presets)){
+      if (count($presets)) {
         $form['uc_option_image']['sizes'] = array(
-            '#type' => 'fieldset',
-            '#title' => t('Sizes'),
-            '#collapsible' => TRUE,
-            '#collapsed' => TRUE,
-            '#description' => t('Previously uploaded images will not resize when changes are made. Image derivatives must be rebuilt at !path', array('!path' => 'admin/content/node')),
-          );
-        foreach($presets as $preset) {
+          '#type' => 'fieldset',
+          '#title' => t('Sizes'),
+          '#collapsible' => TRUE,
+          '#collapsed' => TRUE,
+          '#description' => t('Previously uploaded images will not resize when changes are made. Image derivatives must be rebuilt at !path', array('!path' => 'admin/content/node')),
+        );
+        foreach ($presets as $preset) {
           $preset['width'] = 0;
           $preset['height'] = 0;
           foreach ($preset['actions'] as $action) {
             foreach (array('width','height') as $ord) {
               if (isset($action['data'][$ord])) {
                 if ((int)($action['data'][$ord])) {
-	          $preset[$ord] = $action['data'][$ord];
+                  $preset[$ord] = $action['data'][$ord];
                 }
-                elseif (!empty($preset[$ord]) && preg_match('/^([1-9][0-9]*)%$/',$action['data'][$ord],$matches)) {
-	          $preset[$ord] = $preset[$ord] * $matches[1][0] / 100;
+                elseif (!empty($preset[$ord]) && preg_match('/^([1-9][0-9]*)%$/', $action['data'][$ord], $matches)) {
+                  $preset[$ord] = $preset[$ord] * $matches[1][0] / 100;
                 }
               }
             }
           }
 
           $form['uc_option_image']['sizes']['uc_option_image_size_' . $preset['presetname'] . '_width'] = array(
-              '#type' => 'textfield',
-              '#title' => $preset['presetname'] . ' ' . t('Width'),
-              '#default_value' => $preset['width'],
-              '#required' => TRUE,
-            );
+            '#type' => 'textfield',
+            '#title' => $preset['presetname'] . ' ' . t('Width'),
+            '#default_value' => $preset['width'],
+            '#required' => TRUE,
+          );
           $form['uc_option_image']['sizes']['uc_option_image_size_' . $preset['presetname'] . '_height'] = array(
-              '#type' => 'textfield',
-              '#title' => $preset['presetname'] . ' ' . t('Height'),
-              '#default_value' => $preset['height'],
-              '#required' => TRUE,
-            );
+            '#type' => 'textfield',
+            '#title' => $preset['presetname'] . ' ' . t('Height'),
+            '#default_value' => $preset['height'],
+            '#required' => TRUE,
+          );
         }
       }
                                               
@@ -286,9 +337,7 @@ function uc_option_image_form_alter(&$fo
 }
 
 /* -----------------------------------------------------------------
-
   General Functionality 
-
 ------------------------------------------------------------------ */
 
 /**
@@ -353,29 +402,48 @@ function uc_option_image($node, $attribu
     $data = array();
     $data['size'] = $size;
     $data['effect'] = variable_get('uc_option_image_effect', 'fade');
-    $data['noimage'] = theme('uc_option_image_no_image_path', $node, $size);
+    if (variable_get('uc_option_image_show_noimage',FALSE)) {
+      $data['noimage'] = theme('uc_option_image_no_image_path', $node, $size);
+    }
     $data['attributes'] = variable_get('uc_option_image_attributes', '');
+    $data['use_lightbox'] = variable_get('uc_option_image_use_lightbox',FALSE);
+    $data['nodeid'] = $node->nid;
     $prep = TRUE;
   }                             
-  
+
+  $lightbox_preset = variable_get('uc_option_image_lightbox_size', 'preview');
+  $shownoimage = variable_get('uc_option_image_show_noimage', FALSE);
   // Populate Drupal.settings.UCOI.images
   // this prevents the module from needing additional
   // HTTP requests in order to find the image needed
   // for each option.                               
-  if (count($attributes)){               
-    foreach($attributes AS $aid => $attribute){  
-      if (count($attribute->options)){     
-        foreach($attribute->options AS $oid => $option){
-          $file = uc_option_image_load($node->nid, $aid, $oid);
-                            
-          if ($file->filepath){              
+  if (count($attributes)) {             
+    $attribute_same_image = variable_get('uc_option_image_same_image', array()); 
+    foreach ($attributes as $aid => $attribute) {  
+      if (count($attribute->options)) {             
+        $same = $attribute_same_image[$aid];
+        foreach ($attribute->options as $oid => $option) {
+          $file = uc_option_image_load($node->nid, $aid, $oid, $same);
+          $lightbox_path = '';                 
+          if ($file->filepath) { 
+            $derivative= $node->option_images_cached[$oid];
+            if (!$shownoimage && $file->filename=='option_image_0_0_0') {
+              $derivative = '';   
+            }
+            if ($lightbox_preset == '_original') {
+              $lightbox_path = '/'. $file->filepath;
+            }
+            else {
+              $lightbox_path = imagecache_create_url($lightbox_preset, $file->filepath);           
+            }
             $data['images'][$node->nid][$aid][$oid] = array(
-                'nid' => $node->nid,
-                'aid' => $aid,
-                'oid' => $oid,
-                'filepath' => $file->filepath,
-                'derivative' => $node->option_images_cached[$oid],
-              );     
+              'nid' => $node->nid,
+              'aid' => $aid,
+              'oid' => $oid,
+              'filepath' => $file->filepath,
+              'derivative' => $derivative,
+              'lightbox' => $lightbox_path
+            );     
           }
         }
       }
@@ -391,21 +459,38 @@ function uc_option_image($node, $attribu
  * 
  * @todo: static cache
  */
-function uc_option_image_load($nid, $aid, $oid) {
+function uc_option_image_load($nid, $aid, $oid, $same) {
   static $files;
-  $filename = uc_option_image_id($nid, $aid, $oid);
-  if (empty($files[$filename])){
-    $files[$filename] = db_fetch_object(db_query("SELECT * FROM {files} WHERE filename = '%s'", $filename));
-    if (empty($files[$filename])) {
+  // try most specific image first
+  $filename = uc_option_image_id($nid, $aid, $oid, FALSE);
+  
+  $filenamesame = uc_option_image_id($nid, $aid, $oid, $same);
+
+  if (empty($files[$filename]) && empty($files[$filenamesame])) {
+    //try most specific filename first
+    $files[$filename] = db_fetch_object(db_query("SELECT * FROM {files} WHERE filename like '%s'", $filename));
+    //if most specific image not found then try to find least specific
+    //try to find an image from an available node
+
+    if (empty($files[$filename]) && $same) {
+        $files[$filenamesame] = db_fetch_object(db_query("SELECT * FROM {files} WHERE filename like '%s' order by fid limit 1", $filenamesame));
+    }
+        
+    if (empty($files[$filename]) && empty($files[$filenamesame])) {      
       if ($nid) {
-        return uc_option_image_load(0, $aid, $oid);
+        return uc_option_image_load(0, $aid, $oid, $same);
       }
       elseif ($aid && $oid) {
-        return uc_option_image_load(0, 0, 0);
+        return uc_option_image_load(0, 0, 0, $same);
       }
     }
   }
-  return $files[$filename];
+  if (!empty($files[$filename])) {
+    return $files[$filename];
+  }
+  else {
+    return $files[$filenamesame];
+  }
 }
 
 /**
@@ -429,11 +514,11 @@ function uc_option_image_save($nid, $aid
     'file_validate_is_image' => array(),
   );
 
-  if ($file = file_save_upload(uc_option_image_id($nid, $aid, $oid),$validators)) {
+  if ($file = file_save_upload(uc_option_image_id($nid, $aid, $oid), $validators)) {
     $dest = file_create_path(file_directory_path() . '/option-images');
     file_check_directory($dest, FILE_CREATE_DIRECTORY);
     if (file_copy($file->filepath, $dest, FILE_EXISTS_REPLACE)) {
-      $file->filename = uc_option_image_id($nid,$aid,$oid);
+      $file->filename = uc_option_image_id($nid, $aid, $oid);
       $file->uid = $user->uid;
       $file->status = FILE_STATUS_PERMANENT;
       uc_option_image_delete($nid, $aid, $oid);
@@ -462,7 +547,7 @@ function uc_option_image_save($nid, $aid
  *   Results of db_query().
  */
 function uc_option_image_delete($nid, $aid, $oid) {
-  return db_query("DELETE FROM {files} WHERE filename = '%s'", uc_option_image_id($nid,$aid,$oid));    
+  return db_query("DELETE FROM {files} WHERE filename = '%s'", uc_option_image_id($nid, $aid, $oid));    
 }
 
 /**
@@ -478,13 +563,13 @@ function uc_option_image_get_size_option
   
   // imagecache 1.x
   if (IMAGECACHE_BASE == 1){
-    foreach((array) $presets AS $preset){ 
+    foreach((array) $presets as $preset){ 
       $options[$preset] = $preset;
     }
   }
   // imagecache 2.x
   else {
-    foreach((array) $presets AS $preset){ 
+    foreach((array) $presets as $preset){ 
       $options[$preset['presetname']] = $preset['presetname'];
     }
   }
@@ -509,14 +594,16 @@ function uc_option_image_get_attribute_o
 /**
  * Returns a unique ID corrosponding to the node id, attribute id, and option.
  */
-function uc_option_image_id($nid, $aid, $oid) {
-  return 'option_image_' . $nid . '_' . $aid . '_' . $oid;
+function uc_option_image_id($nid, $aid, $oid, $same=FALSE) {
+  $result =  'option_image_' . $nid . '_' . $aid . '_' . $oid;  
+  if ($same) {
+    $result =  'option_image_%_' . $aid . '_' . $oid;
+  }
+  return $result;
 }
 
 /* -----------------------------------------------------------------
-
   Form Handling 
-
 ------------------------------------------------------------------ */
 
 /**
@@ -524,10 +611,16 @@ function uc_option_image_id($nid, $aid, 
  */
 function uc_option_image_uc_object_options_form($form, &$form_state) {
   if ($aids = element_children($form_state['values']['attributes'])){ 
-    foreach($aids AS $aid){ 
-      if ($oids = element_children($form_state['values']['attributes'][$aid]['options'])){ 
-        foreach($oids AS $oid){
-          uc_option_image_save($form_state['values']['id'], $aid, $oid);
+    foreach ($aids as $aid){ 
+      if ($oids = element_children($form_state['values']['attributes'][$aid]['options'])) { 
+        foreach($oids as $oid) {
+          //uc_option_image_save($form_state['values']['id'], $aid, $oid);
+          if ($form_state['values']['attributes'][$aid]['options'][$oid]['remove']) {
+            uc_option_image_delete($form_state['values']['id'], $aid, $oid);
+          }
+          else {
+            uc_option_image_save($form_state['values']['id'], $aid, $oid);
+          }
         }                                                             
       } 
     } 
@@ -535,9 +628,7 @@ function uc_option_image_uc_object_optio
 }
 
 /* -----------------------------------------------------------------
-
   Themes 
-
 ------------------------------------------------------------------ */
 
 /**
@@ -586,17 +677,45 @@ function uc_option_image_theme() {
  * @return string
  *   Markup.
  */
-function theme_uc_option_image($file, $size = '_original') {
+function theme_uc_option_image($aid, $file, $size = '_original') {
+  $shownoimage = TRUE;
+  if(!variable_get('uc_option_image_show_noimage',FALSE) && $file->filename=='option_image_0_0_0') {
+    $file->filepath = NULL;
+    $shownoimage = FALSE;
+  }
   $info = pathinfo($file->filepath);               
   $attributes = array('class' => 'uc-option-image');
        
   // Display imagecache preset or the original image      
-  if ($size != '_original'){                    
-    return theme('imagecache', $size, $file->filepath, NULL, NULL, $attributes);                       
-  }      
-  else {                             
-    return theme('image', $file->filepath, NULL, NULL, $attributes, FALSE);  
+  $imagecache_image = '';
+  if ($shownoimage) {
+    if ($size != '_original'){                    
+      $imagecache_image = theme('imagecache', $size, $file->filepath, NULL, NULL, $attributes);                       
+    } else {                             
+      $imagecache_image =  theme('image', $file->filepath, NULL, NULL, $attributes, FALSE);  
+    }
+  } 
+
+  //create lightbox link 
+  $lightbox_preset = variable_get('uc_option_image_lightbox_size', 'preview');
+  if ($lightbox_preset == '_original') {
+    $lightbox_path = '/'.$file->filepath;
+  }
+  else {
+    $lightbox_path = imagecache_create_url($lightbox_preset, $file->filepath );
+  }
+
+  //determine if lightbox will be used
+  if (variable_get('uc_option_image_use_lightbox',FALSE)) {
+    $option_image = '<a rel="lightbox" alt=" " title="" href="'.$lightbox_path.'">'. $imagecache_image .'</a>';
+  }
+  else {
+    $option_image = $imagecache_image;
   }
+
+  $option_image = '<div class="uc-option-image-block">'.$option_image.'</div>';
+
+  return $option_image;
 }
 
 /**
@@ -610,17 +729,15 @@ function theme_uc_option_image($file, $s
  * @return string
  *   Markup.
  */
-function theme_uc_option_image_preloaded($node, $size = '_original') {
+function theme_uc_option_image_preloaded($aid, $node, $size = '_original') {
   $output = '<div id="uc-option-image-preloaded-' . $node->nid . '" class="uc-option-image-preloaded" style="display: none;">';
   
-  foreach((array) $node->option_images AS $i => $option_image){   
-    $output .= theme('uc_option_image', $option_image, $size);
+  foreach ((array) $node->option_images as $i => $option_image) {   
+    $output .= theme('uc_option_image', $aid, $option_image, $size);
   }
   
   $output .= theme('uc_option_image_no_image', $node, $size);
-  
   $output .= '</div>';
-          
   return $output;
 }
 
@@ -628,21 +745,45 @@ function theme_uc_option_image_preloaded
  * Theme the no image placeholder. 
  */
 function theme_uc_option_image_no_image($node, $size = '_original') {
+   
   $attributes = array('class' => 'uc-option-image');
-  $filename = path_to_theme() . '/images/noimage.png';
+  $filename = path_to_theme() . '/noimage.png';
   
-  if ($size != '_original'){           
-    return theme('imagecache', $size, $filename, t('No Image'), NULL, $attributes);
+  $imagecache_image = '';
+  if (variable_get('uc_option_image_show_noimage',FALSE)) {
+    if ($size != '_original') {           
+      $imagecache_image = theme('imagecache', $size, $filename, t('No Image'), NULL, $attributes);
+    }
+    else {                                                        
+      $imagecache_image = theme('image', $filename, t('No Image'), NULL, $attributes, FALSE);
+    }
   }
-  else {                                                        
-    return theme('image', $filename, t('No Image'), NULL, $attributes, FALSE);
+
+  //create lightbox link 
+  $lightbox_preset = variable_get('uc_option_image_lightbox_size', 'preview');
+  if ($lightbox_preset == '_original') {
+    $lightbox_path = '/'.$filename;
+  }
+  else {
+    $lightbox_path = imagecache_create_url($lightbox_preset, $filename );
   }
+  //determine if lightbox will be used
+  if (variable_get('uc_option_image_use_lightbox',FALSE)) {
+    $option_image = '<a rel="lightbox" alt=" " title="" href="'.$lightbox_path.'">'. $imagecache_image .'</a>';
+  }
+  else {
+    $option_image = $imagecache_image;
+  }
+
+  $option_image = '<div class="uc-option-image-block">'.$option_image.'</div>';
+  
+  return $option_image;
 }
 
 /**
  * Theme the no image placeholder. Must be a valid imagepath.
  */
-function theme_uc_option_image_no_image_path($node, $size = '_original') {
+function theme_uc_option_image_no_image_path($node, $size = '_original') {  
   $filename = path_to_theme() . '/images/noimage.png';
   
   if ($size != '_original'){           
@@ -654,9 +795,7 @@ function theme_uc_option_image_no_image_
 }
 
 /* -----------------------------------------------------------------
-
   Theme Overrides 
-                                                                                   
 ------------------------------------------------------------------ */
 
 /**
@@ -665,7 +804,7 @@ function theme_uc_option_image_no_image_
  * Add our image fields to the table.
  */
 function phptemplate_uc_object_options_form($form) {
-  $header = array(t('Attribute'), t('Options'), t('Default'), t('Cost'), t('Price'), t('Weight'), t('Order'), t('Image'), t('Preview'));
+  $header = array(t('Attribute'), t('Options'), t('Default'), t('Cost'), t('Price'), t('Weight'), t('Order'), t('Image'), t('Remove'));
 
   foreach (element_children($form['attributes']) as $key) {
     $row = array();
@@ -680,12 +819,14 @@ function phptemplate_uc_object_options_f
         $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['price']);
         $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['weight']);
         $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['ordering']);
-        // MOD: added the image field and preview
-        $row[] = drupal_render($form['attributes'][$key]['options'][$oid][uc_option_image_id(arg(1), $key, $oid)]); 
-        $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['option_image_preview']);
-        
+        if ($form['attributes'][$key]['options'][$oid][uc_option_image_id(arg(1), $key, $oid)]['#type'] == 'file') {
+          $row[] = drupal_render($form['attributes'][$key]['options'][$oid][uc_option_image_id(arg(1), $key, $oid)]);
+        }
+        else {
+          $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['option_image_preview']);
+        }
+        $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['remove']);      
         if (!$first) {
-          // MOD: changed from -7 to -9 to accomidate the new columns 
           $row = array_pad($row, -9, '');
         }
         else {
@@ -702,13 +843,13 @@ function phptemplate_uc_object_options_f
       $rows[] = $row;
     }
 
-    // MOD: changed colspan from 7 to 9
     $rows[] = array(array('data' => '<hr />', 'colspan' => 9));
   }
 
   if (count($rows) == 0) {
     $rows[] = array(
-      array('data' => t('This !type does not have any attributes.', array('!type' => $form['type']['#value'] == 'product' ? t('product') : t('product class'))), 'colspan' => 7),
+      array('data' => t('This !type does not have any attributes.',
+                        array('!type' => $form['type']['#value'] == 'product' ? t('product') : t('product class'))), 'colspan' => 9),
     );
   }
 
@@ -717,4 +858,3 @@ function phptemplate_uc_object_options_f
 
   return $output;
 }
-
