var hppItemCount = 0, hppCurrentItem = null;
var hppStarted = false;
var hppContainer, hppNavigation, hppNavigationScroller, hppNavigationBullets, hppNavigationBar;
var hppNavigationWhiteout, hppNavigationFiller, hppNavigationItems, hppTimer;
// Don't start the homepage promo unit until the page is fully ready
window.onload = function() {
	
	hppStart();
}

$(document).ready(function() {
	// jQuery Shortcuts
	hppContainer          = $(".homepage-promo-container");
	hppNavigation         = hppContainer.children(".homepage-promo-navigation");
	hppNavigationScroller = hppNavigation.children(".homepage-promo-scroller");
	hppNavigationHover    = hppNavigation.children(".homepage-promo-hover");
	hppNavigationBullets  = hppNavigation.children(".homepage-promo-bullets");
	hppNavigationFillers  = hppNavigation.children(".homepage-promo-fillers");
	hppNavigationWhiteout = hppContainer.find(".homepage-promo-whiteout");
	hppNavigationItems    = hppContainer.find(".homepage-promo-item");
	hppLoader             = hppContainer.find(".homepage-promo-loader");

	// Determine the item count
	hppItemCount = hppNavigationItems.size();
	
	// Hide all items
	//hppNavigationItems.css("opacity", 0);
	hppNavigationItems.addClass("homepage-promo-item-hidden");

	if ( hppItemCount == 0 ) {
		hppContainer.remove();
	} else if ( hppItemCount == 1 ) {
		hppNavigation.remove();
	} else {
		// Create events to stop timer when hovering over the promo area
		$(hppContainer).hover(hppStopTimer, hppStartTimer);
		hppNavigationFillers
			.width( (hppItemCount * hppNavigationScroller.width()) )
			.click(function(e) {
				var position = e.pageX - $(this).offset().left;
				var item = Math.ceil(position / (hppNavigationFillers.width() / hppItemCount));

				hppScrollTo( item - 1 );
				hppStartTimer();
			});
			
	}

	// If it hasn't loaded within 3.5 seconds, load it anyway.
	setTimeout(function() {
		hppStart();
	}, 3500);
});

function hppStart() {
	if ( hppStarted == false ) {
		hppLoader.remove();
	
		if ( hppItemCount > 1 ) {
			// Begin timer
			hppStartTimer();
		}
	
		// Scroll to first item
		hppScrollTo(0);
		
		hppStarted = true;
	}
}

function hppStopTimer() {
	clearInterval( hppTimer );
	hppTimer = null;
}

function hppScrollTo( i ) {
	if ( hppCurrentItem == i ) {
		return false;
	}

	hppNavigationItems.eq(hppCurrentItem).animate( { opacity : 0 }, "fast",function(){
		hppNavigationItems.addClass("homepage-promo-item-hidden");
		hppNavigationItems.eq(i).removeClass("homepage-promo-item-hidden");
	});
	hppNavigationItems.eq(i).animate( { opacity : 1 }, "fast");
	
	hppNavigationScroller.animate({
		opacity: 0
	}, "fast", function() {
		$(this).css({
			left: (i * hppNavigationScroller.width()) + 5
		})
		.animate({
			opacity: 1
		}, "fast");
	});

	hppCurrentItem = i;
}

function hppStartTimer() {
	clearInterval( hppTimer );
	hppTimer = null;

	hppTimer = setInterval( function() {
		if ( hppCurrentItem == (hppItemCount - 1) ) {
			hppScrollTo(0);
		} else {
			hppScrollTo(hppCurrentItem + 1);
		}
	}, 5000);
}