/*
init(id string分割符 || id array || 'auto' || '' || null, callback): 序列初始化 
add(obj || id, callback):    单个添加，绑定事件
remove(obj || id, callback): 单个删除，消除事件，外观变成普通盒子
update(obj || id, callback): 单个更新，重绑事件
id: 盒子的id,  状态判定：图标为准*/
var CollapseBox = {
	collection: [],
	config: {
		queuebreak: ',',
		classname: {
			'box':         'nBox',
			'head':        'head',
			'body':        'body',
			'status':      'status',
			'icoExpand':   'ico__expand',
			'icoCollapse': 'ico__collapse'
		},
		cursor: {
			'available': 'pointer',
			'disabled':  'default'	
		},
		status: {
			'expand':   'expand',
			'collapse': 'collpase'
		},
		group: 'group'
	},
	init: function(queue, callback){
		var queue    = arguments[0];
		var callback = arguments[1]
		if(queue && queue != 'auto'){
			if(typeof(queue) == 'string'){queue = queue.split(this.config.queuebreak);}
			for(var i=0; i<queue.length; i++){this.add(queue[i].replace(/(^\s*)|(\s*$)/g, ""));};
		}else{
			var nodes = document.getElementsByTagName('DIV');
			for(var i=0; i<nodes.length; i++){this.add(nodes[i]);}
		}
		if(callback){if(typeof(callback) == 'function'){callback();}}
	},
	add: function(box, callback){	
		var box = this.check(box);
		if(box){
			if(this.exist(box)<0){
				this.bind(box);
				this.collection.push(box);
				if(callback){if(typeof(callback) == 'function'){callback();}}
			}
		}
	},
	remove: function(box, callback){
		var box = this.check(box);
		if(box){
			var index = this.exist(box); 
			if(index>0){
				this.release(box);
				this.collection.splice(index, 1);
				if(callback){if(typeof(callback) == 'function'){callback();}}
			}
		}
	},
	update: function(box, callback){
		var box = this.check(box);
		if(box){
			if(this.exist(box) <0){this.add(box);}
			else{this.bind(box);}
			if(callback){if(typeof(callback) == 'function'){callback();}}
		}
	},
	bind: function(box){
		var _this = this;
		var part = this._getPart(box);
		var oHead = part.head;
		var oBody = part.body;
		var oStatus = part.status;
		//以图标状态为基准 修正错误的状态
		if(oStatus.style.display != 'block'){oStatus.style.display = 'block'}
		if(this.getStatus(box) == this.config.status.expand){oBody.style.display = 'block';}
		else{oBody.style.display = 'none';}
		oHead.style.cursor = this.config.cursor.available;
		oHead.onclick = function(){
			var box = this.parentNode;
			_this.toggle(box);
		}
		//阻止链接触发事件
		var links = oHead.getElementsByTagName('A');
		for(var i=0; i<links.length; i++){
			var op = links[i].parentNode;
			op.onclick = function(evt){
				evt = window.event || evt;
				if(evt.preventDefault){ evt.stopPropagation();}
				else{evt.cancelBubble = true;}
			}
		}
	},
	release: function(box){
		var part = this._getPart(box);
		var oHead = part.head;
		var oBody = part.body;
		var oStatus = part.status;
		
		oHead.style.cursor = this.config.cursor.disabled;
		oHead.onclick = null;
		oStatus.style.display = 'none';
		oBody.style.display = 'block';
	},
	check: function(o){
		var o = this._convert(o);
		var result = false;
		if(o){
			if(o.className){
				if(o.className == this.config.classname.box){
					var part = this._getPart(o);
					if(part.ico && part.head && part.body){result = o;}
				}
			}
		}
		return result;
	},
	exist: function(box){
		this._clean();
		var len = this.getLength();
		for(var i=0; i<len; i++){
			if(box == this.collection[i]){return i}	
		}
		return -1;
	},
	getLength: function(){
		return this.collection.length;
	},
	getStatus: function(box){
		var part = this._getPart(box);
		var ico  = part.ico;
		var body = part.body;
		if(ico){
			var icoclass = ico.className;		
			if(icoclass == this.config.classname.icoCollapse){return this.config.status.collapse;}
			if(icoclass == this.config.classname.icoExpand){return this.config.status.expand;}
		}
	},
	getGroup: function(box){
		var group = box.getAttribute(this.config.group);
		return group;
	},
	changeStatus: function(box, status){
		var part = this._getPart(box);
		var ico = part.ico;
		var body = part.body;
		if(status == this.config.status.expand){
			ico.className = this.config.classname.icoExpand;
			body.style.display = 'block';
		}else{
			ico.className = this.config.classname.icoCollapse;
			body.style.display = 'none';
		}
	},
	toggle: function(box){
		var group = this.getGroup(box);
		if(this.getStatus(box) == this.config.status.collapse){
			if(group){
				var boxs = [];
				var len = this.collection.length;
				for(var i=0; i<len; i++){
					var b = this.collection[i];
					if(this.getGroup(b) == group){boxs.push(b);	}
				}
				this.changeStatus(box, this.config.status.expand);
				for(var i=0; i<boxs.length; i++){
					if(boxs[i] != box){this.changeStatus(boxs[i], 
									   this.config.status.collapse);}	
				}
			}else{
				this.changeStatus(box, this.config.status.expand);
			}
		}else if(this.getStatus(box) == this.config.status.expand){
			this.changeStatus(box, this.config.status.collapse);
		}
	},
	_getPart: function(box){
			var status = null;
			var ico   = null;
			var head  = null;
			var body  = null;
			var nodes = box.getElementsByTagName('DIV');
			for(var i=0; i<nodes.length; i++){
				if(nodes[i].className == this.config.classname.status){
					status = nodes[i];
					ico = nodes[i].firstChild;	
				}
				if(nodes[i].className == this.config.classname.head){head = nodes[i];}
				if(nodes[i].className == this.config.classname.body){body = nodes[i];}
			}
			return {
				'status': status,
				'ico':    ico,
				'head':   head,
				'body':   body
			}
	},
	_getStyle: function(obj, attribute){     
		return obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj, false)[attribute];
	},
	_convert: function(o){
		var o = (typeof(o) == 'string') ? document.getElementById(o) : o;
		return o;
	},
	_clean: function(){
		var index = -1;
		for(var i=0; i<this.collection.length; i++){
			if(!this.check(this.collection[i])){ index = i; }
		}
		if(index != -1){
			this.collection.splice(index, 1);
			this._clean();
		}
	}
}
window.nova_init_hook_js_collapsebox=function(){CollapseBox.init();}

