/***********************************************************************
playVideo initializes the SWFObject for video play and
sets up the parameters of the video
required: videoURL, swfObject (v2.2), jQuery 1.3.2+
params:

// Example event setup:
    // Give the absolute path to the Flash Files
    var flashPrefix = "Utils/FlashVideoPlayer_scale.swf";
    // Grabs all links with a class of videoLink and sets them up to play a video on click
    $("a.videoLink").click(function(e) {
		e.preventDefault();
		var vidParams = $(this).attr("rel").split("::")[1].split("|");
		playVideo($(this).attr("href"), "lbVideo", vidParams[0], vidParams[1], vidParams[2], flashPrefix + "FlashVideoPlayer_scale.swf", '');
	});
	
// Example Link:
    <a href="/relative/path/to/the/video.mp4" rel="videoParams::532|420|99" class="videoLink">Play Video</a>
***********************************************************************/
function playVideo(videoURL, lbid, vidWidth, vidHeight, vidDuration, playerURL, optionalParamStr) {
	// SWFplayer and ContainerDiv sizes are dynamic based on vidWidth & vidHeight


	//Check if video is larger than the viewport, if so, resize video proportionally to fit
	var viewportY = $(window).height();
	var viewportX = $(window).width();
	var dimChanged = false;

	vidWidth = parseInt(vidWidth);
	vidHeight = parseInt(vidHeight);
	if (vidWidth + 20 > viewportX || vidHeight + 20 > viewportY) {
		dimChanged = true;
		var vidPorportions = vidWidth / vidHeight;
		var orgW = vidWidth;
		var orgH = vidHeight;
		if (vidHeight + 20 > viewportY) {
			vidHeight = viewportY * .75;
			vidWidth = orgW > orgH ? vidHeight * vidPorportions : vidHeight / vidPorportions;
			if (vidWidth + 20 > viewportX) {
				vidWidth = viewportX * .75;
				vidHeight = orgW > orgH ? vidWidth / vidPorportions : vidWidth * vidPorportions;
			}
		} else {
			vidWidth = viewportX * .75;
			vidHeight = orgW > orgH ? vidWidth / vidPorportions : vidWidth * vidPorportions;
			if (vidHeight + 20 > viewportY) {
				vidHeight = viewportY * .75;
				vidWidth = orgW > orgH ? vidHeight * vidPorportions : vidHeight / vidPorportions;
			}
		}
	}

	// test function variables
	if (videoURL == "") { return false; }
	if (lbid == "") { lbid = "lbVideo"; }
	if (vidWidth == "") { vidWidth = "532"; }
	if (vidHeight == "") { vidHeight = "420"; }
	if (vidDuration == "") { vidDuration = "9999"; }
	if (playerURL == null || playerURL == "") { playerURL = flashPrefix + "FlashVideoPlayer_scale.swf"; }

	var playerWidth = parseInt(vidWidth);
	// Add 22 for the height of the control bar in the Flash player, edit if the control bar height changes
	var playerHeight = parseInt(vidHeight) + 22;
	if (optionalParamStr == null || optionalParamStr == "") {
		optionalParamStr = "";
	}
	var flash1_contentVersion = "9.0.115";


	// get div if exists, create if it doesn't
	var lightboxDiv = $("#" + lbid);
	if (lightboxDiv.length == 0) {
		lightboxDiv = createLBdiv(lbid, 'body');
	}

	// setup SWFObject
	if (swfobject.hasFlashPlayerVersion(flash1_contentVersion)) {
		//EMBED SWFObject Flash Player
		var vidDiv = lbid + "noflash";
		var flashvars = {};
		flashvars.videoUrl = videoURL;
		flashvars.videoWidth = vidWidth;
		flashvars.videoHeight = vidHeight;
		flashvars.videoDuration = vidDuration;
		var flashparams = {};
		flashparams.menu = "false";
		flashparams.quality = "high";
		flashparams.wmode = "opaque";
		flashparams.bgcolor = "#3E3C3C";
		var flashattributes = {};

		swfobject.embedSWF(playerURL, vidDiv, playerWidth, playerHeight, flash1_contentVersion, null, flashvars, flashparams, flashattributes);

	} else if (supports_html5_h264_video()) {
		//EMBED HTML5 Video Player
		// NOTE that iPhone OS2 does not support HTML5 Video
		var videoattributes = {};
		videoattributes.src = videoURL;
		videoattributes.width = vidWidth;
		videoattributes.height = vidHeight;
		videoattributes.controls = "true";
		var video = $("<video></video>").attr(videoattributes);
		lightboxDiv.html('');
		lightboxDiv.append(video);

	}

	if (!dimChanged) {
		// open lightbox to display
		// ScrollFreeze is used for Firefox only because it repositions the page to the top when
		// overflow: hidden is applied to the HTML element
		$.fn.colorbox({
			//outerWidth: playerWidth + 38,
			//outerHeight: playerHeight + 49,
			innerWidth: playerWidth,
			innerHeight: playerHeight,
			inline: true,
			href: "#" + lbid,
			onOpen: function() { if ($.browser.mozilla) ScrollFreeze.on(); $('html').css({ overflow: 'hidden' }); }, //prevent scrolling
			onCleanup: function() { lightboxDiv.hide(); $('html').css({ overflow: 'auto' }); }, //re-enable scrolling
			onClosed: function() { lightboxDiv.remove(); if ($.browser.mozilla) ScrollFreeze.off(); }
		});
	} else {  // Don't disable scrolling if video had to be resized to fit
		// open lightbox to display
		$.fn.colorbox({
			//outerWidth: playerWidth + 38,
			//outerHeight: playerHeight + 49,
			innerWidth: playerWidth,
			innerHeight: playerHeight,
			inline: true,
			href: "#" + lbid,
			onOpen: function() { },
			onCleanup: function() { lightboxDiv.hide(); },
			onClosed: function() { lightboxDiv.remove(); }
		});
	}

}

/***********************************************************************
supports_html5_h264_video tests the browser to see if it can
play HTML5_h264 video natively
***********************************************************************/
function supports_html5_h264_video() {
	return !!document.createElement('video').canPlayType('video/mp4;');
}


/***********************************************************************
createsLBdiv creates a div for the video to go into
params: id for the lightbox div, width of the video, where to append it
returns the jQuery div object
***********************************************************************/
function createLBdiv(lbid, appendIdent) {
	var lightboxDiv = $("<div></div>").attr({ id: lbid });
	var nonflashVideoContentPart1 = "Flash player or HTML 5 Video support is required to display this content.";
	var nonflashVideoContentPart2LinkUrl = "http://get.adobe.com/flashplayer/";
	var nonflashVideoContentPart2LinkText = "Download Flash Player";
	var nonflashVideoContentPart2Text = " to view this content.";
	//create noflash div and insert
	var noflashDiv = $("<div></div>").attr("id", lbid + "noflash").addClass("no-flash");
	var noflashP1 = $("<p></p>").text(nonflashVideoContentPart1).attr("style", "padding-top:30px");
	var noflashP2link = $("<a></a>").attr({ href: nonflashVideoContentPart2LinkUrl, target: "_blank" }).text(nonflashVideoContentPart2LinkText);
	var noflashP2 = $("<p></p>").text(nonflashVideoContentPart2Text);
	noflashP2.prepend(noflashP2link);
	noflashDiv.append(noflashP1).append(noflashP2);
	lightboxDiv.append(noflashDiv);

	//Insert lightboxDiv
	$(appendIdent).append(lightboxDiv);
	return lightboxDiv;
}



/*********************************************************************************************
	ScrollFreeze stops the page from scrolling while on
	Originally downloaded from 
	http://bytes.com/topic/javascript/answers/498334-document-body-scroll-does-not-work-firefox
			
	Usage: ScrollFreeze.on() , ScrollFreeze.off()
	NOTE: The X freeze is currently not enabled, the script only freezes the Y axis scrolling
*********************************************************************************************/
ScrollFreeze = {
	propFlag: true,
	Ydisp: 0,
	Xdisp: 0,
	on: function() {
		if (this.getProp())
			$(window).bind("scroll", function() { ScrollFreeze.setXY(); });
	},
	off: function() {
		$(window).unbind("scroll");
		if ($(window).scrollTop() != $(window).data("yOffset"))
			setTimeout(this.setXY, 5);
	},
	getProp: function() {
		this.Ydisp = $(window).scrollTop();
		$(window).data("yOffset", this.Ydisp);
		return this.propFlag;
	},
	setXY: function() {
		$(window).scrollTop($(window).data("yOffset"));
	}
}
