$(document).ready(function(){
	prepLightbox();
	whichVideo();
	introVideo();
});

function prepLightbox() {
	$(".showLightbox").click(function(event) {
		if ($("#lightbox")) {
			$("#lightbox").remove();
		}
		var thisVideo = $(this).attr("id");
		doLightbox(thisVideo);
		event.preventDefault();
	});
}

function prepHideLightbox() {
	$(".hideLightbox").click(function(event) {
		$("#lightbox").fadeOut("slow");	
		event.preventDefault();
	});
}

function doLightbox(thisVideo) {
	$("#footer").after("<div id=\"lightbox\" class=\"" + thisVideo + "\"><div id=\"flashVideo\"><a href=\"#\" class=\"hideLightbox\">Hide Lightbox</a><div id=\"flashReplace\"><p><a href=\"http://get.adobe.com/flashplayer/\">Please download the latest version of Flash to view this content.</a></p></div></div></div>");
	insertFlashVideo(thisVideo);
	var document_height = $(document).height();
	$("#lightbox").css("height", document_height);

	/* 
		TODO 
		 - fix video for mp4
		 - make video player scale to video size
		 - test in all browsers
	*/
	
	$("#lightbox").fadeIn("slow");
	$(document).scrollTop(0);
	prepHideLightbox();
}

function whichVideo(){
	if ( exists( '#flashVideo' ) ) {
		var id = $("body").attr("id");
	}
	insertFlashVideo(id);
}

function insertFlashVideo(id) {
		var videoFile = id + ".flv";
		if (id == "dougherty") {
			videoFile = "tv_promo_large.flv";
			var height = "354";
			var width = "706";
		}	
		else if (id == "giegerich") {
			var height = "240";
			var width = "316";
		}
		else {
			var height = "356";
			var width = "508";
		}
		var flashvars = {};
		// path from flash folder
		flashvars.video = "../flash/data/video/"+videoFile;
		// path from page
		flashvars.poster= "img/"+id+"_poster.jpg";
		flashvars.clickToPlay = "true";
		flashvars.onComplete = "videoComplete";
		var params = {};
		params.wmode = "transparent";
		params.allowscriptaccess = "sameDomain";
		var attributes = {};
		attributes.id = "wavCam1";
		swfobject.embedSWF("flash/waveCam_VP.swf", "flashReplace", width, height, "9.0.124", "flash/data/swfs/expressinstall/expressinstall.swf", flashvars, params, attributes);
}

function introVideo() {
	if( exists( "#intro" ) ){
		var params = {};
		params.wmode = "transparent";
		params.allowscriptaccess = "always";
		var attributes = {};
		attributes.id = "intro";
		swfobject.embedSWF("flash/shim.swf", "intro", "100%", "450", "9.0.0", "flash/data/swfs/expressinstall/expressinstall.swf", null, params, attributes);
	}
}

function exists( e ){
	return $(e).length > 0;
}
