﻿$(function () {
    var timer;
    var _width = "480";
    var _height = "296";
    var _timeout = "300000";
    var _continuemessage = "Please click here to continue stream";

    if ($("#continuemessage").index() != -1) {
        _continuemessage = $("#continuemessage").val();
    }

    if ($("#streamtimeout").index() != -1) {
        _timeout = $("#streamtimeout").val();
    }

    if ($("#streamcontainer").index() != -1) {
        _width = $("#streamcontainer").width();
        _height = $("#streamcontainer").height();

        loadVideoWithTimout("7012724");
    }

    $(".streamlink").click(function () {
        loadVideoWithTimout($(this).attr("id"));
    });

    $("#continuevideo").live("click", function () {
        loadVideoWithTimout($(this).find("input").val());
    });



    function loadVideoWithTimout(id) {
        clearTimeout(timer);
        $("#streamcontainer").html(videoCode(id, "true"));
        $("#continuevideo").slideUp();
        $("#continuevideo").remove();
        timer = setTimeout(function () {
            promptContinueVideo(id)
        }, _timeout);

    }

    function promptContinueVideo(id) {
        $("#streamcontainer").html("<div class=\"continuevideo\" style=\"width:" + _width + "px;height:" + _height + "px;\" id=\"continuevideo\"><span class=\"continuevideotext\" >" + _continuemessage + "</span><input type=\"hidden\" value=\"" + id + "\"/></div>");
    }

    function videoCode(id, autoplay) {
        width = _width;
        height = _height;

        var videocode = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"" + width + "\" height=\"" + height + "\" id=\"playerobject\">\n";
        videocode += "<param name=\"flashvars\" value=\"autoplay=" + autoplay + "&amp;brand=embed&amp;cid=" + id + "&amp;v3=1\"/>\n";
        videocode += "<param name=\"allowfullscreen\" value=\"true\"/>\n";
        videocode += "<param name=\"allowscriptaccess\" value=\"always\"/>\n";
        videocode += "<param name=\"movie\" value=\"http://www.ustream.tv/flash/viewer.swf\"/>\n";
        videocode += "<embed flashvars=\"autoplay=" + autoplay + "&amp;brand=embed&amp;cid=" + id + "&amp;v3=1\" \n";
        videocode += "width=\"" + width + "\" height=\"" + height + "\" \n";
        videocode += "allowfullscreen=\"true\" \n";
        videocode += "allowscriptaccess=\"always\" \n";
        videocode += "id=\"playerobject\" \n";
        videocode += "name=\"playerobject\" \n";
        videocode += "src=\"http://www.ustream.tv/flash/viewer.swf\" \n";
        videocode += "type=\"application/x-shockwave-flash\" />\n";
        videocode += "</object>\n";
        return videocode;
    }
});
