/*______________________________________________________

  Info Balloon (rev005_JAL_BRANCH_1)

________________________________________________________*/

// require common.js


/* -- JLJS_InfoBalloon Core API -- */

function JLJS_InfoBalloon(content) {
	if (typeof JLJS != 'object' || !JLJS.env.DOMok) return null;

	this.popupDelay   = 0;      // number (ms)
	this.popupSustain = 5000;   // number (ms)
	this.offsetX      = 0;      // number (px)
	this.offsetY      = 0;      // number (px)
	this.useRevisePos = true;   // 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    = 'JLJS_InfoBalloon';  // string (className of balloon container box)

 	this.createContainer();
 	this.setContent(content);
 	this.toggleHidden();
}

JLJS_InfoBalloon.prototype = {
	createContainer : function() {
		if (this.container) return;
		var oBODY = JLJS.getElementsByTagName('body')[0];
		if (!oBODY) return;
		this.container    = JLJS.createElement('ins');
		JLJS.classAttr.add(this.container, this.className);
		this.setId();
		oBODY.appendChild(this.container);
		JLJS.addEvent(oBODY, 'mousemove', JLJS.getMousePos);
	},

	setId : function(id) {
		if (!this.container) return;
		if (id) {
			this.id = id;
		} else {
			var num  = 0;
			var oINS = JLJS.getElementsByTagName('ins');
			for (var i = 0; i < oINS.length; i++) {
				if (JLJS.classAttr.check(oINS[i], this.className)) num++;
			}
			this.id = this.className + num;
		}
		JLJS.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._JLJSIB_) window._JLJSIB_ = [];
		window._JLJSIB_[this.container.id] = this;
		this.popupTimer1  = setTimeout('_JLJSIB_.' + this.container.id + '.toggleVisible()', this.popupDelay);
		this.popupTimer2  = setTimeout('_JLJSIB_.' + this.container.id + '.moveTo()'       , this.popupDelay);
		if (this.popupSustain > 0) {
			this.sustainTimer = setTimeout('_JLJSIB_.' + 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 = JLJS.geom.windowW - (JLJS.geom.windowX + this.offsetX + this.offsetWidth  + 20);
		var reviseY = JLJS.geom.windowH - (JLJS.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 (JLJS.env.isIE && JLJS.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 (JLJS.getAttr(node, attrs[i])) {
				JLJS.setAttr(node, attrs[i], '');
			}
		}
		for (var i = 0; i < node.childNodes.length; i++) {
			this.delNoisyAttrs(node.childNodes[i]);
		}
	}
};



/* --- JLJS_InfoBalloonInterface extends JLJS_InfoBalloon ---
    balloon interface auto setup using external data.   */

function JLJS_InfoBalloonInterface() {
	this.popupDelay   = 500;   // number (ms)
	this.popupSustain = 5000;  // number (ms)
	this.offsetX      = 0;     // number (px)
	this.offsetY      = 20;    // number (px)
	this.ignoreCName  = 'collapsed';  // string (className for prevent balloon)
}

JLJS_InfoBalloonInterface.prototype = new JLJS_InfoBalloon;

JLJS_InfoBalloonInterface.prototype.revisePosition = function() {
	var reviseX = JLJS.geom.windowW - (JLJS.geom.windowX + this.offsetX + this.offsetWidth  + 20);
	var reviseY = JLJS.geom.windowH - (JLJS.geom.windowY + this.offsetY + this.offsetHeight + 20);
	this.posX = JLJS.geom.mouseX + this.offsetX + ((reviseX < 0 && this.useRevisePos) ? reviseX : 0);
	this.posY = JLJS.geom.mouseY + this.offsetY + ((reviseY < 0 && this.useRevisePos) ? reviseY : 0);
}
	
JLJS_InfoBalloonInterface.prototype.mouseover = function(e) {
	if (JLJS_IBInterface.ignoreCName && JLJS.classAttr.check(e.currentTarget, JLJS_IBInterface.ignoreCName)) return;
	JLJS_IBInterface.delNoisyAttrs(e.currentTarget);
	JLJS_IBInterface.setContent(JLJS_IBInterface.data[e.currentTarget.id]);
	JLJS_IBInterface.revisePosition();
	JLJS_IBInterface.popup();
}

JLJS_InfoBalloonInterface.prototype.mousemove = function(e) {
	if (JLJS_IBInterface.ignoreCName && JLJS.classAttr.check(e.currentTarget, JLJS_IBInterface.ignoreCName)) return;
	JLJS_IBInterface.revisePosition();
	JLJS_IBInterface.moveTo();
}

JLJS_InfoBalloonInterface.prototype.mouseout = function(e) {
	if (JLJS_IBInterface.ignoreCName && JLJS.classAttr.check(e.currentTarget, JLJS_IBInterface.ignoreCName)) return;
	JLJS_IBInterface.hide();
}

JLJS_InfoBalloonInterface.prototype.init = function() {
	if (typeof window.JLJS_InfoBalloonData != 'object') return;
 	JLJS_IBInterface.createContainer();
 	JLJS_IBInterface.toggleHidden();
	JLJS_IBInterface.data = window.JLJS_InfoBalloonData;

	var oBODY = JLJS.getElementsByTagName('body')[0];
	JLJS.addEvent(oBODY, 'click' , JLJS_IBInterface.mouseout);
	for (var id in JLJS_IBInterface.data) {
		var targetNode = document.getElementById(id);
		if (!targetNode) continue;
		JLJS.addEvent(targetNode, 'mouseover', JLJS_IBInterface.mouseover);
		JLJS.addEvent(targetNode, 'mousemove', JLJS_IBInterface.mousemove);
		JLJS.addEvent(targetNode, 'mouseout' , JLJS_IBInterface.mouseout );
	}
}

JLJS_InfoBalloonInterface.prototype.fromAttr = function(fromNode, source) {
	if (!this.container || !fromNode || !source || (fromNode.id && this.data[fromNode.id])) return;
	if (JLJS.getAttr(fromNode, 'alt')) JLJS.setAttr(fromNode, 'alt', '');
	this.setContent(source);
	this.revisePosition();
	this.popup();
	JLJS.addEvent(fromNode, 'mousemove', this.mousemove);
	JLJS.addEvent(fromNode, 'mouseout' , this.mouseout );
}

if (typeof JLJS == 'object' && JLJS.env.DOMok) {
	var JLJS_IBInterface = new JLJS_InfoBalloonInterface;
	JLJS.addOnload(JLJS_IBInterface.init);
}