/******************************
*                             
*   Name:     Accordion V2.0
*   Author:   Aken li           
*   Date:     2008.07.22 
*   Modified: 2008.11.10
*   Blog:     www.kxbd.com      
*                             
******************************/
var Accordion = Class.create();
Accordion.prototype = {
	initialize:function(id,option){
		this.id = id;
		this.obj = $(id);
		this.option = Object.extend({event:"click",isOne:true,dur:0.3,type:0,className:'nav_on'}, option || {});
		this.isOne = this.option.isOne;//是否只显示一个
		this.dur = this.option.dur;//动画时间，为零时无动画
		this.event = this.option.event;//鼠标事件
		this.type = this.option.type;//展开方向，0:上下展开，1:左右展开
		this.prop = this.type ? 'width' : 'height';
		this.className = this.option.className;
		this.dimsArr = [];//所有子菜单的长宽
		this.isShowNum;//当前显示的子菜单
		this.isShowAll = false;
		this.numSub=0;//所有子菜单的个数
		this.numIsShow=0;//所有显示的个数
		this.init();
	},

	init:function() {
		this.obj.childElements().each(function(o,i){
			var o1 = o.down();
			var o2 = o.down(1);
			if(o2) {
				this.numSub++;
				var content_dims = o2.getDimensions();
				this.dimsArr.push(content_dims);
				this.type ? o2.setStyle({width:"0px"}).makeClipping() : o2.setStyle({height:"0px"}).makeClipping();
				o1.observe(this.event,this.toggleWhich.bindAsEventListener(this,i));
			}else{
				this.dimsArr.push({width:0,height:0});
			}
		}.bind(this));
	},

	showSub:function(element,oH,dur) {
		element.previous().addClassName(this.className);
		if(!dur){
			this.type ? element.setStyle({width:oH+"px"}): element.setStyle({height:oH+"px"});
		}else{
			new Effect.Morph(element, {
				style: this.prop+':'+oH+'px',
				transition:Effect.Transitions.linear,
				duration:dur
			});
		}
		this.numIsShow++;
		if(this.numIsShow >= this.numSub) this.numIsShow = this.numSub;
		if(this.numIsShow == this.numSub) this.isShowAll = true;
	},

	hideSub:function(element,dur) {
		element.previous().removeClassName(this.className);
		new Effect.Morph(element, {
			style: this.prop+':0px',
			transition:Effect.Transitions.linear,
			duration:dur!=null? dur :this.dur
		});
		this.numIsShow--;
		if(this.numIsShow <= 0) this.numIsShow = 0;
		if(this.numIsShow == 0) this.isShowAll = false;
	},

	showWhich:function(i,dur){
		var _o = this.obj.childElements();
		if(_o[i].down(1) && this.isShowNum != i){
			if(this.isOne && this.isShowNum != null){
				this.hideSub(_o[this.isShowNum].down(1));
			}
			this.showSub(_o[i].down(1),(this.type?this.dimsArr[i].width:this.dimsArr[i].height),dur ? dur :0);
			this.isShowNum = i;
		}
	},

	hideWhich:function(i){
		var _o = this.obj.childElements()[i].down(1);
		if(_o && parseInt(_o.getStyle(this.prop))){
			this.hideSub(_o);
			this.isShowNum = null;
		}
	},

	toggleWhich:function(e, i){
		var href = e.element().readAttribute('href');
		if(href=='#' || href.indexOf('javascript')!=-1){
			var _o = this.obj.childElements()[i].down(1);
			parseInt(_o.getStyle(this.prop)) ? this.hideWhich(i,this.dur) : this.showWhich(i,this.dur);
		}
	},

	showAll:function() {
		if(!this.isOne){
			$(this.id).childElements().each(function(o,i){
				if(o.down(1)){
					this.showSub(o.down(1),(this.type?this.dimsArr[i].width:this.dimsArr[i].height),this.dur);
				}
			}.bind(this));
			this.isShowAll = true;
		}
	},

	hideAll:function() {
		if(!this.isOne){
			$(this.id).childElements().each(function(o){
				if(o.down(1)){
					this.hideSub(o.down(1));
				}
			}.bind(this));
			this.isShowAll = false;
		}
	},
	
	toggleAll:function() {
		this.isShowAll ? this.hideAll() : this.showAll();
	}
};