var projectVideoOpen = false;
var previousSelection = false;

$('#pageFrame').ready(function() {
	videoLinks = $('#projectSidebar #aboutProject li.video a, #projectSidebar #interviewParts li a').click(function() {
		
		// if this is not already selected, launch it
		if (!$(this).parent().hasClass('selected')) {
			
			// if the previous selection is not a video, bookmark it
			if (!projectVideoOpen) {
				previousSelection = $("#projectSidebar .selected").eq(0);
				previousSelection.removeClass('selected');
			}
			
			// otherwise just remove the selected class from the previous video link
			else { $("#projectSidebar .selected").eq(0).removeClass('selected'); }
			
			// make this new one selected
			$(this).parent().addClass('selected');
			
			// if something else was previously "now playing", remove that label
			$("#nowplaying").remove();
			
			// add the "now playing" text to this video
			if ( $(this).find('br').length == 0 ) { $(this).append('<span id="nowplaying"><br>Now Playing</span>'); }
			else { $(this).find('br').eq(0).before('<span id="nowplaying"><br>Now Playing</span>'); }
			
			// open the video and move the view to the top of the page
			OpenProjectPageVideo($(this).attr('href'));
			if (document.location.href.indexOf("#") == -1) { document.location.href = document.location.href + "#top"; }
			else { document.location.href = document.location.href.substr(0,document.location.href.indexOf("#")) + "#top"; }
			return false;
		}
	});
});

function OpenProjectPageVideo(vidfile) {
	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);

	if (projectVideoOpen == false) {
		$("#videoFrameShadow").append('<div id="videoFrameClose">Close the Video</div>');
		$("#videoFrameClose").click(function () { 
			CloseProjectPageVideo(); 
    	});
		
		$("#pageArea").animate({paddingTop: "410px"}, 500);
		$("#videoFrameShadow").animate({top: "75px"}, 500);
		projectVideoOpen = true;
	}
}

function CloseProjectPageVideo() {
	$("#pageArea").animate({paddingTop: "140px"}, 500);
	$("#videoFrameShadow").animate({top: "200px"}, 500);
	$("#videoFrameClose").remove();
	$("#videoPlayer").replaceWith('<div id="videoPlayer"></div>');
	projectVideoOpen = false;
	$("#projectSidebar .selected").eq(0).removeClass('selected');
	$("#nowplaying").remove();
	if (previousSelection != false) {
		previousSelection.addClass('selected');
		previousSelection = false;
	}	
}
