/*
  function aimg_fix()
  Quick function to add class to links containing images.
  This has to be detected through the DOM since CSS does not allow
  traversing backwards in the rules.
  
  @steinhaug - Kim Steinhaug, 17 April 2009
*/
function explode(d, s) {
  var ar = s.split(d);
  var oar = new Array();
  for (var i = 0; i<ar.length; i++) {
    if (ar[i] != "")
      oar[oar.length] = ar[i];
    }
  return oar;
}
function addCSSClass(elm, cls){
  removeCSSClass(elm, cls);
  var ar = explode(' ', elm.className);
  ar[ar.length] = cls;
  elm.className = ar.join(' ');
}
function removeCSSClass(elm, cls) {
  var ar = explode(' ', elm.className);
  for (var i=0; i<ar.length; i++){
    if (ar[i] == cls)
    ar[i] = '';
  }
  elm.className = ar.join(' ');
}
function aimg_fix(){
  var block = document.getElementById("hoyre");
  var links = block.getElementsByTagName("a"); 
  var i;
  for ( i=0;i<links.length;i++ ) {
    var imgs = links[i].getElementsByTagName("img");
    if(imgs.length){
      var classes = links[i].getAttribute('class');
      if(classes == null){
        links[i].setAttribute('class','has-image');
        links[i].setAttribute('className','has-image');
      } else {
        addCSSClass(links[i],'has-image');
      }
    }
  }
}
ELO.functionsToCallOnload.push("aimg_fix()");


/* Product selection - attribute with color */
    function attrib_update_image(id){
      if(id == null){
        attrib = document.getElementById('attrib');
        for (var i=0;i<attrib.length;i++) {
          if(attrib[i].selected){
            if(attributes_images_replace_mainimage){
              var myElem = document.getElementById('popup-bilde');
              if(myElem != null){
                myElem.setAttribute('href','/files/attributes/large/' + attributes_images[attrib[i].value][0]);
                popup_bilde_x = attributes_images[attrib[i].value][1];
                popup_bilde_y = attributes_images[attrib[i].value][2];
              }
              img = document.getElementById('product_image');
            } else {
              img = document.getElementById('attribute-preview-image');
            }
            if(attrib_update_image_size == 'extra'){
              img.src = attributes_images[attrib[i].value][6];
              img.width = attributes_images[attrib[i].value][7];
              img.height = attributes_images[attrib[i].value][8];
            } else if(attrib_update_image_size == 'large'){
              img.src = '/files/attributes/large/' + attributes_images[attrib[i].value][0];
              img.width = attributes_images[attrib[i].value][1];
              img.height = attributes_images[attrib[i].value][2];
            } else {
              img.src = '/files/attributes/large/' + attributes_images[attrib[i].value][3];
              img.width = attributes_images[attrib[i].value][4];
              img.height = attributes_images[attrib[i].value][5];
            }
          }
        }
      } else {
        if(attributes_images[id] == undefined){
          return false;
        }
        attrib = document.getElementById('attrib');
        for (var i=0;i<attrib.length;i++){
          if(attrib[i].value == id) {
            attrib[i].selected = true;
            if(attributes_images_replace_mainimage){
              myElem = document.getElementById('popup_bilde');
              if(myElem != null){
                myElem.setAttribute('href','/files/attributes/large/' + attributes_images[attrib[i].value][0]);
                popup_bilde_x = attributes_images[attrib[i].value][1];
                popup_bilde_y = attributes_images[attrib[i].value][2];
              }
              img = document.getElementById('product_image');
            } else {
              img = document.getElementById('attribute-preview-image');
            }
            if(attrib_update_image_size == 'extra'){
              img.src = attributes_images[attrib[i].value][6];
              img.width = attributes_images[attrib[i].value][7];
              img.height = attributes_images[attrib[i].value][8];
            } else if(attrib_update_image_size == 'large'){
              img.src = '/files/attributes/large/' + attributes_images[attrib[i].value][0];
              img.width = attributes_images[attrib[i].value][1];
              img.height = attributes_images[attrib[i].value][2];
            } else {
              img.src = '/files/attributes/large/' + attributes_images[attrib[i].value][3];
              img.width = attributes_images[attrib[i].value][4];
              img.height = attributes_images[attrib[i].value][5];
            }
          }
        }
      }
    }


