// rotating banner images
var bannerContainer;
var bannerImages;
var bannerIndex;
var curImg;
var fadeInterval = 500;
var showInterval = 6000;

//  will be overwritten in the event a visitor comes from a shopForTires email
var showIS = false;

// fade the given element from 100 to 0 opacity
function fadeOut() {
  var curOpacity = 100;
  var miliSecondInterval = 50;
  var opacityDecrementBy = Math.round((100 * miliSecondInterval) / fadeInterval);
  curOpacity -= opacityDecrementBy;

  function hideIt() {
    if (curOpacity <= 0) {
      // hide the element
      curImg.style.opacity = 0.0;
      curImg.style.filter = 'alpha(opacity=0)';
      curImg.style.display = 'none';
      curImg.style.visibility = 'hidden';

      // clear the interval
      clearInterval(outInterval);

      // increase or reset the index for next time and set the curImg
      bannerIndex = (bannerIndex == (bannerImages.length - 1)) ? 0 : bannerIndex + 1;
      curImg = bannerImages.item(bannerIndex);

      // start fading in the next image
      fadeIn();
    } else {
      curImg.style.opacity = curOpacity / 100;
      curImg.style.filter = 'alpha(opacity=' + curOpacity + ')';
      curOpacity -= opacityDecrementBy;
    }
  }

  var outInterval = setInterval(hideIt, miliSecondInterval);
}

// fade the given element from 0 to 100 opacity
function fadeIn() {
  var curOpacity = 0;
  var miliSecondInterval = 50;
  var opacityIncrementBy = Math.round((100 * miliSecondInterval) / fadeInterval);
  curOpacity += opacityIncrementBy;

  // make sure the element is visible and starting from 0 opacity
  curImg.style.opacity = 0.0;
  curImg.style.filter = 'alpha(opacity=0)';
  curImg.style.display = 'block';
  curImg.style.visibility = 'visible';

  function showIt() {
    if (curOpacity >= 100) {
      // set the opacity to 100
      curImg.style.opacity = 1.0;
      curImg.style.filter = 'alpha(opacity=100)';

      // clear the interval
      clearInterval(inInterval);

      // set the timer for fading the current image out
      setTimeout(fadeOut, showInterval);
    } else {
      curImg.style.opacity = curOpacity / 100;
      curImg.style.filter = 'alpha(opacity=' + curOpacity + ')';
      curOpacity += opacityIncrementBy;
    }
  }

  var inInterval = setInterval(showIt, miliSecondInterval);
}


function listen(elm, evt, func) {
  if (elm.addEventListener) {
    elm.addEventListener(evt, func, false);
  } else if (elm.attachEvent) {
    elm.attachEvent('on' + evt, func);
  }
}

function initHome(evt) {
  // get all banner images
  bannerContainer = document.getElementById('bannerContainer');
  bannerImages = bannerContainer.getElementsByTagName('IMG');

  // if any images are there then start fading them in/out
  if (bannerImages.length > 0) {
    // setup the first banner image and force the first display
    bannerIndex = 0;
    curImg = bannerImages.item(bannerIndex);
    fadeIn();
  }

  // should the Internet Specials be shown onload?
  if (showIS === true) {
    showInternetSpecials();
  }

  // featured links
  var feat0 = document.getElementById('feature0');
  var feat1 = document.getElementById('feature1');
  var feat2 = document.getElementById('feature2');
  var feat3 = document.getElementById('feature3');
  var feat4 = document.getElementById('feature4');
  listen(feat0, 'click', function(evt) {document.location.href = 'http://www.jackwilliams.com/shopForTires/';});
  listen(feat1, 'click', function(evt) {document.location.href = 'http://www.jackwilliams.com/myJack.php';});
  listen(feat2, 'click', function(evt) {document.location.href = 'http://www.jackwilliams.com/Wheels/';});
  listen(feat4, 'click', function(evt) {document.location.href = 'http://www.jackwilliams.com/Specials/';});
}

if (window.addEventListener) {
  window.addEventListener('load', initHome, false);
} else if (window.attachEvent) {
  window.attachEvent('onload', initHome);
} else {
  window.onload = initHome;
}
