﻿<!--
//
// Client notice JS.
//
var _cn_noticeIndex = 1;

function _cn_Init()
{
    $('headNotice').style.display = "";
    $('_cn_Next').style.display = (_cn_noticeIds.length > 1) ? "" : "none";
}

function _cn_AdvanceNotice(dir)
{
    var stats = _cn_NoticeStats();
    var minIndex = stats[0];
    var maxIndex = stats[1];
    var undismissedCount = stats[2];
    var newIndex;
    
    if(dir > 0)
    {
        //
        // Find the next undismissed notice.
        //
        var leaps = 1;
        for(d=_cn_noticeIndex;d<_cn_noticeIds.length;d++)
        {
            if(!_cn_noticeIds[d].Dismissed)
            {
                break;
            }
            leaps = leaps + 1;
        }
        newIndex = _cn_noticeIndex + leaps;
    }
    else
    {
        //
        // Find the previous undismissed notice.
        //
        var leaps = 1;
        for(d=_cn_noticeIndex-2;d>=0;d--)
        {
            if(!_cn_noticeIds[d].Dismissed)
            {
                break;
            }
            leaps = leaps + 1;
        }
        newIndex = _cn_noticeIndex - leaps;
    }
    
    //
    // Should we show the prev/next controls?
    //
    $('_cn_Previous').style.display = (newIndex > minIndex) ? "" : "none";
    $('_cn_Next').style.display = (newIndex < maxIndex) ? "" : "none";
            
    $(_cn_JSPrefix+"notice_"+_cn_noticeIndex).hide();
    $(_cn_JSPrefix+"notice_"+(newIndex)).show();
    $('currentNotice').innerHTML = newIndex;
    //$(_cn_JSPrefix+'noticeCount').innerHTML = undismissedCount;
    
    _cn_noticeIndex = newIndex;
}

function _cn_AcknowledgeNotice()
{
    //
    // Ackowledge the notice.
    //
    var notice = _cn_noticeIds[_cn_noticeIndex-1];
    notice.Dismissed = true;
    AsyncSessionManager.AcknowledgeNotice(notice.Id,notice.PermanentlyAcknowledged,_cn_NOOP);

    //
    // If this is the only and/or final notice, hide the entire notice div, else go to the next notice.
    //
    var stats = _cn_NoticeStats();
    if(stats[2] == 0)
    {
        new Effect.Fade("headNoticeInner", {from: 1.0,to: 0.0});
    }
    else
    {        
        //
        // If there is an undismissed notice after this one, progress to it. Otherwise move to the previous notice.
        //
        if(_cn_noticeIndex < stats[1])
        {
            _cn_AdvanceNotice(1);
        }
        else
        {
            _cn_AdvanceNotice(-1);
        }
    }
}

function _cn_NoticeStats()
{
    var minIndex = 0;
    var maxIndex = 0;
    var undismissedCount = 0;    
    for(i=0;i<_cn_noticeIds.length;i++)
    {
        if(!_cn_noticeIds[i].Dismissed)
        {
            if(minIndex == 0)
                minIndex = i+1;
                
            maxIndex = i+1;
            undismissedCount = undismissedCount + 1;
        }
    }
    
    return [minIndex,maxIndex,undismissedCount];
}

function _cn_NOOP()
{
    // NOOP.
}

-->
