/* ----------------------------------------------------------------
  EVENTS
---------------------------------------------------------------- */

document.observe("dom:loaded", function() {
  // on the product page
  if ($('product_thumbs')) {
    Product.setup_thumb_links();
  }
  Popup.setup_popups();
});


/* ----------------------------------------------------------------
  FUNKS
---------------------------------------------------------------- */
var Product = {
  setup_thumb_links : function () {
    var product_groups = $('product_thumbs').getElementsByTagName('li');
    $A(product_groups).each(function(el) {
      // make sure all links return false on click
      var el_link = el.getElementsByTagName('a');
      el_link[0].onclick = function() { return false; }
      
      // attach
      Event.observe(el, 'click', Product.swap_list_from_event, false);
    });
    
    Product.swap_list('0');
  },
  
  // find id from click event
  swap_list_from_event : function (e) {
    // event source (which element?)
    // unfortunately will return whatever element is clicked on (ie; could be the img or p of the li)
    // hence we'll have to move to the parent li to get the info we need
    var el = getEventSource(e);
    el = $(el).up('li');
    Product.swap_list(el.getAttribute('id'));
  },
  
  // swap list based on numerical id of product group (needs to be passed as a string i think)
  swap_list : function (id) {
    // subtitle
    $('product_subtitle').innerHTML = '<h3>' + $(id).down('a').getAttribute('alt') + '</h3>';
    // product list
    $('product_list_inner').innerHTML = $('p' + id).innerHTML;
    // draw attention to one's self
    var products = $('product_list_inner').getElementsByTagName('a');
    $A(products).each(function(el) {
      new Effect.Pulsate(el, { pulses: 2, duration: 1.0 });
    });
  }
}

var Popup = {
  setup_popups : function () {
    /*$$('.popup').each(function(el) {
      // the to-be parent link
      var link = document.createElement('a');
      link.setAttribute('href', el.getAttribute('src'));
      link.setAttribute('class', 'highslide');
      link.setAttribute('onclick', 'return hs.expand (this)');
      // add the original image as a child of our new link element
      el.setAttribute('id', 'coolthumb');
      link.appendChild(el.cloneNode(true));
      // replace the current image with a the new linked image
      el.parentNode.replaceChild(link, el);
    });*/
  }
}

function stopVideo() {
  Effect.BlindUp($('video_wrapper'));
  $('button_stop').style.display = 'none';
  $('button_play').style.display = 'block';
}
function playVideo() {
  Effect.BlindDown($('video_wrapper'));
  $('button_play').style.display = 'none';
  $('button_stop').style.display = 'block';
}


/* ----------------------------------------------------------------
  HELPERS
---------------------------------------------------------------- */

function getEventSource(e) {
  if (!e) e = window.event;
  if (e.originalTarget)
    return e.originalTarget;
  else if (e.srcElement)
  return e.srcElement;
}
