/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       CompanyList controller.
 *
 *    @version rev013.2008-12-01
 *    @requires ajax.js
 *    @requires common.js
 */
/* -------------------------------------------------------------------------- */

/* -------------------- Settings for CompanyList -------------------- */
var SBHD_GROUP_PAGE                      = {};
var SBHD_GROUP_SELECTORS                 = [];
var SBHD_GROUP_SEGMENTS                  = [];
var SBHD_GROUP_VOWELS                    = [];
var SBHD_GROUP_COMPANY_LIST_BLOCK_ID     = "company-list-block";
var SBHD_GROUP_COMPANY_LIST_CONTAINER_ID = "company-list-content";
var SBHD_GROUP_COMPANY_LIST_CNAME        = "company-list";
var SBHD_GROUP_SELECTOR_CNAME            = "selector";

var SBHD_GROUP_COMPANY_LIST_ARG    = new BAAjaxRequestArg();
SBHD_GROUP_COMPANY_LIST_ARG.method = "GET";
SBHD_GROUP_COMPANY_LIST_ARG.url    = "/ja/design_set/data/info/business/group/list.xml";



/* -------------------- CompanySelectorAnimation -------------------- */
function CompanySelectorAnimation() {
	this.node         = null;
	this.toggleSwitch = null;
	this.container    = null;
	this.infoArea     = null;
	this.fontSizeObserver = null;

	this.init();
}
CompanySelectorAnimation.prototype.init = function() {
	this.node = document.getElementByIdBA(SBHD_GROUP_COMPANY_LIST_BLOCK_ID);

	this.toggleSwitch = this.node.getElementsByClassNameBA("list-switch", "p")[0];

	this.container = document.getElementByIdBA(SBHD_GROUP_COMPANY_LIST_CONTAINER_ID);

	this.toHeight    = this.container.offsetHeight;
	this.closeHeight = this.toggleSwitch.offsetHeight;

	this.node.style.height = this.container.style.height = this.closeHeight + "px";

	this.containerEffectW = new BAEffect.Style(this.container, "width");
	this.containerEffectH = new BAEffect.Style(this.container, "height");

	this.nodeEffect = new BAEffect.Style(this.node, "height");

	this.infoArea = document.getElementByIdBA("info-area");
	this.infoAreaEffect = new BAEffect.Style(this.infoArea, "marginTop");

	(this.toggleSwitch.getElementsByTagNameBA("a")[0]).addEventListenerBA("click", function(e){
		e.preventDefault();
		this.toggle();
	}, this);

	if (typeof(BAFontSizeObserver) != "undefined") {
		this.fontSizeObserver = BASingleton(BAFontSizeObserver);
		this.fontSizeObserver.addCallBack("onChange", this.initSize, this);
		this.fontSizeObserver.startObserve();
	}
}
CompanySelectorAnimation.prototype.getNodeHeight = function() {
	return this.toHeight;
}
CompanySelectorAnimation.prototype.getContainerWidth = function() {
	return 942 - 2;
}
CompanySelectorAnimation.prototype.getContainerHeight = function() {
	return this.toHeight - 2;
}
CompanySelectorAnimation.prototype.getInfoAreaMargin = function() {
	return this.toHeight + parseInt((this.node.getCurrentStyleBA("marginBottom") || "0").replace(/px$/,""));
}
CompanySelectorAnimation.prototype.toggle = function() {
	(!this.opened) ? this.open() : this.close();
}
CompanySelectorAnimation.prototype.initSize = function() {
	this.container.style.height = "auto";

	this.toHeight                      = this.container.offsetHeight;
	this.closeHeight                   = this.toggleSwitch.offsetHeight;
	this.nodeEffect.initialValue       = this.closeHeight;
	this.containerEffectH.initialValue = this.closeHeight;

	if (this.opened) {
		this.nodeEffect.setValue(this.getNodeHeight());
		this.containerEffectW.setValue(this.getContainerWidth());
		this.containerEffectH.setValue(this.getContainerHeight());
		this.infoAreaEffect.setValue(this.getInfoAreaMargin());
	} else {
		this.nodeEffect.setValue(this.closeHeight);
		this.containerEffectW.resetValue();
		this.containerEffectH.setValue(this.closeHeight);
		this.infoAreaEffect.resetValue();
	}
}
CompanySelectorAnimation.prototype.open = function() {
	BA.disableSiteCatalyst();
	this.opened = true;
	this.node.appendClassNameBA("pseudo-open");

	this.nodeEffect.changeTo(this.getNodeHeight(), function(){ this.node.appendClassNameBA("pseudo-opened"); BA.resumeSiteCatalyst(); }, this);
	this.containerEffectW.changeTo(this.getContainerWidth());
	this.containerEffectH.changeTo(this.getContainerHeight());
	this.infoAreaEffect.changeTo(this.getInfoAreaMargin());
}
CompanySelectorAnimation.prototype.close = function() {
	BA.disableSiteCatalyst();
	this.opened = false;
	this.node.removeClassNameBA("pseudo-open");
	this.node.removeClassNameBA("pseudo-opened");

	this.nodeEffect.revert(BA.resumeSiteCatalyst, BA);
	this.infoAreaEffect.revert();
	this.containerEffectW.revert();
	this.containerEffectH.revert();
}


/* -------------------- Constructor : CompanySelectorController -------------------- */
/**
 * company-selector controller
 * @class company-selector-controller
 * @constructor
 * @param {BAAjaxRequestArg} req     request args
 */
function CompanySelectorController(req) {
	/** display node.
	    @type BAElement @private */
	this.node      = null;
	/** ajax gateway.
	    @type BAAjax @private */
	this.gateway   = null;
	/** list of Selector.
	    @type Array @private */
	this.selectors = [];
	/** company-data list.
	    @type CompanyList @private */
	this.list = null;

	this.init(req);
}

/** 
 * initialize.
 * @param {BAAjaxRequestArg} req     request args
 * @private
 */
CompanySelectorController.prototype.init = function(req) {
	this.setNode(this.createNode());
	this.gateway = new BAAjax;
	if (req) {
		this.request(req);
	}
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
CompanySelectorController.prototype.createNode = function() {
	var node = document.createElementBA("div");
	node.appendClassNameBA("company-selector");
	return node;
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
CompanySelectorController.prototype.getNode = function() {
	return this.node || null;
}

/** 
 * set display node.
 * @param {BAElement} node   display node.
 */
CompanySelectorController.prototype.setNode = function(node) {
	this.node = node || null;
}

/** 
 * get company-data list.
 * @return company-data list.
 * @type CompanyList
 */
CompanySelectorController.prototype.getList = function() {
	return this.list || null;
}

/** 
 * set company-data list.
 * @param {CompanyList} companyList   company-data list.
 */
CompanySelectorController.prototype.setList = function(companyList) {
	if (companyList) {
		this.list = companyList || null;

		var container = this.getNode();
		var listNode = companyList.getNode();
		if (container && listNode) {
			container.appendChildBA(listNode);
		}
	}
}

/** 
 * get cond-selector node.
 * @return cond-selector node.
 * @type BAElement
 */
CompanySelectorController.prototype.getCondSelector = function() {
	return this.condSelector || null;
}

/** 
 * set cond-selector node.
 * @param {BAElement} node   cond-selector node.
 */
CompanySelectorController.prototype.setCondSelector = function(node) {
	this.condSelector = node || null;
	var container     = this.getNode();
	var condSelector  = this.getCondSelector();
	if (container && condSelector) {
		container.appendChildBA(condSelector);
	}
}

/** 
 * add selector.
 * @param {Selector} selector   selector.
 */
CompanySelectorController.prototype.addSelector = function(selector) {
	if (selector) {
		this.selectors.push(selector);

		var container = this.getNode();
		var selectorNode = selector.getNode();
		if (container && selectorNode) {
			container.appendChildBA(selectorNode);
		}
	}
}

/** 
 * select selector.
 * @param {Number} index   index.
 */
CompanySelectorController.prototype.select = function(index) {
	this.list.select();
	this.selectors.forEach(function(_selector, i){
		var node = _selector.getNode();
		if (i == index) {
			_selector.select(_selector.getSelectedId());
			node.appendClassNameBA("pseudo-selected");
		} else {
			node.removeClassNameBA("pseudo-selected");
		}
	});

	var condSelector = this.getCondSelector();
	if (condSelector) {
		condSelector.getElementsByTagNameBA("li").forEach(function(node, i){
			if (i == index) {
				node.appendClassNameBA("pseudo-selected");
			} else {
				node.removeClassNameBA("pseudo-selected");
			}
		});
	}
}

/** 
 * request company data.
 * @param {BAAjaxRequestArg} req     request args
 */
CompanySelectorController.prototype.request = function(req) {
	req.onComplete = BACreateDelegate(this.onComplete, this);

	var _func = BACreateDelegate(function(){
		var node = this.getNode();
		node.removeClassNameBA("pseudo-loaded");
		node.appendClassNameBA("pseudo-loading");
	}, this);

	if (BA.env.isDOMReady) {
		_func();
	} else {
		BAAddOnload(_func);
	}

	this.gateway.request(req);
}

/** 
 * handler for request complete.
 * @param {Document} responseXML     XMLDocument.
 * @private
 */
CompanySelectorController.prototype.onComplete = function(responseXML) {
	if (!responseXML || !responseXML.documentElement) {
		return;
	}

	var _func = BACreateDelegate(function (){
		var node = this.getNode();
		node.removeClassNameBA("pseudo-loading");
		node.appendClassNameBA("pseudo-loaded");
		node.appendClassNameBA("pseudo-enabled");

		var container = document.getElementByIdBA(SBHD_GROUP_COMPANY_LIST_CONTAINER_ID);
		if (!container) {
			return;
		}

		container.appendChildBA(this.getNode());

		var condSelector = document.createElementBA("ul");
		condSelector.appendClassNameBA("cond-selector");
		this.setCondSelector(condSelector);
		SBHD_GROUP_SELECTORS.forEach(function(item, index){
			var li = document.createElementBA("li");
			var link = document.createElementBA("a");
			link.setAttributeBA("href", item.url || "#");
			link.addEventListenerBA("click", function(e){
				e.preventDefault();
				this.select(index);
			}, this);
			link.appendChildBA(item.title);
			li.appendChildBA(link);
			this.getCondSelector().appendChildBA(li);
		}, this);

		if (SBHD_GROUP_PAGE && SBHD_GROUP_PAGE.title && SBHD_GROUP_PAGE.url && SBHD_GROUP_PAGE.text) {
			var li = document.createElementBA("li");
			var text = document.createElementBA("p");
			text.appendChildBA(SBHD_GROUP_PAGE.text);
			li.appendChildBA(text);
			var linkContainer = document.createElementBA("p");
			var link = document.createElementBA("a");
			link.setAttributeBA("href", SBHD_GROUP_PAGE.url);
			link.appendChildBA(SBHD_GROUP_PAGE.title);
			li.appendClassNameBA("list-page");
			linkContainer.appendChildBA(link);
			li.appendChildBA(linkContainer);
			this.getCondSelector().appendChildBA(li);
		}

		var de = responseXML.documentElement;

		var segmentList = new SegmentList(de);
		segmentList.addCallBack("onChange", function(key, value){
			list.sortBy(key);
			list.select(key, value);
		}, list);
		this.addSelector(segmentList);

		var kanaList = new KanaList(de);
		kanaList.addCallBack("onChange", function(key, value){
			list.sortBy(key);
			list.selectByKana(key, value);
		}, list);
		this.addSelector(kanaList);

		var list = new CompanyList(de);
		this.setList(list);

		this.select(0);
		segmentList.select(segmentList.items[0].getId());

		new CompanySelectorAnimation;
	}, this);

	if (BA.env.isDOMReady) {
		_func();
	} else {
		BAAddOnload(_func);
	}
}


/* -------------------- Constructor : CompanyData -------------------- */
/**
 * company-data object
 * @class company-data
 * @constructor
 * @param {Element} xmlData     company element (required)
 */
function CompanyData(xmlData) {
	/** display node.
	    @type BAElement @private */
	this.node = null;
	/** storage of company-data.
	    @type Object @private */
	this.data = {};

	this.init(xmlData);
}

/** 
 * initialize.
 * @param {Element} xmlData     documentElement has company element (required)
 * @private
 */
CompanyData.prototype.init = function(xmlData){
	var datas = xmlData.getElementsByTagName("*");
	for (var i = 0, n = datas.length; i < n; i++) {
		var dataNode = datas[i];
		if (dataNode.nodeType == 1 && dataNode.firstChild) {
			this.setData(dataNode.nodeName, dataNode.firstChild.nodeValue);
		}
	}

	this.setNode(this.createNode());
}

/** 
 * create display node.
 * @return display node.
 * @type BAElement
 * @private
 */
CompanyData.prototype.createNode = function(){
	var node = document.createElementBA("dl");
	node.appendChildBA(this.createNameNode());
	node.appendChildBA(this.createDescriptionNode());
	return node;
}

/** 
 * create display node has company-name.
 * @return display node has company-name.
 * @type BAElement
 * @private
 */
CompanyData.prototype.createNameNode = function(){
	var node = document.createElementBA("dt");
	var name = this.getName();
	var url  = this.getUrl();
	if (name) {
		if (url) {
			var link = document.createElementBA("a");
			link.setAttributeBA("href", url);
			link.appendChildBA(name);

			link.setAttributeBA("target", "_blank");
			var icon = document.createElementBA("img");
			icon.setAttributeBA("src", "/shared/img/icon_05.gif");
			icon.appendClassNameBA("newwindowicon");
			link.appendChildBA(icon);

			node.appendChildBA(link);
			node.appendClassNameBA("has-link");
		} else {
			node.appendChildBA(name);
		}
	}
	return node;
}

/** 
 * create display node has description.
 * @return display node has description.
 * @type BAElement
 * @private
 */
CompanyData.prototype.createDescriptionNode = function(){
	var node        = document.createElementBA("dd");
	var description = this.getDescription();
	if (description) {
		node.appendChildBA(description);
	}
	return node;
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
CompanyData.prototype.getNode = function(){
	return this.node || null;
}

/** 
 * set display node.
 * @param {BAElement} node   display node.
 */
CompanyData.prototype.setNode = function(node){
	this.node = node || null;
}

/** 
 * get company-data.
 * @param {String} prop   property name.
 * @return property value.
 * @type String
 */
CompanyData.prototype.getData = function(prop){
	if (!prop) {
		return "";
	}
	return this.data[prop] || "";
}

/** 
 * set company-data.
 * @param {String} prop    property name.
 * @param {String} value   property value.
 */
CompanyData.prototype.setData = function(prop, value){
	if (prop) {
		this.data[prop] = value || "";
	}
}

/** 
 * check target property has target value.
 * @param {String} prop    property name.
 * @param {String} value   property value.
 * @return true/false.
 * @type Boolean
 */
CompanyData.prototype.hasData = function(prop, value){
	if (!prop || !value) {
		return false;
	}
	return this.getData(prop) == value;
}

/** 
 * get company-name.
 * @return company-name.
 * @type String
 */
CompanyData.prototype.getName = function(){
	var prop = "name";
	return this.getData(prop) || "";
}

/** 
 * get description.
 * @return description.
 * @type String
 */
CompanyData.prototype.getDescription = function(){
	var prop = "description";
	return this.getData(prop) || "";
}

/** 
 * get url for corporate-site.
 * @return url for corporate-site.
 * @type String
 */
CompanyData.prototype.getUrl = function(){
	return this.getData("corporate_site") || "";
}


/* -------------------- Constructor : CompanyList -------------------- */
/**
 * company-data list
 * @class company-data-list
 * @constructor
 * @param {Element} xmlData     documentElement has company element (required)
 */
function CompanyList(xmlData) {
	/** display node.
	    @type BAElement @private */
	this.node  = null;
	/** list of company-data.
	    @type Array @private */
	this.items = [];

	this.init(xmlData);
}

/** 
 * initialize.
 * @param {Element} xmlData     documentElement has company element (required)
 * @private
 */
CompanyList.prototype.init = function(xmlData){
	this.setNode(this.createNode());
	var companies = xmlData.getElementsByTagName("company");
	for (var i = 0, n = companies.length; i < n; i++) {
		this.addItem(new CompanyData(companies[i]));
	}
}

/** 
 * create display node.
 * @return display node.
 * @type BAElement
 * @private
 */
CompanyList.prototype.createNode = function(){
	var node = document.createElementBA("ul");
	node.appendClassNameBA(SBHD_GROUP_COMPANY_LIST_CNAME);
	return node;
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
CompanyList.prototype.getNode = function(){
	return this.node;
}

/** 
 * set display node.
 * @param {BAElement} node   display node.
 */
CompanyList.prototype.setNode = function(node){
	this.node = node || null;
}

/** 
 * add company-data.
 * @param {CompanyData} item   company-data.
 */
CompanyList.prototype.addItem = function(item){
	item.setData("index", this.items.length);

	this.items.push(item);

	var container = this.getNode();
	var itemNode  = item.getNode();

	if (container && itemNode) {
		var li = document.createElementBA("li");
		li.appendChildBA(itemNode);
		container.appendChildBA(li);
	}
}

/** 
 * select company-datas which has target value in target property.
 * @param {String} prop    property name.
 * @param {String} value   property value.
 */
CompanyList.prototype.select = function(prop, value){
	this.items.forEach(function(item){
		var parentNode = item.getNode().getParentNodeBA();
		if (item.hasData(prop, value)) {
			parentNode.appendClassNameBA("pseudo-selected");
		} else {
			parentNode.removeClassNameBA("pseudo-selected");
		}
	});

	new BASetTimeout(function(){
		var node = this.getNode();
		node.scrollTop  = 0;
		node.scrollLeft = 0;
	}, 1, this);
}

/** 
 * select company-datas which company-name start at target value.
 * @param {String} key     property name.
 * @param {String} value   property value.
 */
CompanyList.prototype.selectByKana = function(key, value){
	if (SBHD_GROUP_VOWELS) {
		var vowel = SBHD_GROUP_VOWELS.filter(function(vowel){ return vowel.name == value })[0];
		this.items.forEach(function(item){
			var parentNode = item.getNode().getParentNodeBA();
			var kana = item.getData(key);
			if (vowel.reg.test(kana)) {
				parentNode.appendClassNameBA("pseudo-selected");
			} else {
				parentNode.removeClassNameBA("pseudo-selected");
			}
		});
	} else {
		this.items.forEach(function(item){
			var parentNode = item.getNode().getParentNodeBA();
			var kana = item.getData(key);
			if (kana.substr(0, 1) == value) {
				parentNode.appendClassNameBA("pseudo-selected");
			} else {
				parentNode.removeClassNameBA("pseudo-selected");
			}
		});
	}
}

/** 
 * sort company-datas by target property.
 * @param {String} prop     property name.
 */
CompanyList.prototype.sortBy = function(prop){
	var container = this.getNode();
	container.removeAllChildrenBA();
	this.items.sort(function(a, b){
		var propA = a.getData(prop);
		if (propA) {
			propA = propA.toLowerCase();
		}
		var propB = b.getData(prop);
		if (propB) {
			propB = propB.toLowerCase();
		}
		if (propA >  propB) return  1;
		if (propA == propB) {
			var indexA = parseInt(a.getData("index")) || 0;
			var indexB = parseInt(b.getData("index")) || 0;
			if (indexA >  indexB) return  1;
			if (indexA == indexB) return  0;
			if (indexA <  indexB) return -1;
		}
		if (propA <  propB) return -1;
	});
	this.items.forEach(function(item){
		var parentNode = item.getNode().getParentNodeBA();
		container.appendChildBA(parentNode);
	});
}


/* -------------------- Constructor : SelectorItem inherits BAObservable -------------------- */
/**
 * object for select company-data.
 * @class selector-item
 * @constructor
 * @param {String} id     idName (required)
 */
function SelectorItem(id, url) {
	/** idName.
	    @type String @private */
	this.id    = "";
	/** url.
	    @type String @private */
	this.url   = "";
	/** display node.
	    @type BAElement @private */
	this.node  = null;

	this.init(id, url);
}
SelectorItem.prototype = new BAObservable;

/** 
 * initialize.
 * @param {String} id     idName (required)
 * @private
 */
SelectorItem.prototype.init = function(id, url) {
	this.setId(id);
	this.setUrl(url);
	this.setNode(this.createNode());
}

/** 
 * get id.
 * @return idName.
 * @type String
 */
SelectorItem.prototype.getId = function() {
	return this.id || "";
}

/** 
 * set id.
 * @param {String} id   idName.
 */
SelectorItem.prototype.setId = function(id) {
	this.id = id || "";
}

/** 
 * get url.
 * @return url.
 * @type String
 */
SelectorItem.prototype.getUrl = function() {
	return this.url || "#";
}

/** 
 * set url.
 * @param {String} url   url.
 */
SelectorItem.prototype.setUrl = function(url) {
	this.url = url || "#";
}

/** 
 * create display node.
 * @return display node.
 * @type BAElement
 * @private
 */
SelectorItem.prototype.createNode = function() {
	var node = document.createElementBA("a");
	node.setAttributeBA("href", this.getUrl());
	node.addEventListenerBA("click", function(e){
		e.preventDefault();
		this.doCallBack("onSelect", this.getId());
	}, this);
	node.appendChildBA(this.getId());
	return node;
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
SelectorItem.prototype.getNode = function() {
	return this.node;
}

/** 
 * set display node.
 * @param {BAElement} node   display node.
 */
SelectorItem.prototype.setNode = function(node) {
	this.node = node || null;
}


/* -------------------- Constructor : Selector inherits BAObservable -------------------- */
/**
 * company-data selector
 * @class company-data-selector
 * @constructor
 */
function Selector() {
	/** name of target property.
	    @type String @private */
	this.key        = "";
	/** selected id.
	    @type String @private */
	this.selectedId = "";
	/** display node.
	    @type BAElement @private */
	this.node       = null;
	/** list of selector-item.
	    @type Array @private */
	this.items      = [];
}
Selector.prototype = new BAObservable;

/** 
 * initialize.
 * @private
 */
Selector.prototype.init = function(){
	this.setNode(this.createNode());
}

/** 
 * create display node.
 * @return display node.
 * @type BAElement
 * @private
 */
Selector.prototype.createNode = function() {
	var node = document.createElementBA("ul");
	node.appendClassNameBA(SBHD_GROUP_SELECTOR_CNAME);
	return node;
}

/** 
 * get display node.
 * @return display node.
 * @type BAElement
 */
Selector.prototype.getNode = function() {
	return this.node || null;
}

/** 
 * set display node.
 * @param {BAElement} node   display node.
 */
Selector.prototype.setNode = function(node) {
	this.node = node || null;
}

/** 
 * get name of target property.
 * @return property name.
 * @type String
 */
Selector.prototype.getKey = function() {
	return this.key || "";
}

/** 
 * set name of target property.
 * @param {String} key   property name.
 */
Selector.prototype.setKey = function(key) {
	this.key = key || "";
}

/** 
 * get selected id.
 * @return idName.
 * @type String
 */
Selector.prototype.getSelectedId = function() {
	return this.selectedId || "";
}

/** 
 * set selected id.
 * @param {String} id   idName.
 */
Selector.prototype.setSelectedId = function(id) {
	this.selectedId = id || "";
}

/** 
 * add selector item.
 * @param {SelectorItem} item   selector item.
 */
Selector.prototype.addItem = function(item) {
	if (!this.items.some(function(_item){ return _item.getId() == item.getId(); })) {
		this.items.push(item);
		item.addCallBack("onSelect", this.select, this);
		var li = document.createElementBA("li");
		li.appendChildBA(item.getNode());
		this.node.appendChildBA(li);
	}
}

/** 
 * get item.
 * @param {Number} index   index number of target item in items.
 */
Selector.prototype.getItem = function(index) {
	return this.items[index] || null;
}

/** 
 * select item.
 * @param {String} id   idName.
 */
Selector.prototype.select = function(id) {
	this.setSelectedId(id);
	this.items.forEach(function(item){
		var parentNode = item.getNode().getParentNodeBA();
		if (item.getId() == id) {
			parentNode.appendClassNameBA("pseudo-selected");
		} else {
			parentNode.removeClassNameBA("pseudo-selected");
		}
	});
	this.doCallBack("onChange", this.getKey(), this.getSelectedId());
}


/* -------------------- Constructor : KanaList inherits Selector -------------------- */
/**
 * selector which select by kana
 * @class company-data-selector-kana
 * @constructor
 * @param {Element} xmlData     documentElement has kana element (required)
 */
function KanaList(xmlData) {
	/** name of target property.
	    @type String @private */
	this.key        = "";
	/** selected id.
	    @type String @private */
	this.selectedId = "";
	/** display node.
	    @type BAElement @private */
	this.node       = null;
	/** list of selector-item.
	    @type Array @private */
	this.items      = [];

	this.init(xmlData);
}
KanaList.prototype = new Selector;

/** 
 * initialize.
 * @param {Element} xmlData     documentElement has kana element (required)
 * @private
 */
KanaList.prototype.init = function(xmlData) {
	this.setNode(this.createNode());

	var propName = "kana";
	this.setKey(propName);

	var nodes = xmlData.getElementsByTagName(this.getKey());
	var items = [];
	for (var i = 0, n = nodes.length; i < n; i++) {
		var node = nodes[i];
		if (node.firstChild) {
			var name = node.firstChild.nodeValue;
			var url  = "";
			if (SBHD_GROUP_VOWELS) {
				for (var j = 0, m = SBHD_GROUP_VOWELS.length; j < m; j++) {
					var vowel = SBHD_GROUP_VOWELS[j];
					if (vowel.reg.test(name)) {
						name = vowel.name;
						url  = vowel.url;
						break;
					}
				}
				items.push(new SelectorItem(name, url));
			} else {
				items.push(new SelectorItem(name.substr(0,1)));
			}
		}
	}
	items.sort(function(a, b){
		var propA = a.getId();
		var propB = b.getId();
		for (var indexA = 0, n = SBHD_GROUP_VOWELS.length; indexA < n; indexA++) {
			if (SBHD_GROUP_VOWELS[indexA].name == propA) {
				break;
			}
		}
		for (var indexB = 0, n = SBHD_GROUP_VOWELS.length; indexB < n; indexB++) {
			if (SBHD_GROUP_VOWELS[indexB].name == propB) {
				break;
			}
		}
		if (indexA <  indexB) return -1;
		if (indexA == indexB) return  0;
		if (indexA >  indexB) return  1;
	});
	items.forEach(function(item){
		this.addItem(item);
	}, this);

	this.select(this.getItem(0).getId());
}


/* -------------------- Constructor : SegmentList inherits Selector -------------------- */
/**
 * selector which select by segment
 * @class company-data-selector-segment
 * @constructor
 * @param {Element} xmlData     documentElement has segment element (required)
 */
function SegmentList(xmlData) {
	this.key        = "";
	this.selectedId = "";
	this.node       = null;
	this.items      = [];

	this.init(xmlData);
}
SegmentList.prototype = new Selector;

/** 
 * initialize.
 * @param {Element} xmlData     documentElement has kana element (required)
 * @private
 */
SegmentList.prototype.init = function(xmlData) {
	this.setNode(this.createNode());

	var propName = "segment";
	this.setKey(propName);

	var nodes = xmlData.getElementsByTagName(this.getKey());
	var items = [];
	for (var i = 0, n = nodes.length; i < n; i++) {
		var node = nodes[i];
		if (node.firstChild) {
			items.push(new SelectorItem(node.firstChild.nodeValue));
		}
	}
	items.sort(function(a, b){
		var orderA = -1;
		var propA  = a.getId();
		if (propA) {
			orderA = SBHD_GROUP_SEGMENTS.indexOf(propA);
		}
		var orderB = -1;
		var propB  = b.getId();
		if (propB) {
			orderB = SBHD_GROUP_SEGMENTS.indexOf(propB);
		}
		if (orderA >  orderB) return  1;
		if (orderA == orderB) return  0;
		if (orderA <  orderB) return -1;
	});
	items.forEach(function(item){
		this.addItem(item);
	}, this);

	this.select(this.getItem(0).getId());
}


/* -------------------- Main : register start-up -------------------- */
if (typeof BA == 'object' && BA.ua.isDOMReady && window.XMLHttpRequest && BA.env.isOnline) {
	new CompanySelectorController(SBHD_GROUP_COMPANY_LIST_ARG);
}

