/* 	
	
	Simple toggler by Marty Stake
	Requires prototype.js 1.6.  
	If you want sliding, you need Scriptaculous as well.	
	
	new Toggler( { 
		togglers : 'h3',
		togglees : 'p',
		// extras //
		accordion: true,
		action : 'slide',
		afterShow: function(toggler, togglee) {
			toggler.select('span a')[0].update('[Hide -]');
		},
		afterHide: function(toggler, togglee) {
			toggler.select('span a')[0].update('[More +]');
		}
	} );
	
*/

var Toggler = function(options) {
		
	var togglers = options.togglers || 'dt';
	var togglees = options.togglees || 'dd';
	var expandAll = options.expandAll;
	var hideAll = options.hideAll;
	var onClass = options.onClass || 'open';
	var accordion = options.accordion || false;
	
	var current = '';
	
	var closeHandle = (typeof options.closeHandle == 'function') ? options.closeHandle : function(togglee) {
		
		var hiddenClose = options.hiddenClose || 'Hide';
		
		var ch = new Element('span').setStyle( { 
			'display' : 'block', 
			'zoom' : 1,
			'cursor' : 'pointer',
			'textDecoration' : 'underline',
			'textAlign' : 'right'
		} ).update(hiddenClose);
	
		togglee.insert( ch );
		return ch;
	
	};
	
	$$(togglers).each(function(toggler) {
		
		var togglee = toggler.next(togglees);
		togglee.setStyle( { 'display' : 'none' } );
		
		toggler.setStyle( { 'cursor' : 'pointer' } );
		toggler.observe( 'click', function(e) { 
				toggle(toggler, togglee); 
				e.stop(); 
			} );
		
		var ch = closeHandle(togglee);
		if ( ch )  {
			ch.observe( 'click', function(e) { 
				toggle(toggler, togglee);
				e.stop(); 
			} );
		};
			
		
	});	
	
	$$(expandAll).invoke('observe', 'click', function(e) {
		expandAll();
		accordion = false;
		e.stop();
	})
	
	$$(hideAll).invoke('observe', 'click', function(e) {
		hideAll();
		accordion = false;
		e.stop();
	})
	
	var toggle = function(toggler, togglee) {
	
		if ( accordion ) {
			hideAll();
			if ( current != toggler ) {
				toggleAction(toggler, togglee);
			}
			current = (current == toggler) ? '' : toggler;
		}
		
		else {
			toggleAction(toggler, togglee);
			current = toggler;
		}
		
	}
	
	var Actions = { 
	
		normal : function(toggler, togglee) {
				
			//togglee.toggle();
			
			if (togglee.getStyle('display') == 'block') {
				togglee.setStyle( { 'display' : 'none' } );
				if ( typeof options.afterHide == 'function' ) {
					options.afterHide(toggler, togglee);
				}
			}
			else {
				togglee.setStyle( { 'display' : 'block' } );
				if ( typeof options.afterShow == 'function' ) {
					options.afterShow(toggler, togglee);
				}
			}
			
			swapClass(toggler);
		
		},
	
		slide : function(toggler, togglee) {
		
			if (togglee.getStyle('display') == 'none') {
				togglee.setStyle( { 'display' : '', 'overflow' : 'hidden', 'height' : '0' } );
				var height = togglee.scrollHeight;
				new Effect.Morph( $(togglee), { 
					style: { 'height' : height + 'px' },
					duration: .3,
					afterFinish: function() {
						swapClass(toggler);
						if ( typeof options.afterShow == 'function' ) {
							options.afterShow(toggler, togglee);
						}
					}
				} );
			}
			else {
				new Effect.Morph( $(togglee), { 
					style: { 'height' : '0' + 'px' },
					duration: .3,
					afterFinish: function() {
						if ( typeof options.afterHide == 'function' ) {
							options.afterHide(toggler, togglee);
						}
						togglee.setStyle( { 'display' : 'none', 'overflow' : 'hidden', 'height' : 'auto' } );
						swapClass(toggler);
						
					}
				} );
			}
			
		},
		
		
		// DOESN'T WORK YET!
		accordianSlide : function(toggler, togglee) {
			
			var sync = (!current) ? false : true;
			
			if ( !effect ) var effect = [];
			
			if ( togglee.getStyle('display') == 'none' ) {
				togglee.setStyle( { 'display' : 'block', 'overflow' : 'hidden', 'height' : '0' } );
				var height = togglee.scrollHeight;
				effect.push(new Effect.Morph( $(togglee), { 
					style: { 'height' : height + 'px' },
					duration: .3,
					sync: sync,
					afterFinish: function() {
						swapClass(toggler);
					}
				} ));
			}
			else {
				effect.push(new Effect.Morph( $(togglee), { 
					style: { 'height' : '0' + 'px' },
					duration: .3,
					sync: sync,
					afterFinish: function() {
						togglee.setStyle( { 'display' : 'none', 'overflow' : 'hidden', 'height' : 'auto' } );
						swapClass(toggler);
					}
				} ));
			}
			
			if ( sync ) {
				if ( effect.length == 2 ) {
					alert('yo');
					new Effect.Parallel( effect, { 
						afterFinish: function() { effect = null }, 
						duration: .3
					} );
				}
			
			}
				
		}
		
	};
	
	var toggleAction = Actions[options.action] || Actions.normal;
	
	var expandAll = function() {
		
		$$(togglers).each(function(toggler) {
			
			var togglee = toggler.next(togglees);
			
			if (togglee.getStyle('display') == 'none') {
				toggleAction(toggler, togglee);
			}
		} );

	};
	
	var hideAll = function() {
		
		$$(togglers).each(function(toggler) {
			
			var togglee = toggler.next(togglees);
			
			if (togglee.getStyle('display') != 'none') {
				toggleAction(toggler, togglee);
			}
		} );
		
		
	};
	
	var swapClass = function(toggler) {
		toggler.hasClassName(onClass) ?	toggler.removeClassName(onClass) : toggler.addClassName(onClass);
	}
	
	var bookmark = function() {
	
		var bookmark = window.location.href.split('#');
		
		if ( bookmark[1] ) {
			var mark = $(bookmark[1].toString());
			var togglee = mark.next(togglees);
			toggle(mark, togglee); 
			
		}
	
	};
	
	bookmark();
		
	return {
		hideAll : hideAll,	
		expandAll : expandAll,
		toggle : toggleAction
	}
		
};


