// Instantiate several global variables
var product_prices_rows;
var brand = "";
var collection;

// Overload the current onload event with our own helpful code
var orig_onload = window.onload;
window.onload = function() {
  orig_onload();
  if (document.getElementById('product_prices')) {
    product_prices_rows = document.getElementById('product_prices').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
    for (var i = 0; i < product_prices_rows.length; i++) {
      if (i % 2 != 0) {
        product_prices_rows[i].className = 'odd';
      }
    }
  }
  if (document.getElementById('retail')) {
    retail = document.getElementById('retail');
    discount = document.getElementById('discount');
    note = document.getElementById('product_options').getElementsByTagName('address')[0];
    retail.value = sprintf("$%.2f", parseFloat(product_prices_rows[0].getElementsByTagName('td')[3].firstChild.nodeValue));
    if (product_prices_rows[0].getElementsByTagName('td')[4].firstChild.nodeValue != "na") {
      discount.value = sprintf("$%.2f", parseFloat(product_prices_rows[0].getElementsByTagName('td')[4].firstChild.nodeValue));
      retail.style.textDecoration = "line-through";
      discount.parentNode.style.display = "block";
      note.style.display = "block";
    }
  }
  if (document.getElementById('nav_brand')) {
    nav_brand_list = document.getElementById('nav_brand').getElementsByTagName('li');
    for (var i = 1; i < nav_brand_list.length; i++) {
      if ((i - 1) % 3 == 0) {
        nav_brand_list[i].className = 'middle';
      }
    }
  }
  if (document.getElementById('product_image')) {
    product_img = document.getElementById('product_image');
    product_thumbs_first_alt = document.getElementById('product_thumbs').getElementsByTagName('li')[0].firstChild.alt;
    product_img.alt = product_thumbs_first_alt;
  }
  if (document.getElementById('product_thumbs')) {
    product_thumbs_list = document.getElementById('product_thumbs').getElementsByTagName('li');
    for (var i = 0; i < product_thumbs_list.length; i++) {
      if ((i + 1) % 5 == 0) {
        product_thumbs_list[i].className = 'end';
      }
    }
  }
  if (document.getElementById('product_video')) {
    product_video_container = document.getElementById('product_video');
    vembed = document.createElement('embed');
    vembed.setAttribute('width', '361');
    vembed.setAttribute('height', '281');
    vembed.setAttribute('src', 'http://oneup.vo.llnwd.net/o2/jaxx_sacs.swf');
    vembed.setAttribute('name', 'jaxx_sacs.swf');
    vembed.setAttribute('loop', 'false');
    vembed.setAttribute('type', 'application/x-shockwave-flash');
    vembed.setAttribute('pluginspage', 'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash');
    product_video_container.replaceChild(vembed, product_video_container.firstChild);
  }
}

// Behaviour
// - apply JavaScript using CSS selectors
// - http://bennolan.com/behaviour/
var myrules = {
  '.outlink' : function(element) {
    element.onclick = function() {
      this.target = "_blank";
    }
  },
  '#product_views h2' : function(element) {
    element.onclick = function() {
      list = document.getElementById(this.id.replace(/_header/, ''));
      if (list.style.display != "none") {
        list.style.display = "none";
      } else {
        list.style.display = "block";
      }
    }
  },
  '#product_swatches img' : function(element) {
    element.onmouseover = function() {
      sample = document.getElementById('swatch_sample');
      info = document.getElementById('swatch_info');
      all = info.childNodes;
      color = document.getElementById('product_options').color;
      sample.src = this.src;
      sample.alt = this.alt;
      swatch = this.alt.replace(/.* : /, '').replace(/ /g, '');
      document.getElementById('swatch_name').firstChild.nodeValue = this.alt.replace(/.* : /, '');
      for (var i = 0; i < color.options.length; i++) {
        if (swatch == color.options[i].firstChild.nodeValue.replace(/ /g, '')) {
          collection = document.getElementById(color.options[i].parentNode.label.replace(/ Collection/, '').replace(/ /, ''));
          continue;
        }
      }
      for (var i = 0; i < all.length; i++) {
        all[i].style.display = "none";
      }
      collection.style.display = "block";
    }
    element.onclick = function() {
      product = document.getElementById('product_image');
      if (brand == "") {
        brand = product.src.match(/images\/.*\//) + product.src.replace(/.*products\//, '').replace(/\d*\.jpg/, '');
      }
      swatch = this.src.replace(/.*swatches_/, '').replace(/\.jpg/, '').replace(/_/, '-');
      product.src = brand + swatch.replace(/-/, '_') + ".jpg";
      product.alt = element.alt;
      color = document.getElementById('product_options').color;
      for (var i = 0; i < color.options.length; i++) {
        if (swatch == color.options[i].firstChild.nodeValue.replace(/ /g, '')) {
          color.options.selectedIndex = i;
          continue;
        }
      }
      size = document.getElementById('product_options').size;
      if (size.selectedIndex == 0) {
        selected = 1;
      } else {
        selected = size.selectedIndex;
      }
      prices = document.getElementById('product_prices');
      cells = document.getElementById(prices.title + size.options[selected].value + color.options[color.selectedIndex].value).getElementsByTagName('td');
      document.getElementById('retail').value = sprintf("$%.2f", parseFloat(cells[3].firstChild.nodeValue));
      document.getElementById('discount').value = sprintf("$%.2f", parseFloat(cells[4].firstChild.nodeValue));
    }
  },
  '#swatch_sample' : function(element) {
    element.onmouseover = function() { return; },
    element.onclick = function() {
      product = document.getElementById('product_image');
      if (brand == "") {
        brand = product.src.match(/images\/.*\//) + product.src.replace(/.*products\//, '').replace(/\d*\.jpg/, '');
      }
      swatch = this.src.replace(/.*swatches_/, '').replace(/\.jpg/, '').replace(/_/, '-');
      product.src = brand + swatch.replace(/-/, '_') + ".jpg";
      product.alt = element.alt;
      color = document.getElementById('product_options').color;
      for (var i = 0; i < color.options.length; i++) {
        if (swatch == color.options[i].firstChild.nodeValue.replace(/ /g, '')) {
          color.options.selectedIndex = i;
          continue;
        }
      }
    }
  },
  '#product_thumbs img' : function(element) {
    element.onclick = function() {
      shot = document.getElementById('product_image');
      shot.src = this.src.replace(/_small/, '');
      shot.alt = this.alt;
    }
  },
  'select' : function(element) {
    element.onchange = function() {
      color = document.getElementById('product_options').color;
      if (color.selectedIndex == 0) {
        selected = 1;
      } else {
        selected = color.selectedIndex;
      }
      size = document.getElementById('product_options').size;
      prices = document.getElementById('product_prices');
      cells = document.getElementById(prices.title + size.options[size.selectedIndex].value + color.options[selected].value).getElementsByTagName('td');
      document.getElementById('retail').value = sprintf("$%.2f", parseFloat(cells[3].firstChild.nodeValue));
      document.getElementById('discount').value = sprintf("$%.2f", parseFloat(cells[4].firstChild.nodeValue));
    }
  },
  '#color' : function(element) {
    element.onchange = function() {
      if (this.selectedIndex != 0) {
        sample = document.getElementById('swatch_sample');
        info = document.getElementById('swatch_info');
        all = info.getElementsByTagName('div');
        color = this;
        sample.src = sample.src.replace(/swatches_.*\.jpg/, 'swatches_' + this.options[this.selectedIndex].firstChild.nodeValue.replace(/ /g, "") + '.jpg');
        sample.alt = sample.alt.replace(/ : .*/, ' : ' + this.options[this.selectedIndex].firstChild.nodeValue);
        swatch = this.options[this.selectedIndex].firstChild.nodeValue.replace(/ /g, '');
        size = document.getElementById('product_options').size;
        if (size.selectedIndex == 0) {
          selected = 1;
        } else {
          selected = size.selectedIndex;
        }
        prices = document.getElementById('product_prices');
        cells = document.getElementById(prices.title + size.options[selected].value + color.options[color.selectedIndex].value).getElementsByTagName('td');
        document.getElementById('retail').value = sprintf("$%.2f", parseFloat(cells[3].firstChild.nodeValue));
        document.getElementById('discount').value = sprintf("$%.2f", parseFloat(cells[4].firstChild.nodeValue));
        document.getElementById('swatch_name').firstChild.nodeValue = this.options[this.selectedIndex].firstChild.nodeValue;
        for (var i = 0; i < color.options.length; i++) {
          if (swatch == color.options[i].firstChild.nodeValue.replace(/ /g, '')) {
            collection = document.getElementById(color.options[i].parentNode.label.replace(/ Collection/, '').replace(/ /, ''));
            continue;
          }
        }
        for (var i = 0; i < all.length; i++) {
          all[i].style.display = "none";
        }
        collection.style.display = "block";
        product = document.getElementById('product_image');
        if (brand == "") {
          brand = product.src.match(/images\/.*\//) + product.src.replace(/.*products\//, '').replace(/\d*\.jpg/, '');
        }
        product.src = brand + swatch + ".jpg";
        product.alt = sample.alt;
        for (var i = 0; i < color.options.length; i++) {
          if (swatch == color.options[i].firstChild.nodeValue.replace(/ /g, '')) {
            color.options.selectedIndex = i;
            continue;
          }
        }
      }
    }
  },
  '#buynow' : function(element) {
    element.onclick = function() {
      color = document.getElementById('product_options').color;
      size = document.getElementById('product_options').size;
      prices = document.getElementById('product_prices');
      var wrong = false;
      var msg = "Please correct the following to continue:\n";
      if (color.selectedIndex == 0 && color.value == "0") {
        wrong = true;
        msg += "- Select your choice of color.\n";
      }
      if (size.selectedIndex == 0 && size.value == "0") {
        wrong = true;
        msg += "- Select your choice of size.\n";
      }
      if (wrong) {
        alert(msg);
      } else {
        cells = document.getElementById(prices.title + size.options[size.selectedIndex].value + color.options[color.selectedIndex].value).getElementsByTagName('td');
        window.location = "https://oneupstore.com/detail.aspx?returnURL=studiooneup&ID=" + cells[0].firstChild.nodeValue;
      }
    }
  },
  '#email' : function(element) {
    element.onfocus = function() {
      this.value = "";
      this.style.color = "#7f9db9";
    }
    element.onblur = function() {
      this.value = "e-mail address";
      this.style.color = "#bbccdd";
    }
  },
  '#live_chat' : function(element) {
    element.onclick = function() {
      this.newWindow = window.open('http://livechat.boldchat.com/aid/2453390106116086452/bc.chat?cwdid=3354981549183591622&amp;vr=&amp;vn=&amp;vi=&amp;ve=&amp;vp=&amp;iq=&amp;url=' + escape(document.location.href), 'Chat', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
      this.newWindow.focus();
      this.newWindow.opener = window;
      return false;
    }
  }
};

Behaviour.register(myrules);
