var scrollingInterval;
var scrollingLayer;
var scrollingSpeed;
var scrollLimit;

function performScroll()
{
	scrollY = parseInt(scrollingLayer.style.top);
	if (isNaN(scrollY))
		scrollY = 0;
	newY = parseInt(scrollingLayer.offsetTop) + scrollingSpeed;
	
	if (scrollingSpeed > 0 && newY > scrollLimit)
		newY = scrollLimit;
	if (scrollingSpeed < 0 && newY < scrollLimit)
		newY = scrollLimit;
	
	scrollingLayer.style.top = newY+"px";
	setCookie("navScroll", newY);
}

function recallScroll()
{
	var scrollY = readCookie("navScroll");
	if (scrollY != null)
	{
		scrollingLayer.style.top = scrollY+"px";
	}
}

function scrollLayer(layerID,speed,limit)
{
	scrollingLayer = getElement(layerID);
	if (!scrollingLayer)
		return;
	scrollingSpeed = speed;
	scrollLimit = limit;
	performScroll();
	scrollingInterval = window.setInterval("performScroll()",100);
}

function haltScroll()
{
	window.clearInterval(scrollingInterval);
}

/**
 * Add a new cookie
 *
 * @param string    The name of the cookie
 * @param string    The value of the cookie
 * @param mixed     A pre-set date object or an integer representing the number of days. Default value creates a 'session' cookie.
 * @param string    The domain access of the cookie. Defaults to the current domain.
 * @param string    The path access of the cookie. Defaults to the entire site, "/".
 */
function setCookie(name, value, expires, domain, path)
{
    //------------------------------
    // Build expiry
    //------------------------------
    // Date object
    if (expires)
    {
    	if(expires.toGMTString)
        	expires = '; expires=' + expires.toGMTString();
	}

    // Integer, assume its number of days
    else if(typeof(expires) == 'integer')
    {
        var date = new Date();
		date.setTime( date.getTime() + (expires * 86400) );
		expires = '; expires=' + date.toGMTString();
    }

    // Session cookie
    else
        expires = '';


    //------------------------------
    // Build domain
    //------------------------------
    if(domain)
        domain = '; domain=' + domain;
    else
        domain = '';


    //------------------------------
    // Build path
    //------------------------------
    if(path)
        path = '; path=' + path;
    else
        path = '; path=/';


    //------------------------------
    // Make cookie
    //------------------------------
    document.cookie = name + '=' + escape(value) + expires + domain + path;
}



/**
 * Read the value of a cookie.
 *
 * @param string The name of the cookie you want to gain the value of
 *
 * @return mixed The value of the cookie if it was found, and null if it was not
 */
function readCookie(name)
{
    var doc_cookies = document.cookie.split(';');
    search = name + '=';

    for(var i = 0; i < doc_cookies.length; i++)
    {
        cookie = doc_cookies[i];

        if(cookie.indexOf(search) != -1)
            return unescape(cookie.substring(search.length));
    }

    return null;
}

