﻿//========== Global vars
var _errMsg_Ajax = "";
var _langCode = "";
var _linksLastUpdated = "0";
var _noticeLastUpdated = "0";
var _pollId = "0";
var _pollLinks = false;
var _questionPollDisplay = "";
var _relatedLinksTitle = "";

//========== Functions

function CheckForPageReload()
{
    if (IsLightboxOpen() == false)
    {
        $.ajax({
            type: "POST",
            url: "Default.aspx/CheckForPageReload",
            data: "{'siteLangID':'" + _siteLanguageID.toString() + "','lowRes':'" + document.PageForm.LowRes.value + "','clipURL':'" + document.PageForm.ClipURL.value + "','rtmpURL':'" + document.PageForm.RTMPURL.value + "','slideURL':'" + document.getElementById("SlideURL").src + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success:
                function(msg)
                {
                    if (msg.d)
                    {
                        window.location.reload();
                    }
                    else
                    {
                        CheckForPageReloadRecall();
                    }
                },
            error:
                function(msg)
                {
                    CheckForPageReloadRecall();
                }
        });
    }
    else
    {
        CheckForPageReloadRecall();
    }
}

function CheckForPageReloadRecall()
{
    setTimeout(CheckForPageReload, 60000);
}

function GetMessages()
{
    if (IsLightboxOpen() == false)
    {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetMessages",
            data: "{'siteLangID':'" + _siteLanguageID.toString() + "','lastUpdatedForNotices':'" + _noticeLastUpdated.toString() + "','lastUpdatedForLinks':'" + _linksLastUpdated.toString() + "','pollLinks':'" + _pollLinks.toString() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success:
                function(msg)
                {
                    GetMessagesReturn(msg.d);
                    GetMessagesRecall();
                },
            error:
                function(msg)
                {
                    GetMessagesRecall();
                }
        });
    }
    else
    {
        GetMessagesRecall();
    }
}

function GetMessagesRecall()
{
    setTimeout(GetMessages, 15000);
}

function GetMessagesReturn(responseText)
{    
    if (responseText.length > 0)
    {
        if (responseText != "-1" || responseText != "0")
        {
            var retArray = responseText.split(_cecDelim);
        
            if (retArray.length == 6)
            {
                var html = "";
                var linksHtml = retArray[4].toString();
                var pollLinksHtml = retArray[5].toString();
                
                _pollLinks = (pollLinksHtml.length > 0);
                _noticeLastUpdated = retArray[1].toString();
                html = retArray[2].toString();
                _linksLastUpdated = retArray[3].toString();
                if (linksHtml.length > 0 || _pollLinks)
                {
                    html += _relatedLinksTitle;
                    html += linksHtml + pollLinksHtml;
                }
                if (IsLightboxOpen() == false)
                {
                    document.getElementById("Messages").innerHTML = html;
                    if (_pollLinks)
                    {
                        tb_init('a.thickbox, area.thickbox, input.thickbox');
	                    imgLoader = new Image();
	                    imgLoader.src = tb_pathToImage;
                    }
                }                
            }
        }
    }
}

function GetPollQuestion()
{
    if (IsLightboxOpen() == false)
    {
        $.ajax({
            type: "POST",
            url: "Default.aspx/IsPollAvailable",
            data: "{'savedPollIDList':'" + CookieGet(_SUBMITTEDPOLLS_COOKIE_NAME) + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success:
                function(msg)
                {
                    GetPollQuestionReturn(msg.d.toString())
                    GetPollQuestionRecall();
                },
            error:
                function(msg)
                {
                    GetPollQuestionRecall();
                }
        });
    }
    else
    {
        GetPollQuestionRecall();
    }
}

function GetPollQuestionRecall()
{
    setTimeout(GetPollQuestion, 30000);
}

function GetPollQuestionReturn(retPollId)
{
    var display = "question";

    if (retPollId.length > 0)
    {
        //Value returned from server
        if (retPollId != "0")
        {
            //There is an active poll that the user hasn't responded to
            if (retPollId != _pollId)
            {
                //Poll isn't yet loaded so set it up for display
                _pollId = retPollId;
                display = "poll";
            }
            else
            {
                display = "";
            }
        }
    }
    SetPollQuestionDiv(display);
}
	
function IniForm()
{
    //Load global vars
    _errMsg_Ajax = document.PageForm.AjaxErrorMessage.value;
    _langCode = document.PageForm.LanguageCode.value;
    _relatedLinksTitle = "<div class=\"RelatedLinksTitle\">" + document.PageForm.RelatedLinksTitle.value + "</div>"
    _siteLanguageID = document.PageForm.SiteLanguageID.value;
    //Load Flow Player
    flowplayer
        (
            'RTMPPlayer',
            {
                src: 'flowplayer/flowplayer-3.2.1.swf',
                wmode: 'opaque'
            },
            {
                clip:
                {
                    url: document.PageForm.ClipURL.value,
                    provider: 'rtmp',
                    scaling: "scale",
                    wmode: 'transparent'
                },
                plugins:
                {
                    rtmp:
                    {
                        url: 'flowplayer/flowplayer.rtmp-3.2.0.swf',
                        netConnectionUrl: document.PageForm.RTMPURL.value
                    }
                }
            }
        );
    //Load messages and poll/question sections    
    GetMessages();
    GetPollQuestion();
    //Setup reload check
    CheckForPageReloadRecall();
}

function IsLightboxOpen()
{
    return !document.PageForm.AjaxErrorMessage;
}

function NavLang(siteLangID)
{
    if (siteLangID.toString() != _siteLanguageID)
    {
        location.href = "Default.aspx?SiteLanguageID=" + siteLangID.toString() + "&LowRes=" + document.getElementById("LowRes").value;
    }
}

function SetPollQuestionDiv(display)
{
    if (IsLightboxOpen() == false)
    {
        if (display.length == 0 && _questionPollDisplay.length == 0)    
        {
            display = "question"
        }
        if (display == "poll")
        {
            document.getElementById("PollQuestion").src = "SubmitPoll.aspx?SiteLanguageID=" + _siteLanguageID.toString() + "&PollID=" + _pollId;
        }
        else if (display == "question" && _questionPollDisplay != "question")
        {
            document.getElementById("PollQuestion").src = "SubmitQuestion.aspx?SiteLanguageID=" + _siteLanguageID.toString();
        }
        if (display.length > 0)
        {
            _questionPollDisplay = display;
        }
    }
}

function SwitchBand()
{
    var lowRes = "1";
    
    if (document.getElementById("LowRes").value == "1")
    {
        lowRes = "0";
    }
    location.href = "Default.aspx?SiteLanguageID=" + _siteLanguageID + "&LowRes=" + lowRes;
}

//========== Statements
AddLoadEvent(IniForm);
