var hppItemCount = 0, hppCurrentItem = 0;
var hppStarted = false;
var hppContainer, hppNavigation, hppNavigationScroller, hppNavigationBullets, hppNavigationBar;
var hppNavigationWhiteout, hppNavigationFiller, hppNavigationItems;
var hppTimer = 0, hppAutoPlayedFirst = false, hppFirstVideoRef = null;
var hppVidIsPlaying = false;

// 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");
	hppNavigationBar      = hppNavigation.children(".homepage-promo-bar");
	hppNavigationBarFade  = hppNavigation.children(".homepage-promo-barfade");
	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();

	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);
		
		// Create Navigation Based on total items
		var bulletContainerWidth = (hppItemCount * hppNavigationScroller.width());
		hppNavigationBullets.css( "width", bulletContainerWidth );
		hppNavigationFillers.width(bulletContainerWidth);
		hppNavigationBar.css({
			left: bulletContainerWidth,
			width: hppContainer.width() - bulletContainerWidth - hppNavigationBarFade.width()
		});

		// Apply click event to navigation items
		hppNavigationBullets.bind("mousemove mouseleave click", function(e) {
			var position = e.pageX - $(this).offset().left;
			var item = Math.ceil(position / (hppNavigationBullets.width() / hppItemCount));
	
			switch( e.type ) {
				case "click":
					hppScrollTo( item );
					hppStartTimer();
					break;
				case "mousemove":
					if ( hppCurrentItem != item ) {
						hppNavigationHover.css("left", ((item - 1) * hppNavigationHover.width()) + 5);
						break;
					}
				case "mouseleave":
					hppNavigationHover.css("left", -999999);
					break;
			}
		});

		// Hide white out
		hppNavigationWhiteout.css("opacity", 0);
	}
	
	// Apply Video Click Events
	$(".homepage-promo-video-thumb").click(hppInitVideo);

	// Never let Thumbnail or Video hit the anchor tag
	$(".homepage-promo-video-wrapper").click(function(e) {
		e.stopImmediatePropagation();
	});

	// If it hasn't loaded within 3.5 seconds, load it anyway.
	setTimeout(function() {
		hppStart();
	}, 3500);
	
	
	$(document).bind("PlayerEvent.ON_MEDIA_ENDED", hppVideoEnd);
	$(document).bind("PlayerEvent.ON_READY", hppPlayFirstVideo);
});

function hppStart() {
	if ( hppStarted == false ) {
		hppLoader.remove();
	
		if ( hppItemCount > 1 ) {
			// Begin timer
			hppStartTimer();
		}
	
		// Scroll to first item
		hppScrollTo(1);
		hppStarted = true;
	}
}

function hppScrollTo( i ) {
	$("#homepage-promo-video-wrapper").html("");
	if ( i > hppItemCount || i < 1 || hppCurrentItem == i ) {
		return false;
	}

	// Hide flash elements
	NICK.utils.hideSwfs(".homepage-promo-container");

	hppCurrentItem = i;
	hppNavigationWhiteout.css("display", "block").animate( { opacity: 1	}, "fast");
	hppNavigation.addClass("homepage-promo-animating");
	hppNavigationBar.css("opacity",1);
	hppNavigationScroller.stop().animate({
		left: ((i - 1) * hppNavigationScroller.width()) + 5
	}, "slow", function() {
		hppNavigation.removeClass("homepage-promo-animating");
		hppNavigationBar.css("opacity", 0.6);
		hppNavigationItems.addClass("homepage-promo-item-hidden")
		hppNavigationItems.eq(i - 1).removeClass("homepage-promo-item-hidden");
		hppNavigationWhiteout.animate( { opacity : 0 }, "fast", function() {
			$(this).css("display", "none");
			NICK.utils.showSwfs(".homepage-promo-container");
		});

		// If this is the first video in the list, auto play it
		if ( hppNavigationItems.eq(hppCurrentItem - 1).is(".homepage-promo-item-video:first") && hppAutoPlayedFirst == false ) {
			hppNavigationItems.eq(hppCurrentItem - 1).find(".homepage-promo-video-thumb").click();
			hppAutoPlayedFirst = true;
		} else {
			$(".homepage-promo-video-thumb").show();
		}
	});
}

function hppPlayFirstVideo() {
	if ( hppFirstVideoRef != null ) {
		hppFirstVideoRef.hide();
		$("#homepage-promo-video-wrapper").css("opacity", 1);
	}
}

function hppStartTimer() {
	// If a video is playing, don't ever start the timer
	if ( $("#homepage-promo-video-wrapper").html() != null && $("#homepage-promo-video-wrapper").html() != "" ) {
		return false;
	}

	hppStopTimer();
	hppTimer = setInterval( function() { hppNextItem(); }, 5000);
}

function hppNextItem() {
	if ( hppCurrentItem == hppItemCount ) {
		hppScrollTo(1);
	} else {
		hppScrollTo(hppCurrentItem + 1);
	}
}

function hppStopTimer() {
	clearInterval( hppTimer );
	hppTimer = null;
}

function hppInitVideo(e) {
	// Stop Propagation & Timer (Both of these are EXTREMELY important)
	e.stopImmediatePropagation();
	hppStopTimer();

	// Move Video Wrapper to current Item
	$("#homepage-promo-video-wrapper").remove();
	$("<div />").attr("id", "homepage-promo-video-wrapper").insertAfter( $(this) );
	
	var vid = $(this).attr("id").substring(21);
	var pid = "homepage-promo-video";

	doRegisterSwf(pid ,'http://media.mtvnservices.com/mgid:cms:item:nick.com:' + vid,'homepage-promo-video-wrapper','9.0.124.0','256px','342px',{configParams:'site%3Dnick%26playerName%3DtoutPlayerNormal%26adRuleSet%3Ddefault%26demo%3Dnull%26clickableVideoScreen%3Dfalse'},'false','false','/dynamo/javascript/swfobject/expressinstall.swf','/common/detect/get_flash.jhtml',{wmode:"opaque",salign:"TL",allowScriptAccess:"always",swliveconnect:"false"},{name:pid,id:pid});
	NICK.videoPlayer.init(pid);

	// Never let Thumbnail or Video hit the anchor tag
	$("#homepage-promo-video-wrapper *").click(function(e) {
		e.preventDefault();
		e.stopImmediatePropagation();
	});

	if ( hppAutoPlayedFirst == true ) {
		$(this).hide();
	} else {
		$("#homepage-promo-video-wrapper").css("opacity", 0);
		hppFirstVideoRef = $(this);
	}

	return false;
}

function hppVideoEnd() {
	$("#homepage-promo-video-wrapper").remove();
	$(".homepage-promo-video-thumb").show();
	hppStartTimer();
	hppNextItem();
}

/* Must run this outside of document.ready to avoid IE blank screens */
var hppFont = {	src: "/assets/sifr/maxiFuturaBold.swf" };	
sIFR.fitExactly = true;
sIFR.activate(hppFont);
sIFR.replace(hppFont, {
	selector: ".homepage-promo-item-text h2",
	wmode: "transparent",
	css: { ".sIFR-root": { "cursor": "pointer", "color": "#fb5c0b" } },
	forceSingleLine: true
});