/*_____________________________________________________________

   Image rollover actions auto setup. (rev004b_JAL_BRANCH2)

______________________________________________________________*/

// require common.js

/* --- constructor of roll over target object --- */

function JLJS_Rollover_Target (node, exceptCName) {
	if (!node || !node.nodeName) return;

	this.node = node;
	this.exceptCName = (exceptCName) ? exceptCName : '';
	this.classNames = {
		'hover' : 'pseudo-hover'
	};
	this.targets = [];
	this.node._RON_instance_ = this;

	this.setNode();
}

JLJS_Rollover_Target.prototype = {
	setNode : function () {
		if (this.isTargetNode(this.node)) {
			this.targets[this.targets.length] = new JLJS_Rollover_Image(this.node);
		} else {
			var nodes = JLJS.concatNodeList(JLJS.getElementsByTagName('img', this.node), JLJS.getElementsByTagName('input', this.node));
			for (var i = 0; i < nodes.length; i++) {
				if (this.isTargetNode(nodes[i])) {
					this.targets[this.targets.length] = new JLJS_Rollover_Image(nodes[i]);
				}
			}
		}
	},

	isTargetNode : 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) && !JLJS.getAttr(node, 'type').match(/^image$/i))
			return false
		else if (this.exceptCName && JLJS.classAttr.check(node, this.exceptCName))
			return false
		else
			return true;
	},

	setStatus : function(status) {
		if (!this.node || typeof status != 'string' || this.classNames[status] == 'undefined') return;
		for (var i = 0; i < this.targets.length; i++) {
			this.targets[i].setStatus(status);
		}
		for (var i in this.classNames) {
			JLJS.classAttr.remove(this.node, this.classNames[i]);
		}
		if (this.classNames[status]) {
			JLJS.classAttr.add(this.node, this.classNames[status]);
		}
	}
};


/* --- constructor of roll over target images --- */

function JLJS_Rollover_Image (imgNode) {
	this.fileSuffixPtn   = /\.(jpe?g|gif|png)$/i;
	this.statusSuffixPtn = /_[nso]$/;
	this.statusSuffix    = {
		'normal'  : '_n',
		'hover'   : '_o'
	};
	this.setNode(imgNode);
}

JLJS_Rollover_Image.prototype = {
	setNode : function (imgNode) {
		var src     = JLJS.getAttr(imgNode, 'src');
		var fSuffix = (src)     ? src.match(this.fileSuffixPtn)[0]                                      : null;
		var fStatus = (fSuffix) ? src.replace(this.fileSuffixPtn, '').match(this.statusSuffixPtn)       : null;
		var fRemain = (fStatus) ? src.replace(this.fileSuffixPtn, '').replace(this.statusSuffixPtn, '') : null;

		if (fRemain && fStatus && fSuffix) {
			this.statusSuffix['default'] = fStatus;
			this.src = {};
			for (var i in this.statusSuffix) {
				this.src[i] = fRemain + this.statusSuffix[i] + fSuffix;
			}
			this.node = imgNode;
			this.preloadImage();
		}
	},
	
	preloadImage : function() {
		if (!this.node) return;
		for (var i in this.src) {
			if (this.src[i]) {
				JLJS.preloadImage(this.src[i]);
			}
		}
	},

	setStatus : function (status) {
		if (!this.node || typeof status != 'string' || !this.statusSuffix[status]) return;
		JLJS.setAttr(this.node, 'src', this.src[status]);
	}
};


/* --- rollover main --- */

function JLJS_Rollover_AutoSetup () {
	var targetCName = 'rollover';
	var exceptCName = 'norollover';

	var nodes = JLJS.getElementsByClassName(targetCName);
	for (var i = 0; i < nodes.length; i++) {
		if (nodes[i]._RON_instance_) continue;

		var RON = new JLJS_Rollover_Target(nodes[i], exceptCName);

		if (RON && RON.node && RON.node._RON_instance_) {
			JLJS.addEvent(RON.node, 'mouseover', function (e) {
				var obj = e.currentTarget._RON_instance_;
				obj.setStatus('hover');
			});
			JLJS.addEvent(RON.node, 'mouseout' , function (e) {
				var obj = e.currentTarget._RON_instance_;
				obj.setStatus('default');
			});
		}
	}
}



/* --- register start up method --- */

if (typeof JLJS == 'object' && JLJS.env.DOMok) {
	JLJS.addOnload(JLJS_Rollover_AutoSetup);
}


/* -- Subpage tab rollover --*/
function swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function imageOver(imgs) {
	imgs.src = imgs.src.replace("off.gif", "on.gif");
}
function imageOut(imgs) {
	imgs.src = imgs.src.replace("on.gif", "off.gif");
}