﻿/*************************************************
*   HOME PAGE FEATURED CONTENT
*************************************************/

//var isReady = true;

var features = [];
var currentFeature;
var currentFeatureNum;
var tempHeadline, tempCtaLink, tempCtaName, tempBackgroundImage, tempProductImage;
var changeTimeout = 0;
var timeoutTime = 8000; // 8 seconds

$(document).ready(function () {
	var featureAnchors = $("#home-featured-navigation ul li a");
	currentFeature = features[0];
	currentFeatureNum = 0;
	featureAnchors.click(changeFeature);
	startTimeout();
});

function nextFeature() {
	var featureNum = currentFeatureNum + 1;
	if (featureNum >= features.length){
		featureNum = 0;
	}
	// create "fake" event for next feature
	var e = {};
	e.currentTarget = $("#home-featured-navigation ul li."+ featureNum +" a");
	changeFeature(e);
	startTimeout();
}

function changeFeature(e) {
	if (e.type == "click"){
		clearTimeout(changeTimeout);
		startTimeout(2.5);
	}
	
	var feature = e.currentTarget;
	if (feature != currentFeature) {
		var featureNum = parseInt($(feature).parent().attr('class'));
		var newFeature = features[featureNum];
		// change headline
		tempHeadline = newFeature.headline;
		$("#home-featured-headline span").animate({
			opacity: 0,
			easing: 'easeInExpo'
		}, 800, changeHeadline);
		// change cta
		tempCtaLink = newFeature.ctaLink;
		tempCtaName = newFeature.ctaName;
		$("#home-featured-cta").animate({
			right: '-300px',
			easing: 'easeInExpo'
		}, 900, changeCta);
		// change bg
		tempBackgroundImage = newFeature.backgroundImage;
		if ($("#home-featured-background img").attr("src") != tempBackgroundImage) {
			$("#home-featured-background img").addClass("old").css("z-index", "1");

			$("#home-featured-background").append("<img class=\"new\" src=\"\" alt=\"\" />");
			$("#home-featured-background .new").css("z-index", "2").fadeTo(0, 0).attr("src", tempBackgroundImage).load(changeBackgroundImage);
		}
		// change bike
		tempProductImage = newFeature.productImage;
		$("#home-featured-product").animate({
			right: "-700px",
			easing: 'easeInCirc'
		}, 800, loadProductImage);
		
		$("#home-featured-navigation ul li a").removeClass('active');
		$(feature).addClass('active');
		currentFeature = feature;
		currentFeatureNum = featureNum;
	}
	return false;
}

function changeHeadline(e) {
	$("#home-featured-headline span").text(tempHeadline).animate({
		opacity: 1,
		easing: 'easeOutExpo'
	}, 800);
}

function changeCta(e) {
	$("#home-featured-cta a").attr("href", tempCtaLink);
	$("#home-featured-cta a .name").text(tempCtaName);
	$("#home-featured-cta").animate({
		right: '0',
		easing: 'easeOutExpo'
	}, 500);
}

function changeBackgroundImage(e) {
	$("#home-featured-background .new").fadeTo(1000, 1, function () {
		$("#home-featured-background .old").remove();
		$(this).removeClass("new");
	});
}

function loadProductImage(e){
	$("#home-featured-product img").attr("src", tempProductImage).load(changeProductImage);
}

function changeProductImage(e){
    $("#home-featured-product a").attr('href', tempCtaLink);
    $("#home-featured-product").css("right", "960px").animate({
		right: "0px",
		easing: 'easeOutCirc'
	}, 800);
}

function startTimeout(m){
	var time = timeoutTime;
	if (typeof(m) != 'undefined') time *= m;
	changeTimeout = setTimeout(nextFeature, time);
}
