/**
 * Real Player Module
 * @see com.metafusion.VideoPlayer
 * @author m.donay@meta-fusion.com
 */
/**
 * 0 	Stopped
 * 1 	Contacting
 * 2 	Buffering
 * 3 	Playing
 * 4 	Seeking
 * 5 	Paused
 * @param {Object} oldstate
 * @param {Object} newstate
 */
ff_realplayer_OnPlayStateChange = function(oldstate, newstate){
}

hacked_OnTitleChange = function(scType, Param){
}

com.metafusion.RealPlayer = function(){

    //-----// public members //-----//
    
    com.metafusion.RealPlayer.prototype.classname = "com.metafusion.RealPlayer";
	com.metafusion.PlayerApplication.titleUpdateTimer = null;
    
    
    //-----// private members //-----//
    
    /**
     * _name: DOM id of the player object
     */
	var _nameOriginal = "realplayer";
    var _name = _nameOriginal;
    
    /**
     * _ffname: a tribute to difficult embedding issues
     */
    var _ffnameOriginal = "ff_realplayer";
    var _ffname = _ffnameOriginal;
    
    /**
     * _player: points to the player object
     * call registerPlayer() to initialize.
     * @see registerPlayer()
     */
    var _player = null;
    
    
    //-----// public functions //-----//
    
	
	/**
     * switch visibility of the player object
     * @param {Object} visible
     */
	com.metafusion.RealPlayer.prototype.setVisible = function(visible){
		if ( (typeof visible) == (typeof true))
		{
			_player.style.visibility = visible? 'visible' : 'hidden';
		}
	}
	
    /**
     * assigns parameters to the player object.
     * called by com.metafusion.PlayerApplication.showPlayer(target)
     * @param {String} key
     * @param {Object} value
     */
    com.metafusion.RealPlayer.prototype.addVariable = function(key, value){
        switch (key) {
            case "src":{
                src = value;
            }
            case "width":{
                width = value;
            }
            case "height":{
                height = value;
            }
            case "showPlayerControls":{
                showPlayerControls = value;
            }
			case "scriptEventListener":{
                scriptEventListener = value;
                break;
            }
            case "stateChangeEventListener":{
                stateChangeEventListener = value;
            }
        }
    }
    
    /**
     * gets the current playhead position
     * @return time in milliseconds
     */
    com.metafusion.RealPlayer.prototype.getPosition = function(){
        if (!_player) {
            error(this.classname + ': Player object not registered');
            return;
        }
        return _player.GetPosition();
    }
    
    /**
     * gets the total length of the clip currently loaded
     * @return time in milliseconds
     */
    com.metafusion.RealPlayer.prototype.getLength = function(){
        if (!_player) {
            error(this.classname + ': Player object not registered');
            return;
        }
        return _player.GetLength();
    }
    
    /**
     * TODO: implement this
     * sets the playhead position
     * @param {Number} position: new position in milliseconds
     */
    com.metafusion.RealPlayer.prototype.setPosition = function(position){
        if (!_player) {
            error(this.classname + ': Player object not registered');
        }
        _player.SetPosition(position);
        //error(this.classname + ".setPosition is not implemented!");
    }
/**
 * 091102_1747_malte hotfix for backend:workprogramme/agenda link compatibility
 * document.MediaPlayer.SetPosition(0*1000);
 */
com.metafusion.RealPlayer.prototype.SetPosition = com.metafusion.RealPlayer.prototype.setPosition;
	
	/**
     * Shows or hides the player controls)
     */
    com.metafusion.RealPlayer.prototype.showControls = function(show){
        //nothing
    }
    
    /**
     * stops the videoplayback
     */
    com.metafusion.RealPlayer.prototype.stop = function(){
        if (!_player) {
            error(this.classname + ': Player object not registered');
            return;
        }
        _player.DoStop();
    }
    
    /**
     * pauses the videoplayback
     */
    com.metafusion.RealPlayer.prototype.pause = function(){
        if (!_player) {
            error(this.classname + ': Player object not registered');
            return;
        }
        _player.DoPause();
    }
    /**
     * starts the videoplayback
     */
    com.metafusion.RealPlayer.prototype.play = function(){
        if (!_player) {
            error(this.classname + ': Player object not registered');
            return;
        }
        _player.DoPlay();
    }
    
    /**
     * writes the player object into the DOM.
     * currently distincts between Firefox and IE
     * TODO: check other browser / OS
     * @param {String} target: id of the HTMLObject to contain the player
     */
    com.metafusion.RealPlayer.prototype.write = function(target){
        var _bgcolor = "#FFFFFF";
        var output = '';
		
		 _name = _nameOriginal+target;
        _ffname = _ffnameOriginal+target;
        
        output += '<object';
        output += ' classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"';
        output += ' id="' + _name + '"';
        output += ' width="' + width.toString() + '"';
        if (showPlayerControls == true) {
            output += ' height="' + (height - 28).toString() + '"';
        }
        else {
            output += ' height="' + height.toString() + '"';
        }
        output += '>';
        output += '<param name="src" value="' + src + '"/>';
        //output += '<param name="center" value="true"/>'; 					// This parameter conflicts with the "maintainascpect" parameter 
        output += '<param name="maintainascpect" value="true"/>'; // This parameter conflicts with the "center" parameter
        output += '<param name="controls" value="' + 'ImageWindow' + '"/>';
        output += '<param name="autostart" value="' + 'true' + '"/>';
        output += '<param name="backgroundcolor" value="' + _bgcolor + '"/>';
        output += '<embed';
        output += ' name="' + _ffname + '"';
        output += ' id="' + _ffname + '"';
        output += ' backgroundcolor="' + _bgcolor + '"';
        output += ' src="' + src + '"';
        //output += ' center="true"';										// This parameter conflicts with the "maintainascpect" parameter 
        output += ' maintainascpect="true"'; // This parameter conflicts with the "center" parameter
        output += ' type="' + 'audio/x-pn-realaudio-plugin' + '"';
        output += ' width="' + width.toString() + '"';
        if (showPlayerControls == true) {
            output += ' height="' + (height - 28).toString() + '"';
        }
        else {
            output += ' height="' + height.toString() + '"';
        }
        output += ' console="' + 'Live' + '"';
        output += ' controls="' + 'ImageWindow' + '"';
        output += ' autostart="' + 'true' + '"';
        output += ' scriptcallbacks="onPlayStateChange" ';
		//output += ' scriptcallbacks="onTitleChange" ';
        
        output += ' nojava="' + 'false' + '"';
        output += '/>';
        output += '</object>';
        
        if (showPlayerControls == true) {
            output += '<br>';
            output += '<object';
            output += ' classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"';
            output += ' id="' + _name + '"';
            output += ' width="' + width.toString() + '"';
            
            output += ' height="' + '28' + '"';
            output += '>';
            output += '<param name="src" value="' + src + '"/>';
            output += '<param name="console" value="' + 'Live' + '"/>';
            output += '<param name="controls" value="' + 'Controlpanel' + '"/>';
            output += '<param name="autostart" value="' + 'true' + '"/>';
            output += '<param name="backgroundcolor" value="' + _bgcolor + '"/>';
            output += '<embed';
            output += ' backgroundcolor="' + _bgcolor + '"';
            output += ' src="' + src + '"';
            output += ' type="' + 'audio/x-pn-realaudio-plugin' + '"';
            output += ' width="' + width.toString() + '"';
            
            output += ' height="' + '28' + '"';
            output += ' console="' + 'Live' + '"';
            output += ' controls="' + 'Controlpanel' + '"';
            output += ' autostart="' + 'true' + '"';
            output += ' nojava="' + 'false' + '"';
            output += '/>';
            output += '</object>';
        }
        $$(target).innerHTML = output;
        
        registerPlayer();
        ff_realplayer_OnPlayStateChange = stateChangeEventListener;
		//[malte]
		if (papp.sessionData.is_livesession == 1 
		|| papp.sessionData.is_livesession == "1" 
		|| papp.sessionData.is_livesession == "true"
		|| papp.sessionData.is_livesession == true) {
			hacked_OnTitleChange = scriptEventListener;
			clearInterval(com.metafusion.RealPlayer.titleUpdateTimer);
			com.metafusion.RealPlayer.titleUpdateTimer = setInterval(titleUpdate, 1000);
		}
    }
	
	function titleUpdate ()
	{
		hacked_OnTitleChange(null, _player.GetTitle());
	}
	
	/**
	 * gets some available metadata. If available..
	 */
	com.metafusion.RealPlayer.prototype.getMetadata = function(){
		return "";
	}
    
    /**
     * sets up an DOM pointer to the player object, referenced by _name
     */
    function registerPlayer(){
        if ($$(_ffname)) {
            _player = $$(_ffname);
        }
        else 
            if ($$(_name)) {
                _player = $$(_name);
            }
            else {
                error(this.classname + ': registration of player failed');
            }
    }
}

