var hppItemCount = 0, hppCurrentItem = null;
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");
	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();
			});
			
	}
	
	// Apply Video Click Events
	$(".homepage-promo-video-thumb").click(hppInitVideo);

	// Never let Thumbnail or Video hit the anchor tag
	$("#homepage-promo-video-wrapper").live("click", function(e) {
		e.stopImmediatePropagation();
		return false;
	});
	
	// 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(0);
		
		hppStarted = true;
	}
}

function hppScrollTo( i ) {
	$("#homepage-promo-video-wrapper").html("");
	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");

		// If this is the first video in the list, auto play it
		if ( hppNavigationItems.eq(hppCurrentItem).is(".homepage-promo-item-video:first") && hppAutoPlayedFirst == false ) {
			hppNavigationItems.eq(hppCurrentItem).find(".homepage-promo-video-thumb").click();
			hppAutoPlayedFirst = true;
		} else {
			$(".homepage-promo-video-thumb").show();
		}
	});

	hppCurrentItem = i;
}

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;
	}
	
	clearInterval( hppTimer );
	hppTimer = null;

	hppTimer = setInterval( function() {
		if ( hppCurrentItem == (hppItemCount - 1) ) {
			hppScrollTo(0);
		} else {
			hppScrollTo(hppCurrentItem + 1);
		}
	}, 5000);
}

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";

	//run the player off the proper stage environment
	var hostName = window.location.toString();
	var hostPathName = window.location.pathname.toString();
	var feedHub = "home";
	
	doRegisterSwf(pid ,'http://media.nick.com/mgid:cms:video:teennick.com:' + vid,'homepage-promo-video-wrapper','9.0.124.0','256px','342px',{sid:'TeenNick__Home'},'false','false','/dynamo/javascript/swfobject/expressinstall.swf','/common/detect/get_flash.jhtml',{wmode:"opaque",salign:"TL",allowScriptAccess:"always",allowFullScreen:"true",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();
}
