/*_____________________________________________________

   common lib (rev007b_EXPO2005_BRANCH1)
_______________________________________________________*/

// require /shared/js/common.js

/* --- supplements of common lib  --- */

function BALibrarySupplements() {
	BA.preloadImage = function(src) {
		if (!document.images || !src) return null;
		var img = new Image();
		img.src = src;
		return img;
	}

	BA.addEvent_ = function(obj, type, listener) {
		if(!obj || !type || !listener) return;
		function EventObj(obj) {
			this.type            = window.event.type;
			this.target          = window.event.srcElement;
			this.currentTarget   = obj;
			this.clientX         = window.event.clientX;
			this.clientY         = window.event.clientY;
			this.pageX           = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + window.event.clientX;
			this.pageY           = (document.documentElement.scrollTop  ? document.documentElement.scrollTop  : document.body.scrollTop ) + window.event.clientY;
			this.stopPropagation = function() { window.event.cancelBubble = true  };
			this.preventDefault  = function() { window.event.returnValue  = false };
		}

		if (obj.addEventListener) {  // Std DOM Events
			obj.addEventListener(type, listener, false);
		} else if (obj.attachEvent) {   // WinIE
			obj.attachEvent('on' + type, function() { listener(new EventObj(obj)) });
		} else {                 // MacIE
			var exists = obj['on' + type];
		    obj['on' + type] = (exists) ?
				function() { exists(); listener(new EventObj(obj)); }:
				function() {           listener(new EventObj(obj)); };
		}
	}

	BA.geom        = {};
	BA.getMousePos = function(e) {
		if (typeof BA != 'object' || typeof BA.geom != 'object') return;
		var w = window;
		var d = document.documentElement;
		var b = d.getElementsByTagName('body')[0];
		var isMacIE = (BA.env.isMac && BA.env.isIE);

		BA.geom.scrollX = (w.scrollX) ? w.scrollX : (d.scrollLeft) ? d.scrollLeft : b.scrollLeft;
		BA.geom.scrollY = (w.scrollY) ? w.scrollY : (d.scrollTop)  ? d.scrollTop  : b.scrollTop;
		BA.geom.windowW = (w.innerWidth)  ? w.innerWidth  : (!isMacIE) ? d.offsetWidth : b.scrollWidth;
		BA.geom.windowH = (w.innerHeight) ? w.innerHeight : (!isMacIE) ? d.offsetHeight: b.scrollHeight;
		BA.geom.pageW   = (!isMacIE) ? b.scrollWidth  : d.offsetWidth;
		BA.geom.pageH   = (!isMacIE) ? b.scrollHeight : d.offsetHeight;
		BA.geom.windowX = e.clientX - (( BA.env.isSafari) ? BA.geom.scrollX : 0);
		BA.geom.windowY = e.clientY - (( BA.env.isSafari) ? BA.geom.scrollY : 0);
		BA.geom.mouseX  = e.clientX + ((!BA.env.isSafari) ? BA.geom.scrollX : 0);
		BA.geom.mouseY  = e.clientY + ((!BA.env.isSafari) ? BA.geom.scrollY : 0);

		// for debug
		/*
		window.status = 'windowX : '    + BA.geom.windowX + ' | windowY : ' + BA.geom.windowY
		              + ' | scrollX : ' + BA.geom.scrollX + ' | scrollY : ' + BA.geom.scrollY
		              + ' | left : '    + BA.geom.mouseX  + ' | top : '     + BA.geom.mouseY;
		*/
	}
}



/*_____________________________________________________________

   Image rollover actions (rev004c_EXPO2005_BRANCH1)
_______________________________________________________________*/

// require /shared/js/common.js

/* --- constructor of roll over target images --- */

function BA_Rollover_Image (imgNode) {
	this.fileSuffixPtn   = /\.(jpe?g|gif|png)$/i;
	this.statusSuffixPtn = /_01[oa]?$/;
	this.statusSuffix    = {
		'normal'  : '_01',
		'hover'   : '_01o',
		'active'  : '_01a'
	};
	this.setNode(imgNode);
}

BA_Rollover_Image.prototype = {
	setNode : function (imgNode) {
		var src     = BA.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.node._BAROI_instance_ = this;
			this.preloadImage();
		}
	},
	
	preloadImage : function() {
		if (!this.node) return;
		for (var i in this.src) {
			if (this.src[i]) {
				BA.preloadImage(this.src[i]);
			}
		}
	},

	setStatus : function (status) {
		if (!this.node || typeof status != 'string' || !this.statusSuffix[status]) return;
		BA.setAttr(this.node, 'src', this.src[status]);
	}
};



/*______________________________________________________

  Info Balloon (rev005_EXPO2005_BRANCH_001)
________________________________________________________*/

/* -- BAInfoBalloon Core API -- */

function BAInfoBalloon(content) {
	if (typeof BA != 'object' || !BA.env.DOMok) return null;

	this.popupDelay   = 0;                // number (ms)
	this.popupSustain = 0;                // number (ms)
	this.offsetX      = 0;                // number (px)
	this.offsetY      = 0;                // number (px)
	this.useRevisePos = false;            // boolean

	this.posX         = 0;                // number (px) internal variable
	this.posY         = 0;                // number (px) internal variable
	this.offsetWidth  = 0;                // number (px) readonly
	this.offsetHeight = 0;                // number (px) readonly
	this.className    = 'BAInfoBalloon';  // string      fixed - no need to change

 	this.createContainer();
 	this.setContent(content);
 	this.toggleHidden();
}

BAInfoBalloon.prototype = {
	createContainer : function() {
		if (this.container) return;
		var oBODY = BA.getElementsByTagName('body')[0];
		if (!oBODY) return;
		this.container = BA.createElement('ins');
		(new ClassNames(this.container)).add(this.className);
		this.setId();
		oBODY.appendChild(this.container);
		BA.addEvent_(oBODY, 'mousemove', BA.getMousePos);
	},

	setId : function(id) {
		if (!this.container) return;
		if (id) {
			this.id = id;
		} else {
			var num  = 0;
			var oINS = BA.getElementsByTagName('ins');
			for (var i = 0; i < oINS.length; i++) {
				if ((new ClassNames(oINS[i])).has(this.className)) num++;
			}
			this.id = this.className + num;
		}
		BA.setAttr(this.container, 'id', this.id);
	},

	setContent : function(content) {
		if (!this.container || !content) return;
		if (typeof content == 'string') {
			this.container.innerHTML = content;
		} else if (typeof content == 'object' /*  && content instanceof Node */) {
			this.container.appendChild(content);
		} else {
			return;
		}
		this.offsetWidth  = this.container.offsetWidth;
		this.offsetHeight = this.container.offsetHeight;
	},

	popup : function(x, y) {
		if (!this.container) return;
		if (this.sustainTimer) clearTimeout(this.sustainTimer);
		if (x && !isNaN(x)) this.posX = x;
		if (y && !isNaN(y)) this.posY = y;
		if (!window._BAIB_) window._BAIB_ = [];
		window._BAIB_[this.container.id] = this;
		this.popupTimer1  = setTimeout('_BAIB_.' + this.container.id + '.toggleVisible()', this.popupDelay);
		this.popupTimer2  = setTimeout('_BAIB_.' + this.container.id + '.moveTo()'       , this.popupDelay);
		if (this.popupSustain > 0) {
			this.sustainTimer = setTimeout('_BAIB_.' + this.container.id + '.hide()', this.popupDelay + this.popupSustain);
		}
	},

	moveTo : function(x, y) {
		if (!this.container) return;
		if (x && !isNaN(x)) this.posX = x;
		if (y && !isNaN(y)) this.posY = y;
		this.revisePosition();
		if (!isNaN(this.posX)) this.container.style.left = this.posX + 'px';
		if (!isNaN(this.posY)) this.container.style.top  = this.posY + 'px';
	},
	
	revisePosition : function() {
		var reviseX = BA.geom.windowW - (BA.geom.windowX + this.offsetX + this.offsetWidth  + 20);
		var reviseY = BA.geom.windowH - (BA.geom.windowY + this.offsetY + this.offsetHeight + 20);
		this.posX = this.posX + this.offsetX + ((reviseX < 0 && this.useRevisePos) ? reviseX : 0);
		this.posY = this.posY + this.offsetY + ((reviseY < 0 && this.useRevisePos) ? reviseY : 0);
	},

	hide : function(){
		if (!this.container) return;
		if (this.popupTimer1)  clearTimeout(this.popupTimer1);
		if (this.popupTimer2)  clearTimeout(this.popupTimer2);
		if (this.sustainTimer) clearTimeout(this.sustainTimer);
		this.toggleHidden();

		if (BA.env.isIE && BA.env.isMac) {
			// below causes crash on WinIE
			this.container.innerHTML = '';
		} else {
			// below causes crash on MacIE
			while (this.container.firstChild) {
				this.container.removeChild(this.container.firstChild);
			}
		}
	},

	toggleVisible : function() {
		if (!this.container) return;
		this.container.style.visibility = 'visible';
	},

	toggleHidden : function() {
		if (!this.container) return;
		this.container.style.visibility = 'hidden';
	},

	delNoisyAttrs : function(node) {
		if (!node.nodeType || node.nodeType != 1) return;
		var attrs = ['alt', 'title'];
		for (var i in attrs) {
			if (BA.getAttr(node, attrs[i])) {
				BA.setAttr(node, attrs[i], '');
			}
		}
		for (var i = 0; i < node.childNodes.length; i++) {
			this.delNoisyAttrs(node.childNodes[i]);
		}
	}
};

/* --- BAInfoBalloonInterface instance of BAInfoBalloon --- */

function BAInfoBalloonInterface_Setup() {
	var baseNodeID             = 'collectionBlock';
	var balloonTargetCellCName = 'balloon';
	var balloonDataCellCName   = 'balloonData';
	var balloonTargetNodeName  = 'img';
	var balloonInnerROImgCName = 'button';

	if (!baseNodeID || !balloonTargetCellCName || !balloonDataCellCName || !balloonTargetNodeName) return;
	
	var baseNode    = document.getElementById(baseNodeID);
	var targetNodes = (baseNode) ? BA.getElementsByClassName(balloonTargetCellCName, 'td', baseNode) : [];
	var dataNodes   = (baseNode) ? BA.getElementsByClassName(balloonDataCellCName,   'td', baseNode) : [];

	if (targetNodes.length == 0 || targetNodes.length != dataNodes.length) return;
	
	BAInfoBalloon_Interface = new BAInfoBalloon;

	for (var i = 0; i < targetNodes.length; i++) {
		var targetNode = BA.getElementsByTagName(balloonTargetNodeName, targetNodes[i])[0];
		if (!targetNode) continue;
		targetNode.BAInfoBalloonInterface_Data = dataNodes[i].innerHTML;

		BA.addEvent_(targetNode, 'mouseover', function(e) {
			if (typeof BAInfoBalloon_Interface != 'object') return;
			e.stopPropagation();
			var target = e.currentTarget;
			var node   = (e.relatedTarget) ?
				e.relatedTarget : (window.event) ?
					window.event.fromElement : document;

			if (node != BAInfoBalloon_Interface.container && target.BAInfoBalloonInterface_Data) {
				BAInfoBalloon_Interface.delNoisyAttrs(target);
				BAInfoBalloon_Interface.setContent(target.BAInfoBalloonInterface_Data);
				var targetX  = BA.getOffset(target, 'X');
				var targetY  = BA.getOffset(target, 'Y');
				var targetW  = target.offsetWidth;
				var targetH  = target.offsetHeight;
				var balloonW = BAInfoBalloon_Interface.offsetWidth;
				var balloonH = BAInfoBalloon_Interface.offsetHeight;
				var balloonX = targetX - (balloonW - targetW) / 2;
				var balloonY = targetY - (balloonH - targetH) / 2;
				BAInfoBalloon_Interface.moveTo(balloonX, balloonY);
				BAInfoBalloon_Interface.popup();
				target.BAInfoBalloonInterface_isTargetNode = true;

				var img = BA.getElementsByClassName(balloonInnerROImgCName, 'img', BAInfoBalloon_Interface.container)[0];
				if (img) {
					new BA_Rollover_Image(img);
					BA.addEvent_(img, 'mouseover', function(e) { e.currentTarget._BAROI_instance_.setStatus('hover') });
					BA.addEvent_(img, 'mousedown', function(e) { e.currentTarget._BAROI_instance_.setStatus('active') });
					BA.addEvent_(img, 'mouseout' , function(e) { e.currentTarget._BAROI_instance_.setStatus('default') });
				}
			}
		});

		BA.addEvent_(targetNode, 'mouseout', function(e) {
			if (typeof BAInfoBalloon_Interface != 'object') return;
			var node = (e.relatedTarget) ?
				e.relatedTarget : (window.event) ?
					window.event.toElement : document;
			var flag = (function(node) {
				return (node == BAInfoBalloon_Interface.container) ?
					true : (node.parentNode) ?
						arguments.callee(node.parentNode) : false
			})(node);
			if (!flag) {
				BAInfoBalloon_Interface.hide();
			}
		});
	}

	BA.addEvent_(BAInfoBalloon_Interface.container, 'mouseout', function(e) {
		if (typeof BAInfoBalloon_Interface != 'object') return;
		e.stopPropagation();
		var node = (e.relatedTarget) ?
			e.relatedTarget : (window.event) ?
				window.event.toElement : document;
		var flag = (function(node) {
			return (node == BAInfoBalloon_Interface.container) ?
				true : (node.parentNode) ?
					arguments.callee(node.parentNode) : false
		})(node);
		if (!flag && !node.BAInfoBalloonInterface_isTargetNode) {
			BAInfoBalloon_Interface.hide();
		}
	});
}

/* --- BAInfoBalloonInterface preparation --- */

function BAInfoBalloonInterface_Preparation() {
	var tag = '';
	tag += '<style type="text/css" media="screen, tv">';
	tag += '#contentsArea #collectionBlock td.balloon img { cursor: hand; cur\\sor: pointer }';
	tag += '#contentsArea #collectionBlock td.balloonData * { display: none }';
	tag += '</style>';
	document.write(tag);
}








/*_____________________________________________________

                        startup
  _____________________________________________________ */

if (typeof BA == 'object' && BA.env.DOMok && !(BA.env.isOpera && BA.env.ver < 7.5)) {
	BALibrarySupplements();
	BAInfoBalloonInterface_Preparation();
	BA.addOnload(BAInfoBalloonInterface_Setup);
}


