/*
	francis,
	20 Feb 2008
	- fixed css padding problem
	
	cyrus,
	07 Feb 2009
	- add list all option, opened option
*/
var Blind = Class.create(
	{
		
		initialize:function(ele,options){
			this.ele = $(ele);
			
			this.options = Object.extend({
				head_select:".head",
				content_select:".content_blind",
				switch_on:"click",
				switch_func:function(){this[(!this.isExpanded)?"expand":"collapse"]()}.bind(this),
				fix_padding:true,
				expand_effect: {effect:Effect.BlindDown,options:{duration:0.2}},
				collapse_effect: {effect:Effect.BlindUp,options:{duration:0.2,queue:'end'}},
				beforeExpand:Prototype.emptyFunction,
				first_open:this.ele.readAttribute('opened')=='true',
				list_all:false
			},options);
			
			
			
			this.isExpanded = this.options.first_open;
			
			
			//this.head_ele = this.ele.down(this.options.head_select);
			this.head_ele_id = this.ele.down(this.options.head_select).identify();
			
			this.content_ele = this.ele.down(this.options.content_select);
			
			
			
			if(this.options.fix_padding){
				var fixpadding=function(e){
					var p ={
						paddingTop:e.getStyle("padding-top"),
						paddingLeft:e.getStyle("padding-left"),
						paddingBottom:e.getStyle("padding-bottom"),
						paddingRight:e.getStyle("padding-right")
					};
					e.setStyle({padding:0});
					var html = e.innerHTML;
					
					/*var d = e.childElements().last();
					d.setStyle(p);
					d.insert({bottom:e});
					alert(e.innerHTML);*/
					//d.update(e);
					
					
//					e.update("<div></div>");
//					e.down("div").setStyle(p);
//					e.down("div").insert({bottom:html});
					e.select(".blind").each(
						function(e1){
							regcmp(new Blind(e1.identify()),e1.identify());
						}
					);
					//html
					//alert(e.innerHTML);
					
				}
				
				fixpadding($(this.head_ele_id));  
				fixpadding(this.content_ele);
			}
			
			
			
			
			this.effecting=false;
			this.effect=null;
			
			if(!this.options.first_open) this.content_ele.hide();
			
			
			$(this.head_ele_id).observe(this.options.switch_on,this.options.switch_func.curry(this));
			
			
			
		},
		expand:function(){
			
			/* list all */
			if (!this.options.list_all) $$('.blind').each(function(e){getcmp(e).collapse()});
			/************/
			
			if(!this.isExpanded){
				this.options.beforeExpand(this);
				var o = Object.extend(this.options.expand_effect.options,{afterFinish:function(){
					this.content_ele.setStyle({height:""});																   
				}.bind(this)});
				//alert(this.options.expand_effect.options);
				this.effect = this.options.expand_effect.effect(this.content_ele, o);	
			}
			this.isExpanded=true;
			
		},
		collapse:function(){
			if(this.isExpanded)
				this.effect =   this.options.collapse_effect.effect(this.content_ele, this.options.collapse_effect.options);	
			this.isExpanded=false;
		}
	}
);

BlindController = {
	expandAll:function(){
		$$('.blind').each(function(e){getcmp(e).expand()});	
	},
	collapseAll:function(){
		$$('.blind').each(function(e){getcmp(e).collapse()});
	}
};


document.observe(
	"dom:loaded",
	function(){
		(function(){
		$$(".blind").each(function(e){e.identify()});
		$$(".blind").each(
			function(ele){
				if(!ele.up(".accordion")){
					regcmp(new Blind(ele.identify()),ele.identify());
				}
			}
		);
		}).defer();
	}
);