
function Player(options) {
	//additional player events
	this.defaultOptions = {
		id:            null,
		title:         null,
		src:           null,
		objectId:      null,
		//events
		onInitialized: function(){},
		onPlay:        function(){},
		onPause:       function(){},
		onStop:        function(){},
		onEnded:       function(){},
		onState:       function(state){},
		onProgress:    function(obj){},
		onMediaLaunched:    function(){},
		onLoaded:      function(obj){}
	};
	
	this.options = extendObject(this.defaultOptions, options);

	this.id 					= this.options.id;
	this.objectId				= this.options.objectId;
	this.onPlayerInitialized 	= this.options.onInitialized;
	this.titleItem				= this.options.title;
	this.fileSrc				= this.options.src;
	this.seekPosition           = this.options.seekPosition;
	
	
	this.getId = function()
	{
		return this.id;
	}
	
	this.setTitleItem = function (titleItem)
	{
		this.titleItem = titleItem;
	}
	
	this.setFileSrc = function (fileSrc)
	{
		this.fileSrc = fileSrc;
	}

	
	this.SceneIntializedTracking = function()
	{
		this.initEventFlashListener();
		this.onPlayerInitialized();
	}
	
	this.initEventFlashListener = function()
	{
		var playerEvents = new PlayerEvents();
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_LAUNCHED, 'players.getPlayer(' + this.getId() +').MediaLaunched()');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_PLAY ,  	 'players.getPlayer(' + this.getId() + ').MediaPlay');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_STATE,  	 'players.getPlayer(' + this.getId() + ').MediaState');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_PAUSE,  	 'players.getPlayer(' + this.getId() + ').MediaPause');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_STOP,  	 'players.getPlayer(' + this.getId() + ').MediaStop');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_ENDED,  	 'players.getPlayer(' + this.getId() + ').MediaEnd');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_PROGRESS, 'players.getPlayer(' + this.getId() + ').MediaProgress');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_LOADED,   'players.getPlayer(' + this.getId() + ').MediaLoaded');
		this.getDomObject().AddExternalListener(playerEvents.MEDIA_BITRATE,   'players.getPlayer(' + this.getId() + ').MediaBitrate');
	}
		
	/**
	 * function to load a XML data
	 */
	this.loadMediaByXml = function (xmlUrl)
	{
		this.getDomObject().loadMediaByXml(xmlUrl);
	}
	
	/**
	 * function to load an Item Id 
	 */	
	this.loadMediaById = function (itemId)
	{
		if (itemId != undefined)
			this.getDomObject().loadMediaById(itemId);
	}

	/**
	 * function to load an Url
	 */	
	this.loadMediaByUrl = function (urlVideo)
	{
		this.getDomObject().loadMediaByUrl(urlVideo);
	}
	
	
	/**
	 * function to playMedia
	 */	
	this.playMedia = function ()
	{
		this.getDomObject().playMedia();
	}
	
	/**
	 * function to pauseMedia
	 */	
	this.pauseMedia = function ()
	{
		this.getDomObject().pauseMedia();
	}
	
	/**
	 * function to stopMedia
	 */	
	this.stopMedia = function ()
	{
		this.getDomObject().stopMedia();
	}
	
	/**
	 * function to switchPlayPauseMedia
	 */	
	this.switchPlayPauseMedia = function ()
	{
		this.getDomObject().switchPlayPauseMedia();
	}
	
	/**
	 * function to seekMedia
	 */	
	this.seekMedia = function (value)
	{
		if (value != undefined)
			this.getDomObject().seekMedia(value);
	}
	
	/***
	 *  Catch this event. Theses functions are the callback of the smartplayer. Active if activePlayerControl is called
	 */
	 this.MediaLaunched = function ()
	{		
		if (this.seekPosition != 0)		
			this.seekMedia(this.seekPosition);
	}
	
	this.MediaPlay = function ()
	{
		this.options.onPlay();
		
		this.logForSimpleStats('item', this.fileSrc, this.titleItem, 4);
		this.logForSimpleStats('state', 'PLAYING', null, 4);
	}
	
	this.MediaEnd = function ()
	{
		playlist.loadPlaylist();
		this.options.onEnded();
		this.logForSimpleStats('state', 'COMPLETED', null, 4);
	}
	
	this.MediaLoaded = function(obj)
	{
		this.options.onLoaded(obj);
		this.logForSimpleStats('load', obj.bytesLoaded, obj.bytesTotal, 4);
	}

	this.MediaProgress = function (obj)
	{
		this.options.onProgress(obj);
		
		if (obj.duration != 'Infinity')
			this.logForSimpleStats('time', obj.position, obj.duration - obj.position, 4);
	}


	this.MediaState = function (state)
	{
		this.options.onState(state);
		
		if (state == true)
		{
			this.logForSimpleStats('state', 'PLAYING', null, 4);
		}
		else
		{
			this.logForSimpleStats('state', 'BUFFERING', null, 4);
		}
		
	}

	this.MediaPause = function ()
	{
		this.options.onPause();
		
		this.logForSimpleStats('state', 'PAUSE', null, 4);
	}

	this.MediaStop = function ()
	{
		this.options.onStop();
		
		this.logForSimpleStats('state', 'ILDE', null, 4);
	}
	
	this.MediaBitrate = function (bitrate)
	{
		this.options.onBitrateChanged(bitrate);
	} 

	this.logForSimpleStats = function (typ, pr1, pr2, version)
	{
		getUpdate(typ, pr1, pr2, this.getId(), version);
	}

	this.getDomObject = function ()
	{
		return document.getElementById(this.options.objectId);
	}
	
	/**
  	* function to load a XML data
	*/
	this.ShowBlog = function ()
	{
		this.getDomObject().ShowBlog();
	}
	 
	/**
	 * function to load a XML data
	 */
	this.ShowSendTo = function ()
	{
		this.getDomObject().ShowSendTo();
	}
	
	/** 
	 * function to change player view mode (custom client FFJ)
	 * 
	 */	
	this.activeLightPlayerViewMode = function (value)
	{
		this.getDomObject().activeLightPlayerViewMode(value);
	}
	 
	
}



// toolkit methods comming from jQuery libs

var extendObject = function() {
	// copy reference to target object
	var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;

	// Handle a deep copy situation
	if ( typeof target === "boolean" ) {
		deep = target;
		target = arguments[1] || {};
		// skip the boolean and the target
		i = 2;
	}

	// Handle case when target is a string or something (possible in deep copy)
	if ( typeof target !== "object" && typeof target !== "function" )
		target = {};

	// extend jQuery itself if only one argument is passed
	if ( length == i ) {
		target = this;
		--i;
	}

	for ( ; i < length; i++ )
		// Only deal with non-null/undefined values
		if ( (options = arguments[ i ]) != null )
			// Extend the base object
			for ( var name in options ) {
				var src = target[ name ], copy = options[ name ];

				// Prevent never-ending loop
				if ( target === copy )
					continue;

				// Recurse if we're merging object values
				if ( deep && copy && typeof copy === "object" && !copy.nodeType )
					target[ name ] = extendObject( deep, 
						// Never move original objects, clone them
						src || ( copy.length != null ? [ ] : { } )
					, copy );

				// Don't bring in undefined values
				else if ( copy !== undefined )
					target[ name ] = copy;

			}

	// Return the modified object
	return target;
};
