/*
	francis,
	20 Feb 2008
	- fixed css padding problem
*/
var Blind = Class.create(
	{
		
		initialize:function(ele,options){
			this.ele = $(ele);
			
			this.options = Object.extend({
				head_select:".head",
				content_select:".content",
				switch_on:"click",
				switch_func:function(){this[(!this.isExpanded)?"expand":"collapse"]()}.bind(this),
				fix_padding:false,
				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:false
			},options);
			
			this.head_ele = this.ele.down(this.options.head_select);
			
			this.content_ele = this.ele.down(this.options.content_select);
			
			this.effecting=false;
			this.effect=null;
			
			
			if(!this.options.first_open) this.content_ele.hide();
			
			
			this.head_ele.observe(this.options.switch_on,this.options.switch_func.curry(this));
			
			
			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;
					/*e.insert({bottom:"<div></div>"});
					var d = e.childElements().last();
					d.setStyle(p);
					d.insert({bottom:e});
					alert(e.innerHTML);*/
					//d.update(e);
					
					e.update("<div>"+html+"</div>");
					e.down("div").setStyle(p);
					
				}
				
				fixpadding(this.head_ele);
				fixpadding(this.content_ele);
			}		
			
		},
		expand:function(){
			if(!this.isExpanded){
				this.options.beforeExpand(this);
				//alert(this.options.expand_effect.options);
				this.effect = this.options.expand_effect.effect(this.content_ele, this.options.expand_effect.options);	
			}
			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){
				regcmp(new Blind(ele.identify()),ele.identify());
			}
		);
		}).defer();
	}
);