/*
    Uniplayer - Universal Web Media Player
    Copyright (C) 2008 Artem Bandur <artico.bandurini@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

UniplayerRealPlayer = function(){
        /*
                KNOWN ISSUES
                
                - no js control in google chrome
                - no js control to fullScreen in Internet Explorer (win)
        */
        this.name = 'realplayer';
        this.title = 'Real Player';
        this.needle = 'RealPlayer';
        this.progid = 'rmocx.RealPlayer G2 Control';
        this.classid = 'clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA';
        this.codebase = 'http://play.radiolla.com/uniplayer/install/RealPlayer.cab';
        this.pluginspage = 'http://www.real.com/';
        this.mime = 'audio/x-pn-realaudio-plugin';
        this.supported = [
                'audio/x-pn-realaudio-plugin',
                'audio/x-pn-realaudio',
                'video/vnd.rn-realvideo',
                'application/vnd.rn-realplayer-javascript'
        ];
        this.maxVolume = 100;
        // build                http://service.real.com/help/library/guides/extend/htmfiles/appc_par.htm
        this.build = function(){
                // show native controls
                // real player requires creating of addition <object> for controls
                if(this.player.options.showNativeControls){
                        // uniqueName is needed for CONSOLE parameter of both objects
                        var uniqueName = 'uniplayer_'+Math.ceil(Math.random()*10000000);
                }
                // create object
                var obj = new this.player.UniplayerObject({
                        id:     this.player.options.id,
                        src: this.player.current.src,
                        width: this.player.options.width,
                        height: this.player.options.height,
                        type: this.mime,
                        classid: this.classid,
                        codebase: this.codebase,
                        pluginspage: this.pluginspage
                });
                obj.addParam('SRC', this.player.current.src, true);
                obj.addParam('AUTOSTART', this.player.options.autoplay);
                obj.addParam('BACKGROUNDCOLOR', this.player.options.background);
                obj.addParam('CONTROLS', 'ImageWindow');
                obj.addParam('NOLOGO', true);
                if(this.player.options.showNativeControls){
                        obj.addParam('CONSOLE', uniqueName);
                }
                this.movie = obj.write(this.player.holders.movie);
                delete obj;

                if(this.player.options.showNativeControls){
                        // create object for controls realplayer
                        var objControls = new this.player.UniplayerObject({
                                src: this.player.current.src,
                                width: this.player.options.width,
                                height: 36,
                                type: this.mime,
                                classid: this.classid,
                                codebase: this.codebase,
                                pluginspage: this.pluginspage
                        });
                        objControls.addParam('SRC', this.player.current.src, true);
                        objControls.addParam('AUTOSTART', this.player.options.autoplay);
                        objControls.addParam('CONTROLS', 'ControlPanel');
                        objControls.addParam('CONSOLE', uniqueName);
                        this.movie = objControls.write(this.player.holders.movie, true);
                        delete objControls;
                }
                this.player.current.width = this.player.options.width;
                this.player.current.height = this.player.options.height + (this.player.options.showNativeControls ? 36 : 0);
                return this.movie;
        };
        /* test
        this.test = function(){
                var result = false;
                try{
                        result = this.getVersion() ? true : false;
                }
                catch(e){}
                return result;
        };*/
        //controls              http://service.real.com/help/library/guides/extend/htmfiles/appa_met.htm
        this.getVersion = function(){
                return this.player.current.movie.GetVersionInfo();
        };
        this.getState = function(){
                var state = parseInt(this.player.current.movie.GetPlayState());
                var currentState = this.player.current.state;
                switch(state){
                        case 1:
                        case 2:
                        case 5:
                                currentState = this.player.states.loading;
                                break;
                        case 3:
                                currentState = this.player.states.playing;
                                break;
                        case 4:
                                currentState = this.player.states.paused;
                                break;
                        case 0:
                                currentState = this.player.states.stopped;
                                break;
                }
                return currentState;
        };
        this.reload = function(){
                return this.player.current.movie.SetSource(this.player.current.src);
        };
        this.play = function(){
                return this.player.current.movie.DoPlay();
        };
        this.pause = function(){
                return this.player.current.movie.DoPause();
        };
        this.stop = function(){
                return this.player.current.movie.DoStop();
        };
        this.fullScreen = function(){
                return this.player.current.movie.SetFullScreen();
        };
        this.getMute = function(){
                return this.player.current.movie.GetMute();
        };
        this.setMute = function(turn){
                return this.player.current.movie.SetMute(turn);
        };
        this.getVolume = function(){
                var volume = this.player.current.movie.GetVolume();
                return isNaN(volume) ? 0 : volume/this.player.current.module.maxVolume;
        };
        this.setVolume = function(volume){
                return this.player.current.movie.SetVolume(volume*this.player.current.module.maxVolume);
        };
        this.getDuration = function(){
                var duration = this.player.current.movie.GetLength();
                return isNaN(duration) ? 0 : duration/1000;
        };
        this.getPosition = function(){
                var position = this.player.current.movie.GetPosition();
                return (isNaN(position) || !this.player.current.duration) ? 0 : position/1000/this.player.current.duration;
        };
        this.setPosition = function(position){
                return this.player.current.movie.SetPosition(parseInt(position*1000*this.player.current.duration));
        };
};

