// An enumerator for the Federated player's page events.
function VideoPlayerEvent(){};
VideoPlayerEvent.PLAYING = "video_play";
VideoPlayerEvent.STOPPED = "video_stop";
VideoPlayerEvent.READY = "video_ready";

function VideoPlayerManager()
{
this._events = new Array();
this._events[VideoPlayerEvent.READY] = new CCEventBroadcaster();
this._events[VideoPlayerEvent.PLAYING] = new CCEventBroadcaster();
this._events[VideoPlayerEvent.STOPPED] = new CCEventBroadcaster();
};
VideoPlayerManager.DEBUG = false;
VideoPlayerManager._instance = null;
VideoPlayerManager.getInstance = function()
{
if( VideoPlayerManager._instance == null ) VideoPlayerManager._instance = new VideoPlayerManager();
return VideoPlayerManager._instance;
};
VideoPlayerManager.prototype = {
_cint:null,// connect interval
_player:null,
_events:null,
_play_buffer:null,
toString:function()
{
return "[VideoPlayerManager]";
},
add_listener:function( event, obj, method ){
if( event == VideoPlayerEvent.PLAYING || event == VideoPlayerEvent.STOPPED )
{
this._events[event].add_listener( obj, method );
return true;
}
return false;
},
play:function( id ){
this._log( this.toString()+" trying to play video "+id );
if( !this._player ) this._play_buffer = id;
else this._player.vp_play( id );
return false;// because it is used in onclick events.
},
pause:function(){
if( this._player ){
this._player.vp_pause();
}
},
position:function(){
if( this._player ){
return this._player.vp_position();
}
return null;
},
open_url:function( url ){
if( window.opener ){
window.opener.document.location = url;
}
else{
window.document.location = url;
}
},
ready:function(){
try
{
this._player = swfo.getObjectById( "video_player" );
}
catch( e )
{
this._player = $( "#video_player" )[0];
}
if( this._play_buffer!=null )
{
this.play( this._play_buffer );
this._play_buffer = null;
}
this._events[VideoPlayerEvent.READY].broadcast( [] );
},
loading:function( id ){
this._log( this.toString()+" playing video "+id );
this._events[VideoPlayerEvent.PLAYING].broadcast( [id] );
},
stopped:function( id ){
this._log( this.toString()+" stopped video "+id );
this._events[VideoPlayerEvent.STOPPED].broadcast( [id] );
},
_log:function( str, level ){
if( VideoPlayerManager.DEBUG )
{
try{
switch( level )
{
case "warn":
console.warn( str );
break;

case "error":
console.error( str );
break;

case "info":
console.info( str );
break;

default:
console.debug( str );
}
}
catch(e){}
}
}
};

function VideoListManager( entry_name, selected_entry_class_name, details_name, selected_details_class_name ){
this._en = entry_name;
this._secn = selected_entry_class_name;
this._dn = details_name;
this._sdcn = selected_details_class_name;
this._index = VideoListManager._instances.push( this ) - 1;

try
{
var fm = VideoPlayerManager.getInstance();
fm.add_listener( VideoPlayerEvent.PLAYING, this, this._playing );
fm.add_listener( VideoPlayerEvent.STOPPED, this, this._stopped );
}
catch(e)
{
VideoPlayerManager.getInstance()._log( this.toString()+" failed to initialize event listeners : "+e.message, "error" );
}
};
VideoListManager._instances = [];
VideoListManager.prototype = {
_index:null,
_en:null,// entry name
_secn:null,// selected entry class name
_dn:null,// details name
_sdcn:null,// selected details class name
_recent_vid:null,
_entries:null,
_details:null,
toString:function(){
return "[VideoListManager]";
},
_playing:function( id ){
VideoPlayerManager.getInstance()._log( this.toString()+" playing event for id '"+id+"'" );
this._recent_vid = id;
if( this._en != null && this._secn != null )
{
this._check_dom();
var i = 0, vid;
var len = this._entries.length>0 ? this._entries.length : this._details.length, entry;
var detail;

for( ; i < len ; i++ )
{
entry = this._entries[i];
detail = this._details[i];
vid = entry ? entry.getAttribute("videoid") : detail.getAttribute("videoid");

if( vid == id )
{
if( entry ) $(entry).addClass( this._secn );
if( detail )$(detail).addClass( this._sdcn );
}
else
{
if( entry ) $(entry).removeClass( this._secn );
if( detail )$(detail).removeClass( this._sdcn );
}
}
}
},
_stopped:function( id ){
if( this._en != null && this._secn != null )
{
this._check_dom();
var i = 0, len = this._entries.length;
for( ; i < len ; i++ )
{
this._entries[i].className = this._entries[i].className.replace( " "+this._secn, "" );
}
}
},
_check_dom:function(){
if( this._entries == null )
{
try
{
var entries_parent = $( "#"+this._en );
this._entries = entries_parent.children("div[videoid]");
}
catch(e)
{
VideoPlayerManager.getInstance()._log( this.toString()+" error findind the entries in the DOM : "+e.message, "error" );
}
}
if( this._details == null )
{
try
{
var details_parent = $( "#"+this._dn );
this._details = details_parent.children("div[videoid]");
}
catch(e)
{
VideoPlayerManager.getInstance()._log( this.toString()+" error findind the details in the DOM : "+e.message, "error" );
}
}
}
};