/*
 *	Requires: 
 *	swfobject for flash video
 *	
 *	Video Player Params:
 *	locale		- (REQUIRED)
 *	guid			- (REQUIRED) guid of video to play
 *	playerID		- Live = 390534381001 or Staging = 390534386001
 *	playerType		- Used to lookup playerID in playerIdMappings object
 *	publisherID		- Nike Corporate Account ID 
 *	playlistId		- ID of playlist to play
 * 	labels			- Url to translated labels xml
 *	width & height	- Default to large player size - can be overridden
 *	wmode		- Flash wmode setting defaults to opaque for non IE, transparent for IE.
 *
 *	USAGE:
 * 	$("#p_video").videoplayer({playerType: "homePlayer", guid: "605060085001"});
 *
**/


(function($) {

	/*
	* Player Type Configuration Object
	*/
	
	var playerIdMappings = {
		defaultPlayer : "110154046001",
		freePlayer : "713898317001",
		russianPlayer : "390534381001",
		homePlayer : "390534386001",
		singlePlayer : "390534386001",
		playlistPlayer : "390534386001",
		simpleNoFSPlayer : "441686739001",
		simplePlayer: "619532068001",
		noFullnoSharePlayer : "644142870001",
		noControlsJustVideoPlayer: "708577629001"
	};

	$.fn.videoplayer = function(settings) {

		return this.each(function() {
			var config = {
				parentContainer:		this,
				locale:					"",
				guid:					"",
				playerType:				"defaultPlayer",
				publisherID:			"72451143001",
				width:					"908",
				height:					"510",
				dynamicStreaming:		"true",
				playlistId:				"",
				wmode: 					""
			};
	
			if (settings) $.extend(config, settings);
			
			var videoContainerId = $(config.parentContainer).attr("id") || '';
			if (videoContainerId == '') {
				videoContainerId = 'video-player-' + new Date().getTime();
				$(config.parentContainer).attr("id", videoContainerId);
			}
			
			var flashVars = {};
			var flashParams = {};
			var flashAttributes = {};
			
			flashVars.videoId = config.guid.replace(/_id[a-zA-Z0-9]*/i,"");
			
			flashVars.locale = config.locale.replace(/__/,"_"); // replace __ with _
			//use specific player for ru_RU
			if (flashVars.locale == "ru_RU"){
				flashVars.playerID = playerIdMappings["russianPlayer"];
			}else {
				flashVars.playerID = playerIdMappings[config.playerType];
			}
			flashVars.labels = "http://admin.nikevideo.nike.com/static/video_player/xml/translate/2.0/" + flashVars.locale + "_Labels.xml";
			flashVars.dynamicStreaming = config.dynamicStreaming;
			flashVars.isVid = "true";
			flashVars.isUI = "true";
			if (config.playlistId != "") {
				flashVars["@playlistTabs"] = config.playlistId;
			}
			flashVars["@videoPlayer"] = config.guid.replace(/_id[a-zA-Z0-9]*/i,"");
			flashVars.isVid = "true";
			flashVars.isUI = "true";
			var shareUrl = window.location.href.replace(/locale\=[^&]+/,"");
			flashVars.linkBaseURL = shareUrl;
			//var regex=new RegExp(/locale=.*(\&|$)/);
			
			if (config.wmode != "") {
				flashParams.wmode = config.wmode;	
			}
			else {
				flashParams.wmode = ($.browser.msie)? "window": "opaque";
			}
			flashParams.scale = "noscale";
			flashParams.allowScriptAccess = "always";
			flashParams.allowFullScreen = "true";
			flashParams.bgColor = "#000000";
			
			flashAttributes.id = videoContainerId
			flashAttributes.name = videoContainerId;
	
			// Video module specific
			var moduleWidth = config.width;
			var moduleHeight = config.height;
			var moduleSWF = "http://c.brightcove.com/services/viewer/federated_f9";
			
			/**
				IE8-specific workaround: IE8 will not play the video more than
				once. The issue has been narrowed to a problem retrieving
				the video data from the browser cache. Adding random number
				parameter to the swf to prevent caching.
			*/
			if($.browser.msie){
				if($.browser.version.indexOf("8.") != -1){
					moduleSWF = moduleSWF + "?rand=" + Math.floor(Math.random()*100000);
				}
			}
			
			swfobject.embedSWF(moduleSWF, videoContainerId, moduleWidth, moduleHeight, "9", flashParams.bgColor, flashVars, flashParams, flashAttributes);
		});
	};
})(jQuery);
