/**
 * flowplayer.embed.js 3.0.1. Flowplayer JavaScript plugin.
 * 
 * This file is part of Flowplayer, http://flowplayer.org
 *
 * Author: Tero Piirainen, <support@flowplayer.org>
 * Copyright (c) 2008 Flowplayer Ltd
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * SEE: http://www.opensource.org/licenses
 * 
 * Version: 3.0.1 - Tue Feb 03 2009 10:49:40 GMT-0000 (GMT+00:00)
 *
 * Altered for Center for American Progress by Evan Hensleigh
 */
 (function() {
    function toAbsolute(url, base) {
        if (url.substring(0, 4) == "http") {
            return url;
        }
        if (base) {
            return base + (base.substring(base.length - 1) != "/" ? "/": "") + url;
        }
        base = location.protocol + "//" + location.host;
        if (url.substring(0, 1) == "/") {
            return base + url;
        }
        var path = location.pathname;
        path = path.substring(0, path.lastIndexOf("/"));
        return base + path + "/" + url;
    }
    $f.addPlugin("embed",
    function(options) {
        var self = this;
        var conf = self.getConfig(true);
		conf.clip.autoPlay = false;
		conf.clip.autoBuffering = false;
        var opts = {
            width: self.getParent().clientWidth,
            height: self.getParent().clientHeight,
            url: toAbsolute(self.getFlashParams().src),
            index: -1,
            allowfullscreen: true,
            allowscriptaccess: 'always'
        };
        $f.extend(opts, options);
        opts.src = opts.url;
        opts.w3c = true ;
        if (opts.index > -1) {
            conf.playlist = [conf.playlist[opts.index]];
        }
        var index = 0;
       $f.each(conf.playlist,
        function() {
            conf.playlist[index++].url = toAbsolute(this.url, this.baseUrl);
        });
        delete conf.playerId;
        delete opts.url;
        delete opts.index;
		conf.playlist[0].autoPlay = false;
		conf.playlist[0].autoBuffering = false;
		console.log(conf);
		console.log(conf.playlist);
       this.getEmbedCode = function(runnable) {
		console.log(conf.playlist);
            var html = flashembed.getHTML(opts, {
                config: conf
            });
            if (!runnable) {
                html = html.replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
            }
			console.log(html);
            return html;
        };
        return self;
    });
})();