//
//  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.rollState = 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.flashVars = new Object();
	this.isReady = false;
	this.zoom = false;
	this.title = null;
	this.currentMediaState = null;
	this.langIso = null;
	this.stats = true;
	this.smartplayerconfig = null;
	this.defaultsmartplayerconfig = null;
	this.blog = null;
	this.player_src = null;
	this.permalink = null;
	
	this.statsFilename = null;
}

SmartPlayer.prototype = {

	init : function() {
		this.tag = new FlashTag(this.base_url + this.flash_player
				+ '?richPlayerConfigFileUrl=' + this.flash_config, this.width, this.height, 'differe',
				'lcId=' + this.uid + this.getFlashVarsAsUrlString(), this.bgcolor, this.version, 'true');
		this.flashProxy = new FlashProxy(this.uid, this.base_url
				+ this.flash_gateway, this);
		this.displayPlayer();
	},

	displayPlayer : function() {
		var newDiv = document.createElement("div");
		newDiv.id = 'player_content';
		newDiv.innerHTML = this.tag;
		document.getElementById(this.target).innerHTML = '';
		document.getElementById(this.target).appendChild(newDiv);
		this.alreadyCreate = true;
	},

	setUrlSmartPlayer : function(smartplayerconfig) {
		this.smartplayerconfig = smartplayerconfig;
	},
	
	setUrlDefaultSmartPlayerConfig : function(smartplayerconfig) {
		this.defaultsmartplayerconfig = 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.RollState = 'preroll';
	},

	setPostRollUrl : function(url, msg) {
		this.url_postRoll = url;
	},

	setMediaUrl : function(url) {
		this.url_media = url;
	},

	setTitle : function(title) {
		this.title = title;
	},

	setLangIso : function(langIso) {
		this.langIso = langIso;
	},

	onMediaLoading : function(arg) {
		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;
	},

	
	setFlashVars : function(vars) {
		if( typeof(vars) != 'object')
		{
			throw 'flashvar must be a javascript object';
		}
		else {
			this.flashVars = vars;
		}
	},
	
	getFlashVarsAsUrlString : function() {
		var flashVarsString = '';
		for (var key in this.flashVars) {
			flashVarsString += '&' + key + '=' + this.flashVars[key];
		}
		return flashVarsString;
	},
	
	
	//
	//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() {
		if (this.flashProxy != null) {
			/*
			this.flashProxy.call("loadMediaAndTitle",urlMedia,this.title);
			*/
			console.log('state', this.rollState);
			console.log('media', this.url_media);
			console.log('preroll', this.url_preRoll);
			console.log('postroll', this.url_postRoll);
				
			
			var mediaUrl = this.url_media;
			var configUrl = this.smartplayerconfig;
			
			if(this.rollState == 'preroll')
			{
				mediaUrl = this.url_preRoll;
				configUrl = this.defaultsmartplayerconfig;
			}
			else if(this.rollState == 'postroll')
			{
				mediaUrl = this.url_postRoll;
				configUrl = this.defaultsmartplayerconfig;
			}
			
			console.log('loadMediaUrl', mediaUrl);
			console.log('smartplayerconfig', configUrl);
			
			this.flashProxy.call("loadMediaWithConfig", mediaUrl, configUrl, 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
	//
	
	onLoadMediaCalled: function()
	{
		if (this.blog != null) {
			this.flashProxy.call("loadBlog", this.player_src, this.blog);
		}
	},


	setMediaPosition : function(time) {

	},

	MediaStop : function() {
		this.MediaState("IDLE");
	},

	MediaPlay : function() {

	},

	MediaPause : function() {
		this.MediaState("PAUSE");
	},

	MediaEnd : function() {
		
		if (this.rollState == 'media' && this.url_postRoll != null && this.url_postRoll != '') {
            this.rollState = 'postroll';
            this.loadMedia();
        }
        else if(this.rollState == 'preroll'){
            this.rollState = 'media';
            this.loadMedia();
        }
		
		this.MediaState("COMPLETED");
	},

	medialoadedIsPub : function() {
		this.flashProxy.call("IsPubMedia", '1');
	},

	medialoadedIsVideo : function() {
		this.flashProxy.call("IsPubMedia", '0');
	},

	zoomPlayer : function() {
	},

	zoomPlayerState : function() {
		$('playlist').style.zIndex = 1;
		$('arbo').style.zIndex = 1;
		$('player_container').style.zIndex = 100;
		this.zoom = true;
	},

	unZoomPlayerState : function() {
		$('player_container').style.zIndex = 1;
		$('playlist').style.zIndex = 100;
		$('arbo').style.zIndex = 100;
		this.zoom = false;
	},

	PlayerInitialised : function() {
		
		if (this.url_media != undefined)
		{
			if(this.url_preRoll != null && this.url_preRoll != '')
			{
	    		this.rollState = 'preroll';
	    	}
	    	else {
	    		this.rollState = '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.statsFilename = filename;
		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 && this.url_media == this.statsFilename)
	{
		getUpdate(typ, pr1, pr2, swf, version);
	}
}

}
