//origional programmed by Ron 20 March 08 in Prototype
//Reporgramed in Mootools 11 DEC 2008-Ron
//this function finds the Saturno_Expander classed div and sets the expantion and contraction buttons. 
//You can have as many expand_expand/expand_shink as you wish, but unly one expand_content
    //format:
    
	//<div class="Saturno_Expander">
	//    <div class="expand_expand">+ Expand</div>
	//    <div class="expand_shink" style="display:none;">- Shrink</div>
	//    <div class="expand_content" style="display:none;"></div>
	//</div> 

//Author: Ronald Eddy
//Beta release: 11 DEC 2008
//Copyright 2008 Saturno Design LLC. All rights reserved.

window.addEvent('domready', function() {
		$$('.Saturno_Expander').each(function(item, index){
			item.store('SaturnoExpander', new SaturnoExpander(item));
		});
})


var SaturnoExpander = new Class({

	initialize: function(obj){
		this.state='';
		this.expanders=[];	
		this.shrinkers=[];
		this.content=[];			
		this.BaseObject=obj;
		this.initChildren($(this.BaseObject));
		this.initNonChildren($(this.BaseObject));
		
	},
	setstate: function(state){
		switch (state)
		{
			case 'expand':
			  this.expanders.each(function(item){
					item.set('styles', {'display' : 'none'});
			  });
			  this.shrinkers.each(function(item){
					item.set('styles', {'display' : ''});
			  });
			  this.content.each(function(item){
					item.set('styles', {'display' : ''});
			  });				  			  
			break;
			case 'shrink':  
			  this.expanders.each(function(item){
					item.set('styles', {'display' : ''});
			  });
			  this.shrinkers.each(function(item){
					item.set('styles', {'display' : 'none'});
			  });
			  this.content.each(function(item){
					item.set('styles', {'display' : 'none'});
			  });				
			break;				
		}
	},
	addControler: function(obj, type){
		switch(type)
		{	
			case 'expand':
				this.expanders.push(obj);
			break;
			case 'shrink':
				this.shrinkers.push(obj);
			break;
			case 'content':
				this.content.push(obj);
			break;			
		}
		
	},
	setChild: function(baseobj,item, state)
	{
		item.store('baseobj', baseobj);		
		this.addControler(item, state);
		item.addEvent('click', function(){
			item.retrieve('baseobj').retrieve('SaturnoExpander').setstate(state);
		});
	},					
	initChildren: function(baseobj)
	{
		baseobj.getElements('.expand_expand').each(function(item, index){
				this.setChild(baseobj, item, 'expand');
			}, this);	
		baseobj.getElements('.expand_shink').each(function(item, index){
				this.setChild(baseobj, item, 'shrink');
			}, this);	
		baseobj.getElements('.expand_content').each(function(item, index){
				this.addControler(item, 'content');
		}, this);							
	},
	initNonChildren: function(baseobj)
	{
		if (baseobj.get('others'))
		{
			baseobj.get('others').split(',').each(function(item, index){
				var obj= $(item.trim());
				if (obj.hasClass('expand_expand'))
				{
					this.setChild(baseobj, obj, 'expand');
				}
				if (obj.hasClass('expand_shrink'))
				{
					this.setChild(baseobj, obj, 'shrink');
				}
			}, this);				
		
		}
		
	}
});
