/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Image Swap / Rollover Controll
 *
 *    @version rev010.2006-03-01
 *    @requires common.js
 */
/* -------------------------------------------------------------------------- */


/* -------------------- Settings for BARolloverAutoSetup -------------------- */

var BAROLLOVER_AUTOSETUP_ENABLED      = true;
var BAROLLOVER_AUTOSETUP_TARGET_CNAME = 'rollover';
var BAROLLOVER_AUTOSETUP_EXCEPT_CNAME = 'norollover';
var BAROLLOVER_AUTOSETUP_STATUS_SET   = { 'normal' : 'n', 'hover' : 'o' };



/* -------------------- Constructor : BARolloverImage() -------------------- */

function BARolloverImage(node, statusSet) {
	this.node      = node;
	this.statusSet = statusSet;
	this.status    = 'default';
	this.suffixPtn = /\.(jpe?g|gif|png)$/i;
	this.images   = {};

	this.init();
}

BARolloverImage.prototype = {
	init : function () {
		var statusPtn = '';
		for (var i in this.statusSet) {
			statusPtn += this.statusSet[i];
		}
		statusPtn = new RegExp('_([' + statusPtn + '])$');

		var src     = this.node.getAttributeBA('src');
		var fSuffix = (src && src.match(this.suffixPtn)) ? src.match    (this.suffixPtn    )[0] : null;
		var fName   = (fSuffix)                          ? src.replace  (this.suffixPtn, '')    : null;
		var fStatus = (fName && fName.match(statusPtn))  ? fName.match  (statusPtn         )[1] : null;
		var fRemain = (fStatus)                          ? fName.replace(statusPtn     , '')    : null;

		if (fRemain && fStatus && fSuffix) {
			this.statusSet['default'] = fStatus;
			for (var status in this.statusSet) {
				this.images[status] = BAPreloadImage(fRemain + '_' + this.statusSet[status] + fSuffix);
				if (this.statusSet[status] == fStatus && status != 'default') {
					this.status = status;
				}
			}
		}
	},
	
	setStatus : function (status) {
		this.status = status;
		if (this.images[status]) {
			this.node.setAttributeBA('src', this.images[status].src);
		}
	}
}



/* -------------------- Constructor : BARolloverTarget() -------------------- */

function BARolloverTarget(node, statusSet, exceptCName) {
	this.node        = node;
	this.statusSet   = statusSet;
	this.status      = 'default';
	this.cNamePrefix = 'pseudo-';
	this.exceptCName = exceptCName;

	this.init();
}

BARolloverTarget.prototype = {
	init : function () {
		this.images = [];
		this.node.__BARolloverTarget__ = this;

		if (this.nodeValidate(this.node)) {
			this.images.push(new BARolloverImage(this.node, this.statusSet));
		} else {
			var nodes = BAConcatNodeList(this.node.getElementsByTagNameBA('img'), this.node.getElementsByTagNameBA('input'));
			for (var i = 0, n = nodes.length; i < n; i++) {
				if (this.nodeValidate(nodes[i])) {
					this.images.push(new BARolloverImage(nodes[i], this.statusSet));
				}
			}
		}
	},

	nodeValidate : function(node) {
		if (!node || !node.nodeName) {
			return false;
		} else if (!node.nodeName.match(/^(img|input)$/i)) {
			return false
		} else if (node.nodeName.match(/^input$/i) && !node.getAttributeBA('type').match(/^image$/i)) {
			return false
		} else if (this.exceptCName && node.hasClassNameBA(this.exceptCName)) {
			return false
		} else {
			return true;
		}
	},

	setStatus : function(status) {
		this.status = status;
		for (var i = 0, n = this.images.length; i < n; i++) {
			this.images[i].setStatus(status);
		}
		for (var i in this.statusSet) {
			this.node.removeClassNameBA(this.cNamePrefix + i);
		}
		if (this.statusSet[status]) {
			this.node.appendClassNameBA(this.cNamePrefix + status);
		}
	}
};



/* -------------------- Function : BARolloverAutoSetup() -------------------- */
function BARolloverAutoSetup(baseNode) {
	if (BAROLLOVER_AUTOSETUP_ENABLED) {
		if (!baseNode || baseNode.nodeType != 1) {
			baseNode = document;
		}
		var nodes = baseNode.getElementsByClassNameBA(BAROLLOVER_AUTOSETUP_TARGET_CNAME);
		for (var i = 0, n = nodes.length; i < n; i++) {
			if (!nodes[i].__BARolloverTarget__) {
				var node = nodes[i];
				var target = new BARolloverTarget(node, BAROLLOVER_AUTOSETUP_STATUS_SET, BAROLLOVER_AUTOSETUP_EXCEPT_CNAME);
				node.addEventListenerBA('mouseover', function (e) {
					e.currentTarget.__BARolloverTarget__.setStatus('hover');
				});
				node.addEventListenerBA('mouseout',  function (e) {
					e.currentTarget.__BARolloverTarget__.setStatus('default');
				});
			}
		}
	}
}





/* -------------------- Main : register start-up -------------------- */

if (typeof BA == 'object' && BA.ua.DOMok) {
	BAAddOnload(BARolloverAutoSetup);
}
