﻿///////////////////////////////////////////////////////////
//
// Name: Scroller.js
// Desc: Automatic scrolling of a content.
// Date: 29.8.2007
// Prog: Lukas Mitvalsky
//
// Copyright © 2007 CT-Group, a.s. & Welldone s.r.o.
///////////////////////////////////////////////////////////

// Constants/Memory - Global settings
var SCROLLER_SPEED = 0.1;                     // defines way what is to be scolled in one cycle
var SCROLLER_INTERVAL = 1;                  // defines interval among particular shifts
var SCROLLER_PAUSEINTERVAL = 1;          // defines interval between two scolling process
var SCROLLER_PAUSEBEFORERETURN = 8000;      // defines pause interval before back movement
var SCROLLER_LEAVEVISIBLED = 50;            // defines distance what must be visibled after scrolling (px)
var SCROLLER_RETURNMODE = "move";           // defines mode of backward movement (none = do nothing, move = easy back movement, skip = jump on begin)
var MINIMAL_SIZE = 500;                    // defines minimal height of div to start scrolling

// Memory - temporary storage
var scroller_actualTop = 0;
var scroller_direction = -1;
var scroller_actualRolledWay = 0;
var scroller_pause = true;
var scroller_timer = null;

//-----------------------------------------
// Secondary setting of the scroller
// Negative values are not saved
//-----------------------------------------
function scroller_Setup(speed, interval, pauseInterval, pauseBeforeReturn, leaveVisibled, returnMode)
{
    if (speed >= 0) SCROLLER_SPEED = speed;
    if (interval >= 0) SCROLLER_INTERVAL = interval;
    if (pauseInterval >= 0) SCROLLER_PAUSEINTERVAL = pauseInterval;
    if (pauseBeforeReturn >= 0) SCROLLER_PAUSEBEFORERETURN = pauseBeforeReturn;
    if (leaveVisibled >= 0) SCROLLER_LEAVEVISIBLED = leaveVisibled;
    if (returnMode != null && returnMode != "") SCROLLER_RETURNMODE = returnMode;
}

//-------------------------------
// Scrolling of a content
//-------------------------------
function scroller_Scrolling(contentId, boundaryId)
{
    var objContent = document.getElementById(contentId);
    var objBoundary = document.getElementById(boundaryId);
    
    if (!scroller_pause && objContent != null && objBoundary != null)
    {
        // Calculates new values of a content
        if (scroller_direction == -1001) { scroller_actualTop = 0; scroller_direction = 1; }
        scroller_actualTop = scroller_actualTop + (SCROLLER_SPEED * scroller_direction);
        scroller_actualRolledWay = scroller_actualRolledWay + SCROLLER_SPEED;
    
        // Gets maximal values
        //alert(objContent.offsetHeight);
        if (objContent.offsetHeight > MINIMAL_SIZE)
        {
        var maxTop = -(objContent.offsetHeight - objBoundary.offsetHeight);
        var maxRolledWay = objBoundary.offsetHeight - SCROLLER_LEAVEVISIBLED;   // maximal way what may be scrolled at a time
        //alert(maxRolledWay);
        // Values verification     
        if (scroller_direction == -1 && scroller_actualTop <= maxTop)
        {
            switch (SCROLLER_RETURNMODE)
            {
                case "none":
                    scroller_StopScrolling();
                    break;
                case "skip":
                    scroller_direction = -1001;
                    scroller_actualRolledWay = 0;
                    scroller_actualTop = maxTop;
                    scroller_PauseScrolling(SCROLLER_PAUSEBEFORERETURN);                
                    break;                    
                case "move":
                default:
                    scroller_direction = 1;
                    scroller_actualRolledWay = 0;
                    scroller_actualTop = maxTop;
                    scroller_PauseScrolling(SCROLLER_PAUSEBEFORERETURN);                
            }
        }
        if (scroller_direction == 1 && scroller_actualTop >= 0)
        {
            scroller_direction = -1;
            scroller_actualRolledWay = 0
            scroller_actualTop = 0;
            scroller_PauseScrolling(SCROLLER_PAUSEBEFORERETURN);
        }
        if (!scroller_pause && scroller_actualRolledWay >= maxRolledWay)
        {
            scroller_actualRolledWay = 0;
            scroller_PauseScrolling(SCROLLER_PAUSEINTERVAL);
        }           
        
        // Sets new top value
        objContent.style.top = objBoundary.style.top + scroller_actualTop + "px";
        }
        else scroller_StopScrolling();
    }
}

//------------------------------------------------------
// Start of content scrolling.
// This must be called inside onload event of body tag 
// on your webpage.
//------------------------------------------------------
function scroller_StartScrolling(contentId, boundaryId)
{
    /* alert("ahoj"); */
    scroller_actualTop = 0;
    scroller_direction = -1;
    scroller_actualRolledWay = 999999;
    scroller_pause = false;
    scroller_timer = setInterval('scroller_Scrolling("' + contentId + '","' + boundaryId + '")', SCROLLER_INTERVAL);
}
 
//-----------------------------------------
// Stops content scrolling
//-----------------------------------------
function scroller_PauseScrolling(interval)
{
    scroller_pause = true;
    setTimeout('scroller_ResumeScrolling()', interval);
}

//-----------------------------------------
// Continues in content scrolling
//-----------------------------------------
function scroller_ResumeScrolling()
{
    scroller_pause = false;
}

//-----------------------------------------
// End of scrolling permanently
//-----------------------------------------
function scroller_StopScrolling()
{
    clearTimeout(scroller_timer);
}

//-----------------------------------------
// End of scrolling permanently
//-----------------------------------------
function scroller_StopScrollingReset()
{
    scroller_ResumeScrolling()
    clearTimeout(scroller_timer);
    var objContent = document.getElementById('pnlNewsContent');
    objContent.style.top = "0px";
}