//Global vars
var lastBlock;
var rollingBlock;
var offsetLeft;
var offsetTop;
var rollingHeight;
var bottomLimit;
var footerHeight;
var footerTop;
var diffTop=0;
var increment;

//Move the last box
function makeMeMove(){
    rollingBlock.css({'position':'fixed','top':diffTop+increment+'px','left':offsetLeft+'px'});
}

//Stop the box deppending on its location (reached top or bottom)
function stopMe(spot){
    if(spot=='bottom'){
        rollingBlock.css({'position':'absolute','top':bottomLimit+'px','left':'0px'});
    }
    else rollingBlock.css({'position':'relative','top':'0px','left':'0px'});
}

function initRollingBlock(){
    increment=(typeof(floatMove)!="undefined")?260:0;
    //Works only under IE7 and above
    if(!($.browser.msie && $.browser.version<=6) && $("#lastColItem").length>0){
        //Selecting the last block on this column
        lastBlock=$("#lastColItem");
        rollingBlock=$("#rollingBlock");
        //lastBlock.height(rollingBlock.height());
        
        lastBlock.height(rollingBlock.height());
        lastBlock.css({'position':'relative'});
        rollingBlock.css({'width':'300px'});
        
        
        //Original Box position
        offsetLeft=lastBlock.offset().left;
        offsetTop=lastBlock.offset().top;
        rollingHeight=lastBlock.height();
        
		if($('#leaderbard').length){
	        footerTop=$('#leaderbard').offset().top;
	        footerHeight=$('#leaderbard').height();			
		}
        
        //Limit of the column's bottom
        rollingBottomLimit=(footerTop+footerHeight-rollingHeight)-diffTop-Math.abs(footerHeight-rollingHeight);
        
        //Deppending on the scroll's top, it does different things - Moves, limits the bottom scroll and limit the top scroll
        $(window).scroll(function(){
            if($(window).scrollTop()>=offsetTop-diffTop && $(window).scrollTop()<=rollingBottomLimit){
                makeMeMove();
            }else if($(window).scrollTop()>=rollingBottomLimit){
                stopMe('bottom');
            }else{
                stopMe('top');
            }
        });
    }
}

//On load
// $(document).ready(function() {
// 	// Stuff to do as soon as the DOM is ready;
// 	initRollingBlock(); 
// });

