/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Common Script Libraries.
 *
 *    @version rev011.2006-12-18
 */
/* -------------------------------------------------------------------------- */


var BA;
var BA_STATUSMSG;
var BA_KEYEQUIV;



/* --------------- Constructor : BAEnvironment --------------- */

function BAEnvironment() {
	this.debugMode = false;

	var d  = document;
	var di = d.implementation;
	var de = d.documentElement;
	var ua = navigator.userAgent;
	var lp = location.protocol;
	var lh = location.hostname;

	this.url = {};
	this.url.commonDir   = BAGetCommonDir('shared');
	this.url.cssDir      = this.url.commonDir + 'css/';
	this.url.jsDir       = this.url.commonDir + 'js/';

	this.ns = {};
	this.ns.defaultNS    = (de && de.namespaceURI) ? de.namespaceURI : (de && de.tagUrn) ? de.tagUrn : null;
	this.ns.xhtml1       = 'http://www.w3.org/1999/xhtml';
	this.ns.xhtml2       = 'http://www.w3.org/2002/06/xhtml2';
	this.ns.bAattrs      = 'urn:bA.attrs';

	this.prefix = {};
	this.prefix.bAattrs  = 'bAattrs:';

	this.ua = {};
	this.ua.isGecko      = ua.match(/Gecko\//);
	this.ua.isSafari     = ua.match(/AppleWebKit/);
	this.ua.isOpera      = window.opera;
	this.ua.isIE         = (d.all && !this.ua.isGecko && !this.ua.isSafari && !this.ua.isOpera);
	this.ua.isIE40       = (this.ua.isIE && ua.match(/MSIE 4\.0/));     // IE 4.0x
	this.ua.isIE45       = (this.ua.isIE && ua.match(/MSIE 4\.5/));     // IE 4.5x
	this.ua.isIE50       = (this.ua.isIE && ua.match(/MSIE 5\.0/));     // IE 5.0x
	this.ua.isIE55       = (this.ua.isIE && ua.match(/MSIE 5\.5/));     // IE 5.5x
	this.ua.isIE60       = (this.ua.isIE && ua.match(/MSIE 6\.0/));     // IE 6.0x
	this.ua.isIE70       = (this.ua.isIE && ua.match(/MSIE 7\.0/));     // IE 7.0x
	this.ua.isNN4        = d.layers;                                    // NN 4.x
	this.ua.isMac        = ua.match(/Mac/);
	this.ua.isWin        = ua.match(/Win/);
	this.ua.isWinIE      = this.ua.isWin && this.ua.isIE;
	this.ua.isMacIE      = this.ua.isMac && this.ua.isIE;
	this.ua.productSub   = navigator.productSub;
	this.ua.revision     = (this.ua.isIE    ) ? parseFloat(ua.match(/MSIE ([\d\.]+)/)[1])         :
	                       (this.ua.isGecko ) ? parseFloat(ua.match(/; rv:([\d\.]+)/)[1])         :
	                       (this.ua.isSafari) ? parseFloat(ua.match(/AppleWebKit\/([\d\.]+)/)[1]) :
	                       (this.ua.isOpera ) ? parseFloat(ua.match(/Opera.([\d\.]+)/)[1])        :
	                                            0;
	this.ua.DOMok        = (di) ? di.hasFeature('HTML','1.0') : (this.ua.isIE && de);

	this.env = {};
	this.env.online      = (lp == 'http:' || lp == 'https:');
	this.env.referer     = (typeof document.referrer == 'string') ? document.referrer : '';

	this.css = {};
	this.css.revise      = {
//		'Safari'   : 'revise_safari.css',
//		'IE50.Win' : 'revise_winie50.css'
	};
	this.css.reviseTitle = '';

	this.geom            = {};
}





/* --------------- EventHandler : window.onerror --------------- */

window.onerror = function() {
	if (typeof BA == 'object') {
		if (BA.debugMode) {
			var msg = 'Error: ' + arguments[0] + '\n' +
			          'File: '  + arguments[1] + '\n' + 
			          'Line: '  + arguments[2];
			alert(msg);
		}
		return true;
	}
}





/* ---------- Custom methods / Shortage methods of built-in objects ---------- */

/* ----- Function.apply() ----- */

if (!window.encodeURIComponent) {
	window.encodeURIComponent = function(str) {
		return escape(str);
	}
}

/* ----- Function.apply() ----- */

if (!Function.prototype.apply) {
	Function.prototype.apply = function(aThisObject, anArray) {
		if (typeof anArray != 'null' && typeof anArray != 'undefined' && typeof anArray != 'object') {
			throw 'Function.apply: second argument must be an array.';
		} else {
			if (typeof aThisObject != 'object' || !aThisObject) {
				aThisObject = window;
			}
			if (!anArray) {
				anArray = [];
			}
			var prop = '__Function_Apply_stored__';
			var args = [];
			for (var i = 0, n = anArray.length; i < n; i++) {
				args.push('anArray[' + i + ']');
			}

			aThisObject[prop] = this;
			var ret = eval('aThisObject.' + prop + '(' + args.join(',') + ')');
			try {
				delete aThisObject[prop];
			} catch(err) {
				aThisObject[prop] = null;
			}
			return ret;
		}
	}
}

/* ----- Function.call() ----- */

if (!Function.prototype.call) {
	Function.prototype.call = function(aThisObject /* , arg1, arg2 ... */) {
		var args = [];
		for (var i = 1, n = arguments.length; i < n; i++) {
			args.push(arguments[i]);
		}
		return this.apply(aThisObject, args);
	}
}

/* ----- Array.pop() ----- */

if (!Array.prototype.pop) {
	Array.prototype.pop = function() {
		if (!this.length) {
			return null;
		} else {
			var last = this[this.length - 1];
			--this.length;
			return last;
		}
	}
}

/* ----- Array.push() ----- */

if (!Array.prototype.push) {
	Array.prototype.push = function(args) {
		for (var i = 0, n = arguments.length; i < n; i++) {
			this[this.length] = arguments[i];
		}
		return this.length;
	}
}

/* ----- Array.shift() ----- */

if (!Array.prototype.shift) {
	Array.prototype.shift = function() {
		if (!this.length) {
			return null;
		} else {
			this.reverse();
			var ret = this.pop();
			this.reverse();
			return ret;
		}
	}
}

/* ----- Array.unshift() ----- */

if (!Array.prototype.unshift) {
	Array.prototype.unshift = function(args) {
		this.reverse();
		for (var i = arguments.length - 1; i >= 0; i--) {
			this.push(arguments[i]);
		}
		this.reverse();
		return this.length;
	}
}

/* ----- Array.splice() ----- */

if (!Array.prototype.splice) {
	Array.prototype.splice = function(index, howMany /* , element1, element2 ... */) {
		index     = (index < 0) ? Math.max(index + this.length, 0) : Math.min(index, this.length);
		howMany   = Math.min(Math.max(howMany || this.length, 0), this.length - index);
		var left  = this.slice(0, index);
		var right = this.slice(index + howMany);
		var ret   = this.slice(index, index + howMany);
		Array.prototype.slice.call(arguments, 2).forEach(function(item) { left.push(item) });
		this.length = 0;
		this.push.apply(this, left );
		this.push.apply(this, right);
		return ret;
	}
}

/* ----- Array.indexOf() ----- */

if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(aSearchElement, aFromIndex) {
		if (typeof aFromIndex != 'number') {
			aFromIndex = 0;
		} else if (aFromIndex < 0) {
			aFromIndex = this.length + aFromIndex;
		}
		for (var i = aFromIndex, n = this.length; i < n; i++) {
			if (this[i] === aSearchElement) {
				return i;
			}
		}
		return -1;
	}
}

/* ----- Array.lastIndexOf() ----- */

if (!Array.prototype.lastIndexOf) {
	Array.prototype.lastIndexOf = function(aSearchElement, aFromIndex) {
		if (typeof aFromIndex != 'number') {
			aFromIndex = this.length - 1;
		} else if (aFromIndex < 0) {
			aFromIndex = this.length + aFromIndex;
		}
		for (var i = aFromIndex; i >= 0; i--) {
			if (this[i] === aSearchElement) {
				return i;
			}
		}
		return -1;
	}
}

/* ----- Array.forEach() ----- */

if (!Array.prototype.forEach) {
	Array.prototype.forEach = function(aCallBack, aThisObject) {
		for (var i = 0, n = this.length; i < n; i++) {
			aCallBack.call(aThisObject, this[i], i, this);
		}
	}
}

/* ----- Array.map() ----- */

if (!Array.prototype.map) {
	Array.prototype.map = function(aCallBack, aThisObject) {
		var ret = [];
		for (var i = 0, n = this.length; i < n; i++) {
			ret.push(aCallBack.call(aThisObject, this[i], i, this));
		}
		return ret;
	}
}

/* ----- Array.filter() ----- */

if (!Array.prototype.filter) {
	Array.prototype.filter = function(aCallBack, aThisObject) {
		var ret = [];
		for (var i = 0, n = this.length; i < n; i++) {
			if (aCallBack.call(aThisObject, this[i], i, this)) {
				ret.push(this[i]);
			}
		}
		return ret;
	}
}

/* ----- Array.some() ----- */

if (!Array.prototype.some) {
	Array.prototype.some = function(aCallBack, aThisObject){
		for (var i = 0, n = this.length; i < n; i++) {
			if (aCallBack.call(aThisObject, this[i], i, this)) return true;
		}
		return false;
	}
}

/* ----- Array.every() ----- */

if (!Array.prototype.every) {
	Array.prototype.every = function(aCallBack, aThisObject){
		for (var i = 0, n = this.length; i < n; i++) {
			if(!aCallBack.call(aThisObject, this[i], i, this)) return false;
		}
		return true;
	}
}

/* ----- Array.equal() ----- */

if (!Array.prototype.equal) {
	Array.prototype.equal = function(anArray) {
		if (!anArray || this.length != anArray.length) {
			return false;
		} else {
			return this.every(function(value, i) {
				return (value === anArray[i]);
			});
		}
	}
}

/* ----- Number.formatNumberBA() ----- */

Number.prototype.formatNumberBA = function(format) {
	if (!format || typeof format != 'string') {
		throw 'Number.formatNumberBA: first argument must be a formatting string.';
	} else {
		var ret       = [];
		var intFormat = format.split('.')[0].split('');
		var decFormat = format.split('.')[1] || '';
		var value     = (decFormat) ? Math.abs(this) : Math.round(Math.abs(this));
		var sign      = (this < 0) ? '-' : '';
		var intValue  = value.toString().split('.')[0].split('');
		do {
			var _value  = intValue .pop() || '';
			var _format = intFormat.pop() || '';
			switch (_format) {
				case '0' : ret.push(_value  ? _value : '0');                        break;
				case '#' : ret.push(_value  ? _value : '' );                        break;
				case ''  : /* exit do-while loop */          intValue = [];         break;
				default  : ret.push(_format               ); intValue.push(_value); break;
			}
		} while (intValue.length > 0 || intFormat.length > 0);
		ret = ret.reverse().join('');
		if (decFormat) {
			var scale     = Math.pow(10, decFormat.length);
			var decValue  = (Math.round((value - Math.floor(value)) * scale) / scale).toString().split('.')[1] || '0';
			    decValue  = decValue .split('').reverse().join('');
			    decFormat = decFormat.split('').reverse().join('');
			    ret       = ret + '.' + decValue.formatNumberBA(decFormat).split('').reverse().join('');
		}
		if (decFormat.startsWithBA('#') && ret.endsWithBA('.0')) {
			ret = ret.getBeforeBA('.0');
		}
		return sign + ret.replace(/^\D+/, '');
	}
}

/* ----- String.formatNumberBA() ----- */

String.prototype.formatNumberBA = function(format) {
	var num = parseFloat(this, 10);
	if (isNaN(num)) {
		throw 'String.formatNumberBA: this string is not a number string.';
	} else {
		return num.formatNumberBA(format);
	}
}

/* ----- String.formatTextBA() ----- */

String.prototype.formatTextBA = function(strArray) {
	var str = this;
	if (strArray && strArray.constructor == Array) {
		for (var i = 0, n = strArray.length; i < n; i++) {
			str = str.replace(new RegExp('\\$\\{' + i + '\\}', 'g'), strArray[i]);
		}
	}
	return str;
}

/* ----- String.getBeforeBA() ----- */

String.prototype.getBeforeBA = function(str, include) {
	var idx = this.indexOf(str);
	return (idx == -1) ? '' : this.substring(0, idx) + (include ? str : '');
}

/* ----- String.getAfterBA() ----- */

String.prototype.getAfterBA = function(str, include) {
	var idx = this.indexOf(str);
	return (idx == -1) ? '' : (include ? str : '') + this.substring(idx + str.length, this.length);
}

/* ----- String.startsWithBA() ----- */

String.prototype.startsWithBA = function(str) {
	return (this.indexOf(str) == 0);
}

/* ----- String.endsWithBA() ----- */

String.prototype.endsWithBA = function(str) {
	var idx = this.lastIndexOf(str);
	return (idx > -1 && idx + str.length == this.length);
}

/* ----- String.relToAbsBA() ----- */

String.prototype.relToAbsBA = function(base) {
	var b   = base.split('/');
	var t   = this.split('/');
	var ptn = /^(\/|\w+:)/;
	if (!base.match(ptn)) {
		throw 'String.relToAbsBA: first argument must be an absolute path/URL.';
	} else if (this.match(ptn)) {
		return this;
	} else if (this.charAt(0) == '#' || this.charAt(0) == '?') {
		return base + this;
	} else if (t[0] == '.' || t[0] == '..') {
		return t.slice(1, t.length).join('/').relToAbsBA(b.slice(0, b.length - t[0].length).join('/') + '/');
	} else {
		return b.slice(0, b.length - 1).join('/') + '/' + this;
	}
}

/* ----- String.absToRelBA() ----- */

String.prototype.absToRelBA = function(base) {
	var ptn = /^(\/|\w+:)/;
	if (!base.match(ptn)) {
		throw 'String.absToRelBA: first argument must be an absolute path/URL.';
	} else if (!this.match(ptn)) {
		throw 'String.absToRelBA: String must be an absolute path/URL.';
	} else {
		return _compare(base, this) || base;
	}

	function _compare(base, trgt) {
		var b = base.split('/');
		var t = trgt.split('/');
		if (!base) {
			return trgt;
		} else if (!trgt) {
			return _goup(base);
		} else if (b[0] != t[0]) {
			return _goup(base) + trgt;
		} else {
			return arguments.callee(b.slice(1, b.length).join('/'), t.slice(1, t.length).join('/'));
		}
	}
	
	function _goup(path) {
		path = path.split('/');
		path.shift();
		path.forEach(function(elem, idx) { path[idx] = '..' });
		return path.join('/') + '/';
	}
}





/* ---------- Constructor : BAElement (Abstract Class) ---------- */

function BAElement() { }

BAElement.prototype = {
	instanceOf : 'BAElement',

	/* ----- BAElement.addEventListenerBA() ----- */
	addEventListenerBA : function(type, listener, aThisObject) {
		function _event_IE(_node) {
			var e  = window.event;
			var de = document.documentElement;
			var db = document.body;
			this.currentTarget   = _node;
			if (!e) return;
			this.type            = e.type;
			this.target          = e.srcElement;
			this.relatedTarget   = (e.srcElement == e.toElement) ? e.fromElement : e.toElement;
			this.clientX         = e.clientX;
			this.clientY         = e.clientY;
			this.pageX           = (de.scrollLeft ? de.scrollLeft : (db ? db.scrollLeft : 0)) + e.clientX;
			this.pageY           = (de.scrollTop  ? de.scrollTop  : (db ? db.scrollTop  : 0)) + e.clientY;
			this.charCode        = /* (this.type == 'keypress') ? */ e.keyCode /* : 0 */;
			this.keyCode         = /* (this.type != 'keypress') ? */ e.keyCode /* : 0 */;
			this.ctrlKey         = e.ctrlKey;
			this.shiftKey        = e.shiftKey;
			this.altKey          = e.altKey;
			this.metaKey         = e.metaKey;
			this.detail          = e.detail;
			this.wheelDelta      = e.wheelDelta;
			this.stopPropagation = function() { e.cancelBubble = true  };
			this.preventDefault  = function() { e.returnValue  = false };
		}

		function _callListeners(e) {
			var stored = this.__addEventListenerBA_stored__;
			var type   = e.type;
			if (BA.ua.isGecko && type == 'DOMMouseScroll') {
				e.wheelDelta = e.detail * -40;
			} else if (BA.ua.isSafari && BA.ua.revision < 412) { // preventDefault() doesn't work on Safari before 2.0.4
				e.preventDefault = function() { this.currentTarget['on' + this.type] = function() { return false } };
			}
			if (stored && stored[type]) {
				stored[type].forEach(function(func) { func.call(null, e) });
			}
		}

		// preparations.
		if (!type || typeof type != 'string') {
			throw 'BAElement.addEventListenerBA: first argument must be a string (event type).';
		} else if (!listener || typeof listener != 'function') {
			throw 'BAElement.addEventListenerBA: second argument must be a function (event listener).';
		} else if (BA.ua.isOpera && BA.ua.revision < 9.02 && type == 'mousewheel') {
			return;  // Opera before 9.02 has a bug on 'mousewheel' event (user cannot scroll-up by mousewheel).
		} else if (BA.ua.isGecko && type == 'mousewheel') {
			type = 'DOMMouseScroll';
		}

		// create event caller.
		var stored = this.__addEventListenerBA_stored__;
		if (!stored) {
			stored = this.__addEventListenerBA_stored__ = {};
		}
		var funcs = stored[type];
		if (!funcs) {
			funcs = stored[type] = [];
			if (this.addEventListener) { // standard compliant browsers
				this.addEventListener(type, _callListeners, false);
			} else {                     // WinIE/MacIE
				var _this  = (this.window) ? this.window : this; // workaround for WinIE
				var exists = _this['on' + type];
				_this['on' + type] = (exists) ?
					function() { exists(); _callListeners.call(_this, new _event_IE(_this)) } :
					function() {           _callListeners.call(_this, new _event_IE(_this)) } ;
			}
		}

		// register event listener.
		if (funcs.indexOf(listener) > -1) {
			return;
		} else if (typeof aThisObject == 'object' && aThisObject != null) {
			funcs.push(BACreateDelegate(listener, aThisObject));
		} else {
			funcs.push(listener);
		}

		// preparations for BACleanUpEventListeners()
		var nodes = window.BA_EVENTLISTENER_STORED_NODES;
		if (!nodes) {
			nodes = window.BA_EVENTLISTENER_STORED_NODES = [];
		}
		if (nodes.indexOf(this) == -1) {
			nodes.push(this);
		}
	},

	/* ----- BAElement.removeEventListenerBA() ----- */
	removeEventListenerBA : function(type, listener) {
		// preparations.
		if (!type || typeof type != 'string') {
			throw 'BAElement.removeEventListenerBA: first argument must be a string (event type).';
		} else if (!listener || typeof listener != 'function') {
			throw 'BAElement.removeEventListenerBA: second argument must be a function (event listener).';
		} else if (BA.ua.isGecko && type == 'mousewheel') {
			type = 'DOMMouseScroll';
		}

		// remove event listener.
		var stored = this.__addEventListenerBA_stored__;
		if (stored && stored[type] && stored[type].length > 0) {
			var funcs = stored[type];
			var index = funcs.indexOf(listener);
			if (index > -1) {
				funcs.splice(index, 1);
			}
		}
	},

	/* ----- BAElement.dispatchEventBA() ----- */
	
	dispatchEventBA : function(e) {
		if (!e || typeof e != 'object') {
			throw 'BAElement.dispatchEventBA: first argument must be an Event object.';
		} else if (this.dispatchEvent) {
			this.dispatchEvent(e);
		} else if (this.fireEvent) {
			this.fireEvent('on' + e.type);
		} else if (this['on' + e.type]) {
			this['on' + e.type]();
		}
	},

	/* ----- BAElement.createElementBA() ----- */
	createElementBA : function(tagName) {
		if (!tagName || typeof tagName != 'string') {
			throw 'BAElement.createElementBA: first argument must be a string (tagName).';
		} else {
			var node = (BA.ns.defaultNS && document.createElementNS && tagName.match(/:/)) ?
			           	document.createElementNS(BA.ns[tagName.split(':')[0]], tagName.split(':')[1]) :
			           	(BA.ns.defaultNS && document.createElementNS) ?
			           		document.createElementNS(BA.ns.defaultNS, tagName) : 
			           		document.createElement(tagName) ;
			return BARegisterDOMMethodsTo(node);
		}
	},

	/* ----- BAElement.getElementsByTagNameBA() ----- */
	getElementsByTagNameBA : function(tagName) {
		var ret = [];
		if (!tagName || typeof tagName != 'string') {
			throw 'BAElement.getElementsByTagNameBA: first argument must be a string (tagName).';
		} else if (tagName == '*') {
			ret = this.getElementsByTagName(tagName);
			if (BA.ua.isIE || ret.length == 0) {
				ret = (document.all && this === document) ?
					document.all :
					(function(_node) {
						var _nodes = _node.childNodes;
						var _ret   = [];
						for (var i = 0, n = _nodes.length; i < n; i++) {
							var __node = _nodes[i];
							if (__node.nodeType == 1 && __node.nodeName != '!') {
								_ret.push(__node);
							}
							_ret = _ret.concat(arguments.callee(__node));
						}
						return _ret;
					})(this);
			}
		} else if (tagName.match(/:/)) {
			var prfx = tagName.split(':')[0];
			var name = tagName.split(':')[1];
			     ret = (BA.ns.defaultNS && this.getElementsByTagNameNS) ?
			           	this.getElementsByTagNameNS(BA.ns[prfx], name) :
			           	this.getElementsByTagName(tagName) ;
			if (ret.length == 0) {
				       ret = (name == '*') ? this.getElementsByTagNameBA(name) : this.getElementsByTagName(name);
				var _nodes = [];
				for (var i = 0, n = ret.length; i < n; i++){
					var _node = ret[i];
					if (BA.ns.defaultNS && _node.namespaceURI == BA.ns[prfx] || _node.tagUrn == BA.ns[prfx]) {
						_nodes.push(_node);
					}
				}
				if (_nodes.length == 0) {
					       ret = (name == '*') ? ret : this.getElementsByTagNameBA('*');
					var _nodes = [];
					for (var i = 0, n = ret.length; i < n; i++) {
						var _node = ret[i];
						var _prfx = _node.nodeName.split(':')[0];
						var _name = _node.nodeName.split(':')[1];
						if (_name && _prfx == prfx && (name == '*' || _name.toLowerCase() == name.toLowerCase())) {
							_nodes.push(_node);
						}
					}
				}
				ret = _nodes;
			}
		} else {
			ret = (BA.ns.defaultNS && this.getElementsByTagNameNS) ?
			      	this.getElementsByTagNameNS(BA.ns.defaultNS, tagName) :
			      	(tagName.match(/^body$/i) && document.body) ?
			      		/* workaround for Netscape7.1 */ [document.body] :
			      		this.getElementsByTagName(tagName) ;
			if (typeof document.documentElement.tagUrn == 'string') {
				var _nodes = [];
				for (var i = 0, n = ret.length; i < n; i++){
					var _node = ret[i];
					if (!_node.tagUrn || _node.tagUrn == BA.ns.defaultNS) {
						_nodes.push(_node);
					}
				}
				ret = _nodes;
			}
		}

		['indexOf', 'lastIndexOf', 'forEach', 'map', 'filter', 'some', 'every', 'equal'].forEach(function(method) {
			if (!ret[method]) ret[method] = function() { return Array.prototype[method].apply(this, arguments) }
		});
		return ret;
	},

	/* ----- BAElement.getElementsByClassNameBA() ----- */
	getElementsByClassNameBA : function(className, tagName) {
		if (!className || typeof className != 'string') {
			throw 'BAElement.getElementsByClassNameBA: first argument must be a string (className).';
		} else {
			return this.getElementsByTagNameBA(tagName || '*').filter(function(node) {
				return (node.hasClassNameBA && node.hasClassNameBA(className));
			});
		}
	},

	/* ----- BAElement.getAncestorsByTagNameBA() ----- */
	getAncestorsByTagNameBA : function(tagName) {
		if (!tagName || typeof tagName != 'string') {
			throw 'BAElement.getAncestorsByTagNameBA: first argument must be a string (tagName).';
		} else {
			var fNode = document.getElementsByTagNameBA(tagName);
			var cNode = this.parentNode;
			var ret   = [];
			while (cNode) {
				var idx = fNode.indexOf(cNode);
				if (idx != -1) {
					ret.push(fNode[idx]);
				}
				cNode = cNode.parentNode;
			}
			return ret;
		}
	},

	/* ----- BAElement.getAncestorsByClassNameBA() ----- */
	getAncestorsByClassNameBA : function(className, tagName) {
		if (!className || typeof className != 'string') {
			throw 'BAElement.getAncestorsByClassNameBA: first argument must be a string (className).';
		} else {
			var fNode = document.getElementsByClassNameBA(className, tagName);
			var cNode = this.parentNode;
			var ret   = [];
			while (cNode) {
				var idx = fNode.indexOf(cNode);
				if (idx != -1) {
					ret.push(fNode[idx]);
				}
				cNode = cNode.parentNode;
			}
			return ret;
		}
	},

	/* ----- BAElement.appendChildBA() ----- */
	appendChildBA : function(content, forceAsHTML) {
		if (typeof content == 'undefined' || typeof content == 'object' && !content) {
			throw 'BAElement.appendChildBA: first argument must be a node or a string or a BATag instance.';
		} else if (content.nodeType == 1 || content.nodeType == 3) {
			return this.appendChild(content);
		} else if (content.instanceOf == BATag || forceAsHTML) {
			content = content.toString();
			if (document.createRange) {            // Gecko/Safari
				var range = document.createRange();
				range.selectNode(this);
				content = range.createContextualFragment(content); // createContextualFragment() is not standard-DOM?
				this.appendChild(content);
			} else if (this.insertAdjacentHTML) {  // WinIE/MacIE
				this.insertAdjacentHTML('beforeEnd', content);
			} else {                               // Others
				this.innerHTML += content;
			}
			BARegisterDOMMethodsTo(this, true);
			return content;
		} else if (content.toString) {
			content  = content.toString();
			var node = document.createTextNode(content);
			if (this.insertAdjacentText) {         // WinIE/MacIE
				this.insertAdjacentText('beforeEnd', content);
				return node;
			} else {                               // Others
				return this.appendChild(node);
			}
		}
	},

	/* ----- BAElement.removeChildBA() ----- */
	removeChildBA : function(node) {
		if (!node || typeof node.nodeType != 'number') {
			throw 'BAElement.removeChildBA: first argument must be a DOM node.';
		} else {
			return BARegisterDOMMethodsTo(this.removeChild(node));
		}
	},

	/* ----- BAElement.removeAllChildsBA() ----- */
	removeAllChildsBA : function() {
		var ret = [];
		while (this.hasChildNodes()) {
			ret.push(this.removeChildBA(this.firstChild));
		}
		return ret;
	},

	/* ----- BAElement.getInnerTextBA() ----- */
	getInnerTextBA : function() {
		var nodes = this.childNodes;
		var ret   = [];
		for (var i = 0, n = nodes.length; i < n; i++) {
			if (nodes[i].hasChildNodes()) {
				ret.push(nodes[i].getInnerTextBA());
			} else if (nodes[i].nodeType == 3) {
				ret.push(nodes[i].nodeValue);
			} else if (nodes[i].alt) {
				ret.push(nodes[i].alt);
			}
		}
		return ret.join('').replace(/\s+/g, ' ');
	},

	/* ----- BAElement.getAttributeBA() ----- */
	getAttributeBA : function(attr) {
		if (!attr || typeof attr != 'string') {
			throw 'BAElement.getAttributeBA: first argument must be a string (atribute name).';
		} else {
			if (BA.ua.isIE && attr == 'class') {
				attr += 'Name';
			}
			var ret = this.getAttribute(attr);
			if (!ret && this.getAttributeNS && attr.match(/:/)) {
				var prfx = attr.split(':')[0];
				var attr = attr.split(':')[1];
				return this.getAttributeNS(BA.ns[prfx], attr)
			}
			return ret;
		}
	},

	/* ----- BAElement.setAttributeBA() ----- */
	setAttributeBA : function(attr, value) {
		if (!attr || typeof attr != 'string') {
			throw 'BAElement.setAttributeBA: first argument must be a string (atribute name).';
		} else if (attr.match(/:/)) {
			var prfx = attr.split(':')[0];
			var attr = attr.split(':')[1];
			if (this.setAttributeNS && this.namespaceURI || BA.ua.isSafari) {
				this.setAttributeNS(BA.ns[prfx], attr, value);
			} else {
				this.setAttribute('xmlns:' + prfx, BA.ns[prfx]);
				this.setAttribute(prfx + ':' + attr, value);
			}
		} else {
			if (BA.ua.isIE && attr == 'class') attr += 'Name';
			this.setAttribute(attr, value);
		}
	},

	/* ----- BAElement.hasClassNameBA() ----- */
	hasClassNameBA : function(className) {
		if (!className || typeof className != 'string') {
			throw 'BAElement.hasClassNameBA: first argument must be a string (className).';
		} else if (this.nodeType != 1) {
			return false;
		} else {
			return (this.getAttributeBA('class') || '').split(' ').some(function(name) { return (name == className) });
		}
	},

	/* ----- BAElement.appendClassNameBA() ----- */
	appendClassNameBA : function(className) {
		if (!className || typeof className != 'string') {
			throw 'BAElement.appendClassNameBA: first argument must be a string (className).';
		} else if (!this.hasClassNameBA(className)) {
			var cname = (this.getAttributeBA('class') || '').split(' ');
			cname.push(className);
			this.setAttributeBA('class', cname.join(' '));
		}
	},

	/* ----- BAElement.removeClassNameBA() ----- */
	removeClassNameBA : function(className) {
		if (!className || typeof className != 'string') {
			throw 'BAElement.removeClassNameBA: first argument must be a string (className).';
		} else if (this.hasClassNameBA(className)) {
			var cname = this.getAttributeBA('class').split(' ');
			var index = cname.indexOf(className);
			if (index > -1) {
				cname.splice(index, 1);
				this.setAttributeBA('class', cname.join(' '));
			}
		}
	},

	/* ----- BAElement.normalizeTextNodeBA() ----- */
	normalizeTextNodeBA : function(deep) {
		(function(curNode) {
			for (var i = 0, n = curNode.childNodes.length; i < n; i++) {
				var node = curNode.childNodes[i];
				if (node.nodeType == 3) {
					node.nodeValue = node.nodeValue.replace(/^\s+/, '');
					node.nodeValue = node.nodeValue.replace(/\s+$/, '');
					if (node.nodeValue.match(/^\s*$/))  {
						node.parentNode.removeChildBA(node);
						i--;
					}
				} else if (deep && node.nodeType == 1 && node.hasChildNodes()) {
					arguments.callee(node);
				}
			}
		})(this);
	},

	/* ----- BAElement.getAbsoluteOffsetBA() ----- */
	getAbsoluteOffsetBA : function() {
		var offset = { X : this.offsetLeft, Y : this.offsetTop };
		if (this.offsetParent) {
			var op = this.offsetParent.getAbsoluteOffsetBA();
			// IE returns wrong value if 'offsetParent block' is position-relative...
			offset.X += op.X;
			offset.Y += op.Y;
		}
		return offset;
	},

	/* ----- BAElement.getCurrentStyleBA() ----- */
	getCurrentStyleBA : function(property, pseudo) {
		if (!property || typeof property != 'string') {
			throw 'BAElement.getCurrentStyleBA: first argument must be a string (property name).';
		} else {
			try {
				return (window.getComputedStyle) ?
					window.getComputedStyle(this, pseudo)[property] : (this.currentStyle) ?
						this.currentStyle[property] : '';
			} catch (err) { // netscape 7.0x causes exception above.
				return '';
			}
		}
	},

	/* ----- BAElement.setPositionFixedBA() ----- */
	setPositionFixedBA : function(setting) {
		if (this.__setPositionFixedBA_applied__) return;
		this.__setPositionFixedBA_applied__ = true;

		var ignoreX    = (setting && setting.ignoreX );
		var ignoreY    = (setting && setting.ignoreY );
		var autoHide   = (setting && setting.autoHide);
		var reviseLeft = 0;
		var reviseTop  = 0;
		var oDisplay   = this.getCurrentStyleBA('display');

		if (BA.ua.isWinIE) {
			this.style.display = 'block';
		} else if (BA.ua.isMacIE) {
			var paddingLeft = this.getCurrentStyleBA('paddingLeft');
			var paddingTop  = this.getCurrentStyleBA('paddingTop' );
			if (/px$/.test(paddingLeft)) reviseLeft = -1 * parseFloat(paddingLeft);
			if (/px$/.test(paddingTop )) reviseTop  = -1 * parseFloat(paddingTop );
		}

		this.style.position = (!ignoreX && !ignoreY && !BA.ua.isWinIE) ? 'fixed' : 'absolute';
		this.style.left     = (this.getAbsoluteOffsetBA().X + reviseLeft) + 'px';
		this.style.top      = (this.getAbsoluteOffsetBA().Y + reviseTop ) + 'px';
		this.style.right    = 'auto';
		this.style.bottom   = 'auto';
		this.style.margin   = '0';
		if (oDisplay) {
			this.style.display = oDisplay;
		}

		var node = this;
		var timer;
		_movePosition();
		window.addEventListenerBA('scroll', _movePosition);

		function _movePosition(e) {
			if (timer) timer.clearTimer();
			BAGetGeometry();
			if (!ignoreX && node.style.position == 'absolute') node.style.marginLeft = BA.geom.scrollX + 'px';
			if (!ignoreY && node.style.position == 'absolute') node.style.marginTop  = BA.geom.scrollY + 'px';
			if (autoHide) {
				node.style.visibility = 'hidden'
				timer = new BASetTimeout(function() { node.style.visibility = 'visible' }, 100);
			}
		}
	}
}





/* --------------- Function : BARegisterDOMMethodsTo --------------- */

function BARegisterDOMMethodsTo(node, deep) {
	if (node && node.instanceOf != 'BAElement' && (node === document || node.nodeType == 1)) {
		if (!BA.ua.isIE50                                                 /* this causes error in WinIE5.0x */
		    && node.nodeName && node.nodeName.toLowerCase() != 'object'   /* this causes crash in WinIE when printing */
		    && document.documentElement.instanceOf == 'BAElement'
		    && node.mergeAttributes) {
		    	node.mergeAttributes(document.documentElement);
		} else {
			for (var i in BAElement.prototype) {
				node[i] = BAElement.prototype[i];
			}
		}
	}
	if (deep === true && deep && node.hasChildNodes()) {
		for (var i = 0, n = node.childNodes.length; i < n; i++) {
			arguments.callee(node.childNodes[i], true);
		}
	}
	return node;
}



/* --------------- Function : BAAddOnload --------------- */

function BAAddOnload(func) {
	var target = (BA.ua.isGecko || BA.ua.isOpera) ? document           : window;
	var type   = (BA.ua.isGecko)                  ? 'DOMContentLoaded' : 'load';
	target.addEventListenerBA(type, func);
}



/* --------------- Function : BAAddDuringLoad --------------- */

function BAAddDuringLoad(func, wait) {
	if (!wait) wait = 500;
	var oFunc = arguments.callee;
	    oFunc.__store__ = [];
	    oFunc.__store__.push(new BASetInterval(func, wait));
	BAAddOnload(func);
	BAAddOnload(function() {
		for (var i = 0, n = oFunc.__store__.length; i < n; i++) {
			oFunc.__store__[i].clearTimer();
		}
	});
}



/* --------------- Function : BAAddOnunload --------------- */

function BAAddOnunload(func) {
	window.addEventListenerBA('unload', func);
}



/* --------------- Function : BAGetCommonDir --------------- */

function BAGetCommonDir(dirName) {
	var sheets = BAGetStyleSheets();
	var ret    = '';
	if (sheets && sheets[0]) {
		var dir  = '/' + dirName + '/';
		var href = sheets[0].href;
		if (!href.match(/^(\.\.?\/|\w+:)/) && !href.startsWithBA('/')) {
			href = '/' + href;
		}
		if (href.indexOf(dir) > -1) {
			ret = href.getBeforeBA(dir, true);
			if (!ret.match(/^\w+:/)) {   // if ret is not absolute url (workaround for IE)
				var base = document.getElementsByTagName('base')[0];
				var burl = (base) ? base.href : location.href;
				if (ret.startsWithBA('/')) {
					ret = burl.match(/^\w+:\/*[^\/]+/) + ret;
				} else {
					ret = ret.relToAbsBA(burl);
				}
			}
		}
	}
	return ret;
}



/* --------------- Function : BAGetStyleSheets --------------- */

function BAGetStyleSheets() {
	var sheets = document.styleSheets;

	// workaround for Safari's lack of implement of document.styleSheets!
	if (sheets && sheets.length == 0 && document.getElementsByTagName) {
		var nodes  = document.getElementsByTagName('link');
            sheets = [];
		for (var i = 0, n = nodes.length; i < n; i++) {
			var rel = nodes[i].getAttribute('rel') || '';
			if (rel.endsWithBA('stylesheet')) {
				sheets.push(nodes[i]);
			}
		}
	}

	return sheets;
}



/* --------------- Function : BAGetActiveCSSTitle --------------- */

function BAGetActiveCSSTitle() {
	var sheets = BAGetStyleSheets();
	var ret    = '';
	if (sheets) {
		for (var i = 0, n = sheets.length; i < n; i++) {
			if (!sheets[i].disabled && sheets[i].title) {
				ret = sheets[i].title;
				break;
			}
		}
	}
	return ret;
}



/* --------------- Function : BASingleton --------------- */

function BASingleton(_constructor) {
	return _constructor.__BASingleInstance__ || (_constructor.__BASingleInstance__ = new _constructor());
}



/* --------------- Function : BACreateDelegate --------------- */

function BACreateDelegate(func, aThisObject){
	var delegate = function(){
		return func.apply(aThisObject, arguments);
	};
	delegate.func        = func;
	delegate.aThisObject = aThisObject;
	return delegate; 
}



/* --------------- Function : BAAlreadyApplied --------------- */

function BAAlreadyApplied(func) {
	if (!BA.ua.DOMok || func.__BAAlreadyApplied__) return true;
	func.__BAAlreadyApplied__ = true;
	return false;
}



/* --------------- Function : BAConcatNodeList --------------- */

function BAConcatNodeList() {
	var nodes = [];
	(function(list) {
		for (var i = 0, n = list.length; i < n; i++) {
			if (list[i].nodeType == 1) {
				nodes.push(list[i]);
			} else if (list[i].length > 0) {
				arguments.callee(list[i]);
			}
		}
	})(arguments);
	return nodes;
}



/* --------------- Function : BAPreloadImage --------------- */

function BAPreloadImage(src) {
	var img = new Image();
	img.src = src;
	return img;
}



/* --------------- Function : BAOpenWindow --------------- */

function BAOpenWindow(url, target, width, height, options, moveFlag) {
	if (!target) target = '_blank';
	var optVariations = {
		'exampleTarget1' : 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes',
		'exampleTarget2' : 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no'
	}
	options = options || optVariations[target] || '';
	width += (options.match('scrollbars=yes')) ? 16 : 0;
	if (width && height) options = 'width=' + width + ',height=' + height + (options ? ',' + options : '');
	var newWin = window.open(url, target, options);
	newWin.focus();
	if (moveFlag) newWin.moveTo(0, 0);
	if (window.event) window.event.returnValue = false;
	return newWin;
}



/* --------------- Function : BAOpenFullscreenWindow --------------- */

function BAOpenFullscreenWindow(url, target, options) {
	options = options || 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no';
	return BAOpenWindow(url, target, screen.availWidth, screen.availHeight, options, true);
}



/* --------------- Function : BAAppendJS --------------- */

function BAAppendJS(src) {
	var E = new BATag('script');
	E.setAttributeBA('type', 'text/javascript');
	E.setAttributeBA('src' , src.replace(/~/g, '%7E'));
	E.appendChildBA('');
	document.write(E.toString());
}



/* --------------- Function : BAAppendCSS --------------- */

function BAAppendCSS(href, title, media) {
	var act = BAGetActiveCSSTitle();
	var E   = new BATag('link');
	E.setAttributeBA('rel', ((!title || !act || title == act) ? 'stylesheet' : 'alternate stylesheet'));
	E.setAttributeBA('type', 'text/css');
	E.setAttributeBA('media', (media || 'all'));
	E.setAttributeBA('href', href.replace(/~/g, '%7E'));
	if (title) E.setAttributeBA('title', title);
	document.write(E.toString());
}



/* --------------- Function : BAAppendStateClassName --------------- */

function BAAppendStateClassName(className) {
	if (typeof className != 'string' || !className) {
		throw 'BASetStateClassName: argument must be a string.';
	} else {
		var body = document.getElementsByTagNameBA('body')[0];
		if (!body) {
			throw 'BASetStateClassName: <body> element not exists.';
		} else {
			body.appendClassNameBA(className);
		}
	}
}



/* --------------- Function : BARemoveStateClassName --------------- */

function BARemoveStateClassName(className) {
	if (typeof className != 'string' || !className) {
		throw 'BASetStateClassName: argument must be a string.';
	} else {
		var body = document.getElementsByTagNameBA('body')[0];
		if (!body) {
			throw 'BASetStateClassName: <body> element not exists.';
		} else {
			body.removeClassNameBA(className);
		}
	}
}



/* --------------- Function : BASetWording --------------- */

function BASetWording(expression, str) {
	if (typeof expression != 'string' || typeof str != 'string') {
		throw 'BASetWording: argument type must be string.';
	} else if (!expression) {
		throw 'BASetWording: not enough arguments.';
	} else {
		var props  = expression.split('.');
		var target = 'window';
		while (props.length > 1) {
			var type    = null;
			    target += '.' + props.shift();
			try {
				type = (typeof eval(target));
			} catch(err) { }
			switch (type) {
				case 'undefined' :
					eval(target + ' = {}');
					break;
				case 'object' :
					break;
				default  : 
					throw 'BASetWording: specified expression is unavailable.';
					return;
			}
		}
		var type = null;
		try {
			type = typeof eval('window.' + expression);
		} catch(err) {
			throw 'BASetWording: specified expression is unavailable.';
		}
		if (type == 'undefined' || type == 'string') {
			eval(expression + ' = "' + str + '"');
		}
	}
}



/* --------------- Function : BAGetGeometry --------------- */

function BAGetGeometry(e) {
	var w = window;
	var d = document.documentElement;
	var b = document.getElementsByTagNameBA('body')[0];
	var isWinIEqm = BA.ua.isWinIE && (!document.compatMode || document.compatMode == 'BackCompat');
	var isMacIE   = BA.ua.isMacIE;
	var isSafari  = BA.ua.isSafari;

	BA.geom.scrollBar = _getScrollBarWidth();
	BA.geom.scrollX   = w.scrollX     || d.scrollLeft || b.scrollLeft || 0;
	BA.geom.scrollY   = w.scrollY     || d.scrollTop  || b.scrollTop  || 0;
	BA.geom.windowW   = w.innerWidth  || (isMacIE ? b.scrollWidth  : d.offsetWidth );
	BA.geom.windowH   = w.innerHeight || (isMacIE ? b.scrollHeight : d.offsetHeight);
	BA.geom.pageW     = (isMacIE) ? d.offsetWidth  : (isWinIEqm) ? b.scrollWidth  : d.scrollWidth ;
	BA.geom.pageH     = (isMacIE) ? d.offsetHeight : (isWinIEqm) ? b.scrollHeight : d.scrollHeight;
	BA.geom.windowX   = (!e) ? (BA.geom.windowX  ||  0) : e.clientX - ( isSafari ? BA.geom.scrollX : 0);
	BA.geom.windowY   = (!e) ? (BA.geom.windowY  ||  0) : e.clientY - ( isSafari ? BA.geom.scrollY : 0);
	BA.geom.mouseX    = (!e) ? (BA.geom.mouseX   ||  0) : e.clientX + (!isSafari ? BA.geom.scrollX : 0);
	BA.geom.mouseY    = (!e) ? (BA.geom.mouseY   ||  0) : e.clientY + (!isSafari ? BA.geom.scrollY : 0);
	BA.geom.nodeName  = (!e) ? (BA.geom.nodeName || '') : e.target.nodeName;

	if (BA.debugMode) {
		var arr = [BA.geom.windowW, BA.geom.windowH, BA.geom.pageW , BA.geom.pageH , BA.geom.windowX, BA.geom.windowY,
		           BA.geom.scrollX, BA.geom.scrollY, BA.geom.mouseX, BA.geom.mouseY, BA.geom.nodeName];
		var msg ='window: ${0} x ${1} | page: ${2} x ${3} | scroll: ${6}, ${7} | pos (viewport): ${4}, ${5} | pos (absolute): ${8}, ${9} | node: ${10}';
		BA_STATUSMSG.set('[Debug Mode: ON] | ' + msg.formatTextBA(arr));
	}

	function _getScrollBarWidth() {
		var id   = 'BAGetGeometry_getScrollBarWidth_testNode';
		var node = document.getElementById(id);
		if (!node) {
			node = document.createElementBA('ins');
			node.id = id;
			node.style.display    = 'block';
			node.style.visibility = 'hidden';
			node.style.overflow   = 'scroll';
			node.style.position   = 'absolute';
			node.style.border     = 'none';
			node.style.top        = node.style.left    = '-10000px';
			node.style.width      = node.style.height  = '100px';
			node.style.margin     = node.style.padding = '0';
			b.appendChildBA(node);
		}
		return (node.offsetWidth - node.clientWidth);
	}
}



/* ----- BAGetZoomRatio ----- */

function BAGetZoomRatio() {
	var per = 1;
	if (BA.ua.isIE70) {
		if (!document.compatMode || document.compatMode == 'BackCompat') {
			per = document.documentElement.offsetWidth / document.body.offsetWidth;
		} else {
			BAGetGeometry();
			var ins = document.createElementBA('ins');
			ins.style.display  = 'block';
			ins.style.position = 'absolute';
			ins.style.top      = '-100px';
			ins.style.left     = '0px';
			ins.style.width    = (BA.geom.pageW * 10) + 'px';
			document.body.appendChildBA(ins);
			BAGetGeometry();
			per = BA.geom.pageW / ins.offsetWidth;
			document.body.removeChildBA(ins);
		}
	}
	return per;
}



/* --------------- Constructor : BASetTimeout --------------- */

function BASetTimeout(func, ms, aThisObject) {
	this.storeIndex = 0;
	this.timer      = null;
	this.storeName   = 'BA_SETTIMEOUT_STOREDFUNC';
	this.removerName = 'BA_SETTIMEOUT_STOREDFUNC_REMOVER';

	if (arguments.length) this.storeFunc(func, ms, aThisObject);
}

BASetTimeout.prototype = {
	storeFunc : function(func, ms, aThisObject) {
		if (!window[this.storeName]) {
			window[this.removerName] = [];
			window[this.storeName]   = [];
			window[this.storeName].remove = function(_idx) {
				this[_idx] = null;
			};
		}

		this.storeIndex = window[this.storeName].push(BACreateDelegate(func, aThisObject));
		this.storeIndex--;
		this.setTimer(ms);
	},

	setTimer : function(ms) {
		var lang; if (BA.ua.isIE) lang = 'JScript'; // workaround to the page weaved with vbscript.
		this.timer = setTimeout('window.' + this.storeName + '[' + this.storeIndex + ']()', ms, lang);
		this.removeFunc(ms);
	},
	
	clearTimer : function() {
		if (this.timer) {
			this.clearTimerMain();
			clearTimeout(window[this.removerName][this.storeIndex]);
			this.timer = null;
			this.removeFunc(0);
		}
	},

	clearTimerMain : function() {
		clearTimeout(this.timer);
	},

	removeFunc : function(delay) {
		var lang; if (BA.ua.isIE) lang = 'JScript'; // workaround to the page weaved with vbscript.
		window[this.removerName][this.storeIndex]
			= setTimeout('window.' + this.storeName + '.remove(' + this.storeIndex + ')', delay + 1000, lang);
	}
}



/* --------------- Constructor : BASetInterval inherits BASetTimeout --------------- */

function BASetInterval(func, ms, aThisObject) {
	this.storeName   = 'BA_SETINTERVAL_STOREDFUNC';
	this.removerName = 'BA_SETINTERVAL_STOREDFUNC_REMOVER';

	if (arguments.length) this.storeFunc(func, ms, aThisObject);
}

BASetInterval.prototype = new BASetTimeout;

BASetInterval.prototype.setTimer = function(ms) {
	var lang; if (BA.ua.isIE) lang = 'JScript'; // workaround to the page weaved with vbscript.
	this.timer = setInterval('window.' + this.storeName + '[' + this.storeIndex + ']()', ms, lang);
}

BASetInterval.prototype.clearTimerMain = function() {
	clearInterval(this.timer);
}



/* --------------- Instance : BAStatusDisplay --------------- */

function BAStatusDisplay(node, defaultMsg) {
	this.statusNode = node;
	this.defaultMsg = defaultMsg;

	if (arguments.length) this.init();
}

BAStatusDisplay.prototype = {
	init : function() {
		if (!this.defaultMsg) {
			this.setDefaultMsg(this.statusNode.getInnerTextBA());
		}
		this.statusNode.removeAllChildsBA();
		this.statusNode.appendChildBA('');
		this.unset();
	},

	setDefaultMsg : function(msg) {
		this.defaultMsg = msg;
	},

	set : function(msg, delay, sustain) {
		this.msg     = (typeof msg     != 'undefined' && msg        ) ? msg     : '';
		this.delay   = (typeof delay   == 'number'    && delay   > 0) ? delay   :  0;
		this.sustain = (typeof sustain == 'number'    && sustain > 0) ? sustain :  0;
		if (this.delay > 0) {
			this.delayTimer = new BASetTimeout(this.setProcess, this.delay, this);
		} else {
			this.setProcess();
		}
	},

	unset : function() {
		this.clearTimer();
		this.unsetMsg();
	},

	setProcess : function() {
		this.clearTimer();
		if (this.msg) {
			this.setMsg();
			if (this.sustain > 0) {
				this.sustainTimer = new BASetTimeout(this.unset, this.sustain, this);
			}
		} else {
			this.unset();
		}
	},

	setMsg : function() {
		if (this.statusNode) {
			this.statusNode.removeAllChildsBA();
			this.statusNode.appendChildBA(this.msg);
		}
	},

	unsetMsg : function() {
		if (this.statusNode) {
			this.statusNode.removeAllChildsBA();
			this.statusNode.appendChildBA(this.defaultMsg);
		}
	},

	clearTimer : function() {
		if (this.delayTimer  ) this.delayTimer.clearTimer();
		if (this.sustainTimer) this.sustainTimer.clearTimer();
	}
}



/* --------------- constructor : BAStatusMsg inherits BAStatusDisplay --------------- */

function BAStatusMsg() {
	this.defaultMsg = window.defaultStatus || '';
}

BAStatusMsg.prototype = new BAStatusDisplay;

BAStatusMsg.prototype.init = function() { }

BAStatusMsg.prototype.setMsg = function() {
	window.status = this.msg;
}

BAStatusMsg.prototype.unsetMsg = function() {
	window.status = this.defaultMsg;
}



/* --------------- Constructor : BATimer --------------- */

function BATimer() { 
	this.reset();
}

BATimer.prototype = {
	reset : function() {
		this.startTime = (new Date()).getTime();
	},
	
	getTime : function() {
		return (new Date()).getTime() - this.startTime;
	},
	
	getSeconds : function() {
		return this.getTime() / 1000;
	}
}



/* --------------- Constructor : BATag --------------- */

function BATag(tagName, attrs) {
	this.tagName    = tagName;
	this.attributes = attrs || {};
	this.childNodes = [];
	this.instanceOf = BATag;
}

BATag.prototype = {
	setAttributeBA : function(attrName, value) {
		this.attributes[attrName] = value;
	},

	appendChildBA : function(arg) {
		this.childNodes.push(arg);
	},

	toString : function(debug) {
		var tagOpen    = (debug) ? '&lt;' : '<';
		var tagClose   = (debug) ? '&gt;' : '>';
		var tag        = tagOpen + this.tagName;
		var content    = (this.childNodes.length) ? '' : null;
		for (var i = 0, n = this.childNodes.length; i < n; i++) {
			content += this.childNodes[i].toString(debug);
		}
		for (var attr in this.attributes) {
			tag += ' ' + attr + '="' + this.attributes[attr] + '"';
		}
		tag += (content != null) ?
		       	tagClose + content + tagOpen + '/' + this.tagName + tagClose :
		       	' /' + tagClose;
		return tag;
	}
}



/* --------------- Constructor : BAKeyEquivalents --------------- */

function BAKeyEquivalents() {
	this.keyAlias = {
		'$' : { aliasName : 'Shift'    , keyCode : 16, DOMName : 'shiftKey' },  
		'%' : { aliasName : 'Ctrl'     , keyCode : 17, DOMName : 'ctrlKey'  },  
		'~' : { aliasName : 'Alt'      , keyCode : 18, DOMName : 'altKey'   },  /* alt (Win), option  (Mac) */
		'&' : { aliasName : 'Meta'     , keyCode : 91, DOMName : null       },  /* win (Win), command (Mac) (no browsers work this..?) */
		'#' : { aliasName : 'Enter'    , keyCode : 13, DOMName : null       },
		'|' : { aliasName : 'Tab'      , keyCode :  9, DOMName : null       },
		'!' : { aliasName : 'Esc'      , keyCode : 27, DOMName : null       },
		'<' : { aliasName : '\u2190'   , keyCode : 37, DOMName : null       },  /* left  */
		'{' : { aliasName : '\u2191'   , keyCode : 38, DOMName : null       },  /* up    */
		'>' : { aliasName : '\u2192'   , keyCode : 39, DOMName : null       },  /* right */
		'}' : { aliasName : '\u2193'   , keyCode : 40, DOMName : null       }   /* down  */
	}
	this.equivalents = {};
	this.numOfEquivs = 0;
}

BAKeyEquivalents.prototype = {
	init : function() {
		document.addEventListenerBA('keydown', BACreateDelegate(this.dispatchEvent, this));
	},

	addKeyEquivalent : function(key, name, aCallBack, aThisObject) {
		this.numOfEquivs++;
		if (!name) name = 'BAKeyEquivalents' + this.numOfEquivs;
		this.equivalents[name] = { key : key, aCallBack : aCallBack, aThisObject : aThisObject };
		return name;
	},
	
	getKeybind : function(name) {
		var equiv = this.equivalents[name];
		var keys  = equiv.key.split('');
		var ret   = [];
		for (var i = 0, n = keys.length; i < n; i++) {
			ret.push(this.keyAlias[keys[i]] ? this.keyAlias[keys[i]].aliasName : keys[i].toUpperCase());
		}
		return ret;
	},
	
	doCallBack : function(name, e) {
		if (name && this.equivalents[name]) {
			var equiv = this.equivalents[name];
			equiv.aCallBack.call(equiv.aThisObject, e);
		}
	},
	
	dispatchEvent : function(e) {
		for (var name in this.equivalents) {
			var equiv = this.equivalents[name];
			var keys  = equiv.key.split('');
			var flag  = true;
			for (var i = 0, n = keys.length; (i < n && flag); i++) {
				var key      = keys[i].toLowerCase();
				var keyAlias = this.keyAlias[key];
				if (keyAlias && keyAlias.DOMName) {
					flag = (flag && e[keyAlias.DOMName]);
				} else if (keyAlias && keyAlias.keyCode) {
					flag = (flag && (e.keyCode == keyAlias.keyCode));
				} else {
					var chr = String.fromCharCode(e.charCode ? e.charCode : e.keyCode).toLowerCase();
					flag = (flag && (key == chr));
				}
			}
			if (flag) {
				this.doCallBack(name, e);
			}
		}
	}
}





/* --------------- Startup Functions (non-DOM / pre onload) --------------- */

/* ----- BARegisterDOMMethods ----- */

function BARegisterDOMMethods() {
	window.addEventListenerBA = BAElement.prototype.addEventListenerBA;
	if ((typeof Node == 'object' || typeof Node == 'function') && Node.prototype) {
		for (var i in BAElement.prototype) {
			Node.prototype[i] = BAElement.prototype[i];
		}
	}
	BARegisterDOMMethodsTo(document);
}

/* ----- BAAppendReviseCSS ----- */

function BAAppendReviseCSS() {
	for (var i in BA.css.revise) {
		var ua = i.split('.')[0];
		var os = i.split('.')[1];
		if (os && BA.ua['is' + os] && BA.ua['is' + ua] || !os && BA.ua['is' + ua]) {
			BAAppendCSS(BA.url.cssDir + BA.css.revise[i], BA.css.reviseTitle);
			break;
		}
	}
}





/* --------------- Startup Functions (DOM / post onload) --------------- */

/* ----- BARegisterDOMMethodsRoundRobin ----- */

function BARegisterDOMMethodsRoundRobin() {
	if ((typeof Node == 'object' || typeof Node == 'function') && Node.prototype) return;
	if (BAAlreadyApplied(arguments.callee)) return;
	document.getElementsByTagNameBA('*').forEach(BARegisterDOMMethodsTo);
}

/* ----- BAScrollToFragmentIDTarget ----- */

function BAScrollToFragmentIDTarget() {
	if (BAAlreadyApplied(arguments.callee)) return;
	if (!BA.ua.isSafari || !BA.css.revise.Safari) return;

	var target = location.hash                   || '';
	    target = target.split('#')[1]            || '';
	    target = document.getElementById(target) || null;
	if (target) {
		window.scrollTo(0, target.getAbsoluteOffsetBA().Y);
	}
}

/* ----- BAStartGeometryMeasure ----- */

function BAStartGeometryMeasure() {
	if (!BA.debugMode || BAAlreadyApplied(arguments.callee)) return;

	BAGetGeometry();
	document.addEventListenerBA('mousemove' , BAGetGeometry);
	document.addEventListenerBA('mousewheel', BAGetGeometry);
}






/* --------------- Postprocess Functions (DOM / onunload) --------------- */

/* ----- BACleanUpEventListeners ----- */

function BACleanUpEventListeners() {
	if (BA_EVENTLISTENER_STORED_NODES) {
		BA_EVENTLISTENER_STORED_NODES.forEach(function(node) {
			for (var type in node.__addEventListenerBA_stored__) {
				if (type != 'unload') {
					node['on' + type] = null;
				}
			}
			node.__addEventListenerBA_stored__ = null;
		});
	}
}

/* ----- BACleanUpDOMMethods ----- */

function BACleanUpDOMMethods() {
	var nodes = document.getElementsByTagName('*');
	for (var i = 0, n = nodes.length; i < n; i++) {
		var node = nodes[i];
		if (node.instanceOf == 'BAElement') {
			for (var prop in BAElement.prototype) {
				node[prop] = null;
			}
		}
	}
}





/* --------------- Main --------------- */

BA           = BASingleton(BAEnvironment);
BA_STATUSMSG = BASingleton(BAStatusMsg);
BA_KEYEQUIV  = BASingleton(BAKeyEquivalents);

BARegisterDOMMethods();
BAAppendReviseCSS();

BAAddOnload(function() {
	BARegisterDOMMethodsRoundRobin();
//	BAAppendStateClassName('dom-enabled'); // MacIE: the pages using 'putflash.js', whole contents will be invisible...
	BAScrollToFragmentIDTarget();
	BAStartGeometryMeasure();
	BA_KEYEQUIV.init();
	BA_STATUSMSG.unset();
});

BAAddOnunload(function() {
	BACleanUpEventListeners();
//	BACleanUpDOMMethods();
});

