/*
 * Balloon-control Script
 * 
 * 
 */

var bAgent = navigator.userAgent;
var bName = navigator.appName;
var bPlugins = navigator.plugins;
var d = document;

function sniffer(){
	/*OS*/
	this.Win = bAgent.indexOf("Win",0) != -1 ? 1 :0;
	this.Mac = bAgent.indexOf("Mac",0) != -1 ? 1 :0;
	this.MacOSX = ((bAgent.indexOf("Mozilla") != -1) && (bAgent.indexOf("Mac OS X") != -1)) ? 1 :0;
	if(bPlugins){for(i=0;i<bPlugins.length;i++){if(this.Mac && (bPlugins[i].filename.indexOf("QuickTime Plugin.plugin")!=-1)){this.MacOSX=true;}}}
	
	/*Browser*/
	this.IE = ((bAgent.indexOf("MSIE") != -1) && (bAgent.indexOf("Opera") == -1)) ? 1 :0;
	this.NS = ((bName == "Netscape") && (bAgent.indexOf("Opera") == -1)) ? 1 :0;
	this.NC = ((bName == "Netscape") && (bAgent.indexOf("Gecko") == -1) && (bAgent.indexOf("Opera") == -1)) ? 1 :0;
	this.Gecko = (bAgent.indexOf("Gecko") != -1) ? 1 :0;
	this.Moz = ((bAgent.indexOf("Gecko") != -1) && (bAgent.indexOf("Safari",0) == -1)) ? 1 :0;
	this.OPERA = bAgent.indexOf("Opera",0) != -1 ? 1 :0;
	this.SAFARI = bAgent.indexOf("Safari",0) != -1 ? 1 :0;
	this.ICAB = bAgent.indexOf("iCab",0) != -1 ? 1 :0;
	
	this.IE3 = (bAgent.indexOf('MSIE 3.',0) != -1);
	this.MacIE4 = (this.Mac && (bAgent.indexOf('MSIE 4.',0) != -1));
	this.MacIE45 = (this.Mac && (bAgent.indexOf('MSIE 4.5',0) != -1));
	this.MacIE5 = (this.Mac && (bAgent.indexOf('MSIE 5.',0) != -1));
	this.NS60 = (bAgent.indexOf("Netscape6/6.0") != -1);
	this.NS6 = (bAgent.indexOf("Netscape6/") != -1);
	this.NS7 = (bAgent.indexOf("Netscape/7") != -1);
	
	/*Object*/
	/*NS4 = 1, IE4 = 2, IE5+,OPERA5,7 = 3, NS6+,SAFARI,OPERA6 = 4, others = 0*/
	this.checkObj = d.all?(d.getElementById?3:2):(d.getElementById?4:(d.layers?1:0));
	this.allObj = ((this.checkObj == 1) || (this.checkObj == 2) || (this.checkObj == 3) || (this.checkObj == 4));
	this.checkObj4 = (d.getElementById && !d.all) ? 1 :0;
	this.checkObj3 = (d.getElementById && d.all) ? 1 :0;
	this.checkObj2 = (d.all && !d.getElementById) ? 1 :0;
	this.checkObj1 = d.layers ? 1 :0;
	
	/*Compat Mode*/
	this.modeStandard = d.compatMode == "CSS1Compat" ? 1 : 0;
	this.modeQuirks = d.compatMode == "BackCompat" ? 1 : 0;
	
	this.WinIE6s = (this.Win && this.IE && this.modeStandard && ((bAgent.indexOf('MSIE 6.',0) != -1) || (bAgent.indexOf('MSIE 7.',0) != -1)));
	this.WinIE6q = (this.Win && this.IE && this.modeQuirks && ((bAgent.indexOf('MSIE 6.',0) != -1) || (bAgent.indexOf('MSIE 7.',0) != -1)));
	
	return this;
}
var usr = new sniffer;

function getWindowWidth(){
	if(usr.WinIE6s){
		return d.documentElement.clientWidth;
	}else if(usr.OPERA || (usr.Mac && (usr.checkObj == 1 || usr.OPERA))){
		return window.innerWidth;
	}else if(usr.Win && usr.checkObj == 1){
		return window.innerWidth - 16;
	}else if(usr.checkObj == 4){
		return window.innerWidth - 15;
	}else if(usr.checkObj == 2 || usr.checkObj == 3){
		return d.body.clientWidth;
	}
}

function getWindowHeight(){
	if(usr.WinIE6s){
		return d.documentElement.clientHeight;
	}else if(usr.OPERA || (usr.Mac && (usr.checkObj == 1 || usr.OPERA))){
		return window.innerHeight;
	}else if(usr.Win && usr.checkObj == 1){
		return window.innerHeight - 16;
	}else if(usr.checkObj == 4){
		return window.innerHeight - 15;
	}else if(usr.checkObj == 2 || usr.checkObj == 3){
		return d.body.clientHeight;
	}
}

function getWindowXOffset(){
	return window.scrollX || d.body.parentNode.scrollLeft || d.body.scrollLeft || 0;
}

function getWindowYOffset(){
	return window.scrollY || d.body.parentNode.scrollTop || d.body.scrollTop || 0;
}

function setObj(idName){
	return d.all ? d.all(idName) : d.getElementById ? d.getElementById(idName) : d.layers[idName];
}

/*---- required flag ----*/
var __BALLOON_EXECUTABLE__ = (document.createElement && window.usr);
if (__BALLOON_EXECUTABLE__) {
	// for some browsers
	__BALLOON_EXECUTABLE__ = (document.createElement('ins') && !(usr.IE && usr.Mac));
}

/*---- preload image ----*/
if (__BALLOON_EXECUTABLE__ && balloonClass.prototype.iconImg && balloonClass.prototype.iconCName) {
	var iconPreload = new Array();
	for (var i in balloonClass.prototype.iconImg) {
		iconPreload[i] = new Image();
		iconPreload[i].src = balloonClass.prototype.iconImg[i];
	}
}

/*---- balloon-control class ----*/
//constructor
function balloonClass() {
	this.init();
}

balloonClass.prototype = {
//methods
	init : function() {
		this.isBalloon = true;

		if (this.balloonTag   == undefined) this.balloonTag   = 'div';
		if (this.balloonCName == undefined) this.balloonCName = 'balloonA1';

		if (this.offsetX == undefined) this.offsetX = 10;
		if (this.offsetY == undefined) this.offsetY = 20;

		if (this.marginX == undefined) this.marginX = 1;
		if (this.marginY == undefined) this.marginY = 1;

		if (this.reverseOffsetX == undefined) this.reverseOffsetX = 5;
		if (this.reverseOffsetY == undefined) this.reverseOffsetY = 5;

		if (this.resource == undefined) this.resource = 'title';

		if (!this.header) {
			this.header = {
				title : '<p class="tBlockA10"><span class="txtUnleadedA1">',
				mark  : '<p class="tBlockA10"><span class="txtUnleadedA1">',
				notes : '<p class="tBlockA11"><span class="txtUnleadedA1">'
			}
		} else {
			this.header = {
				title  : (!this.header.title) ? '<p class="tBlockA10"><span class="txtUnleadedA1">' : this.header.title,
				mark   : (!this.header.mark)  ? '<p class="tBlockA10"><span class="txtUnleadedA1">' : this.header.mark,
				notes  : (!this.header.notes) ? '<p class="tBlockA11"><span class="txtUnleadedA1">' : this.header.notes
			}
		}

		if (!this.joint) {
			this.joint = {
				title : '<br>',
				mark  : '<br>',
				notes : '<br>'
			}
		} else {
			this.joint = {
				title  : (!this.joint.title) ? '<br>' : this.joint.title,
				mark   : (!this.joint.mark)  ? '<br>' : this.joint.mark,
				notes  : (!this.joint.notes) ? '<br>' : this.joint.notes
			}
		}

		if (!this.footer) {
			this.footer = {
				title : '</span></p>',
				mark  : '</span></p>',
				notes : '</span></p>'
			}
		} else {
			this.footer = {
				title  : (!this.footer.title) ? '</span></p>' : this.footer.title,
				mark   : (!this.footer.mark)  ? '</span></p>' : this.footer.mark,
				notes  : (!this.footer.notes) ? '</span></p>' : this.footer.notes
			}
		}

		if (!this.sprintFunc) {
			this.sprintFunc = {
				title : titleSprint,
				mark  : markSprint
			}
		} else {
			this.sprintFunc = {
				title : (!this.sprintFunc.title) ? titleSprint : this.sprintFunc.title,
				mark  : (!this.sprintFunc.mark)  ? markSprint  : this.sprintFunc.mark
			}
		}
		if (!this.styleSet) {
			this.styleSet        = {
				normal : { backgroundColor : (this.bgColor) ? this.bgColor : (this.currentStyle) ? this.currentStyle.backgroundColor : (window.getComputedStyle) ? window.getComputedStyle(this, '').getPropertyValue('backgroundColor') : '#ffffff' },
				hover  : { backgroundColor : '#efefef' }
			}
		} else {
			this.styleSet        = {
				normal : (!this.styleSet.normal) ? { backgroundColor : (this.bgColor) ? this.bgColor : (this.currentStyle) ? this.currentStyle.backgroundColor : (window.getComputedStyle) ? window.getComputedStyle(this, '').getPropertyValue('backgroundColor') : '#ffffff' } : this.styleSet.normal,
				hover  : (!this.styleSet.hover)  ? { backgroundColor : '#efefef' } : this.styleSet.hover
			}
		}

		if (this.iconCName == undefined) this.iconCName = 'balloonIcon';

		if (!this.iconImg) {
			this.iconImg = {
				normal : '/shared/img2/mrk/mrk_link_05.gif',
				hover  : '/shared/img2/mrk/mrk_link_04.gif'
			}
		} else {
			this.iconImg = {
				normal : (!this.iconImg.normal) ? '/shared/img2/mrk/mrk_link_05.gif' : this.iconImg.normal,
				hover  : (!this.iconImg.hover) ? '/shared/img2/mrk/mrk_link_04.gif' : this.iconImg.hover
			}
		}
	},
	make : function() {
		if (!document.createElement) { return false; }

		var id = new Array();
		for(var i=0, n=arguments.length; i < n; i++){
			if (arguments[i].length) {
				id = id.concat(arguments[i]);
			} else {
				id.push(arguments[i].toString());
			}
		}
		this.resourceObject = new Array();
		if (id.length) {
			for(var i=0, n=id.length; i < n; i++){
				var obj = setObj(id[i]);
				if (!!obj) {
					this.resourceObject[this.resourceObject.length] = obj;
				}
			}
		}
		if (!this.resourceObject && !this.resource) { return false; }

		this.balloon = document.createElement(this.balloonTag);
		this.balloon.className = this.balloonCName;
		this.balloon.style.position = 'absolute';
		this.balloon.style.visibility = 'hidden';

		if (this.resourceObject.length) {
			this.resourceId = id; // Array
			var htmlTxt = '';
			var group = '';
			for(var i=0, n=this.resourceObject.length; i < n; i++){
				this.resourceId[i].match(/^([^0-9]+)[0-9]*$/);

				if (group != RegExp.$1) {
					if (this.footer[group]) {
						htmlTxt += this.footer[group];
					}
					group = RegExp.$1;
					if (this.header[group]) {
						htmlTxt += this.header[group];
					}
				} else {
					if (this.joint[group]) {
						htmlTxt += this.joint[group];
					}
				}

				if (this.sprintFunc[group]) {
					htmlTxt += this.sprintFunc[group](this.resourceObject[i]);
				} else {
					htmlTxt += this.resourceObject[i].innerHTML;
				}
			}
			if (this.footer[group]) {
				htmlTxt += this.footer[group];
			}

			htmlTxt = htmlTxt.replace(/<a [^>]*>/ig, '');
			htmlTxt = htmlTxt.replace(/<\/a>/ig, '');

			this.balloon.innerHTML = htmlTxt;
		} else if (this.resource) {
			var htmlTxt = '';

			if (this.header[this.resource]) {
				htmlTxt += this.header[this.resource];
			}

			if (this.joint[this.resource]) {
				this[this.resource] = (this[this.resource].split('\n')).join(this.joint[this.resource]);
			}

			if (this.sprintFunc[this.resource]) {
				htmlTxt += this.sprintFunc[this.resource](this[this.resource]);
			} else {
				htmlTxt += this[this.resource];
			}

			if (this.footer[this.resource]) {
				htmlTxt += this.footer[this.resource];
			}
			this.balloon.innerHTML = htmlTxt;
		}
		document.body.appendChild(this.balloon);
		return true;
	},
	show : function(e) {
		if (!this.balloon) { return false; }
		if (!e) { e = window.event; }

		this.currentTitle = this.title;
		this.title = '';

		this.move(e);
		this.changeIcon(e);

		if (this.style && this.styleSet['hover']) {
			for (var i in this.styleSet['hover']) {
				this.style[i] = this.styleSet['hover'][i];
			}
		}
		this.balloon.style.visibility = 'visible';

		return true;
	},
	hide : function(e) {
		if (!this.balloon) { return false; }
		if (!e) { e = window.event; }

		// vs IE
		if (e.toElement) {
			var toElem = e.toElement;
			while (toElem) {
				if (toElem == this) { return false; }
				toElem = toElem.parentElement;
			}
		}

		this.balloon.style.left = '0px';
		this.balloon.style.visibility = 'hidden';
		if (this.style && this.styleSet['normal']) {
			for (var i in this.styleSet['normal']) {
				this.style[i] = this.styleSet['normal'][i];
			}
		}

		this.title = this.currentTitle;

		this.changeIcon(e);

		return true;
	},
	move : function(e) {
		if (!e) { e = window.event; }
		if (!this.balloon || !e) { return false; }

		var zoomRatio = BAGetZoomRatio();

		var windowW  = getWindowWidth();
		var scrollX  = getWindowXOffset();
		var ex = (e.x) ? e.x : e.clientX;
		// vs Safari
		if(usr.SAFARI && e.x == e.clientX && e.x == e.pageX) ex = ex - scrollX;
		var x = ex + scrollX + this.offsetX;
		if (x+this.balloon.offsetWidth+this.marginX > scrollX+windowW) {
			x = scrollX+windowW - (this.balloon.offsetWidth+this.marginX);
			if (!this.offsetX && x-this.marginX < ex+scrollX+this.marginX) {
				x = ex+scrollX+this.marginX - this.balloon.offsetWidth - this.reverseOffsetX;
			}
		}
		if (x < scrollX) { x = scrollX; }

		var windowH  = getWindowHeight();
		var scrollY  = getWindowYOffset();
		var ey = (e.y) ? e.y : e.clientY;
		// vs Safari
		if(usr.SAFARI && e.y == e.clientY && e.y == e.pageY) ey = ey - scrollY;
		var y = ey + scrollY + this.offsetY;
		if (y+this.balloon.offsetHeight+this.marginY > scrollY+windowH) {
			y = scrollY+windowH - (this.balloon.offsetHeight+this.marginY);
			if (y < ey+scrollY+this.marginY) {
				y = ey+scrollY+this.marginY - this.balloon.offsetHeight - this.reverseOffsetY;
			}
		}
		if (y < scrollY) { y = scrollY; }
		
		x /= zoomRatio;
		y /= zoomRatio;
		
		return objectMoveTo(this.balloon, x, y);

	},
	changeIcon : function(e) {
		if (!e) { e = window.event; }
		if (!this.iconImg || !this.iconCName || !e) { return false; }
		var mode = (e.type == 'mouseover') ? 'hover' : 'normal';

		if (!this.iconNodes) {
			var nodes = getElementsByClassNameInNode(this, this.iconCName);
			if (nodes.length) {
				this.iconNodes = nodes;
			} else {
				this.iconNodes = 'none';
			}
		}

		if (this.iconNodes && this.iconNodes != 'none') {
			for (var i = 0, n = this.iconNodes.length; i < n; i++) {
				if (this.iconNodes[i].src && this.iconImg[mode]) {
					this.iconNodes[i].src = this.iconImg[mode];
				}
			}
		}

		return true;
	}
}

/*---- text-format ----*/

function titleSprint(str) {
	str = str.replace('¢¨', '</span></p><p class="tBlockA11"><span class="txtUnleadedA1">¢¨');
	str = (str.split('\n')).join('<br>');
	return str;
}

function markSprint(obj) {
	var firstElem = getChildElement(obj, 0);
	var secondElem = getChildElement(obj, 1);
	if (firstElem && secondElem) {
		return getInnerText(firstElem) + '¡§' +getInnerText(secondElem);
	} else {
		return obj.innerHTML;
	}
}


/*---- function ----*/

//Array.shift()(for some old browsers)
if (!Array.prototype.shift) {
	Array.prototype.shift = function () {
		var first = this[0];
		for(var i=0, n=this.length-1; i < n; i++){
			this[i] = this[i+1];
		}
		--this.length;
		return first;
	}
}


//add balloon
function setBalloon() {
	if (!__BALLOON_EXECUTABLE__ || !document.body || !arguments) { return false; }

	var arg = new Array();
	for(var i=0, n=arguments.length; i < n; i++){
		if (arguments[i].length) {
			arg = arg.concat(arguments[i]);
		} else {
			arg.push(arguments[i]);
		}
	}

	var e = arg.shift();
	var obj = arg.shift();
	var id = arg;

	if (!e) { e = window.event; }

	if (obj.isBalloon) {
		if (e.type == 'mouseover') {
			obj.show(e);
		} else if (e.type == 'mousemove') {
			obj.move(e);
		} else if (e.type == 'mouseout') {
			obj.hide(e);
		} else {
			return false;
		}
		return true;
	}

	for (var i in balloonClass.prototype) {
		obj[i] = balloonClass.prototype[i];
	}
	obj.init();

	var flg = true;
	for (var i=0,n=id.length;i<n;i++) {
		if (!id[i].match(/^notes/)) flg = false;
	}
	if (flg) obj.header.notes = obj.header.notes.replace(/BlockA11/, 'BlockA10');

	if (obj.make(id)) {
		obj.show(e);
	}

	obj.onmouseover  = obj.show;
	obj.onmousemove  = obj.move;
	obj.onmouseout   = obj.hide;

	return true;
}

//move object
function objectMoveTo(obj, x, y) {
	if (!obj) { return false; }

	if (document.layer) {
		if (x != isNaN) { x = 0; }
		if (y != isNaN) { y = 0; }
		obj.moveTo(x, y);
		return true;
	}

	if (x != isNaN) {
		obj.posX = x;
		if (obj.style) { obj.style.left = x + 'px'; }
	}

	if (y != isNaN) {
		obj.posY = y;
		if (obj.style) { obj.style.top  = y + 'px'; }
	}

	return true;
}

//get elements with target class-name in target node
function getElementsByClassNameInNode(node, className, reg) {
	if (!node || !className) { return false; }

	var nodeArray = new Array();
	if (!reg) {
		reg = new RegExp('\\b' + className + '\\b');
	}
	if (reg.test(node.className)) {
		nodeArray[0] = node;
	}

	var nodes = node.childNodes;
	for (var i=0, n = nodes.length; i < n; i++) {
		nodeArray = nodeArray.concat(getElementsByClassNameInNode(nodes[i], className, reg));
	}

	return nodeArray;
}

//get child element(without text-node)
function getChildElement(node, n) {
	if (!node.childNodes.length || n < 0) { return false; }
	var count = 0;
	var cN = node.childNodes[0];
	while (cN) {
		if (cN.tagName) {
			if (count++ == n) { return cN; }
		}
		cN = cN.nextSibling;
	}
	return false;
}

//get innerText(like winIE)
function getInnerText(node) {
	if (!node) { return false; }
	if (node.innerText) { return node.innerText;}

	var nV = '';
	if (node.nodeValue) {
		nV = node.nodeValue;
	}
	if (node.childNodes.length) {
		var cN = node.childNodes[0];
		while(cN) {
			nV += getInnerText(cN);
			cN = cN.nextSibling;
		}
	}

	return nV;
}



