/***************************************************************************************************
JQUERY SIMPLEFLVSHADOWBOX PLUGIN V.1.0  
@Author => Keegan Watkins 2008 
**************************************************************************************************/

(function($) {

// This function needs to be global, called by the player after loading
var updateContainer = window.updateContainer = function(jsonString) {
	// Parse JSON string into object...
	var params = eval('(' + jsonString + ')');
	// ...and create reference to video's width...
	var w = params.playerWidth;
	// ...and the video's height
	var h = params.playerHeight;
	var player = $("#player-container");
	var close = $("#close-button");
	// Style the player's container DIV
	player.css({
		width:  w + "px",
		height: (h - 45) + "px",  // override reserved space
		position: (FLVOverlay.utils.isIE() == 6) ? "absolute" : "fixed",
		zIndex: "2000",
		//Center player on page
		top: parseInt(FLVOverlay.utils.getHeightOffset(h) -2) + "px",
		left: FLVOverlay.utils.getWidthOffset(w) + "px",
		opacity: "1",
		filter : "alpha(opacity=100)"
	});
	// Style the close button
	// Create the close button link
	close.css({
		height: "20px",
		width: "20px",
		position: (FLVOverlay.utils.isIE() == 6) ? "absolute" : "fixed",
		// Place over top-right corner of player-container
		top: parseInt(FLVOverlay.utils.getHeightOffset(h) - 2) + "px",
		left: (parseInt(FLVOverlay.utils.getWidthOffset(w)) + parseInt(w) - 20) + "px",
		zIndex: "2001",
		background: "transparent url(/images/misc/neutral/flvshadowbox/close.png) no-repeat"
	});
}

// Expose a single object to the global scope
var FLVOverlay = window.FLVOverlay = { 
               
		utils : {
			removeShadowBox : function() {
					$(".shadow_parts").remove();
			},
			// Returns version of IE as Number or false
			isIE : function() {
				return ($.browser.msie) ? $.browser.version : false;
			},
			// Returns the offset needed to vertically center player on page, based on height argument
			getHeightOffset : function(h /*Number*/) {
				if ($.browser.msie) { 
					return ($.browser.version == 6) ? ($(window.document).height() - h) / 2: ($(window).height() - h) / 2  ;
				} else {
					return (window.innerHeight - h) / 2; 
				}
			},
			// Returns the offset needed to horizontally center player on page, based on width argument
			getWidthOffset : function(w /*Number*/) {
				return ($.browser.msie) ? ($(window).width() - w) / 2 : (window.innerWidth - w) / 2;
			}
		}, 

		// Method for creating new Lightbox
		create : function(path) {
			// Create and style the player's container DIV
			$("<div id='player-container' class='shadow_parts'></div>").appendTo("body").css({position: "absolute", top: "0", left: "0"});
			// IE and Firefox have a weird flicker, hide player for now. Use opacity/alpha, becuase "display:none" and "visibility:hidden" both
			// keep the player from loading properly. 
			if ($.browser.mozilla || $.browser.msie) {
				$("#player-container").css({
					opacity: "0",
					filter: "alpha(opacity=0)"
				});
			}
			
			// Create the close-button, styles applied in the updateContainer() method.
			$("<a id='close-button' class='shadow_parts' href='#'></a>").appendTo("body").click(function() {
				FLVOverlay.utils.removeShadowBox();
				return false;
			});
			// Create and style the overlay DIV
			$("<div id='shadow_overlay' class='shadow_parts'></div>").appendTo("body").stretchToPage().css({
				background: "#000 fixed",
				position: "absolute",
				top: "0",
				left: "0",
				zIndex: "100",
				opacity : ".7",
				filter: "alpha(opacity=70)"
			}).click(function() {
				FLVOverlay.utils.removeShadowBox();
			});
			// Embed the FLVPlayer swf using swfobject
			var swfPath = "http://www.ni.com/swf/flv/flvplayer2.swf?url=" + path;
			var player = new SWFObject(swfPath,"flvplayer","100%","100%","#000","8");
		    player.addParam("wmode", "transparent");
		    player.addParam("allowScriptAccess","always");
		    player.write('player-container');
			$("html, body").animate({scrollTop : 0}, 1000);
		}
}; //closes FLVShadowBoxController Singleton

// Add "$().stretchToPage()" method
$.fn.stretchToPage = function() {
	return this.each(function() {
		$(this).css({ height: $(document).height(), width: $(document).width() });
	});
}

})(jQuery);
