/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Collapsing Block.
 *
 *    @version rev002.2007-06-27
 *    @requires common.js
 *    @requires effects.js (when using effects)
 */
/* -------------------------------------------------------------------------- */


var BANA_REFERENCEAREA;



/* -------------------- Settings for BACollapsingBlock -------------------- */

var BA_COLLAPSINGBLOCK_NODE_CNAME = {
	'control' : 'collapsing-btns',
	'target'  : 'collapsing-target'
};
var BA_COLLAPSINGBLOCK_STATUS_CNAME = {
	'enabled'   : 'pseudo-enabled',
	'expanded'  : 'pseudo-expanded',
	'collapsed' : 'pseudo-collapsed'
};



/* -------------------- Settings for BACollapsingBlockAutoSetup -------------------- */

var BA_COLLAPSINGBLOCK_AS_ENABLED            = true;
var BA_COLLAPSINGBLOCK_AS_USE_VISUAL_EFFECT  = true;
var BA_COLLAPSINGBLOCK_AS_BASENODE_SEARCH_ID = 'content-body';
var BA_COLLAPSINGBLOCK_AS_BASENODE_SELECTOR  = 'div.collapsing-block';



/* ---------- Constructor : BACollapsingBlock inherits BAObservable ---------- */

function BACollapsingBlock(baseNode, useEffect) {
	this.node           = baseNode;
	this.controlNode    = null;
	this.targetNode     = null;
	this.collapsed      = false;
	this.useEffect      = useEffect;
	this.effect         = null;
	this.callBackChains = {};
}

BACollapsingBlock.prototype = new BAObservable;

BACollapsingBlock.prototype.init = function() {
	if (!this.node) {
		throw 'BACollapsingBlock.init : base node is not specified.';
	} else if (!this.node.__BACollapsingBlock_BaseNode__) {
		this.node.__BACollapsingBlock_BaseNode__ = true;
		this.targetNode = Array.prototype.filter.call(this.node.childNodes, function(node) {
			BARegisterDOMMethodsTo(node);
			return (node.hasClassNameBA && node.hasClassNameBA(BA_COLLAPSINGBLOCK_NODE_CNAME.target));
		})[0];

		if (this.targetNode) {
			this.node.appendClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.enabled);
			this.collapsed = this.node.hasClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.collapsed);

			if (this.useEffect) {
				this.initEffect();
			}

			var _targetNode = this.targetNode;
			var searchNodes = Array.prototype.filter.call(this.node.childNodes, function(node) {
				return (node.nodeType == 1 && node != _targetNode && !node.__BAEffectBase__);
			}, this);
			for (var i = 0, n = searchNodes.length; i < n; i++) {
				var node = searchNodes[i];
				this.controlNode = (node.hasClassNameBA(BA_COLLAPSINGBLOCK_NODE_CNAME.control)) ?
				                   	node :
				                   	node.getElementsByClassNameBA(BA_COLLAPSINGBLOCK_NODE_CNAME.control)[0];
				if (this.controlNode) break;
			}
			if (this.controlNode) {
				this.controlNode.addEventListenerBA('click', function(e) {
					e.preventDefault();
					this.toggle();
				}, this);
			}
		}
	}
}

BACollapsingBlock.prototype.initEffect = function() {
	if (typeof BAEffect_Blind == 'function') {
		this.effect = new BAEffect_Blind(this.targetNode, 20, 10, 250);
		this.effect.init();

		this.effect.setCallBack('onStart'   , function(process) {
			switch (process) {
				case 'open'  : this.preProcess('expand'  ); break;
				case 'close' : this.preProcess('collapse'); break;
				default      :                              break;
			}
		}, this);
		this.effect.setCallBack('onResizing', function(process, height) {
			this.doCallBack('onResizing', process, height, this);
		}, this);
		this.effect.setCallBack('onComplete', function(process) {
			switch (process) {
				case 'open'  : this.postProcess('expand'  ); break;
				case 'close' : this.postProcess('collapse'); break;
				default      :                               break;
			}
		}, this);

		this.effect.close('noAnimate');
		if (!this.collapsed) {
			this.effect.open();
		}
	}
}

BACollapsingBlock.prototype.expand = function() {
	this.collapsed = false;
	if (this.effect) {
		this.effect.open();
	} else {
		this.postProcess('expand');
	}
}

BACollapsingBlock.prototype.collapse = function() {
	this.collapsed = true;
	if (this.effect) {
		this.effect.close();
	} else {
		this.postProcess('collapse');
	}
}

BACollapsingBlock.prototype.toggle = function() {
	if (this.collapsed) {
		this.expand();
	} else {
		this.collapse();
	}
}

BACollapsingBlock.prototype.preProcess = function(process) {
	switch (process) {
		case 'expand' :
			this.doCallBack('onStart', process, this);
			break;
		case 'collapse' :
			this.doCallBack('onStart', process, this);
			break;
		default :
			throw 'BACollapsingBlock.preProcess: first argument must be eather "collapse" or "expand".';
			break;
	}
}

BACollapsingBlock.prototype.postProcess = function(process) {
	switch (process) {
		case 'expand' :
			this.node.appendClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.expanded );
			this.node.removeClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.collapsed);
			this.doCallBack('onComplete', process, this);
			break;
		case 'collapse' :
			this.node.appendClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.collapsed);
			this.node.removeClassNameBA(BA_COLLAPSINGBLOCK_STATUS_CNAME.expanded );
			this.doCallBack('onComplete', process, this);
			break;
		default :
			throw 'BACollapsingBlock.postProcess: first argument must be eather "collapse" or "expand".';
			break;
	}
}

BACollapsingBlock.prototype.getStatus = function() {
	return (this.collapsed ? 'collapsed' : 'expanded');
}






/* -------------------- Function : BACollapsingBlockAutoSetup() -------------------- */

function BACollapsingBlockAutoSetup() {
	if (BA_COLLAPSINGBLOCK_AS_ENABLED && !BAAlreadyApplied(arguments.callee)) {
		var searchBaseID  = BA_COLLAPSINGBLOCK_AS_BASENODE_SEARCH_ID;
		var searchBase    = (typeof searchBaseID == 'string' && searchBaseID != '') ?
		                    	document.getElementByIdBA(searchBaseID) :
		                    	document.getElementsByTagNameBA('body')[0];
		if (searchBase) {
			var selector  = BA_COLLAPSINGBLOCK_AS_BASENODE_SELECTOR;
			var tagName   = selector.split('.')[0] || '*';
			var className = selector.split('.')[1];
			searchBase.getElementsByClassNameBA(className, tagName).forEach(function(node) {
				var BACB = new BACollapsingBlock(node, BA_COLLAPSINGBLOCK_AS_USE_VISUAL_EFFECT);
				BACB.init();
				BACB.setCallBack('onStart'   , function() {
//					if (typeof BANA_FOOTER_POS_REVISE == 'object') {
//						BANA_FOOTER_POS_REVISE.hideFooter();
//					}
				});
				BACB.setCallBack('onResizing', function() {
					if (typeof BANA_FOOTER_POS_REVISE == 'object') {
						BANA_FOOTER_POS_REVISE.revise();
					}
				});
				BACB.setCallBack('onComplete', function() {
					if (typeof BANA_FOOTER_POS_REVISE == 'object') {
//						BANA_FOOTER_POS_REVISE.showFooter();
						BANA_FOOTER_POS_REVISE.revise();
					}
				});
			});
		}
	}
}






/* -------------------- Main : register start-up -------------------- */

if (typeof BA == 'object' && BA.ua.DOMok) {
	BAAddOnload(BACollapsingBlockAutoSetup);
}
