/*_______________________________________________

   Copyright (C) 2004 Business Architects Inc.
_________________________________________________*/

function BAcommon() {
	this.env  = {};
	this.ns   = {};
	this.prfx = {};

	this.env.ua       = navigator.userAgent;
	this.env.ver      = navigator.appVersion;
	this.env.isMac    = this.env.ua.match(/Mac/),
	this.env.isWin    = this.env.ua.match(/Win/),
	this.env.isNN     = document.layers,
	this.env.isMoz    = this.env.ua.match(/Gecko\//),
	this.env.isN6     = this.env.ua.match(/Netscape\/6/),
	this.env.isN7     = this.env.ua.match(/Netscape\/7/),
	this.env.isSafari = this.env.ua.match(/AppleWebKit/),
	this.env.isOpera  = (window.opera);
	this.env.isIE     = (document.all && !this.env.isOpera);
	this.env.DOMok    = (document.documentElement && document.getElementsByTagName);
	this.ns.rootNS    = (this.env.DOMok) ? document.documentElement.namespaceURI : null
	this.ns.xhtml1    = 'http://www.w3.org/1999/xhtml';
	this.ns.bAattrs   = 'urn:bA.attrs';
	this.prfx.bAattrs = 'bAattrs:';
	this.sharedDir    = this.getSharedDir();

	if(RegExp){
		if(this.env.isIE){
			this.env.ver = this.env.ua.match(/MSIE (\d(.\d+)?)/)[1];
		}
		if(this.env.isMoz){
			this.env.ver = this.env.ua.match(/(\d(.\d+)?) \(/)[1];
		}
		if(this.env.isNN || this.env.isN6 || this.env.isN7){
			this.env.ver = this.env.ua.match(/Netscape\/(\d(.\d+)?)/)[1];
		}
		if(this.env.isOpera){
			this.env.ver = this.env.ua.match(/Opera\/? ?(\d(\.\d+)?)/)[1];
		}
		this.env.ver = parseFloat(this.env.ver);
	}else{
		this.env.ver = undefined;
	}

	this.showErrMsg   = false;
	window.onerror    = this.errorHandler;
}

BAcommon.prototype = {
	getSharedDir : function() {
		var sheets = document.styleSheets;
		var ptn    = /(.*\/?shared\/).+$/;
		var wl = window.location;
		var temp_sd = wl.protocol + '//' + wl.host + '/jp/shared/'
		return (sheets && sheets.length && sheets[0].href && sheets[0].href.match(ptn)) ? RegExp.$1 : temp_sd;
	},
	
	loadJS : function() {
		for(var i=0;i<arguments.length;i++){
			document.write('<script type="text/javascript" src="' + this.sharedDir + 'js/' + arguments[i] + '"><\/script>');
		}
	},

	loadReviseCSS : function() {
		var prefix = 'revise_';
		var suffix = '.css';
		var label  = (BA.env.isIE  && BA.env.isWin) ? ''     :
		             (BA.env.isOpera  && BA.env.ver < 7.2) ? 'oldop'     :
		             '';

		if (label && BA.sharedDir) {
			var href = this.sharedDir + 'css/' + prefix + label + suffix;
//			document.write('<link rel="stylesheet" type="text/css" href="' + href + '">')
//			alert('<link rel="stylesheet" type="text/css" href="' + href + '">')
		}
	},

	getElementsByTagName : function(tagName, baseNode) {
		if (!tagName) tagName = '*';
		if (!baseNode || !baseNode.nodeType || baseNode.nodeType != 1) baseNode = document;
		return (!baseNode.getElementsByTagName) ?
			null :
			(BA.ns.rootNS) ? baseNode.getElementsByTagNameNS(env.ns.xhtml1, tagName) :
			                 baseNode.getElementsByTagName(tagName);
	},

	getElementsByClassName : function(className, tagName, baseNode) {
		if (!className) return null;
		var ret  = [];
		var objs = BA.getElementsByTagName(tagName, baseNode);
		for (var i = 0; i < objs.length; i++) {
			if (!objs[i].className) continue;
			var classes = objs[i].className.split(' ');
			for (var j = 0; j < classes.length; j++)
				if (classes[j] == className) { ret[ret.length] = objs[i]; break; }
		}
		return ret;
	},
	
	createElement : function(tagName) {
		return (BA.ns.rootNS) ?
			document.createElementNS(env.ns.xhtml1, tagName) :
			document.createElement(tagName);
	},

	createText : function(str, node) {
		if (!str || !node || node.nodeType != 1) return;
		if (BA.env.isMac && BA.env.isIE && BA.env.ua.match(/MSIE 5.0/))
			node.innerHTML += str;
		else
			node.appendChild(document.createTextNode(str));
		return node;
	},

	getAttr : function(node, attr) {
		if (!node || !attr) return null;
		attr += (document.all && attr == 'class') ? 'Name' : ''; // Measure for IE
		var ret = node.getAttribute(attr)
		if (!ret && node.getAttributeNS && attr.match(/:/)) {
			var prfx = attr.split(':')[0];
			var attr = attr.split(':')[1];
			return node.getAttributeNS(BA.ns[prfx], attr)
		} else
			return ret;
	},

	setAttr : function(node, attr, value) {
		if (!node || !attr) return;
		if (attr.match(/:/)) {
			var prfx = attr.split(':')[0];
			var attr = attr.split(':')[1];
			if (node.setAttributeNS && node.namespaceURI || BA.env.isSafari)
				node.setAttributeNS(BA.ns[prfx], attr, value);
			else {
				node.setAttribute('xmlns:' + prfx, BA.ns[prfx]);
				node.setAttribute(prfx + ':' + attr, value);
			}
		} else {
			attr += (document.all && attr == 'class') ? 'Name' : ''; // Measure for IE
			node.setAttribute(attr, value);
		}
	},

	getOffset : function(obj, axis, sum) {
		if (!sum) var sum = 0;
		sum += (axis == 'X') ? obj.offsetLeft : obj.offsetTop;
		return (obj.offsetParent) ? BA.getOffset(obj.offsetParent, axis, sum) : sum;
	},

	processDuringLoad : function(fn, wait) {
		if (!BA.psDuringLoad) BA.psDuringLoad = [];
		fn();
		BA.psDuringLoad[BA.psDuringLoad.length] = setInterval(fn, wait);
		BA.addOnload(fn);
		BA.addOnload(BA.clearProcessDuringLoad);
	},
	
	clearProcessDuringLoad : function(){
		if (!BA.psDuringLoad) return;
		for (var i = 0; i < BA.psDuringLoad.length; i++)
			clearInterval(BA.psDuringLoad[i]);
	},
	
	addEvent : function (obj, type, listener) {
	 	var exists = obj[type];
		obj[type]  = (exists) ? function() { exists(); listener(); } : listener;
	},

	addOnload : function(listener) {
		BA.addEvent(window, 'onload', listener)
	},

	addTitle : function() {
		if (!BA.env.DOMok) return;
		var objs = BA.getElementsByTagName('img');
		for (var i = 0; i < objs.length; i++)
			if (!objs[i].title && objs[i].alt)
				objs[i].title = objs[i].alt;
	},

	errorHandler : function() {
		if (BA.showErrMsg) {
			var msg = 'Error: ' + arguments[0] + '\n' +
			          'File: '  + arguments[1] + '\n' + 
			          'Line: '  + arguments[2];
			alert(msg);
		}
		return true;
	}
};


var BA = new BAcommon;


if(!Array.push){
	Array.prototype.push = function(elem){
		this[this.length] = elem;
	}
}

Array.prototype.shuffle = function(){
	var len = this.length;
	var rand,temp,i;
	for (i=0; i<len; i++){
		rand = Math.floor(Math.random()*len);
		temp = this[i];
		this[i] = this[rand];
		this[rand] = temp;
	}
}

Array.prototype.indexOf = function(value){
	var i = 0;
	while(i < this.length){
		if(this[i] == value) return i;
		i++;
	}
	return -1;
}

Array.prototype.remove = function(value){
	this.splice(this.indexOf(value),1);
}

function ClassNames(obj){
	this.obj = obj;
	this.cns = obj.className.split(/\s+/);
};

ClassNames.prototype.add = function(value){
	if(!this.has(value)){
		this.cns.push(value);
		this.obj.className = this.cns.join(' ');
	}
}

ClassNames.prototype.remove = function(value){
	this.cns.remove(value);
	this.obj.className = this.cns.join(' ');
}

ClassNames.prototype.has = function(value){
	if(this.cns.indexOf(value) == -1){
		return false;
	}
	return true;
}

function setPseudoClass(tagname){
	var objs = document.getElementsByTagName(tagname);
	for(var i=0;i<objs.length;i++){
		var obj = objs[i];
		if(!obj.classNames) obj.classNames = new ClassNames(obj);
		if(obj == obj.parentNode.firstChild){
			obj.classNames.add('first-child');
		}
		if(obj == obj.parentNode.lastChild){
			obj.classNames.add('last-child');
		}
	}
}

function randomizeBG(){
	if(BA.env.isSafari) return;
	var obj = document.getElementById('breadcrumbsBlock');
	if(!obj) return;
	var bgn = Math.floor(Math.random()*12);
	bgn++;
	var bgi = BA.sharedDir + 'img/frame/lf_bc_' + ((bgn<10)? 0 : '') + bgn + '.gif';
	obj.style.backgroundImage = 'url(' + bgi + ')';
}

function preventEmptySubmit(formID,queryID){
	var fobj = document.getElementById(formID);
	if(!fobj) return;
	var qobj = (document.all)? document.all[queryID] : document.getElementById(queryID);
	fobj.onsubmit = function(){
		if(qobj.value == ''){
			alert('検索するキーワードを入力してください。');
			qobj.focus();
			return false;
		}
	}
}

function reviseOldOperaStyle(){
	var tna = document.getElementById('topNavigationArea');
	if(BA.env.isOpera  && BA.env.ver < 7.2 && tna){
		tna.style.marginTop = '-6px';
	}
}

//BA.loadReviseCSS();
BA.addOnload(randomizeBG);
BA.addOnload(function(){setPseudoClass('h2');});
BA.addOnload(function(){preventEmptySubmit('searchform','searchkey');});
BA.addOnload(function(){preventEmptySubmit('eventSearchform','eventSearchkey');});
BA.addOnload(reviseOldOperaStyle);
BA.loadJS('rollover.js');
