/**
 * DrPepper.VideoController
 *
 * Controls video embedding and interaction on the videos page.
 *
 * Author:	Silvan Reinhold
 * Date:	2009-04-16
 * See:		
 **/

var DrPepper = DrPepper || {};


/**
 * Constructor
 **/
DrPepper.VideoController = function()
{
	alert("WARNING: DrPepper.VideoController is a static class.");
}

// Internal video player or external embed
//
DrPepper.VideoController.VIDEOTYPE_INTERNAL = 0;
DrPepper.VideoController.VIDEOTPE_EXTERNAL = 1;

// Internal video player SWF
DrPepper.VideoController.INTERNAL_PLAYER_URL	= "/swf/com/drpepper/videoplayer/DrPepperVideoPlayer.swf";
DrPepper.VideoController.INTERNAL_PLAYER_ID		= "videoplayer-internal";

// Video player dimensions
//
DrPepper.VideoController.INTERNAL_PLAYER_WIDTH	= 526;
DrPepper.VideoController.INTERNAL_PLAYER_HEIGHT	= 370;	// account for reflection at bottom
DrPepper.VideoController.EXTERNAL_PLAYER_WIDTH	= 526;
DrPepper.VideoController.EXTERNAL_PLAYER_HEIGHT	= 330;


/**
 * Plays the specified video.
 * This video may be internal, in which case the video URL is passed to this function.
 * The video may also be external, in which case the embed code is passed.
 *
 * @param	p_xPlayerContainerElement		Element to contain the actual video player
 * @param	p_xPlayerInfoElement			Element to contain textual information (title / "added on" date) about the video
 * @param	p_sApplicationUrl				Application URL for streaming video (null for progressive download video)
 * @param	p_sVideoUrl						Video file path and file name (for both streaming and progressive download) *or* embed code for external video
 * @param	p_sStillUrl						URL of still image for the video
 * @param	p_sVideoCategoryTitle			Category title (essentially: ModuleBox title) for the video; needed to generate Omniture tracking tags
 * @param	p_sVideoTitle					Video display title
 * @param	p_sAddedDate					"Date added" for the video
 **/
DrPepper.VideoController.playVideo = function(p_xPlayerContainerElement, p_xPlayerInfoElement, p_sApplicationUrl, p_sVideoUrl, p_sStillUrl, p_sVideoCategoryTitle, p_sVideoTitle, p_sAddedDate)
{
	// Check parameters for validity
	//
	if (!p_sVideoUrl || (p_sVideoUrl == "")) {
		return;
	}

	p_sApplicationUrl = p_sApplicationUrl || "";
	p_sStillUrl = p_sStillUrl || "";
	p_sVideoTitle = p_sVideoTitle || "Untitled";
	p_sAddedDate = p_sAddedDate || "N/A";
	
	l_sFullVideoUrl = (p_sApplicationUrl == "" ? "" : p_sApplicationUrl + "*") + p_sVideoUrl;
	
	// Get references to video player container and video info box
	//
	p_xPlayerContainerElement = $(p_xPlayerContainerElement);
	p_xPlayerInfoElement = $(p_xPlayerInfoElement);


	// Internal videos are always FLVs, external videos may be any embed code
	// NOTE: p_sVideoUrl.substr(-4) won't work in IE
	//
	var l_nVideoType = (p_sVideoUrl.substr(p_sVideoUrl.length - 4, 4).toLowerCase() == ".flv") ? DrPepper.VideoController.VIDEOTYPE_INTERNAL : DrPepper.VideoController.VIDEOTYPE_EXTERNAL; 

	// Display video information
	//
	var l_sTitleColor = "ffffff";
	var l_nTitleSize = 15;
	var l_nTitleWidthLimit = 174;
	var l_sTitleUrl = DrPepper.Util.getDynamicTextUrl(p_sVideoTitle, l_nTitleSize, l_sTitleColor, null, l_nTitleWidthLimit);


	// Purely to satisfy the IE6 PNG fix, remove and re-attach; otherwise, the fix will break.
	$(".title", p_xPlayerInfoElement).remove();
	var l_sTitle = $("<div />").addClass("title").css("background", "url('" + l_sTitleUrl + "') 0% 100% no-repeat");
	$(p_xPlayerInfoElement).prepend(l_sTitle);
	
	var l_sLocalizedAddedOnText = (g_sLangAbbrev == "es") ? "Agregado el" : "Added on";
	
	$(".added-on", p_xPlayerInfoElement).html(l_sLocalizedAddedOnText + " <span class=\"date\">" + p_sAddedDate + "</span>");


	// If the still image URL does not contain a protocol (and at the same time, presumably the host is missing, too),
	// add the protocol and current host to make the URL absolute, so that the embedded player will pull its still URL
	// from the server the player originated from (i.e. from the current host, which this VideoController resides on).
	//
	// Likewise, add a protocol and host to the full video URL, if necessary.
	//
	if (p_sStillUrl.substr(0, 7) != "http://") {
		p_sStillUrl = "http://" + window.location.host + p_sStillUrl;
	}
	if (l_nVideoType == DrPepper.VideoController.VIDEOTYPE_INTERNAL) {
		if (l_sFullVideoUrl.indexOf("://") < 0) {
			l_sFullVideoUrl = "http://" + window.location.host + l_sFullVideoUrl;
		}
	}

	
	// or internal videos: If internal video player is already on page, reuse it.
	//
	var l_xInternalVideoPlayer = $("#" + DrPepper.VideoController.INTERNAL_PLAYER_ID); 
	if (l_nVideoType == DrPepper.VideoController.VIDEOTYPE_INTERNAL)
	{
		if (($(l_xInternalVideoPlayer).length > 0) && $(l_xInternalVideoPlayer).get(0).changeVideo) {
			l_xInternalVideoPlayer.get(0).changeVideo(l_sFullVideoUrl, p_sStillUrl, p_sVideoCategoryTitle, p_sVideoTitle, "true", "true", "true");
			return;
		}
	}

	// Otherwise, remove previous video player (if applicable)
	//
	if ($(l_xInternalVideoPlayer).length > 0) {
		// remove internal player
		swfobject.removeSWF(DrPepper.VideoController.INTERNAL_PLAYER_ID);
	}
	else {
		// remove (external) embed
		$(p_xPlayerContainerElement).empty();	
	}

	// Insert new video player
	//
	switch (l_nVideoType)
	{
		case DrPepper.VideoController.VIDEOTYPE_INTERNAL:
			var l_xVideoPlayerDiv = $("<div />").attr("id", DrPepper.VideoController.INTERNAL_PLAYER_ID);
			$(p_xPlayerContainerElement).append(l_xVideoPlayerDiv);
			
			var l_xFlashVars = {
				video: l_sFullVideoUrl,
				still: p_sStillUrl,
				categoryTitle: escape(p_sVideoCategoryTitle),
				title: escape(p_sVideoTitle),
				autoplay: "true",
				allowfullscreen: "true"
			};

			var l_xParams = {
				allowfullscreen: "true",
				allowscriptaccess: "always",
				wmode: "transparent"
			};

			swfobject.embedSWF(
				DrPepper.VideoController.INTERNAL_PLAYER_URL,
				DrPepper.VideoController.INTERNAL_PLAYER_ID,
				DrPepper.VideoController.INTERNAL_PLAYER_WIDTH,
				DrPepper.VideoController.INTERNAL_PLAYER_HEIGHT,
				"9.0.124",	// irrelevant, as the main site will check for JS/Flash anyway
				null,		// express install is irrelevant
				l_xFlashVars,
				l_xParams,
				DrPepper.VideoController.INTERNAL_PLAYER_ID // required for SWFAddress, if used
			);
			break;
			
		case DrPepper.VideoController.VIDEOTYPE_EXTERNAL:
			var l_sPlayerEmbedCode = this.cleanEmbedCode(p_sVideoUrl);
			$(p_xPlayerContainerElement).html(l_sPlayerEmbedCode);
			break;
		
		default:
			// ?
	}
}


/**
 * Returns cleaned-up embed code for external video
 *
 * @param	p_sEmbedCode	URL of the video, or embed code for external video
 * @return	Cleaned-up embed code
 **/
DrPepper.VideoController.cleanEmbedCode = function(p_sEmbedCode)
{
	var l_sEmbedCode = p_sEmbedCode;
	l_sEmbedCode = l_sEmbedCode.replace(/width=["']?( )*([0-9])+( )*["']?/gi, 'width=\"' + DrPepper.VideoController.EXTERNAL_PLAYER_WIDTH + '\"');
	l_sEmbedCode = l_sEmbedCode.replace(/height=["']?( )*([0-9])+( )*["']?/gi, 'height=\"' + DrPepper.VideoController.EXTERNAL_PLAYER_HEIGHT + '\"');
	l_sEmbedCode = l_sEmbedCode.replace(/name=["']?( )*(wmode)( )*["']?/gi, 'remove=\"\"');
//	l_sEmbedCode = l_sEmbedCode.replace(/autoplay=1/, '');	
	l_sEmbedCode = l_sEmbedCode.replace(/<embed/gi, '<param name="wmode" value="transparent"></param><embed wmode=\"transparent\"');  
	
	return l_sEmbedCode;
}
