﻿/// <reference path="~/Content/js/lib/jquery/jquery.min.js" />

$(document).ready(function() {
    if (!presentation_rendered) {
        // If they're not logged in, do nothing; if they are logged
        // in, but the event hasn't started yet, use Ajax to check
        // for the event start.
        if (!$(".v_watch .not_logged_in").size() && !$(".v_watch .unauthorized_for_event").size()) {
            checkEventStart();
        }
    }
    else {
        if (singleLoginEnabled == 'True' && loginRequired == 'True') {
            userTimeout(); 
        }
    }
});

function checkEventStart()
{
    if (type == "Live")
    {
        $.post(siteRoot + "/" + communityId + "/Ajax/EventStarted.aspx", { id: presentation_id }, 
            function(data) {
                if (data == "True")
                    startEvent();
                else
                {
                    $(".not_yet_started").show();
                    setTimeout(
                        function() {
                            checkEventStart();
                        }, 5000);
                }
            });
    }
    else
    {
        startEvent();
    }
}

function startEvent()
{
    var rand = (new Date()).getTime();
    $(".text_container").hide();
    $(".iframe_container").show();
    
    var randStr = "?";
    if (window.location.href.indexOf("?") != -1) randStr = "&";
    
    window.location.href = window.location.href + randStr + "rand=" + rand;
}

function userTimeout() {
    setTimeout("checkUser()", 30000);
}

function checkUser() {
    // Make a request to see if the user is still valid.
    var rand = Math.floor(Math.random() * 10001);
    $.get(siteRoot + "/" + communityId + "/Ajax/CheckIfUserValid.aspx?rand=" + rand, function(data) {
        // If they're not valid, re-direct them to the site root.
        if (data.toLowerCase() == "false") {
            //alert(siteRoot + "/" + communityId + "/Redirector/Unauthorized.aspx");
            top.location.href = siteRoot + "/" + communityId + "/Redirector/Unauthorized.aspx";
        }

        userTimeout();
    });
}


