var Rotating = { 
	init: function(blockId, blockSize, root_url) {
		var block = document.getElementById(blockId);
		var image = new Image();
		image.src = root_url + "/" + blockId + ".jpg";
		var height = image.height;
		var count = height / blockSize;
		
		block.hash = {};
		block.hash["blockSize"] = blockSize;
		block.hash["count"] = count;
		block.hash["current"] = -1;
		
		block.rotate = Rotating.rotate;
		
		block.rotate();
		Rotating.hookRotate(block);
	},
	
	rotate: function() {
		var current = this.hash["current"];
		var blockSize = this.hash["blockSize"];
		var count = this.hash["count"];
		var number;
		
		while (true)
		{		
			number = Math.round(Math.random() * (count - 1));
			if (number != current)
			{
				break;
			}
		}
		
		this.hash["current"] = number;
		
		this.style.backgroundPosition = "0px " + number * blockSize + "px";
		Rotating.hookRotate(this);
	},
	
	hookRotate: function(block) {
		var seconds = 3; //Math.round(Math.random() * 4) + 1;
		setTimeout("Rotating._rotate('" + block.id + "')", seconds * 1000);
	},
	
	_rotate: function(id) {
		var block = document.getElementById(id);
		block.rotate();
	}
};


/* -----------------------------------------------------------------------------------------*/

/* Generic attach event listener (from http://www.alistapart.com/articles/popuplinks/) */
function attach(event, elem, func) {
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event, func, false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

/* Used by generic attach event listener (from http://www.alistapart.com/articles/popuplinks/) */
function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() 
    	{ 
    		if (window.event != null)
    			window.event.returnValue = false; 
    	};
    return this;
}

/* -----------------------------------------------------------------------------------------*/

