// Branchement des gestionnaires d'événements ----------------------------- */
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent(obj, type, fn)
{
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
  }
  else if (obj.attachEvent) {
    obj["e" + type + fn] = fn;
    obj[type + fn] = function()
    {
      if (obj["e" + type + fn]) {
        obj["e" + type + fn](window.event);
      }
    };
    obj.attachEvent("on" + type, obj[type + fn]);
  }
  else {
    // http://wdirect.apple.com/home/wdirect/ticker.js
    var oldhandler = obj["on" + type];
    obj["on" + type] = (typeof obj["on" + type] != 'function') ?
     fn : function(ev) { oldhandler(ev); fn(ev); };
  }
}

function getElementsByTagAndClass(parent, localName, cssClass) {
  var elmarAll = parent.getElementsByTagName(localName);
  var elmarWithClass = [];
  for (var i = 0; i < elmarAll.length; i++) {
    if (elmarAll[i].className.indexOf(cssClass) != -1) {
      elmarWithClass[elmarWithClass.length] = elmarAll[i];
    }
  }
  return elmarWithClass;
}

function fixShoppingCart(ev)
{
  var imgCart = document.getElementById("scv_imgCart");
  var elmarParent = imgCart.parentNode.childNodes;
  var divScroll = null;
  var i = 0;
  while ((divScroll == null) && (i < elmarParent.length)) {
    if (elmarParent[i].className == "scroll") {
      divScroll = elmarParent[i];
    }
    else {
      i++;
    }
  }
  if ((divScroll != null) && (divScroll.scrollHeight > divScroll.clientHeight)) {
    divScroll.scrollTop = divScroll.scrollHeight - divScroll.clientHeight;
  }
}

function findBalloons(imgCart)
{
  var i;
  var divarInCart = imgCart.parentNode.getElementsByTagName("div");
  var divarSuggest = [];
  for (i = 0; i < divarInCart.length; i++) {
    if (divarInCart[i].className == "suggest") {
      divarSuggest.push(divarInCart[i]);
    }
  }
  return divarSuggest;
}

function fixBalloonsPosition(ev)
{
  var imgCart = document.getElementById("scv_imgCart");
  var divarSuggest = findBalloons(imgCart);
  var elmImgGrandpa = imgCart.parentNode.parentNode;
  var elmContainer = elmImgGrandpa.parentNode.parentNode.parentNode;
  var intOffsetLeft = elmContainer.offsetLeft + elmImgGrandpa.offsetLeft + imgCart.offsetLeft;
  for (var i = 0; i < divarSuggest.length; i++) {
    if (divarSuggest[i].style.position == "relative") {
      divarSuggest[i].relativeLeft = parseInt(divarSuggest[i].style.left, 10);
      divarSuggest[i].relativeTop = parseInt(divarSuggest[i].style.top, 10);
      divarSuggest[i].style.position = "absolute";
      divarSuggest[i].style.zIndex = 2;
    }
    divarSuggest[i].style.left = (intOffsetLeft + divarSuggest[i].relativeLeft) + "px";
    divarSuggest[i].style.top = (elmImgGrandpa.offsetTop +
     divarSuggest[i].parentNode.parentNode.parentNode.parentNode.offsetTop +
     divarSuggest[i].relativeTop) + "px";
  }
}

function animateBalloons(attackMsec, decayMsec, sustainMsec, releaseMsec)
{
  var divarSuggest = findBalloons(document.getElementById("scv_imgCart"));
  for (var i = 0; i < divarSuggest.length; i++) {
    attackBalloon(divarSuggest[i], attackMsec, decayMsec, sustainMsec, releaseMsec);
  }
}

function attackBalloon(divBalloon, attackMsec, decayMsec, sustainMsec, releaseMsec)
{
  setTimeout(function() { decayBalloon(divBalloon, decayMsec, sustainMsec, releaseMsec); }, attackMsec);
}

function decayBalloon(divBalloon, decayMsec, sustainMsec, releaseMsec)
{
  setTimeout(function() { sustainBalloon(divBalloon, sustainMsec, releaseMsec); }, decayMsec);
}

function sustainBalloon(divBalloon, sustainMsec, releaseMsec)
{
  setTimeout(function() { releaseBalloon(divBalloon, releaseMsec); }, sustainMsec);
  divBalloon.getElementsByTagName("a")[0].style.visibility = "visible";
}

function releaseBalloon(divBalloon, releaseMsec)
{
  divBalloon.getElementsByTagName("a")[0].style.visibility = "hidden";
  setTimeout(function() { nextNote(divBalloon) }, releaseMsec);
}

function nextNote(divBalloon)
{
  divBalloon.style.display = "none";
  var imgSuggest = divBalloon.parentNode.getElementsByTagName("img")[0];
  imgSuggest.style.visibility = "visible";
  imgSuggest.src = "images/exclamation-anim.gif";
}

function animateTaxesIncluded()
{
  if (document.getElementById('scv_divTween') != null) {
    var fxTaxes = new Fx.Morph('scv_divTween', { duration: 3000, transition: Fx.Transitions.Sine.easeOut });

    fxTaxes.start({
      'color': ['#003366', '#ffffff']
    });
  }
  var elmarMorph = getElementsByTagAndClass(document, '*', 'morph');
  for (var i = 0; i < elmarMorph.length; i++) {
    var fxMorph = new Fx.Morph(elmarMorph[i], { duration: 3000, transition: Fx.Transitions.Sine.easeOut });
    fxMorph.start({
      'font-weight': ['400', '700']
    });
  }
}

addEvent(window, "load", fixShoppingCart);
addEvent(window, "load", fixBalloonsPosition);
addEvent(window, "resize", fixBalloonsPosition);
addEvent(window, "load", animateTaxesIncluded);

