/**
 * Automatically creates rollover actions for images of the class "autoroll." 
 * 
 * Image naming and location conventsions
 *   -The static state image can be named anything and be located anywhere (e.g.: /images/hats/pants.cactus.gif).
 *   -The dynamic/roll state image must be located in the same directory, and have the same name as the static
 *    state image except for having "_o" inserted before the file's extension (e.g.: /images/hats/pants.cactus_o.gif).
 * 
 * @author Justin Johnson <justin@fryewiles.com>, Justin@booleangate.org
 * @version 0.0.2a 20071017 JJ
 */

autoroll = {
	preload: function(img) {
		(new Image).src = img.src.replace(/(\.[^\.]*)$/, "_o$1");
	},
	
	over: function(img) {
		img.src = img.src.replace(/(\.[^\.]*)$/, "_o$1");
	},
	
	out: function(img) {
		img.src = img.src.replace(/_o(\.[^\.]*)$/, "$1");
	},
	
	behaviours: {
		'.autoroll' : function(element) {
			// Preloading is turned on by default
			if ( element.className.toLowerCase().indexOf('autoroll-nopreload') == -1 ) {
				autoroll.preload(element);
			}

			element.onmouseover = function(event) {
				try {
					autoroll.over(this);
				}
				catch (e) { delete e; }
			}
			element.onmouseout  = function(event) {
				try {
					autoroll.out(this);	
				}
				catch (e) { delete e; }
			}
		}
	}
};

Behaviour.register(autoroll.behaviours);