﻿/// <reference path="~/Content/js/lib/jquery/jquery.min.js" />

var id = "";
var FLV_PERCENT = 90;
var eventId = 0;

$(document).ready(function() {
    $(".publishButton").click(function () 
    {
        $("#publish_finished_area").hide();
        $("#progress").progressbar({ value: 0 });
        setProgress(5);
        $(".c_publisher .lightbox .floating_window").showFloater(0.8, true, null, null, true);
        eventId = $(this).attr("href").substring(1);
        getIdentifier();
    });
});

function getIdentifier()
{
    $.ajax({
            type: "GET",
            url: siteRoot + "/" + communityId + "/Admin/Live/GetArchiveIdentifier.aspx",
            data: "id=" + eventId,
            success: function(msg)
            {
                id = msg;
                publishFLV(0);
            }
        });
}

function publishFLV(bytes)
{
    $.ajax({
            type: "GET",
            url: siteRoot + "/" + communityId + "/Admin/Live/PublishFLV.aspx",
            data: "id=" + eventId + "&marker=" + bytes + "&directoryIdentifier=" + id,
            success: function(msg)
            {
                var responseArray = msg.split(" ");
                var bytesDone = responseArray[0];
                var bytesTotal = responseArray[1];
                
                if (bytesDone == "-1")
                {
                    setProgress(FLV_PERCENT);
                    publish();
                    return;
                }
                
                setProgress((bytesDone / bytesTotal) * FLV_PERCENT);
                publishFLV(bytesDone);
            }
        });
}

function publish()
{
    $.ajax({
            type: "GET",
            url: siteRoot + "/" + communityId + "/Admin/Live/Publish.aspx",
            data: "id=" + eventId + "&directoryIdentifier=" + id,
            success: function(msg)
            {
                var archiveId = msg;
                
                $("#publish_edit").attr("href", siteRoot + "/" + communityId + "/Admin/Archived/edit.aspx?id=" + archiveId);
                $("#publish_watch").attr("href", siteRoot + "/" + communityId + "/Watch/" + archiveId + ".aspx");
                
                
                $("#progress_info").text("The presentation has published successfully.");
                $("#publish_finished_area").show(300);
                setProgress(100);
            }
        });
}

function setProgress(progress)
{
    $('#progress').progressbar('option', 'value', progress);
}
