

ctv.ca.PlayerControl = function (controlWindowDivId) {
    
    //vars	
	this.holderDiv = controlWindowDivId;
    this.isMuted = false;
    this.showPlayer = true;
    this.currentPosition = 0;
    this.mediaLength = 100;
    this.isTracker = false;
    this.mouseDown = false;
    this.clockLength = "00:00";
    this.isPaused = false;
    this.playState = undefined;
    this.sliderVolPos = 0;
    
    this.controlType = "swf";
    
    this.controlObj = null;
    this.uiReady = false;
    
	//create the event Listeners
	
	ctv.ca.PlayerControl.createEvent("play");
    ctv.ca.PlayerControl.createEvent("pause");
    ctv.ca.PlayerControl.createEvent("stop");
    ctv.ca.PlayerControl.createEvent("next");
    ctv.ca.PlayerControl.createEvent("prev");
    ctv.ca.PlayerControl.createEvent("setVolume");
    ctv.ca.PlayerControl.createEvent("mute");
    ctv.ca.PlayerControl.createEvent("getPosition");
    ctv.ca.PlayerControl.createEvent("gotoPosition");
    ctv.ca.PlayerControl.createEvent("getMediaLength");    
    ctv.ca.PlayerControl.createEvent("fullScreen");
    ctv.ca.PlayerControl.createEvent("show");
      
}

ctv.ca.PlayerControl.prototype = {
    createUI : function() {
        if (typeof (ctvConfigController.control) == 'undefined' || ctvConfigController.control == 'swf')
        {
            this.createSWFControl();
        }
        else if (this.configController.control == 'html')
        {
            this.createHTMLControl();
        }
    },
    
    createSWFControl : function()
    {
    	try
        {
            this.controlType = "swf";
            var so = new SWFObject(appPath + '/swf/videoControlSkinned.swf','swfControl','416','70','9');
            //var so = new SWFObject('swf/ContentStack.swf','swfStack','390','340','9');
            so.addVariable("width","416");
            so.addVariable("height","70");
            so.addParam("wmode", "transparent");
            so.write($get(this.holderDiv));        
            //fix ie bug: external interface fails with flash obj in a form
            window.swfControl = $get('swfControl');
            
            this.controlObj = new ctv.ca.PlayerControlSwf($get('swfControl'));
        }
        catch (e)
        {
            //debug(e);
        }
    },
    
    createHTMLUI : function()
    {
        this.controlType = "html";
    },
    
    play : function () {
    	this._raiseEvent("play");   	       
    },
    
    pause : function () {
    	this._raiseEvent("pause");   	       
    },
    
    stop : function () {        
    	this._raiseEvent("stop");
    },
    
    next : function () {
    	this._raiseEvent("next");
    },
    
    prev : function () {
    	this._raiseEvent("prev");
    },
    
    gotoPosition : function (moveAmt, doMove) {   
        if(doMove)
            this._raiseEvent("gotoPosition", moveAmt);
    },
    
    showMediaLength : function(sender, length) {
        if (!this.uiReady)
            return;
        
		try
		{
			if (this.controlObj != null)
                if (typeof(this.controlObj.onSetLive) != "undefined" )
                    this.controlObj.onSetLive(ctvVideoWindow.isLive); 
		}
		catch (e)
		{
		}
		
        if (this.mediaLength != length)
        {
            this.mediaLength = length; 

            if (this.controlObj != null)
                if (typeof(this.controlObj.onSetMediaLength) != "undefined" )
                    this.controlObj.onSetMediaLength(length); 
        }
    },
 
    changeMediaLength : function () {

    },
    
    createTrackers :function () {
        
    },
    
    moveSlider : function (sender, pos){
        if (this.controlObj != null)
            if (typeof(this.controlObj.onSetPos) != "undefined" )
                this.controlObj.onSetPos(pos); 
    },  
        
        
    setVolume : function (vol){        	            	
            this._raiseEvent("setVolume", vol);                        
    },
        
        
    mute : function () {
    
        muteButton = $get("muteButton");
    
        if (this.isMuted == false)
        {        	
            this.MuteIt();
    	}
    	else
    	{
            this.unMuteIt();
    	}
    	    
    },
    
    MuteIt : function() {
        
        
    	//set the slider to zero and record the value for 'off' mute event
    	//set to false to it doesn't trigger a volume event when moved
    	
    	//this.sliderVolPos = window.A_SLIDERS[1].n_value;
    	
    	//debug(this.sliderVolPos);         
    	
        //raise Mute event, record state and change image
        this._raiseEvent("mute", true);
    	this.isMuted = true;    	    
   	
    },
    
    unMuteIt : function() {
    
        //window.A_SLIDERS[1].f_setValue(this.sliderVolPos, "", false);         
        
	    this._raiseEvent("mute", false);
        this.isMuted = false;
                
    },
    
    
    getPosition : function () {
        this.currentPosition = this._raiseEvent("getPosition");
    },     
    
            
    fullScreen : function () {
    	this._raiseEvent("fullScreen");
    },
    
    
    show : function () {
    
        if (showPlayer == false)
        {
            this._raiseEvent("show", true);
    	    this.showPlayer = true;
    	    
    	}
    	else
    	{
    	    this._raiseEvent("show", false);
    	    this.showPlayer = false;
    	}
    	
    },
  	 
	 
	 onPlaystateChange : function(sender, playState){	
	    	    
	    //	0: The playback state is undefined. 					
	    //	1: Playback is stopped. 
	    //	2: Playback is paused. 
	    //	3: The player is playing a stream. 
	    //	4: The player is scanning a stream forward. 
	    //	5: The player is scanning a stream in reverse. 
	    //	6: The player is buffering media. 
	    //	7: The player is waiting for streaming data. 
	    //	8: The player has reached the end of the media. 
	    //	9: The player is preparing new media. 
	    // 10:  The player is ready to begin playback. 
	
		/*
        var playState_btn=new Array()	
		var playerMsg = $get("playerMsg");	
		var playerDiv = $get("playerDiv");
		
		this.playState = playState;
				
		playState_btn[0] = 'Undefined';	
		playState_btn[1] = 'Stopped';	
		playState_btn[2] = 'Paused';
		playState_btn[3] = 'Playing';
		playState_btn[4] = 'FF';
		playState_btn[5] = 'Rev';
		playState_btn[6] = 'Buffering...';
		playState_btn[7] = 'Waiting';
		playState_btn[8] = 'Stopped';
		playState_btn[9] = 'Preparing';
		playState_btn[10] = 'Ready';
		
		if (playState == 6 || playState == 7 || playState == 9 || playState == 10)
		{		    
		    playerMsg.innerHTML = playState_btn[6];				    
		}
		else
		{
		    playerMsg.innerHTML = "";		    		    		    
		}
		*/	
        if (!this.uiReady)
            return;
        
        //debug("control: " + playState);
        
        if (playState == 1)
        {
            if (this.controlObj != null)
                if (typeof(this.controlObj.onVideoStopped) != "undefined" )
                    this.controlObj.onVideoStopped();
        }        
        else if (playState == 2)
        {
            if (this.controlObj != null)
                if (typeof(this.controlObj.onPause) != "undefined" )
                    this.controlObj.onPause();
        }
        else if (playState == 3)
        {
            if (this.controlObj != null)
                if (typeof(this.controlObj.onPlaying) != "undefined" )
                    this.controlObj.onPlaying();
        }
        else if (playState == 8)
        {
            if (this.controlObj != null)
                if (typeof(this.controlObj.onStop) != "undefined" )
                    this.controlObj.onStop();
        }
        else if (playState == 6 || playState == 7 || playState == 9 || playState == 10)
        {        	
            if (this.controlObj != null)
                if (typeof(this.controlObj.onBuffering) != "undefined" )
                    this.controlObj.onBuffering();
        }
	},
	
	togglePause : function (){

	},
	
	initialize :function (){
		
    },
	
	onSetCurrentTitle: function(title) {
        if (this.controlObj != null)
            if (typeof(this.controlObj.onSetCurrentTitle) != "undefined" )
                this.controlObj.onSetCurrentTitle(title);
    },
    
    onVideoControlUIReady: function() {
        this.uiReady = true;
        //debug("Control uiReady");
    },
    
    onAdPlaying: function(sender, value) {
        if (this.controlObj != null)
            if (typeof(this.controlObj.onAdPlaying) != "undefined" )
                this.controlObj.onAdPlaying(value);
    },
    
    onVideoStopped: function(sender) {
        if (this.controlObj != null)
            if (typeof(this.controlObj.onVideoStopped) != "undefined" )
                this.controlObj.onVideoStopped();
    }

    

}

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

var ctvPlayerControl = new ctv.ca.PlayerControl("controlsHolder");


    	

 
