
var oFeatContainer = null;
var oSelectorContainer = null;
var oFeatCurrent = null;
var oFeatLast = null;
var oSelectorCurrent = null;
var oSelectorLast = null;
var oTimer = null;

hfInit();

function hfInit() {
  oFeatContainer = $('#home-features');
  oSelectorContainer = $('#homeRotatorSelector');
  animateToFeature($(':first', oFeatContainer), $(':eq(1)', oSelectorContainer));
  if (oTimer === null) {
  	oTimer = setInterval(function () {nextFeature(null, 1);}, 4000);
  }
  
  oFeatContainer.mouseover(function() {
	 if (oTimer != null) {
		clearTimeout(oTimer);
		oTimer = null;
	 }
  });
  oFeatContainer.mouseout(function() {
	  if (oTimer === null) {
		oTimer = setInterval(function () {nextFeature(null, 1);}, 4000);
	  }
  });

  $('#feat-next').click(function () {
    nextFeature(this, 1);
  });
  $('#feat-prev').click(function () {
    nextFeature(this, -1);
  });

  
  $('#gotoFeat1').click(function () {
    gotoFeature(this, 1);
  });
  $('#gotoFeat2').click(function () {
    gotoFeature(this, 2);
  });
  $('#gotoFeat3').click(function () {
    gotoFeature(this, 3);
  });
  $('#gotoFeat4').click(function () {
    gotoFeature(this, 4);
  });
  $('#gotoFeat5').click(function () {
    gotoFeature(this, 5);
  });

}

function gotoFeature(ojqWhere, iFeature) {
  var oFeatNew = $('> div:eq(' + (iFeature - 1) + ')', oFeatContainer);
  var oSelectorNew = $(':eq(' + iFeature + ')', oSelectorContainer);
  animateToFeature(oFeatNew, oSelectorNew);
}

function animateToFeature( oFeatNew, oSelectorNew ) {
  if (oFeatNew == oFeatCurrent || oFeatLast != null) {
    return;
  }
  if (oFeatCurrent != null) {
    oFeatLast = oFeatCurrent;
	oSelectorLast = oSelectorCurrent;
	var oTeaserBox = oFeatNew.children('.teaserBox');
	oTeaserBox.css('top', '500px');
	oTeaserBox.animate({ top: 0 }, { duration: 1000 });
    $(oFeatLast).animate({
      opacity: 0.0
    }, {
      duration: 1000,
      complete: function () {
        showFeature(oFeatLast, false);
        oFeatLast = null;
		oSelectorLast = null;
      }
    });
  }
  oFeatCurrent = oFeatNew;
  $(oFeatCurrent).css('opacity', 0.0);
  oSelectorCurrent = oSelectorNew;
  showFeature(oFeatCurrent, true);
  $(oSelectorLast).removeClass("selected");
  $(oSelectorCurrent).addClass("selected");
  $(oFeatCurrent).animate({ opacity: 1.0 }, { duration: 1000 });
}

function showFeature( oFeat, fShow ) {
  if (fShow) {
    $(oFeat).addClass("home-feature-visible");
  } else {
    $(oFeat).removeClass("home-feature-visible");
  }
}

function nextFeature(ojqWhere, direction) {
  var oFeatNew;
  var oSelectorNew;
  if (direction == 1) {
    oFeatNew = $(oFeatCurrent).next();
	oSelectorNew = $(oSelectorCurrent).next();
    if (oFeatNew.size() == 0) {
      oFeatNew = $('.home-feature', oFeatContainer).first();
	  oSelectorNew = $(':eq(1)', oSelectorContainer);
    }
  } else {
    oFeatNew = $(oFeatCurrent).prev();
	oSelectorNew = $(oSelectorCurrent).prev();
    if (oFeatNew.size() == 0) {
      oFeatNew = $('.home-feature', oFeatContainer).last();
	  oSelectorNew = $(':eq(5)', oSelectorContainer);
    }
  }
  if (oFeatNew.size() == 0)
    alert('oops');
  animateToFeature(oFeatNew, oSelectorNew);
}

