YUI.add( "cbc.video.embed.swfvideoplayer", function (Y) {
	
	var o = Y.namespace( "cbc.video.embed" );
	
	o.SWFVideoPlayer = function ( uservalues )
	{
		this.Y.cbc.video.embed.SWFVideoPlayer.superclass.constructor.apply(this, this.Y.Array(arguments) );
		
	};
	
	o.SWFVideoPlayer.NAME = "swfvideoplayer";
	
	o.SWFVideoPlayer.STATES = {
			MEDIA_PLAYING:0,
			MEDIA_PAUSED:1,
			AD_PLAYING:2
	};
	
	o.SWFVideoPlayer.ATTRS = {
		id: null,
		autoPlay: { value: false },
		clipId: null,
		categoryTitle: null,
		categoryId: null,
		bgcolor: { value: "#000000" },
		site: null,
		adCategory: null,
		image: null,
		backgroundAlpha: { value: 0.2 },
		player_url: null,
		width: { value:360 },
		height: { value:255 },
		state: {
			value: "embed",
			setter: function ( val ) {
				switch( val.toLowerCase() ) {
				
					case "audio" :
					return "audio";
				}
				return "embed";
			}
		}
	};
	
	var proto = {
			
			Y: Y,
			
			_static: o.SWFVideoPlayer,
			
			_ready: false,
			
			_onOpen: function () {
				var oElement = this.Y.Node.one( this.get("id") );
				if( oElement ) {
					oElement.setStyle( "visibility", "hidden" );
					this.pause();
				}
			},
	
			_onClose: function () {
				var oElement = this.Y.Node.one( this.get("id") );
				if( oElement ) {
					oElement.setStyle( "visibility", "visible" );
					this.pause();
				}
			},
	
			initializer: function ()
			{
				var p_oAttributes = { version: "9.0.0",
									
										fixedAttributes: {	allowFullScreen: true,
															scale:"noscale",
															salign:"tl" },
														 
										flashVars: {PID: "_DyE_l_gC9yXF9BvDQ4XNfcCVLS4PQij",
													RSI: "cbc-production",
													URI: "http://feed.theplatform.com/f/h9dtGB/r3VD0FujBumK"
													},
										
										useExpressInstall: true
									};
				
				var id = this.get( "id" );
				if( !id ) {
					id = this.Y.guid( "vp_" );
					this.set( "id", id );
				}
				
				
				this.Y.mix( p_oAttributes.fixedAttributes, this.getAttrs( ["bgcolor"] ) );
				this.Y.mix( p_oAttributes.flashVars, this._getFlashVars() );
				
				var url = this.get( "player_url" );
				if( !url ){
					url = "/video/swf/UberPlayer.swf";
				}
				
				this._swf = new this.Y.SWF( id, url, p_oAttributes );
				this._swf.once( "wrongflashversion", function () { 
					var n = this.Y.Node.one( this.get( "id" ) );
					n.set( "innerHTML", "To view this content, you need the latest version of <a href='http://get.adobe.com/flashplayer/'>Adobe Flash Player</a>" )
				}, this );
				this._setSize();
				this._addListeners();
			},
			
			_setSize: function () 
			{
				var n = this.Y.Node.one( this.get( "id" ) );
				
				if( n ) 
				{
					//check if container div has set width and height
					if( !parseInt( n.getStyle("width"), 10 ) || !parseInt( n.getStyle("height"), 10 ) ) 
					{
						n.setStyle( "width", this.get( "width" ) + "px" );
						n.setStyle( "height", this.get( "height" ) + "px" );
					}
					//use instance width and height if set
					if( this.Y.Lang.isNumber(this.get("width")) && this.Y.Lang.isNumber(this.get("height")) )
					{
						n.setStyle( "width", this.get( "width" ) + "px" );
						n.setStyle( "height", this.get( "height" ) + "px" );
						
						this._swf.once( "swfReady", function () { 
							this._getSWFNode().setStyle( "display", "block" );
						}, this );
					}
				}
			},
			
			_addListeners: function () 
			{
				var o = this, instanceRef;
				this._swf.once( "swfReady", function () { 
					o._ready = true;
					o.fire( "swfReady" );
				} );
				this._swf.on( "stateChange", function () { 
					o.fire( "onStateChange" );
				} );
				
				this.Y.Global.on('cbc_modalboxopen', this._onOpen, this );
				this.Y.Global.on('cbc_modalboxclose', this._onClose, this );
				//"{module}:{instanceRef}:{action}"
				instanceRef = 'cbc.video.embed.swfvideoplayer:' + this.get( "id" )  + ":";
				this.Y.Global.on( instanceRef + "setClipId", this.setClipId, this );
			},
			
			_removeListeners: function () {
				var instanceRef;
				this._swf.detachAll();
				this.detachAll();
				this.Y.Global.detach('cbc_modalboxopen', this._onOpen );
				this.Y.Global.detach('cbc_modalboxclose', this._onClose );
				instanceRef = 'cbc.video.embed.swfvideoplayer:' + this.get( "id" )  + ":";
				this.Y.Global.detach( instanceRef + "setClipId" , this.setClipId );
			},
			
			isReady: function () {
				return this._ready;
			},
			
			_getFlashVars : function () {
				
				var o = {}, v, i, fv = [ "state", "autoPlay", "clipId", "categoryTitle", "categoryId", "site", "image", "adCategory" ];
				i = fv.length;
				while( i-- ) {
					v = this.get( fv[i] );
					if( v ) {
						o[ fv[i] ] = v + "";
					}
				}
				return o;
			},
			
			destructor: function ()
			{
				this.once( "onRemove", function () { 
					this._removeListeners();
					this._swf._swf._node = null;
					this._swf._swf = null;
					this._swf = null;
				}, this );
				this._removeSWFNode();
			},
			
			pause: function () {
				this._swf.callSWF( "pause" );
			},
			
			resume: function () {
				this._swf.callSWF( "resume" );
			},
			
			setClipId: function ( id ) {
				this._swf.callSWF( "setClipId", [id] );
			},
			
			getState: function () { 
				var state = this._swf.callSWF("getState");
				if( !isNaN( state ) ) {
					return state;
				}
				return -1;
			},
			
			_getSWFNode: function () 
			{
				if( this._swf ) 
				{
					return this.Y.Node.one( "#" + this._swf._id );
				}
				return null;
			},
			
			_removeSWFNode: function () 
			{
				var n = this._getSWFNode();
				
				if( n ) 
				{
					//copy of the swfobject remove functionality
					if( n.get("nodeName") == "OBJECT" )
					{
						if( this.Y.UA.ie && this.Y.UA.os == 'windows' ) 
						{
							n.setStyle( "display", "none" );
							
							var swf_vp = this;

							(function(){
								if (n._node.readyState == 4 ) {
									setTimeout( function () { swf_vp._removeObjectInIE(); }, 100 );
								}
								else {
									setTimeout(arguments.callee, 100 );
								}
							})();
						}
						else
						{
							n.remove(true);
							this.fire( "onRemove" );
						}
					}
				}
			},
			
			_removeObjectInIE: function () {
				
				var n =  this._getSWFNode();
				
				if (n) {
					for (var i in n._node) {
						if (typeof n._node[i] == "function") {
							n._node[i] = null;
						}
					}
					
					n.remove(true);
					
					this.fire( "onRemove" );
				}
			}
	};
			
	
	Y.extend( o.SWFVideoPlayer, Y.Base, proto );
	
	/*
	 * StaticSWFPlayer 
	 * 
	 * YUI 3 relies on a dom element containing the object tag. 
	 * This has caused problems with inline embedded players, 
	 * which has resulted in creating StaticSWFPlayer which replaces
	 * the DOM element with the nested object node.
	 * 
	 */
	
	o.StaticSWFPlayer = function () 
	{
		this.Y.cbc.video.embed.StaticSWFPlayer.superclass.constructor.apply( this, this.Y.Array(arguments) );
	};
	
	proto = 
	{
		_setSize: function ()
		{
			var n, o;
			
			//use instance width and height if set
			if( this.Y.Lang.isNumber(this.get("width")) && this.Y.Lang.isNumber(this.get("height")) )
			{
				n = this.Y.Node.one( this.get( "id" ) );
				on = this._getSWFNode();
				if( n && on ) 
				{ 
					on.setStyle( "width", this.get("width") );
					on.setStyle( "height", this.get("height") );
					n.replace( on );
					
					//override 
					this._getContainerNode = function ()
					{
						return this._getSWFNode();
					};
				}
			}
		}
	};
	
	Y.extend( o.StaticSWFPlayer, o.SWFVideoPlayer, proto );
	
}, '0.0.1', { requires:[ "swf", "node", "base" ] } );
