var players = new Array();
var send_event_timer = new Array();
function add_player( config, container_id, width, height, autostart, player_id,
                     frontcolor, backcolor, screencolor,
                     wmode, flash_base_url, player_file, express_install, flash_version )
{
    if( empty( container_id ) )
    {
        return;
    }
    var flashvars = {};
    if( !empty( config ) )
    {
       flashvars.config = config;
    }
    
    if( empty( width ) )
    {
        width = "600";
    }
    if( empty( height ) )
    {
        height = "390";
    }
    flashvars.width = width;
    flashvars.height = height;
    if( empty( flash_base_url ) )
    {
        flash_base_url = "";
    }
    if( empty( player_file ) )
    {
        player_file = "player.swf";
    }
    player_file = flash_base_url + player_file;

    if( empty( player_id ) )
    {
        player_id = "player";
    }
    if( empty( autostart ) || !( autostart == "true" || autostart == "false" ) )
    {
        autostart = "true";
    }
    flashvars.autostart = autostart;
    if( empty(express_install) )
    {
        express_install = "expressInstall.swf";
    }
    express_install = flash_base_url + express_install;
    if( empty( wmode ) )
    {
        wmode = "transparent";
    }
    if( empty( frontcolor ) )
    {
        frontcolor = "0xFFFFFF";
    }
    if( empty( backcolor ) )
    {
        backcolor = "0xFFFFFF";
    }
    if( empty( screencolor ) )
    {
        screencolor = "0x000000";
    }
    if( empty( flash_version ) )
    {
        flash_version = "9.0.115";
    }

	var params = {};
	params.quality = "high";
	params.wmode = wmode;
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	params.allownetworking = "all";
	var attributes = {};
	attributes.id = player_id;

	swfobject.embedSWF( player_file, container_id, width, height, flash_version, express_install, flashvars, params, attributes );
}
function send_event( player_id, event, param, status )
{
    clearInterval( send_event_timer[player_id] );
    if( empty( status ) || status == "started" )
    {
        status = "continued";
        send_event_start_hook( player_id, event, param );
    }

    if( empty( players[player_id] ) )
    {
        send_event_delay_hook( player_id, event, param );
        send_event_timer[player_id] = setInterval( "send_event(" + player_id + "," + event + "," + param + ", " + status + ")", 20 );
    }
    else
    {
        try
        {
    		players[player_id].sendEvent( event, param );
            send_event_success_hook( player_id, event, param );
            players[player_id].addViewListener( event,
                "send_event_complete_hook(" + player_id + ",'" + event + "','" + param + ")" );
    	}
        catch( err )
        {
            send_event_error_hook( player_id, event, param, err );
    	}
    }
}
function get_player( player_id )
{
    if( navigator.appName.indexOf("Microsoft") != -1 )
    {
        return window[player_id];
    }
    else
    {
        return document[player_id];
    }
}
function load_file( player_id, file, type )
{
    if( empty(type) )
    {
        type = "normal";
    }

    if( type == "normal" )
    {
        load_normal_file_start_hook();
    }
    else if( type == "promo" )
    {
        load_promo_file_start_hook();
    }
    else
    {
        load_other_file_start_hook();
    }

    get_player( player_id ).loadFile( file );
}
function playerReady( player )
{
	players[player['id']] = get_player( player['id'] );
}
function empty( val )
{
    if( val == undefined || val == null || val == "" || val == 0 )
    {
        return true;
    }
    else
    {
        return false;
    }
}
function print_r( obj, tag_id )
{
    if( obj.constructor == Array || obj.constructor == Object )
    {
        print_r_write( "<ul>", tag_id );
        for( var p in obj )
        {
            if( obj[p].constructor == Array || obj[p].constructor == Object )
            {
                print_r_write( "<li>[" + p + "] => "+typeof(obj) + "</li>", tag_id  );
                print_r_write( "<ul>", tag_id  );
                print_r( obj[p], tag_id );
                print_r_write( "</ul>", tag_id  );
            }
            else
            {
                print_r_write( "<li>[" + p + "] => " + obj[p] + "</li>", tag_id  );
            }
        }
        print_r_write( "</ul>", tag_id  );
    }
}
function print_r_write( str, tag_id )
{
    if( tag_id == undefined )
    {
        document.write( str );
    }
    else
    {
        var prev_html = document.getElementById( tag_id ).innerHTML;
        document.getElementById( tag_id ).innerHTML = prev_html + str;
    }
}
function  send_event_start_hook( player_id, event, param )
{
    alert( "Event:" + event + " SENT from js for player with id:"
        + player_id + "with param:" + param );
}
function send_event_delay_hook( player_id, event, param )
{
    alert( "Event" + event + " DELAYED from js for player with id:"
        + player_id + "with param:" + param );
}
function send_event_success_hook( player_id, event, param )
{
    alert( "Event" + event + " SUCCESSFUL from js for player with id:"
        + player_id + "with param:" + param );
}
function send_event_complete_hook( player_id, event, param )
{
    alert( "Event" + event + " COMPLETE from js for player with id:"
        + player_id + "with param:" + param );
}
function send_event_error_hook( player_id, event, param, err )
{
    alert( "Event" + event + " ERROR from js for player with id:"
        + player_id + "with param:" + param + ", error:" + err );
}
