var player;

function playerReady(obj) {
	player = jQuery('#'+obj["id"])[0];
	//if (addPlayerListeners) { addPlayerListeners(); };
};

jQuery.fn.createPlayer = function (movie_url, options) {
    var settings = jQuery.extend({
        autoplay: false,
        width: this.width(),
        height: this.height(),
        allowFullScreen: true,
        thumb_url: "",  // TODO: Use url to default stock image
        showControls: true,
        showIcons: false
    }, options);
    
    function vs(v) { return v.major + '.' + v.minor + '.' + v.rev; };
    
    return this.each(function() {
        var flashvars = {
            file: movie_url,
            bufferlength: 4,
            icons: false,
            repeat: "list",
            width: settings.width,
            height: settings.height,
            lightcolor: "B34717",
            backcolor: "000000",
            frontcolor: "AAAAAA",
            skin: "/media/ams/swf/player_skin.swf",
            autostart: settings.autoplay
        };
        var params = {
            allowFullScreen: settings.allowFullScreen,
            allowScriptAccess: "always",
            wmode: "transparent"
        };

        var attributes = {};

        if (settings.thumb_url) {
            flashvars.image = settings.thumb_url;
        }

        if (settings.showControls) {
            flashvars.controlbar = "over";
        } else {
            flashvars.controlbar = "none";
        }
        flashvars.icons = settings.showIcons;
        if (settings.autoplay) {
            flashvars.autostart = true;
        }
        var player_url = "/media/ams/swf/player.swf";
        flash_version = swfobject.getFlashPlayerVersion();
        ver = flash_version.major + "." + flash_version.minor + "." + flash_version.release;
        swfobject.embedSWF(player_url, this.id, settings.width, settings.height, ver,"expressInstall.swf", flashvars, params, attributes);
                     //so.addVariable("lightcolor"   , "339900" );
                     //so.addVariable("backcolor"    , "000000" );
                     //so.addVariable("frontcolor"   , "AAAAAA" );
                //         so.addVariable("repeat"       , "list" );
                //         so.addVariable("icons"       , "false" );
                //         so.addVariable("shuffle"      , "false" );
                //         so.addVariable("showdigits"   , "true" );
                //         so.addVariable("linkfromdisplay", "true")
                //         so.addVariable("bufferlength" , 4 );
                //         //so.addVariable("overstretch"  , "true" );
                //         //so.addVariable("displayclick" , "play");
                //         if (settings.autoplay) {
                //             so.addVariable("autostart", "true");
                //         }      
                //         else  {        
                //             so.addVariable("autostart", "false");
                //         }
                //         console.log('thumb:',settings.thumb_url);
                //         so.addVariable("image", settings.thumb_url );
                //         so.addVariable("file",  movie_url );
                //         so.addVariable("stretching",  "uniform" );
                //         var mode = "over";
                //         if (!settings.showControls) {
                //             mode = "none";
                //         };
                // so.addVariable("controlbar", mode );

        //so.write(this);
    });
};
