// new code should use embed.js; old code should be rewritten to use embed.js
if(!window.Msn) { window.Msn = {}; }
if(!window.Msn.Video) { window.Msn.Video = {}; }

// inject embed.js onto the page
if(document.createElement && document.getElementsByTagName)
{
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.setAttribute('type','text/javascript');
    script.setAttribute('src',"http://images.video.msn.com/flash/script/embed.js");
    head.appendChild(script);
}
else
{
    document.write('<script type="text/javascript" src="http://images.video.msn.com/flash/script/embed.js"></' + 'script>');
}

// deprecated functions, implemented in terms of embed.js for backwards compatibility

Msn.Video.Build = function(id,dl,fv,w,h)
{
    // this ensures all div's are built before the main functionality of BuildPlayer runs --
    // which also means that any downlevel experience may be briefly visible;
    // to remove this behavior, recode in terms of embed.js rather than using these methods.
    var o = { id: id,dl: dl,fv: fv,w: w,h: h };
    Msn.Video.exec(Msn.Video.Build2,o);
}

Msn.Video.Build2 = function(o)
{
    var el = document.getElementById(o.id);
    if(el)
    {
        Msn.Video.buildPlayerAdElements(el,o.id)
        Msn.Video.BuildPlayer2(o);
    }
}

Msn.Video.BuildPlayer = function(id,fv,w,h)
{
    // this ensures all div's are built before the main functionality of BuildPlayer runs --
    // which also means that any downlevel experience may be briefly visible;
    // to remove this behavior, recode in terms of embed.js rather than using these methods.
    var o = { id: id,fv: fv,w: w,h: h };
    Msn.Video.exec(Msn.Video.BuildPlayer2,o);
}

Msn.Video.BuildPlayer2 = function(o)
{
    var id = o.id;
    var dl = o.dl;
    var flashvars = o.fv;
    var w = o.w;
    var h = o.h;
    var container = document.getElementById(id);
    var contentDiv = document.getElementById(id + "_p");

    this.checkDefined = function(o) { return ((typeof (o) != "undefined") && (null != o)); }

    if(!checkDefined(container))
    {
        if(checkDefined(contentDiv))
        {
            contentDiv.setAttribute("id",id + "_content");
            var v = '<div id="' + id + '_p"></div>';
            contentDiv.innerHTML = v;

            container = contentDiv;
            contentDiv = document.getElementById(id + '_p');
        }
    }

    if(checkDefined(container) && checkDefined(contentDiv))
    {
        var detectDiv = document.createElement("div");
        detectDiv.setAttribute("id",id + "_detect");
        container.appendChild(detectDiv);

        var detectObj = new Msn.Video.FlashVersionDetect();

        var fvv = { ifs: "true",playlistmin: "2" };
        for(var i in flashvars)
        {
            fvv[i] = flashvars[i].replace(/\&/g,"%26");
        }

        var fvs = "";
        for(var i in fvv)
        {
            fvs += i + "=" + fvv[i] + "&";
        }
        fvs += "mode=inline&ch=true&cbprefix=Msn.Video.&cbdata=" + id +
            "&adDivs=" + (checkString(fvv["adDivs"]) ? (fvv["adDivs"] + ";") : "") + id + "_a,300,60";

        var src = "http://images.video.msn.com/flash/soapbox1_1.swf";
        params = new Object();
        params["flashvars"] = fvs;
        params["base"] = src;

        var flash = new Msn.Video.FlashObject(id + "_flash",src,w ? w : 300,h ? h : 269,params);
        flash.setInstallCtrl(new Msn.Video.FlashInstall(dl),9);

        detectObj.render(detectDiv);
        flash.render(contentDiv);
    }
}

// deferred execution until embed.js is loaded and parsed.
Msn.Video._extQueue = [];

Msn.Video.exec = function(func,arg)
{
    if(Msn.Video._loaded)
    {
        try
        {
            func(arg);
        }
        catch(e) { }
    }
    else
    {
        Msn.Video._extQueue.push({ "func": func,"arg": arg });
        setTimeout(Msn.Video.execQueuedCommands,100);
    }
}

Msn.Video.execQueuedCommands = function()
{
    if(Msn.Video._extQueue.length > 0)
    {
        var queue = [];
        queue.push.apply(queue,Msn.Video._extQueue);
        Msn.Video._extQueue.splice(0,Msn.Video._extQueue.length);

        for(var i = 0;i < queue.length;i++)
        {
            Msn.Video.exec(queue[i].func,queue[i].arg);
        }
    }
}

// forward references; function will be overridden in embed.js when that file is parsed
if(!Msn.Video._loaded)
{
    Msn.Video.buildPlayerAdElements = function(div,id) { }
    Msn.Video.FlashVersionDetect = function() { }
    Msn.Video.FlashObject = function(el,id) { this.setInstallCtrl = function(o,l) { };this.render = function(d) { }; }
    Msn.Video.FlashInstall = function(d) { }
}