/********************************
* WASABI fold                   *
* ----------------------------- *
* CR: DKone 25.03.2009 20:03:38 *
********************************/



/**********************
*  WASABI_FOLD CLASS  *
**********************/
var wasabi_fold=Class.create({
	cssRule_container:'.wasabi_fold_container',//cssRule to detect the containerElement
	cssRule_trigger:'.wasabi_fold_trigger',//cssRule to detect the triggerElement in the container
	cssRule_content:'.wasabi_fold_content',//cssRule to detect the contentElement in the container
	closeAllOther:false,//if true hide all opend objects and show only one open
	itemArray:[],//Array with all foldItem-Objects
	effect_hide:'hide',//the effect to hide the content
	effect_show:'show',//the effect to show the content
	duration:2,//duration of effects

	initialize:function(optionHash) {
		if(optionHash.cssRule_container) {this.cssRule_container=optionHash.cssRule_container;}
		if(optionHash.cssRule_trigger) {this.cssRule_trigger=optionHash.cssRule_trigger;}
		if(optionHash.cssRule_content) {this.cssRule_content=optionHash.cssRule_content;}
		if(optionHash.closeAllOther) {this.closeAllOther=optionHash.closeAllOther;}
		if(optionHash.effect_hide) {this.effect_hide=optionHash.effect_hide;}
		if(optionHash.effect_show) {this.effect_show=optionHash.effect_show;}
		if(optionHash.duration) {this.duration=optionHash.duration;}
		this.initObserver();
	},
	initObserver:function() {
		$$(this.cssRule_container).each(function(item){
			var trigger=item.down(this.cssRule_trigger);
			var content=item.down(this.cssRule_content);
			var currentItem=new wasabi_fold_item(content,this.effect_hide,this.effect_show,this.duration);
			this.itemArray.push(currentItem);
			trigger.observe('click',function(event){
				if(this.closeAllOther) {this.closeAll(currentItem);}
				currentItem.toggle();
				Event.stop(event);
				return false;
			}.bind(this));
		}.bind(this));
	},
	closeAll:function(currentItem) {
		this.itemArray.each(function(item){
			if(!item.hidden && item.id!=currentItem.id) {item.hide();}
		}.bind(this));
	}
});
//make Obj like this: wasabi.fold=new wasabi_fold({closeAllOther:true,effect_hide:'Fade',effect_show:'Appear'});



/**************************************
*  WASABI_FOLD_ITEM CLASS (internal)  *
**************************************/
var wasabi_fold_item=Class.create({
	content:null,//the HTML-object to show/hide
	hidden:false,//am i hidden or not
	id:'',//unique id of item
	effect_hide:'',//the effect to hide the content
	effect_show:'',//the effect to show the content
	duration:0,//duration of effects

	initialize:function(content,effect_hide,effect_show,duration) {
		this.content=content;
		this.id=wasabi.lib.getRandomString(10);
		this.effect_hide=effect_hide;
		this.effect_show=effect_show;
		this.duration=duration;
		if(this.content.getStyle('display')=='none') {this.hidden=true;};
	},
	toggle:function() {
		if(this.hidden) {
		this.show();
		} else {
		this.hide();
		}
	},
	hide:function() {
		//alert('hide '+this.id);
		this.hidden=true;
		this.effect(this.effect_hide);
	},
	show:function() {
		//alert('show '+this.id);
		this.hidden=false;
		this.effect(this.effect_show);
	},
	effect:function(effect) {
		switch(effect) {
			case 'hide':
				this.content.hide();
				break;
			case 'show':
				this.content.show();
				break;
			case 'BlindUp':
				Effect.BlindUp(this.content,{duration:this.duration});
				break;
			case 'BlindDown':
				Effect.BlindDown(this.content,{duration:this.duration});
				break;
			case 'SlideUP':
				Effect.SlideUp(this.content,{duration:this.duration});
				break;
			case 'SlideDown':
				Effect.SlideDown(this.content,{duration:this.duration});
				break;
			case 'Fade':
				Effect.Fade(this.content,{duration:this.duration});
				break;
			case 'Appear':
				Effect.Appear(this.content,{duration:this.duration});
				break;
			case 'Shrink':
				Effect.Shrink(this.content,{duration:this.duration,direction:'top-left'});
				break;
			case 'Grow':
				Effect.Grow(this.content,{duration:this.duration,direction:'top-left'});
				break;
		}
	}
});


