function GetPlayerEmbedCode(idPlayer, playerLocation, presentationTitle, presentationUrl, playerVars, width, height, wmode, forXhtml /* = false */)
{
    var matches = presentationUrl.match(/^(http:\/\/[^\/]*)(\/|$)/i);
    var rootUrl = matches ? matches[1] : "";

    var html = '<div style="width:' + width + 'px;text-align:left">'
                + '<a style="font:14px Helvetica,Arial,Sans-serif;color: #0000CC;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="' + presentationUrl + '" title="' + presentationTitle + '">' + presentationTitle + '</a>';

    if (forXhtml)
        html += GetPlayerEmbedForXhtml(idPlayer, playerLocation, playerVars, width, height, wmode);
    else
        html += GetPlayerEmbed(idPlayer, playerLocation, playerVars, width, height, wmode);

    html += '<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View <a href="' + rootUrl + '" style="color: #0000CC;">more presentations</a> or <a href="' + rootUrl + '/upload" style="color: #0000CC;">Upload</a> your own.</div>'
                + '</div>';
    return html;
}

function GetPlayerEmbed(idPlayer, playerLocation, playerVars, width, height, wmode)
{
    if (wmode === undefined)
    {
        wmode = "opaque";
    }
    var htmlVars = GetFlashVars(playerVars);

    var flashParams =
    {
        allowScriptAccess: "always",
        quality:           "high",
        bgcolor:           "#ffffff",
        allowFullScreen:   "true",
        flashVars:         htmlVars
    };
    if (wmode)
        flashParams['wmode'] = wmode;

    var prefix = document.location.protocol + '//';

    var html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="' + prefix + 'fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="' + width + '" height="' + height + '" id="' + idPlayer + '">'
                + '<param name="movie" value="' + playerLocation + '" />';

    for(var i in flashParams)
        html += '<param name="' + i + '" value="' + flashParams[i] + '" />';

    html += '<embed src="' + playerLocation  + '" width="' + width + '" height="' + height + '" name="' + idPlayer + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"';

    for (i in flashParams)
        html += i + '="' + flashParams[i] + '" ';

    html += '></embed></object>';

    return html;
}

function GetPlayerEmbedForXhtml(idPlayer, playerLocation, playerVars, width, height, wmode)
{
    if (wmode === undefined)
    {
        wmode = "opaque";
    }
    var htmlVars = GetFlashVars(playerVars);

    var flashParams =
    {
        allowScriptAccess: "always",
        quality:           "high",
        bgcolor:           "#ffffff",
        allowFullScreen:   "true",
        flashVars:         htmlVars
    };
    if (wmode)
        flashParams['wmode'] = wmode;

    var html = '<object type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" data="' + playerLocation + '" id="' + idPlayer + '">'
                + '<param name="movie" value="' + playerLocation + '" />';

    for(var i in flashParams)
        html += '<param name="' + i + '" value="' + flashParams[i] + '" />';

    html += '</object>';

    return html;
}

function InsertFlashMovie(idObject, movieLocation, flashVars, width, height, wmode)
{
    document.write(GetPlayerEmbed(idObject, movieLocation, flashVars, width, height, wmode));
}

function InsertPlayer(idPlayer, playerLocation, playerVars, width, height, wmode)
{
    document.write(GetPlayerEmbed(idPlayer, playerLocation, playerVars, width, height, wmode));
}

function InsertPlayerIntoDiv(divId, idPlayer, playerLocation, playerVars, width, height, wmode)
{
    var divElement = GetElement(divId);
    if (divElement)
    {
         divElement.innerHTML = GetPlayerEmbed(idPlayer, playerLocation, playerVars, width, height, wmode);

         var theOuterHTML = "" + divElement.outerHTML;
         var re = /<param name="FlashVars" value="">/ig;
         theOuterHTML = theOuterHTML.replace(re,"<param name='FlashVars' value='" + GetFlashVars(playerVars) + "'>");
         divElement.outerHTML = theOuterHTML;
    }
}

function GetFlashApp(appName)
{
    if (IsIE())
        return window[appName];
    else
        return document[appName];
}

function GetFlashVars(playerVars)
{
    var htmlVars = '';
    for (var i in playerVars)
        htmlVars += i + '=' + playerVars[i] + '&';
    htmlVars = htmlVars.substr(0, htmlVars.length - 1);

    return htmlVars;
}

