function updateSelection(sel, val, conf_obj, opt_cfg, opt_num) {

  //alert('sel: ' + sel + "\n" + 'val: ' + val  + "\n" + 'conf: ' + conf_obj  + "\n" + ' opt num: ' + opt_num + "\n" );
  var s = document.getElementById(sel);
  var prefix = 'attribute-current-';
  
  if(s == undefined && conf_obj == undefined) return;
  
  for (i = 0; i < s.length; i++) {
    
    if(s.options[i].value == val) {
      s.options[i].selected = true;
    }
    
  }
  
  updateDisplay(opt_cfg[opt_num], prefix);
  conf_obj.reloadPrice();
  
  if(hundredGramPrice) {
    hundredGramPrice.changePrice('product', Number(opt_cfg[opt_num]['price_hundred']));
    hundredGramPrice.reload();
  }
    
}  

function updateDisplay(cfg, prefix) {
  for (var s in cfg) {
   
    var elem = document.getElementById(prefix + s);
    if(!elem) continue;
    
    elem.innerHTML = cfg[s];
  }
  
  var gallery_elem = document.getElementById("image_color");
  if(!gallery_elem || !cfg["image_org"] || cfg["image_org"].indexOf("placeholder") != -1) return;
  
  var image = new Image();
  image.onload = function() {
    if (image.width > 100) {
      if (gallery_elem.src != cfg["image_org"]) {
        gallery_elem.onload = function() {
          gallery_elem.onload = function() {}
          changeImage("image_color", cfg["newclass"]);
        }
        gallery_elem.src = cfg["image_org"];
      } else {
        changeImage("image_color", cfg["newclass"]);
      }
    }
  }
  image.src = cfg["image_org"];
}

function showColorSelector(btn, elemid, tableid, state, image_org, option_id, newclass) {
  
  var elem = $(elemid);
  var tble = $(tableid);
  var enode = elem.parentNode.removeChild(elem);
  tble.parentNode.appendChild(elem);
  
  if (state && btn.offsetParent.offsetTop)
    elem.style.top = (btn.offsetParent.offsetTop - 30) + "px";
  
  if(!elem) return;
  
  if(state) 
    elem.style.display = 'block';
  else 
    elem.style.display = 'none';
    
  if(state)
    return;
  
  var gallery_elem = document.getElementById("image_color");
  if(!gallery_elem || !image_org || image_org.indexOf("placeholder") != -1) return;
  
  var image = new Image();
  image.onload = function() {
    if (image.width > 100) {
      if (gallery_elem.src != image_org) {
        gallery_elem.onload = function() {
          gallery_elem.onload = function() {}
          changeImage("image_color", newclass);
        }
        gallery_elem.src = image_org;
      } else {
        changeImage("image_color", newclass);
      }
    }
  }
  image.src = image_org;
  
  var option_image = document.getElementById("bundle-option-"+option_id+"-current-image");
  if(!option_image) return;
  
  option_image.onclick = function() {
    showColorSelector(this, 'bundle-option-colors-' + option_id, 'product-options-table', false, image_org, option_id, newclass);
  };
}

