//
//  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
//

/* instance smartPlayer*/
var smartPlayer = null

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            = 320;
	this.width             = 240;
	this.target            = target;
	this.url_media         = null;
	this.url_preRoll       = null;
	this.url_postRoll      = null;
	this.url_for           = null;
	this.state			       = null;
	this.showMsg           = 0;
	this.fullscreen        = 'true';
	this.version           = '8,0,0,0';
	this.bgcolor           = '000000';
	this.uid               = eval(new Date().getTime()+Math.random());
	this.isReady           = false;
	this.zoom              = false;
	this.title             = null;
	this.currentMediaState = null;
	this.langIso		       = null;
	this.stats             = true;
	this.smartplayerconfig = null;
	this.blog = null;
	this.player_src = null;
	this.permalink = null;
}

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);
	this.displayPlayer();
  },
  
  displayPlayer: function()
  {
	   document.getElementById(this.target).innerHTML = this.tag;
		 this.alreadyCreate = true;
  },
  
  setUrlSmartPlayer: function(smartplayerconfig)
  {
  	 this.smartplayerconfig = smartplayerconfig;
  },
  
  setBlog: function(blog, player_src)
  {
  	this.player_src = player_src;
  	this.blog = blog;
  },
  
  setUrlPerma: function(urlperma)
  {
  	this.urlperma = urlperma;
  },


  setPreRollUrl: function(url)
  {
   this.url_preRoll = url;
   this.state = 'preroll';
  },

  setPostRollUrl: function(url,msg)
  {
  	this.url_postRoll = url;
  },

  setUrl: function(url)
  {
  	this.url_media = url;
  },

  setTitle: function(title)
  {
  	this.title = title;
  },

  setLangIso: function(langIso)
  {
    this.langIso = langIso;
  },

  setMediaUrl: function(media_url,msg)
  {
  	this.url_media = media_url;
    this.loadMedia(this.url_media);
  },

  onMediaLoading: function (arg)
  {
    if (this.blog != null)
	{
		this.flashProxy.call("loadBlog", this.player_src, this.blog);
	}
	
	if (this.blog == null && this.urlperma != null)
	{
		this.flashProxy.call("loadSendto", "", this.urlperma);
	}
	
  },
  
  onBlogLoaded: function (arg)
  {
  	if (this.urlperma != null)
	{
		this.flashProxy.call("loadSendto", "", this.urlperma);
	}
  },
  
  onSendtoLoaded: function(arg)
  {
	
  },

  setShowMsg: function(showMsg)
  {
  	this.showMsg = showMsg;
  },


  //
  //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)
	  {
	  	//this.flashProxy.call("loadMediaAndTitle",urlMedia,this.title);
		this.flashProxy.call("loadMediaWithConfig", urlMedia, this.smartplayerconfig, this.title);
      }
  },
  
  loadResume: function(resume)
  {
		this.resume = resume
  },

  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)
	{
	 
	},

  MediaStop:function()
  {
	   this.MediaState(this.uid,"IDLE");
  },

  MediaPlay:function()
  {  
  	
	
  },

  MediaPause:function()
  {
  	this.MediaState("PAUSE");
  },

  MediaEnd:function()
  {   
  	this.MediaState("COMPLETED");
        
  	this.url_preRoll = "";
  	this.url_postRoll = "";
  	playList.loadPlayList();
  },
  
  medialoadedIsPub: function()
	{
		this.flashProxy.call("IsPubMedia",'1');
	},
	      
	medialoadedIsVideo: function()
	{
		this.flashProxy.call("IsPubMedia",'0');
	},
	
	zoomPlayer: function()
	{	
  },
  
  zoomPlayerState: function()
  {
  	
  },

  unZoomPlayerState: function()
  {
  	
  },

  PlayerInitialised: function()
  {
  	if(this.url_media != undefined)
    	this.setMediaUrl(this.url_media);
    this.isReady = true;
	jQuery('#useless_p').trigger('eventSmartPlayerReady');
  },    

  setMediaDuration:function(time)  
  {     
  },
    
  ButtonStopPress:function()
  {
  	this.MediaState("IDLE");
  },
  
	
	//
  // Call at bsStats/simplestats
  //
	
  MediaLoaded:function(bytesLoaded, bytesTotal)
  {
    this.simplestats('load', bytesLoaded, bytesTotal, this.uid);
  },

  MediaItem:function(filename, title)
  { 
    this.simplestats('item', filename, this.title, this.uid);   
  },

	MediaResize:function()
	{
	   this.simplestats('size', obj.width, obj.height, obj.id, 4);
	},
	     
  MediaState:function(state)
  {   
  	if(this.currentMediaState != state)
	  {
    this.simplestats('state', state, null, this.uid);    
      this.currentMediaState = state;
    }
 	},
	      
	MediaTime:function(position,duration)
	{
  	if(duration != Infinity)
    {
    	this.simplestats('time', position, duration - position, this.uid);
    }
  },
	       
  MediaVolume:function()
  {
  	 this.simplestats('volume', obj.percentage, null, obj.id, 4);
  },
	
	simplestats:function(typ, pr1, pr2, swf, version)
	{
		// Call the log function of simplestats.js
		if (this.stats)
			getUpdate(typ, pr1, pr2, swf, version);
	}
   
}