var DomLoaded =
{
  onload: [],
  loaded: function()
  {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i]();
  },
  load: function(fireThis)
  {
    this.onload.push(fireThis);
    if (document.addEventListener) 
      document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null);
    if (/KHTML|WebKit/i.test(navigator.userAgent))
    { 
      var _timer = setInterval(function()
      {
        if (/loaded|complete/.test(document.readyState))
        {
          clearInterval(_timer);
          delete _timer;
          DomLoaded.loaded();
        }
      }, 10);
    }
    /*@cc_on @*/
    /*@if (@_win32)
    var proto = "src='javascript:void(0)'";
    if (location.protocol == "https:") proto = "src=//0";
    document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            DomLoaded.loaded();
        }
    };
    /*@end @*/
     window.onload = DomLoaded.loaded;
  }
};

var do_behaviors = function() {

  $$('.JHvr').each(function(element) {
    element.onmouseover = function(evt) {
      $(this).addClassName('hover_opacity');
    };
    element.onmouseout = function(evt) {
      $(this).removeClassName('hover_opacity');
    };
  });

  $$('a.JRemote').each(function(element) {
    element.onclick = function(evt) {
      new Ajax.Request(this.href, { method: 'get' });
      if (evt) evt.stopPropagation();
      return false;
    };
  });

  window.product_photos = $$('.JPhoto');
  window.product_photo_captions = $$('.JPhotoCaption');

  $$('a.JPhotoLink').each(function(element) {
    element.onclick = function(evt) {
      var img_id = this.id.split('_').last();

      window.product_photos.each(function(photo_img) {
        photo_img.hide();
      });
      window.product_photo_captions.each(function(photo_img) {
        photo_img.hide();
      });
      
      $('photo_' + img_id).show();
      $('photo_caption_' + img_id).show();

      if (evt) evt.stopPropagation();
      return false;
    };
  });

  $$('form.JRemote').each(function(element) {
    ajaxify_form(element);
  });
  set_up_cart();

}

DomLoaded.load(do_behaviors);

Ajax.Responders.register({
  onComplete: do_behaviors
});

function highlight_minicart() {
  var minicart = $('minicart');
  if (minicart && !minicart.hasClassName('minicartHighlight')) {
    minicart.addClassName('minicartHighlight');
    setTimeout(function () {
                  minicart.removeClassName('minicartHighlight');
                }, 3000);
  }
}

function ajaxify_form(theForm) {
  theForm.onsubmit = function() {
    new Ajax.Request(this.action, {asynchronous:true, evalScripts:true, method:'GET', parameters:Form.serialize(this)}); return false;
  };
}

function set_up_cart() {
  $$('.JUpdateCart').each(function(element) {
    element.hide();
    var theForm = element.parentNode;
    ajaxify_form(theForm);
    new Form.EventObserver(theForm, function(form) { form.onsubmit(); });
  });

  if (el = $('j_update_whole_cart')) el.show();
}
