
 
// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling){	
    bubbling = bubbling || false;
    if(window.addEventListener)	{ // Standard
        element.addEventListener(type, expression, bubbling);
		return true;
   } else if(window.attachEvent) { // IE
        element.attachEvent('on' + type, expression);
        return true;
    } else 
    return false;
}

ctv.ca.VideoWindow = function (videoWindowDivId, w, h) {
    this.cutrrentMediaType = '';
    this.videoWindowDivId = videoWindowDivId;
    this.videoWindowObj = null;
    this.useInternalControl = false;
    this.adPlaying = false;
    this.width = w;
    this.height = h;
    this.queuedUrl = null;
    
    this.slCachedWindow = null;
    this.wmvCachedWindow = null;
    this.flvCachedWindow = null;
	
	this.isLive = false;
    
    //events
    ctv.ca.VideoWindow.createEvent("playstateChange");
    ctv.ca.VideoWindow.createEvent("fullScreen");
    ctv.ca.VideoWindow.createEvent("mouseDown");
    ctv.ca.VideoWindow.createEvent("positionChange");
    ctv.ca.VideoWindow.createEvent("mediaLength");
    ctv.ca.VideoWindow.createEvent("adPlaying");
    ctv.ca.VideoWindow.createEvent("mediaError");
    ctv.ca.VideoWindow.createEvent("ready");
    ctv.ca.VideoWindow.createEvent("newMedia");
}

ctv.ca.VideoWindow.prototype = {
    createVideoWindow : function (mediaType) {
        //mediaType:  wmv-silverlight
        //drmedwmv-wm
        //flv-flash
        
        //cannot in fullscreen mode to switch
        this.fullScreen(false);
        this.cleanUp();
        var thisObj = this;
        switch (mediaType) {
            case 'wmv':
                setTimeout(function() {thisObj.createSilverlightWindow()}, 100);
                break;
            case 'drmedwmv':
                setTimeout(function() {thisObj.createWMWindow()}, 100);
                break;
            case 'flv':
                setTimeout(function() {thisObj.createFlashWindow()}, 100);
                break;
        }
    },
    
    cleanUp : function() {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.cleanUp) != "undefined" )
                this.videoWindowObj.cleanUp();
            
        this.stop();
        this.show(this,false);
        
        var parking = $get("parkingDiv");
        var holder = $get(this.videoWindowDivId);    
        //if (this.wmvCachedWindow !=null)
            //dom.moveChildren (holder, parking);     
        if (holder != null) {
            holder.innerHTML = '';
        }

    },
    
    createSilverlightWindow : function() {
        var holder = $get(this.videoWindowDivId);
        if (holder != null) {
            this.cutrrentMediaType = 'wmv';
            this.videoWindowObj = new ctv.ca.SlVideoWindow(this, holder);
            this.slCachedWindow = this.videoWindowObj;
        }
    },
    
    createWMWindow : function() {
        /*
        if (this.wmvCachedWindow != null) {
            var parking = $get("parkingDiv");
            var holder = $get(this.videoWindowDivId);    
            dom.moveChildren (parking, holder);     

            this.cutrrentMediaType = 'drmedwmv';
            this.videoWindowObj = this.wmvCachedWindow;
            this.onReady();
            
            return;
        }
*/
        
        var uiMode = "none";
        if (this.useInternalControl)
            uiMode = "full";
                           

        var embedPlayer = '<object name="Player" id="Player" width="' + this.width + '" height="' + this.height + '" type="application/x-oleobject" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" >';
        embedPlayer += '<param name="uiMode" value="' + uiMode + '" />';
        embedPlayer += '<param name="stretchToFit" value="true" />';
        embedPlayer += '<param name="enableContextMenu" value="false" />';
        embedPlayer += '	<embed TYPE="application/x-mplayer2"';
        embedPlayer += '		pluginspage="http://www.microsoft.com/Windows/MediaPlayer/en/download/" ';
        embedPlayer += '		src=""';
        embedPlayer += '		fileName=""';
        embedPlayer += '		AutoStart="1" ';
        embedPlayer += '		Name="Player"';
        embedPlayer += '		ID="Player"';
        embedPlayer += '		AnimationAtStart="0"';
        embedPlayer += '		Width="' + this.width + '" Height="' + this.height + '"';
        embedPlayer += '		transparentAtStart="1"';
        embedPlayer += '		ShowStatusBar="1"';
        if (this.useInternalControl)
            embedPlayer += '		ShowControls="1" ';
        else
            embedPlayer += '		ShowControls="0" ';
        embedPlayer += '		autoSize="1" ';
        embedPlayer += '		displaySize="1">';
        embedPlayer += '	</embed>';
        embedPlayer += '</object>';
        
        var str = ""; 
  

        /*
        var embedPlayer = "";
        if (render.html.ie) { 
            embedPlayer =  '<object name="Player" id="Player" width="' + this.width + '" height="' + this.height + '" type="application/x-oleobject" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" >';
        } else { 
            embedPlayer = '<object name="Player" id="Player" width="' + this.width + '" height="' + this.height + '" type="application/x-mplayer2" >'; 
        }

        embedPlayer += '<param name="uiMode" value="' + uiMode + '" />';
        embedPlayer += '<param name="stretchToFit" value="true" />';
        embedPlayer += '<param name="enableContextMenu" value="false" />';
        embedPlayer += '</object>';
        */
                
        if ($.browser.msie) {
            embedPlayer += '\n<script type="text/javascript" event="PlayStateChange(s)" for="Player">ctvVideoWindow.onPlaystateChange(s);</script>';
            //embedPlayer += '\n<script type="text/javascript" event="Buffering(s)" for="Player">ctvVideoWindow.onBuffering(s);</script>';
            //embedPlayer += '\n<script type="text/javascript" event="currentItemChange()" for="Player">ctvVideoWindow.onCurrentItem();</script>';
            //embedPlayer += '\n<script type="text/javascript" event="PositionChange(o,n)" for="Player">ctvVideoWindow.onPosition(o,n);</script>';
            embedPlayer += '\n<script type="text/javascript" event="MouseDown(nButton, nShiftState, fX, fY)" for="Player">ctvVideoWindow.onMouseDown(nButton,nShiftState,fX,fY);</script>';
        	embedPlayer += '\n<script type="text/javascript" event="MediaError()" for="Player">ctvVideoWindow.onMediaError();</script>';

        } 

        var holder = $get(this.videoWindowDivId);
        if (holder != null) {
            holder.innerHTML = embedPlayer;
            
            if (!$.browser.msie) {
             var scr = document.createElement('script');
             scr.setAttribute("type", "text/javascript");
             scr.setAttribute("event", "PlayStateChange(s)");
             scr.setAttribute("for", "Player");
             scr.innerHTML = "ctvVideoWindow.onPlaystateChange(s);";          
             holder.appendChild(scr);
             
             scr = document.createElement('script');
             scr.setAttribute("type", "text/javascript");
             scr.setAttribute("event", "MouseDown(nButton, nShiftState, fX, fY)");
             scr.setAttribute("for", "Player");
             scr.innerHTML = "ctvVideoWindow.onMouseDown(nButton,nShiftState,fX,fY);";          
             holder.appendChild(scr);
             
             scr = document.createElement('script');
             scr.setAttribute("type", "text/javascript");
             scr.setAttribute("event", "MediaError()");
             scr.setAttribute("for", "Player");
             scr.innerHTML = "ctvVideoWindow.onMediaError();";          
             holder.appendChild(scr);
             
             
            }
            
            /*
            var obj = document.createElement('object');
            obj.setAttribute("name", "Player");
            obj.setAttribute("id", "Player");
            obj.setAttribute("width", this.width);
            obj.setAttribute("height", this.height);
            if (render.html.ie) { 
                obj.setAttribute("type", "application/x-oleobject");
                obj.setAttribute("classid", "CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6");
            }
            else
                obj.setAttribute("type", "application/x-ms-wmp");
            //obj.innrtHTML = embedPlayer;
            
            var paramNode = document.createElement('param');
            paramNode.setAttribute("name", "uiMode");
            paramNode.setAttribute("value", uiMode);
            obj.appendChild(paramNode);
            
            paramNode = document.createElement('param');
            paramNode.setAttribute("name", "stretchToFit");
            paramNode.setAttribute("value", "true");
            obj.appendChild(paramNode);
            
            paramNode = document.createElement('param');
            paramNode.setAttribute("name", "enableContextMenu");
            paramNode.setAttribute("value", "false");
            obj.appendChild(paramNode);
                       
            holder.appendChild(obj);
            */
           
            
            this.cutrrentMediaType = 'drmedwmv';
            this.videoWindowObj = new ctv.ca.WMVVideoWindow(this, $get("Player"));
            this.wmvCachedWindow = this.videoWindowObj;
            
        
        var thisObj = this;
        setTimeout(function() {thisObj.onReady();}, 500);
        }
    },
    
    createFlashWindow : function() {
        var so = new SWFObject('swf/mediaplayer.swf','flvPlayer','416','312','9');
        so.addVariable("javascriptid","flvPlayer");
        so.addParam("allowfullscreen","true");
    	so.addVariable("autostart", "true");
        so.addVariable("enablejs","true");
        so.addVariable("displayheight","312");
        so.write(this.videoWindowDivId);
	//fix ie bug: external interface fails with flash obj in a form
	window.flvPlayer = $get("flvPlayer");
	this.cutrrentMediaType = 'flv';
        this.videoWindowObj = new ctv.ca.FlvVideoWindow(this, $get("flvPlayer"));
        this.flvCachedWindow = this.videoWindowObj;
    },
    
    PlayVideo : function (mediaUrl, mediaType) {
        if (mediaType == 'wmv' && ctvConfigController.forceWM)
            mediaType = 'drmedwmv';  //play with WM
            
        //use sl for none ie browser
        if (!$.browser.msie)
        {
            mediaType = 'wmv';
        }
               
		var version = "1.0"; //deliberate over-version
		var isInstalled = Silverlight.isInstalled(version);
    
	    // use sl if installed.
	    if (isInstalled)
	    {
	        mediaType = 'wmv';
	    }
           
        if (mediaType != this.cutrrentMediaType)
        {
            this.queuedUrl = mediaUrl;
            this.createVideoWindow(mediaType);
        }
        else
        {
            if (this.videoWindowObj != null)
                if (typeof(this.videoWindowObj.playUrl) != "undefined" )
                {
                    this.videoWindowObj.playUrl(mediaUrl.url);
                    this._raiseEvent("newMedia",mediaUrl);    
                }
        }
    },
        
    play : function () {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.play) != "undefined" )
                this.videoWindowObj.play();
    },
    
    stop : function () {
        if (this.adPlaying)
            return;

        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.stop) != "undefined" )
                this.videoWindowObj.stop();    
    },
    
    pause : function () {
        if (this.adPlaying)
            return;

        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.pause) != "undefined" )
                this.videoWindowObj.pause();
    },
    
    mute : function (sender, goMute) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.mute) != "undefined" )
                this.videoWindowObj.mute(goMute);
    },
    
    setVolume : function (sender, value) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.setVolume) != "undefined" )
                this.videoWindowObj.setVolume(value);
    },
    
    gotoPosition : function (sender, pos) {
        if (this.adPlaying)
            return;
            
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.gotoPosition) != "undefined" )
                this.videoWindowObj.gotoPosition(pos);
    },
    
    getMediaLength: function () {
        this.isLive = false;
		if (this.videoWindowObj != null)
        {
            if (typeof(this.videoWindowObj.getMediaLength) != "undefined" )
			{
                var length = this.videoWindowObj.getMediaLength();
				if (length == 0)
					this.isLive = true;
				return length;
			}
            else 
                return 0;
        }
        else
            return 0;
    },
    
    getPosition: function () {
        if (this.videoWindowObj != null)
            //return this.videoWindowObj.getPosition();
            return;
        else
            return 0;
    },
    
    fullScreen : function (sender, gotoFullScreen) {
        if (this.videoWindowObj != null)
        {
            if (typeof(this.videoWindowObj.fullScreen) != "undefined" )
                this.videoWindowObj.fullScreen(gotoFullScreen);
            this.onFullScreen(gotoFullScreen);
        }
    },
    
    show : function (sender, visible) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.show) != "undefined" )
                this.videoWindowObj.show(visible);
    },
    
    resize : function(sender, w, h) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.resize) != "undefined" )
                this.videoWindowObj.resize(w, h);
    },
    
    moveTo : function(sender, x, y) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.moveTo) != "undefined" )
                this.videoWindowObj.moveTo(x, y);
    },
    
    setCaption : function (sender, text, visible) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.setCaption) != "undefined" )
                this.videoWindowObj.setCaption(text, visible);
    },
    
    setOverlay : function (sender, imageUrl, visible) {
        if (this.videoWindowObj != null)
            if (typeof(this.videoWindowObj.setOverlay) != "undefined" )
                this.videoWindowObj.setOverlay(imageUrl, visible);
    },
    
    showOverlay : function (show) {
        return;
        if (this.videoWindowObj != null) {
            if (typeof(this.videoWindowObj.showOverlay) != "undefined" ) {
                this.videoWindowObj.showOverlay(show);
            }
        }
    },
         
    //events
    onPlaystateChange : function(s) {
        /*
                        0 Undefined Windows Media Player is in an undefined state. 
                        1 Stopped Playback of the current media item is stopped. 
                        2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location. 
                        3 Playing The current media item is playing. 
                        4 ScanForward The current media item is fast forwarding. 
                        5 ScanReverse The current media item is fast rewinding. 
                        6 Buffering The current media item is getting additional data from the server. 
                        7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin. 
                        8 MediaEnded Media item has completed playback.  
                        9 Transitioning Preparing new media item. 
                        10 Ready Ready to begin playing. 
                        11 Reconnecting 
                    */

        //debug(s);
        if (s == 3) {
           this.show(this,true);
           if (this.cutrrentMediaType == 'drmedwmv')
               this.onMediaLength(this.getMediaLength());
               
            if (this.cutrrentMediaType == 'wmv')
            {
                //showbug
                //this.setOverlay(this, appPath + "/images/bugs/8.png", true);
            }
            
            if (this.adPlaying)
                this.showOverlay(false);
            else
                this.showOverlay(true);
        }
        else if (s == 0 || s == 1 || s == 6 || s == 7 || s == 8 || s == 9 || s == 10 || s == 11)
        {
            this.show(this,false);
        }
                    
        this._raiseEvent("playstateChange",s);
    },
    onBuffering : function (s) {},
    onCurrentItem : function () {},
    onPosition : function (p) {this._raiseEvent("positionChange",p);},
    onOpenstate : function (s) {},
    onMediaError : function () {this._raiseEvent("mediaError");},
    onMouseDown : function (nButton,nShiftState,fX,fY){this.fullScreen(); this._raiseEvent("mouseDown",{button:nButton,state:nShiftState,x:fX,y:fY});},
    onFullScreen : function (v){this._raiseEvent("fullScreen",v);},
    onMediaLength : function (v){this._raiseEvent("mediaLength",v);},
    onAdPlaying : function (v){
        this.adPlaying = v;
        this._raiseEvent("adPlaying",v);
    },
    onReady :function()
    {
        //debug('subWindow Ready');
        this._raiseEvent("ready");
        
        if (this.queuedUrl != null)
        {
            if (this.videoWindowObj != null)
                if (typeof(this.videoWindowObj.playUrl) != "undefined" )
                {
                    this.videoWindowObj.playUrl(this.queuedUrl.url);
                    this._raiseEvent("newMedia",this.queuedUrl);
                    this.queuedUrl = null;
                }
        }
    }


}

ctv.ca.VideoWindow.registerClass('ctv.ca.VideoWindow', Sys.Component);

var ctvVideoWindow = new ctv.ca.VideoWindow("playerDiv", 416, 312);



