//////////////////////// initialize page timeout ////////////////////////////
var timeoutControlTimer;
var timeoutCountdownSeconds;

function setPageTimeout()
{
    if( timeoutControlTimer ) clearTimeout( timeoutControlTimer );
    timeoutControlTimer = setTimeout("showTimeoutWarning()", 1000*TIMEOUT_WARNING_START_SECONDS );
    var timerPanel = document.getElementById(TimeoutPanel);
    if( timerPanel ) timerPanel.style.visibility = 'hidden';
}

function showTimeoutWarning()
{
    clearTimeout( timeoutControlTimer );
    var timerPanel = document.getElementById(TimeoutPanel);
    if( timerPanel )
    {
        timerPanel.onmousedown = startMovePanel;
        //if the browser is IE4+
        timerPanel.onselectstart = disableSelect;
        //if the browser is NS6
        if (window.sidebar)
        {
            timerPanel.onmousedown = disableSelect;
            timerPanel.onclick = reEnableSelect;
        }
        timerPanel.onmouseup = stopMovePanel;
        timerPanel.style.top = TimeoutPanelPositionTop;
        timerPanel.style.left = TimeoutPanelPositionLeft;
        timerPanel.style.visibility = 'visible';
    }
    timeoutCountdownSeconds = COUNTDOWN_SECONDS;
    display();
}

//////////////////////// measure countdown and expire the page /////////////
var windowWithWarningGotFocus = false;
function display()
{ 
    if( timeoutCountdownSeconds >= 0 )
    {
        var timerDisplay = document.getElementById(PageTimeoutCouter);
        if( timerDisplay ) timerDisplay.innerHTML = FormatTime( timeoutCountdownSeconds );
        timeoutCountdownSeconds--;
        timeoutControlTimer = setTimeout( "display()", 1000 );
        if ( !windowWithWarningGotFocus ){ windowWithWarningGotFocus = true; window.focus(); }
    }
    else
    {
        clearTimeout( timeoutControlTimer );
        // hide the warning panel and show the expired panel
        var timerPanel = document.getElementById(TimeoutPanel);
        if( timerPanel ) timerPanel.style.visibility = 'hidden';
        // cover the whole screen and position the internal panel over the warning
        var timedoutPanel = document.getElementById(PageTimedOutBackground);
        //alert(timedoutPanel);
        if( timedoutPanel )
        {
            timedoutPanel.style.visibility = 'visible';
            timedoutPanel.style.width = window.screen.width;
            var docHeigth = document.body.clientHeight;
            var winHeigth = window.screen.height;
            timedoutPanel.style.height = Math.max( winHeigth, docHeigth );
        }
        var internalPanel = document.getElementById(PageTimedOutPanel);
        //alert(internalPanel);
        if( internalPanel )
        {
            if( timerPanel )
            {
                internalPanel.style.width = timerPanel.style.width;
                if (internalPanel.style.height < timerPanel.style.height) internalPanel.style.height = timerPanel.style.height;
                internalPanel.style.top = timerPanel.style.top;
                internalPanel.style.left = timerPanel.style.left;
            }
            internalPanel.style.visibility = 'visible';
        }
        else alert( EXPIRED_MSGBOX_MESSAGE );
        // grab all clicks from the whole screen in case the PageTimedOutBackground does not cover everything
        // todo: soften this after inplementing the 2nd life!
        document.onmousedown = LoginAgain;
        document.body.onnkeydown = LoginAgain;
        //if (element.captureEvents) element.captureEvents(Event.CLICK);
    }
}

function FormatTime( sec )
{
    switch( CountdownFormat )
    {   case 1:
            return String( Math.floor( sec / 60 ) + ":" + PadNumberLeft( sec % 60 ));
        case 2:
            return PadNumberLeft( sec );
        default: // incl default 0
            return sec.toString();
    }
}
function PadNumberLeft( num )
{
    if( num < 10 ) { return "0" + num; }
    else { return num.toString(); }
}
//////////////////////// enable moving timeout warning panel ////////////////
var bMovePanel = false;
var mousePositionInPanelX;
var mousePositionInPanelY;
function startMovePanel()
{
    bMovePanel = true; 
    mousePositionInPanelX = window.event.offsetX;
    mousePositionInPanelY = window.event.offsetY; 
}
function movePanel( me )
{
    if( bMovePanel ) 
    {
        me.style.top = window.event.y - mousePositionInPanelY;
        me.style.left = window.event.x - mousePositionInPanelX;
    }
}
function stopMovePanel() { bMovePanel = false; }

function disableSelect(e) { return false; }

function reEnableSelect(){ return true; }

//////////////////////// postback without changing the page /////////////
function RequestMoreTime( me )
{
    SetPageTimeout();
    // hide the warning
    var timerPanel = document.getElementById(TimeoutPanel);
    timerPanel.style.visibility = 'hidden';
}

//////////////////////// return to the login page /////////////
function LoginAgain(e)
{
    // honor the copyright link
    var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
	if( targ.id != 'ExpiredPanelLink' )
	{
	    if( RedirectUrl != '' ) window.location = RedirectUrl;
	    else window.document.forms[0].submit();
	}
}