// Make the last banner sticky (don't let it scroll up past the top of the window)
// addEvent function by John Resig:
// http://ejohn.org/projects/flexible-javascript-events/url

function addEvent( obj, type, fn )
{
    if (obj.attachEvent)
    {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function()
        {
            obj['e' + type + fn](window.event);
        }
        obj.attachEvent('on' + type, obj[type + fn]);
    }
    else
    {
        obj.addEventListener(type, fn, false);
    }
}

var grabbed = false;
var cumuStatic = $('last_extra_right_banner').cumulativeOffset();
addEvent(window, 'scroll', function(event)
{
    var coordinates = $('last_extra_right_banner').viewportOffset();
    if (grabbed)
    {
        // test if time to release:
        if(coordinates[1] > -cumuStatic[1])
        {
            // release
            $('last_extra_right_banner').setStyle({position: 'static'});
            grabbed = false;
        }
    }
    else
    {
        // not grabbed:
        // test if time to grab:
        if (coordinates[1] < 0 )
        {
            // grab
            $('last_extra_right_banner').setStyle({ position: 'fixed', top: '5px'});
            grabbed = true;
        }
    }
} );
