/*
	francis,
	20 Feb 2008
*/

var AccordionSetting={
	duration:0.3
};

var Accordion = Class.create({	
	initialize: function(ele,options) {
		this.ele = $(ele.identify());
		
		this.options = Object.extend(
			{
				node_select:"li",
				head_select:".head",
				content_select:".content",
				switch_on:"mouseover",
				is_float:false,
				auto_first_open:true,
				height:null
			},options				
		);
		
		if(this.options.height==null){
		this.ele.makeClipping();
		this.height=this.ele.getHeight();
		this.ele.undoClipping();
		}else{
			this.height=this.options.height;	
		}
		
		
		
		
		if(this.options.is_float){
			this.ele.select(this.options.node_select).each(
				function(e){
					if(c=e.down(this.options.content_select)){
						c.setStyle({height:e.getHeight()+"px"});
					}
					//e.setStyle({height:e.getHeight()+"px"});
				}.bind(this)
			);
			
		}else{
			var total_head_height=0;
			this.ele.select(this.options.head_select).each(function(he){
				total_head_height+=he.getHeight();
				
			}); 
			this.ele.select(this.options.content_select).each(
				function(e){
					e.setStyle({height:(this.height-total_head_height)+"px"});
					
				}.bind(this)
			);
		}
		
		
		
		this.nodes=[];
		this.node_list={};
		this.ele.cleanWhitespace();
		this.ele.select(this.options.node_select).each(
			function(e){
				e.cleanWhitespace();
				var is_first_open=false;
				if(!e.hasClassName("selected")){
					if(this.options.auto_first_open){
						is_first_open = (this.nodes.length==0);
					}
				}else{
					is_first_open=true;
				}
				
				
				if(is_first_open) {
					e.addClassName("selected");
					this.current_content_ele = e.down(this.options.content_select);
				}
				
				if(e.down(this.options.content_select)){
				var b=new Blind(e,{
										first_open:is_first_open,
										switch_on:this.options.switch_on,
										head_select:this.options.head_select,
										content_select:this.options.content_select,
										expand_effect: Prototype.emptyFunction,
										collapse_effect: Prototype.emptyFunction,
										switch_func:function(ob){
											if(!this.effecting && this.current_content_ele!=ob.content_ele){
												var effects=[];
												effects.push(new Effect.BlindDown(ob.content_ele,{ sync: true }));
												
												this.nodes.each(
													function(cob){	
														if(ob!=cob){
															effects.push(new Effect.BlindUp(cob.content_ele,{ sync: true }));
														}
													}
												);
												
												this.ele.select(this.options.node_select).invoke("removeClassName","selected");
												 e.addClassName("selected");				
												 this.current_content_ele.setStyle("overflow-y:hidden");
												
												this.effecting=true;
												 new Effect.Parallel(
												 effects,
												 {duration: AccordionSetting.duration,
												 afterFinish:function(){
													 this.effecting=false;
													 this.current_content_ele=ob.content_ele;
													 this.current_content_ele.setStyle("overflow-y:auto");
													 if(Prototype.Browser.IE){
														 this.current_content_ele.setStyle({width:(this.ele.getWidth()-8)+"px"});
													 }
												 }.bind(this)}
												 );
												
											}
										}.bind(this)
									});
				
				this.nodes.push(b);
				this.node_list[e.identify()]=b;
				}
			}.bind(this)
		);
		 if(Prototype.Browser.IE){
			 this.current_content_ele.setStyle({width:(this.ele.getWidth()-8)+"px"});
		 }
		//this.nodes.invoke("collapse");
 	},
	expand:function(ele_id){
		var ele=$(ele_id);
		
		this.node_list[ele.identify()].options.switch_func(this.node_list[ele.identify()]);
	}
});

document.observe(
	"dom:loaded",
	function(){
		var bind = function(){
			$$(".accordion").each(
				function(ele){
					regcmp(new Accordion(ele,{switch_on:"mouseover",is_float:false}),ele.identify());
					//regcmp(new Accordion(ele),ele.identify());
				} 
			);
			/*$$(".accordion_click").each(
				function(ele){
					regcmp(new Accordion(ele,{switch_on:"click"}),ele.identify());
				}
			);
			$$(".accordion_float").each(
				function(ele){
					regcmp(new Accordion(ele,{auto_first_open:false,is_float:true}),ele.identify());
				}
			);
			$$(".accordion_float_click").each(
				function(ele){
					regcmp(new Accordion(ele,{switch_on:"click",auto_first_open:false,is_float:true}),ele.identify());
				}
			);*/
			/*$$(".blind").each(
				function(ele){
					alert(ele.id);
					regcmp(new Blind(ele.identify()),ele.identify());
				}
			);*/
			
		}
		bind.defer();
		
	}
);

