
/*--------------------------------------------------------------------------------

	To prevent jumping when auto_close is true use padding instead of margins.

--------------------------------------------------------------------------------*/

	var headings_blind = Class.create({
							  
		initialize: function(element, options)
		{
			this.inprogress = false; // Set to true when animating to prevent events from occuring twice
			
			this.options = {
				auto_close: true,
				allow_close: true,
				duration: 1.0
			}
			Object.extend(this.options, options || {}); 
			
			
			element.select('h1,h2,h3,h4,h5,h6').each(function(heading){
				
				heading.setStyle({'cursor': 'pointer'});
				
				// Place all content after the heading into a div
				var div = new Element('div');
				heading.nextSiblings().each(function(p){
					if (p.tagName.match(/^H[1-6]{1}$/i)){
						throw $break;
					} else {
						div.insert(p.remove());
					}
				});
				
				// Place the div after the heading, hiding it.
				// Forcing the height to fix a bug where the sliding jerks at the end.
				heading.insert({after: div});
				var height = div.getHeight() + 'px';
				div.hide();
				div.setStyle({'height': height});
				
				// Apply the animation effect
				heading.observe('click', function(){
					if (!this.inprogress){
						if (div.getStyle('display') == 'none'){
							
							this.inprogress = true;
							
							element.select('.selected').invoke('removeClassName', 'selected');
							heading.addClassName('selected');
							
							Effect.BlindDown(div, {
								duration: this.options['duration'],
								afterFinish: function(){ this.inprogress = false; }.bind(this)
							});
							
							if (this.options['auto_close']){
								div.adjacent('div').each(function(el){
									if (el.getStyle('display') == 'block'){
										Effect.BlindUp(el, {
											duration: this.options['duration']
										});
									} 
								}.bind(this));
							}
						}
						else if (this.options['allow_close'])
						{
							Effect.BlindUp(div, {
								duration: this.options['duration']
							});
							element.select('.selected').invoke('removeClassName', 'selected');
						}
					}
					
				}.bind(this));
				
			}.bind(this));
			
		}
		
	});
		

