var BetaNGFlashVideo = function(post, videoConfig)
{
	var self = this;
	this.config = videoConfig || new BetaNGFlashVideoConfig();
	this.buzzAppUrl = "http://nmp.newsgator.com/ngbuzz/";
	
	//see if it's an actual config object
	if (!(self.config.isConfig))
	{
		//pass the object along and create a full BetaNGFlashVideoConfig
		self.config = new BetaNGFlashVideoConfig(videoConfig);
	}

	this.setPost = function(postValue)
	{
		var video = self.getFlvVideo(postValue);
		
		if (video != null)
		{
			self.config.url = video.url;
			if (video.Type)
			{
				self.config.mimeType = video.Type;
			}
		}
		else
		{
			self.config.url = "";
			
			debug("no flv videos found");
		}
		return self;
	}
	
	this.setVideoUrl = function(urlValue)
	{
		self.config.url = urlValue;
		return self;
	}
	
	this.setBuzzAppUrl = function(value)
	{
		self.buzzAppUrl = value;
		return self;
	}
	
	this.getMarkup = function()
	{
		if (self.config.url == "")
		{
			debug("no url specified or no flv video found on post");
			return "";
		}
		var playerUrl = self.buzzAppUrl + "scripts/ngvideo.swf";
		
		var getFlashVar = function(key)
		{
			if (self.config[key] != "")
			{
				return "&" + key.toLowerCase() + "=" + encodeURIComponent(self.config[key]);
			}
			return "";
		}
		
		var flashVars = ""
			+ "file=" + encodeURIComponent(self.config.url)
			+ "&height=" + self.config.height
			+ "&width=" + self.config.width
			+ "&autostart=" + self.config.autostart
			+ getFlashVar("thumbnail")
			+ getFlashVar("enableNGThumbnail")
			+ getFlashVar("disableControls")
			+ getFlashVar("acudeoCampaignId")
			+ getFlashVar("acudeoProgId")
			+ getFlashVar("acudeoCustId")
			+ getFlashVar("acudeoAdTarget")
			+ getFlashVar("loadCallback")
			+ getFlashVar("acudeoNoAdFound")
			+ getFlashVar("acudeoDomainNotAllowed");
		
		if (self.config.additionalFlashVars != "")
		{
			var additional = "";
			if (self.config.additionalFlashVars.indexOf("&") != 0)
			{
				additional += "&";
			}
			additional += self.config.additionalFlashVars;
			
			flashVars += additional;
		}		
		
		var out = '<object id="'+ self.config.id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" \
			width="' + self.config.width + '" height="' + self.config.height + '" codebase="http://www.adobe.com/go/getflashplayer"> \
				<param name="movie" value="' + playerUrl + '?' + flashVars + '" /> \
				<param name="quality" value="high" /> \
				<param name="wmode" value="' + self.config.wmode + '" /> \
				<param name="name" value="'+ self.config.id + '" /> \
				<param name="AllowScriptAccess" value="always" /> \
				<param name="allowFullScreen" value="true" /> \
				<embed id="'+ self.config.id + '" name="'+ self.config.id + '" AllowScriptAccess="always" allowfullscreen="true" wmode="' + self.config.wmode + '" type="application/x-shockwave-flash" quality="high" \
					src="' + playerUrl + '?' + flashVars + '" style="width: ' + self.config.width + 'px; height: ' + self.config.height + 'px;"/> \
			</object>';
		
		return out;
	}
	
	this.getFlvVideo = function(postValue)
	{		
		var videos = self.getFlvVideos(postValue);
		
		if (videos.length == 0)
		{
			return null;
		}
	
		var video = null;
		for (var x = 0; x < videos.length; x++)
		{
			if (videos[x].isDefault && videos[x].isDefault == true)
			{
				return videos[x];
			}
		}
		return video || videos[0];
	}
	
	this.getFlvVideos = function()
	{
		function isFlvVideo(url, mimeType, medium){
		return(
		//#pragma NoCompStart  -- Tell the runtime compressor to not screw up our regexes
					(mimeType && /(video\/x-shockwave-flash)|(video\/flv)|(video\/x-flv)/i.test(mimeType)) ||
					(url && /(flv)|(swf)/i.test(url))
		//#pragma NoCompEnd
				);
			};
		
		var videos = [];
	
		//note we could maybe do something with MediaRSS.Player stuff as well, but I'm not sure what.
		if(post.MediaRSS && post.MediaRSS.Contents){
			for(var i = 0; i < post.MediaRSS.Contents.length; i++){
				var c = post.MediaRSS.Contents[i];
				if(isFlvVideo(c.Url, c.Type, c.Medium)){
					videos.push({url:c.Url, mimeType:c.Type});
				}
			}
		}
			
		if(post.EnclosureUrl && isFlvVideo(post.EnclosureUrl, post.EnclosureType)){
			videos.push({url:post.EnclosureUrl, mimeType:post.EnclosureType});
		}
		
		return videos;
	}
	
	this.getPlayerHandle = function(optionalId)
	{
		var id = optionalId || self.config.id;
		
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			return window[id];
		}
		else
		{
			return document[id];
		}
	}
	
	this.play = function()
	{
		executePlayerFunction("playVideo");
	}
	
	this.pause = function()
	{
		executePlayerFunction("pauseVideo");
	}
	
	this.stop = function()
	{
		executePlayerFunction("stopVideo");
	}
	
	var executePlayerFunction = function(funcName)
	{
		if (self.getPlayerHandle()[funcName])
		{
			self.getPlayerHandle()[funcName]();
		}
		else
		{
			debug("unable to find media player within the page.  element name/id: " + self.config.id);
		}
	}
	
	var debug = function(){
		if(window.ng_debug){
			return ng_debug;
		} else if(typeof console != "undefined"){
			return console.debug || console.log || console.warn || function(){};
		}
		return function(){};
	}();
	
	
	//set this value at the end
	if (post != null)
	{
		self.setPost(post);
	}
}

var BetaNGFlashVideoConfig = function(videoConfigObj)
{
	
	var config = videoConfigObj || {};
	
	var getTrueFalseValue = function(obj, defaultValue)
	{
		var s = new String(obj);
		if (s == "undefined")
		{
			return defaultValue;
		}
		else{
			return s;
		}
		
	}
	
	this.mimeType 			= config.mimeType || "video/flv";
	this.url 				= config.url || "";
	
	this.id					= config.id || "ngvideo" + Math.random().toString().substring(2);
	
	this.height				= config.height || 300;
	this.width				= config.width || 400;
	this.wmode				= config.wmode || "transparent";

	this.autostart			= getTrueFalseValue(config.autostart, "false");
	this.enableNGThumbnail	= getTrueFalseValue(config.enableNGThumbnail, "true");
	this.disableControls	= getTrueFalseValue(config.disableControls, "false");
	
	this.thumbnail			= config.thumbnail || "";
	
	this.acudeoCampaignId 	= config.acudeoCampaignId || ""; // only use this if you need a dynamically loaded progId!
	this.acudeoProgId		= config.acudeoProgId || "";
	this.acudeoCustId		= config.acudeoCustId || "";
	this.acudeoAdTarget		= config.acudeoAdTarget || "";
	this.acudeoNoAdFound	= config.acudeoNoAdFound || "";
	this.acudeoDomainNotAllowed = config.acudeoDomainNotAllowed || "";
	
	this.loadCallback		= config.loadCallback || "";
	
	this.additionalFlashVars = config.additionalFlashVars || "";
	
	//method for easy check to see if it's a config object
	this.isConfig = function() { return true; }
}
