/**
 * Cedars
 *
 * A catch-all mouseover event handler
 */

YAHOO.namespace("cedars.Mouseover");


YAHOO.cedars.Mouseover.counter = 0;

/**
 * Adds mouseover handlers to an element.
 */
YAHOO.cedars.Mouseover.add = function(element) {
    if (element.id == "") {
        element.id = 'mouseover-element-' + (++this.counter);
    }
	YAHOO.util.Event.addListener(element, "mouseover", function(e, mouseover) {
			mouseover.enter(this);
		},
		this
	);
	YAHOO.util.Event.addListener(element, "mouseout", function(e, mouseover) {
			mouseover.exit(this);
		},
		this
	);
}


YAHOO.cedars.Mouseover.imageSuffix = {};

YAHOO.cedars.Mouseover.switchImage = function(image, newSuffix) {
	var matches = image.src.match(/^(.*_)(on|off)(\..*)$/);	
	if (matches) {
		var baseName  = matches[1];
        var suffix    = matches[2];
		var extension = matches[3];
        if (this.imageSuffix[image.id] == null) {
            this.imageSuffix[image.id] = suffix;
        }
		image.src = baseName + newSuffix + extension;
	}
		
}

YAHOO.cedars.Mouseover.enter = function(image) {	
	this.switchImage(image, 'on');
}

YAHOO.cedars.Mouseover.exit = function(image) {	
    var initialSuffix = (this.imageSuffix[image.id] == null) ? 'off' : this.imageSuffix[image.id] ;
	this.switchImage(image, initialSuffix);
}
