// track current feature info
var currentThumb = false;

// track currently playing video
var flvPointer = false;

// track if any video is playing
var videoOpen = false;

// width of preview popup
var previewWidth = 262;

// pointers to ongoing actions
var outtimer;
var outtimer2;

// rollover action for thumbnails
$(".videoMenuItems td").hover(
	function() {
		// do not do this if the video for this thumbnail is already playing
		if (!$(this).hasClass('playing')) {

			// don't do this if this thumbnail's preview is already showing
			if (!$(this).hasClass('hovered')) {

				// stop any other actions
				clearTimeout(outtimer);
				clearTimeout(outtimer2);

				// hide any other existing preview
				HidePreview();
				$(".videoMenuItems td.hovered").removeClass('hovered');

				// pause currently playing video
				if (flvPointer) { flvPointer.PauseVideo(); }

				// mark this as hovered
				$(this).addClass('hovered');

				// calculate the position for the thumbnail and the preview box
				var thumb_top = $(this).offset().top;
				var thumb_left = $(this).offset().left;
				var preview_left = thumb_left + (($(this).width())*0.5) - (previewWidth/2);

				// get the preview information
				var preview_title = '<h3>' + $(this).find("div.projTitle").eq(0).text() + '</h3>';
				var preview_summary = '<p>' + $(this).find("div.projSummary").eq(0).text() + ' <span class="launchlink">Launch Now</span></p>';
				var preview_visual = "";

				// insert the preview image or video, if present
				if ($(this).find("div.projPreview").length > 0) {
					var pre_vis = $(this).find("div.projPreview").eq(0);
					if (pre_vis.hasClass("video")) { preview_visual = '<div class="videopadding"><div id="previewVideoHolder"><br/>This video preview requires the<br/>Adobe Flash Plug-in (version 9)</div></div>' }
					else if (pre_vis.hasClass("image")) { preview_visual = '<img border="0" width="240" height="135" alt="" src="'+ pre_vis.find('a').attr('href') +'"/>'; }
				}

				// insert the preview and position it
				// give the preview hover actions
				$('<div id="featurePreview"><div id="previewContent">'+ preview_visual + preview_title + preview_summary +'</div></div>').appendTo("body").css({'top':(thumb_top-$("#featurePreview").innerHeight())+"px",'left':preview_left+"px"}).hover(
					function() { clearTimeout(outtimer); },
					function() { outtimer = setTimeout(HidePreview,100); }
				).click(function() { WatchNow($(this)); });

				// add video if there is a video preview
				if ($(this).find("div.projPreview").eq(0).hasClass("video")) { InsertPreviewVideo($(this).find("div.projPreview a").eq(0).attr('href')); }

				// show the "play now" button
				$(this).find(".content").append('<div id="playNow"></div>');


			}
			else { clearTimeout(outtimer); }
		}
	},
	function() {
		// do not do this if the video for this thumbnail is already playing
		if (!$(this).hasClass('playing')) {

			// do this if this thumbnail's preview is already showing
			if ($(this).hasClass('hovered')) {
				outtimer = setTimeout(HidePreview,100);
			}
		}
	}
);

// event handler for scrolling menu
$("#videoMenu").scroll(function() {
	if ($("#featurePreview").length > 0 && $(".videoMenuItems td.hovered").length > 0) { // the preview popup is showing

		// find the hovered thumbnail
		var t = $(".videoMenuItems td.hovered").eq(0);

		// use the new position of the thumbnail to update the position of the preview popup
		$("#featurePreview").css({'top':(t.offset().top - $("#featurePreview").innerHeight())+"px",'left':(t.offset().left + (t.width()*0.5) - (previewWidth/2))+"px"});
	}
});

// event handler for clicking on menu options
$(".videoMenuItems td").click(function() { WatchNow($(this)); return false; } );


// reused function for hiding the preview popup
function HidePreview() {
	// $("#projectInfo").animate({opacity: 1},250); this doesn't work in IE7; it messes up the png display
	$(".videoMenuItems .hovered #playNow").remove();
	$("#featurePreview").remove();
	$(".videoMenuItems td.hovered").removeClass("hovered");
	if (flvPointer) { flvPointer.PlayVideo(); }
}

// insert preview video into preview popup
function InsertPreviewVideo(href) {
	var flashvars = {};
	flashvars.sourceURL = href;

	var params = {};
	params.menu = "false";
	params.scale = "default";
	params.bgcolor = "#847c55";
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("/ncrising/images/montageplayer.swf", "previewVideoHolder", "240", "135", "8.0.0", "", flashvars, params, attributes);
}

// reused function for launching video
function WatchNow(t) {
	// do not do this if the video for this thumbnail is already playing
	if (!t.hasClass('playing')) {

		// remove the current video
		if ($(".videoMenuItems td.playing").length > 0) {
			if ($(".videoMenuItems td.playing").eq(0).hasClass("project")) { $("#projectInfo").remove(); }
			$("#videoFrameShadow").remove();
			flvPointer = false;
			$(".videoMenuItems td.playing").removeClass("playing").find('#currentlyPlaying').remove();
		}
		else { // nothing was currently playing
			// fade out the intro text
			$("#featureArea .introduction").fadeOut(500);
		}

		// identify the new selected feature
		var myChoice = $(".videoMenuItems td.hovered").eq(0);

		// remove the preview popup
		HidePreview();

		// change the status of the current selection from hovered to playing
		myChoice.removeClass("hovered").addClass("playing");

		// show the "playing" message
		myChoice.find(".content").append('<div id="currentlyPlaying"></div>');

		// if this selection is a project (and not a townhall or a compilation)
		// get and display the project menu

		if ( myChoice.hasClass("project")) {

			// get the url of the feature page
			var featureUrl = myChoice.find(".projTitle a").eq(0).attr("href");
			var featureTitle = myChoice.find(".projTitle a").eq(0).text();

			// add the block for the project info to the page
			$('<div id="projectInfo"><div id="aboutProject"></div></div>').appendTo("#featureArea");

			// grab the "about the project" list from the feature page, insert it here
			$("#aboutProject").load(featureUrl +  " #aboutProject ul", null, function() {

				var basePath = $(location).attr('pathname');
				basePath = basePath.replace('index.html', '');

				// correct link hrefs
				$("#projectInfo a").each(function() {
					if (!($(this).parent().hasClass("video"))) {
						var rewriteURL = 0;
						var fullURL = $(this).attr('href');
						var h = fullURL.substr(fullURL.lastIndexOf("/")+1);
						if (fullURL.indexOf('http:')==0) {
							var fullPath = fullURL.replace('http://'+$(location).attr('hostname'), '');
							if (fullPath==basePath+h) {
								rewriteURL = 1;
							}
						} else {
							rewriteURL = 1;
						}
						if (rewriteURL) {
							$(this).attr('href',featureUrl +'/'+ h);
						}
					}
				});

				// correct image srcs
				$("#projectInfo img").each(function() {
					var s = $(this).attr('src');
					s = s.substr(s.lastIndexOf("/")+1);
					$(this).attr('src',featureUrl +'/'+ s);
				});

				// remove any selected link mark
				$("#projectInfo .selected").removeClass("selected");

				// add the feature project title
				$("#projectInfo").prepend('<h3>'+featureTitle+'</h3>');

				// add the bottom shadow
				$("#projectInfo").append('<div id="aboutProjectEnd"></div>');

				// enable click action for video links
				$('#projectInfo #aboutProject li.video a').click(function() {
					if (!$(this).parent().hasClass('selected')) {
						$("#projectInfo .selected").removeClass("selected");
						$("#nowplaying").remove();
						$(this).parent().addClass("selected");
						$(this).find('br').eq(0).before('<span id="nowplaying"><br>Now Playing</span>');
						OpenProjectPageVideo($(this).attr('href'));
					}
					return false;
				});

				// start the first video
				$("#projectInfo #aboutProject li.video").eq(0).addClass("selected").find("a br").eq(0).before('<span id="nowplaying"><br>Now Playing</span>');;
				OpenProjectPageVideo($("#projectInfo #aboutProject li.video").eq(0).find("a").attr('href'));
			});
		}
		else {
			OpenProjectPageVideo(myChoice.find('.projTitle a').attr('href'));
		}
	}
	return false; // don't execute any default hypertext/hyperlink action.
}


function OpenProjectPageVideo(vidfile) {
	// if a video was already playing, remove it
	if ($("#videoFrame").length > 0) { $("#videoFrame #videoPlayer").replaceWith('<div id="videoPlayer"><br/>This video requires the<br/>Adobe Flash Plug-in (version 9)</div>'); }

	// otherwise add a fresh instance of the video player interface
	else {
		$("#featureArea").append('<div id="videoFrameShadow"><div id="videoFrame"><div id="videoPlayer"><br/>This video preview requires the<br/>Adobe Flash Plug-in (version 9)</div></div></div>');
		$('<div id="videoFrameClose">Close the Video</div>').appendTo('#videoFrameShadow').click(function () { CloseProjectPageVideo(); });
	}

	if ($(".videoMenuItems td.playing").eq(0).hasClass("project")) { $("#videoFrameShadow").removeClass('solo'); }
	else { $("#videoFrameShadow").addClass('solo'); }

	var flashvars = {};
	flashvars.sourceURL = vidfile;

	var params = {};
	params.menu = "false";
	params.allowFullScreen = "true";
	params.scale = "default";
	params.wmode = "transparent";

	var attributes = {};
	swfobject.embedSWF("/ncrising/images/customplayer.swf", "videoPlayer", "480", "300", "9.0.0", "", flashvars, params, attributes);
	flvPointer = $("#videoFrameShadow").find('object').get(0);
}

function CloseProjectPageVideo() {

	// reset all of the project and video playing stuff
	if ($(".videoMenuItems td.playing").eq(0).hasClass("project")) { $("#projectInfo").remove(); }
	$("#videoFrameShadow").remove();
	$(".videoMenuItems td.playing").removeClass("playing").find('#currentlyPlaying').remove();
	flvPointer = false;
	$("#featureArea .introduction").fadeIn(500);
}


