//
//  Connect with a proxy to a Flash SmartPLayer
//  author: Michel Meyer
//  company: Brainsonic
//  version: 0.1
//  date :   2008/02/08
//
//  SmartPlayer class is dependant of the JavaScriptFlashGateway class
//
//  class attributes:
//
//  flash_player:        smartplayer file name
//  flash_config:        smartplayer configuration file name
//  flash_gateway:       javascript proxy gateway file name
//  height:              height for smartplayer
//	width:               width for smartplayer
//  target:              DOM div element where display the smartplayer
//  url_media:           Url of the media to play, must be a flv
//  fullscreen:          FullScreen mode allowed
//  version:             Flash player version
//  bgcolor:             Flash back ground color
//  uid:                 Unique Id for the javascript<->flash proxy

//
//  remote function called by flash when:
//

//  MediaStop:           smartplayer stop playing
//  MediaPlay:           smartplayer start playing
//  MediaPause:          smartplayer pause playing
//  MediaEnd:            smartplayer finish playing
//  PlayerInitialised:   smartplayer is ready to play
//

var test = true;

var SmartPlayer = function(base_url, target){

    this.base_url = base_url;
    this.flash_player = 'player.swf';
    this.flash_config = 'player.xml';
    this.flash_gateway = 'JavaScriptFlashGateway.swf';
    this.height = 352;
    this.width = 264;
    this.target = target;
    this.url_media = null;
	this.url_config_media = null;
    this.url_preRoll = null;
    this.urlPodCast = '';
    this.url_for = '';
    this.state = null;
    this.showMsg = 1;
    this.fullscreen = 'true';
    this.version = '8,0,0,0';
    this.bgcolor = '000000';
    this.uid = eval(new Date().getTime() + Math.random());
    this.isReady = false;
}

SmartPlayer.prototype = {

    init: function(){
        this.tag = new FlashTag(this.base_url + this.flash_player + '?richPlayerConfigFileUrl=' + this.base_url + this.flash_config, this.width, this.height, 'differe', 'lcId=' + this.uid, this.bgcolor, this.version, 'true');
        this.flashProxy = new FlashProxy(this.uid, this.base_url + this.flash_gateway, this);
        
    },
    
    displayPlayer: function(){
        if (this.tag == undefined) 
            this.init();
        else {
            document.getElementById(this.target).innerHTML = this.tag;
            this.alreadyCreate = true;
        }
        
    },
    
    setPreRollUrl: function(url, msg){
        this.url_preRoll = url;
        this.state = 'preroll';
    },
    
    setUrl: function(url_media, url_config_media){
        this.url_media = url_media;
		this.url_config_media = url_config_media;
		
    },
    
    setMediaUrl: function(url_media, url_config_media){
        if ((this.url_preRoll != '') && (this.state == 'preroll')) {
            this.loadMedia(this.url_preRoll);
            this.state = 'media';
        }
        else 
            this.loadMedia(url_media, url_config_media);
    },
    
    setShowMsg: function(showMsg){
        this.showMsg = showMsg;
    },
    
    loadPerma: function(urlPermaLink){
        if (this.flashProxy != null) {
            this.flashProxy.call("loadPerma", urlPermaLink);
        }
    },
    
    setPodcastUrl: function(urlPodCast){
        this.urlPodCast = urlPodCast;
    },
    
    //
    //Javascript to Flash
    //
    
    switchPlayPause: function(){
        this.flashProxy.call("switchPlayPause");
    },
    
    play: function(){
        this.flashProxy.call("play");
    },
    
    pause: function(){
        this.flashProxy.call("pause");
    },
    
    stop: function(){
        this.flashProxy.call("stop");
    },
    
    seek: function(time){
        this.flashProxy.call("seek", time);
    },
    
    loadMedia: function(urlMedia, urlConfigMedia){
        if (this.flashProxy != null && urlMedia != null && urlMedia != '') {
			this.flashProxy.call("loadMediaWithConfig", urlMedia, urlConfigMedia);	
	    }
    },
    
    playerHavetoShowMessage: function(){
        this.flashProxy.call("showMessage");
    },
    
    playerHavetoHideMessage: function(){
        this.flashProxy.call("hideMessage");
    },
    
    showMessageClicked: function(){
    },
    
    //not implemented yet
    getMediaPosition: function(){
    
    },
    
    //
    // Flash to Javascript
    //
    
    setMediaPosition: function(time){
    
    },
    
    //
    // Call methods defined in 
    //
    
    MediaStop: function(){
    },
    
    MediaPlay: function(){
        if (this.showMsg == 1) 
            this.playerHavetoShowMessage();
        else 
            this.playerHavetoHideMessage();
    },
    
    MediaPause: function(){
    },
    
    MediaEnd: function(){
        if (this.state == 'media') {
            this.state = 'postroll';
            this.setMediaUrl(this.url_media, this.url_config_media);
            return;
        }
        playlist.loadPlaylist();
    },
    
    PlayerInitialised: function(){
        if (this.url_media != null) {
            this.setMediaUrl(this.url_media, this.url_config_media);
            this.isReady = true;
        }
    },
    
	setMediaDuration: function(arg)
	{
		
	},

    onMediaLoading: function(arg)
	{
        // This method should be overridden to be used
    },
    
    zoomPlayerState: function()
	{
        // This method should be overridden to be used
    },
    
    unZoomPlayerState: function()
	{
        // This method should be overridden to be used
    },

	LaunchPodcast: function()
	{
		if (this.urlPodCast != '')
			window.open(this.urlPodCast);
	}
}
