//
//  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 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_preRoll    = null;
	this.urlPodCast     = '';
	this.url_for        = '';
	this.state			= null;
	this.showMsg        = 1;
	this.fullscreen     = 'true';
	this.version        = '9.0.115.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(){
  			var config = new FlashConfiguration();
  			if(this.tag == undefined)
				this.init();
			else
			{
				if (config.hasMinFlashVersion(this.version))
				{
					document.getElementById(this.target).innerHTML = this.tag;
		    		this.alreadyCreate = true;
				}
				else
				{
					document.getElementById("alternativeContentLink").style.display = 'block';
				}
			}
			
	},
	
	setPreRollUrl: function(url,msg){
		this.url_preRoll = url;
		this.state = 'preroll';
	},
	
	setUrl: function(url){
		this.url_media = url;
	},

	setMediaUrl: function(media_url){
		if ((this.url_preRoll != '') && (this.state == 'preroll'))
		{
			this.loadMedia(this.url_preRoll);
			this.state = 'media';
		}
		else
			this.loadMedia(this.url_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)
	{
		if (this.flashProxy != null && urlMedia != null && urlMedia != '')
		{
	  		this.flashProxy.call("loadMedia",urlMedia);
	  	}
	},
	
	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);
  		return;
  	}	
  	playList.loadPlayList(this.url_for);
  },
  
  PlayerInitialised: function()
  {
  	if (this.url_media != null)
  	{
	    this.setMediaUrl(this.url_media);
	    this.isReady = true;
    }
  },
  
	setMediaDuration: function(arg)
	{
	},

	onMediaLoading: function(arg)
	{
 	

	},
  
	zoomPlayerState: function()
	{
	},
	
    unZoomPlayerState: function()
    {
    }
}
