function writePopup(flashfile, width, height, description, previewimg, flashPlayerVersion, doFlashCheck, doToggle)
{
    $(".jwbox_content").html(' ');

    var addFlashAlert = false;
    var embedString = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='" + width + "' height='" + height + "' id='single1' name='single1'>";
    embedString += "<param name='movie' value='player-licensed.swf'>";
    embedString += "<param name='allowfullscreen' value='true'>";
    embedString += "<param name='allowscriptaccess' value='always'>";
    embedString += "<param name='wmode' value='transparent'>";
    embedString += "<param name='flashvars' value='autostart=true&file=" + flashfile + "&streamer=rtmp://s2wufme8act55f.cloudfront.net/cfx/st&provider=rtmp'>";
    //embedString += "<param name='flashvars' value='autostart=true&file=https://s3.amazonaws.com/videos.justaddwater.tv/" + flashfile + "&provider=http'>";
    //embedString += "<param name='flashvars' value='autostart=true&file=http://d3t94h5y8mv1fa.cloudfront.net/" + flashfile + "&provider=http'>";
    embedString += "<embed id='single2' name='single2' src='player-licensed.swf' width='" + width + "' height='" + height + "' bgcolor='#2e2e2e' allowscriptaccess='always' allowfullscreen='true' flashvars='autostart=true&file=" + flashfile + "&streamer=rtmp://s2wufme8act55f.cloudfront.net/cfx/st&provider=rtmp'/>";
    //embedString += "<embed id='single2' name='single2' src='player-licensed.swf' width='" + width + "' height='" + height + "' bgcolor='#2e2e2e' allowscriptaccess='always' allowfullscreen='true' flashvars='autostart=true&file=https://s3.amazonaws.com/videos.justaddwater.tv/" + flashfile + "&provider=http'/>";
    embedString += "</object>";
    embedString += "<div id='video-description'><p>" + description + "</p></div>";

    if (doFlashCheck == undefined || doFlashCheck == true) {
        description = description.replace(/\'\'/g, '\"').replace(/"/g, '\\"');
        if (flashPlayerVersion < 10.2) {
            embedString = "<div style='width:" + width + "px;height:" + (height + 44) + "px;'>" + 
            "<div id=\"flashText\" style=\"width:600px;margin:0px auto;padding-top:141px;\">" +
            "<div style=\"font-size:24px;color:#CCCCCC;\">YOU SHOULD UPDATE YOUR <br /><span style=\"color:#707070;\">ADOBE FLASH PLAYER</span><span style=\"color:#000000;\"> RIGHT NOW</span></div>" +
            "<div style=\"margin-top:" + (height * .15) + "px;\">" +
            "<div style=\"font-size:14px;color:#707070;float:left;width:290px;\">Your browser is up to date, but your<br /> current version of Flash may not<br /> deliver " +
            "the videos to you as well as<br /> we want them to. It will only take a<br /> minute to install the <a id=\"GetPlayerLink\" style=\"color:#0597FF;\" class=\"jwlink\">free update</a>.</div>" +
            "<div style=\"font-size:24px;color:#CCCCCC;float:left;width:40px;padding:20px 35px 0px 25px;\">OR</div>" +
            "<div style=\"font-size:14px;color:#707070;float:left;width:160px;\">To continue to the video<br /> without the update,<br /> please " +
            "click <a style=\"color:#0597FF;\" onclick='javascript:writePopup(\"" + flashfile + "\"," + width + "," + height + ",\"" + description + "\",\"" + previewimg + "\"," + flashPlayerVersion + "," + false + "," + false + "); return false;'>here</a>.</div>" +
            "<div style=\"clear:left;\"></div>" +
            "<div style=\"text-align:right;color:#0597FF;position:absolute;top:" + (height + 64) + "px;width:80%;  display:none;\">Download this video</div>" +
            "</div></div></div>";
            addFlashAlert = true;
        }
    }

    $(".jwbox_content").html(embedString);

    if (doToggle == undefined || doToggle != false)
        $.jwbox.toggle(".jwbox");
    else {
        //call toggle code that would start the player
        $.jwbox.lightbox = $(".jwbox_hidden", ".jwbox");
        $.jwbox.lightbox.css("display", "block")
        $.jwbox.center();
        $("object", ".jwbox").each(function () {
            $.jwbox.player = document.getElementById(this.id);
        });      
    }

    if (addFlashAlert)
        $("#GetPlayerLink").click(function () { $.jwbox.toggle(".jwbox"); window.open("http://www.macromedia.com/go/getflashplayer"); return false; });
}

jQuery.fn.center = function () {
    this.css("position", "fixed");
	this.css("top", ( $(window).height() - this.outerHeight() ) / 2 + "px");
	this.css("left", ( $(window).width() - this.outerWidth() ) / 2 + "px");
    return this;
}

jQuery.jwbox = {
    lightbox: null,
    player: null,
    toggle: function (context) {
        if (!$.jwbox.lightbox) {
            $.jwbox.lightbox = $(".jwbox_hidden", context);
            $.jwbox.center();
            $("#jwbox_background").fadeIn("fast");
            $.jwbox.lightbox.css("display", "block")
            $.jwbox.center();
            $("#jwbox_background").fadeTo(0, 0.8);
            $("object", context).each(function () {
                $.jwbox.player = document.getElementById(this.id);
            });
        } else if ((context.className == 'jwbox_content')) {
        } else {
            try {
                $.jwbox.player.sendEvent("STOP");
                $.jwbox.player = null;
            } catch (err) {
            }
            $.jwbox.lightbox.css("display", "none");
            $.jwbox.lightbox = null;
            $("#jwbox_background").fadeOut("fast");
        }
    },
    center: function () {
        if ($.jwbox.lightbox) {
            $.jwbox.lightbox.center();
        }
    }
}

$(document).keyup(function(event){
    if (event.keyCode == 27 && $.jwbox.lightbox) {
		$.jwbox.toggle($("#jwbox_background"));
    }
});


$(document).ready(function () {
    $("body").append('<div id="jwbox_background">&nbsp;</div>');
    $(".jwbox").click(function () { $.jwbox.toggle(this); return false; });
    $("#jwbox_background").click(function () { $.jwbox.toggle(this); return false; });
    $(".jwbox_content").click(function () { $.jwbox.toggle(this); return false; });
    $(window).resize(function () { $.jwbox.center(); });
});
