var blockPause = false;
var blockArray;
var morphArray = new Array();
var margin = 0;
var rotatorInterval = 500;
var startMove = true;
var moveRate = 20; //movement rate in pixels per second
var containerHeight;

Event.observe(window, 'load', function() { 
	 blockArray = new Array(new Array($('rank-p'),false),new Array($('rank-g'),false),new Array($('rank-s'),false));
	
	//Rotator Pause
	Event.observe($('sponsorRotatorContainer'), 'mouseenter', function(e) {
		blockPause = true;
		pauseBlocks();
	});
	Event.observe($('sponsorRotatorContainer'), 'mouseleave', function(e) {
		blockPause = -1;
	});
	
	containerHeight = $$('.rankContainer')[0].getStyle('top');
	containerHeight = containerHeight.substring(0,(containerHeight.length - 2));
		
	setInterval("rotator()", rotatorInterval);
});

	
function rotator() {
	if(blockPause === false || blockPause === -1) {
		blockArray.each(function(s, index) {
			tempHeight = s[0].getHeight();
			tempTop = s[0].getStyle('top');
			tempTop = tempTop.substring(0,(tempTop.length - 2));
			
			//is it time to start
			if(startMove || blockPause === -1) {
				startMove = false;
				
		
				//is it still at the starting position
				if(tempTop == containerHeight || blockPause === -1) {
					//calculate rate
					tempRate = ((tempTop*1) + (tempHeight*1))/moveRate;
					
					blockActive = true;
					
					if(blockPause === -1 && !s[1]) {
						blockActive = false;
					}
					
					if(blockActive) {
//console.log(index + ' -- ' + tempRate);
						morphArray[index] = new Effect.Morph(s[0], {
							style: 'top:-'+tempHeight+'px;',
							duration: tempRate,
							transition: Effect.Transitions.linear,
							afterFinish: function() {
								resetBlock(s);
							}
						});
						s[1] = true;
					}
				}
			}
			
			//is it time to tell the next block to start moving
			if(tempTop <= containerHeight - ((tempHeight*1) + (margin*1))) {
//console.log(tempTop + ' -- ' + (containerHeight - ((tempHeight*1) + (margin*1))));
				startMove = true;
			}
		});
		
		if(blockPause === -1) {
			blockPause = false;
		}
	}
}

function pauseBlocks() {
	morphArray.each(function(s) {
		s.cancel();
	});
}

function resetBlock(block) {
	block[0].setStyle({
		top: containerHeight+'px'
	});
	block[1] = false;
}
