/**
	Simple open close
	Supports:
		Default state of either open or closed
		Free toggler/toglee structure (though it helps if the toggler isn't inside the toglee)
		Text can change freely, or not change
	Doesn't support:
		One toggler toggling several toglees
	
	How it works:
		Set the toggler's to be an a tag with class 'soc'
		It's text and it's rel attribute value will be swapped when clicked. If there is no rel attribute they will not be swapped
		Set the href attribute to be a hash followed id of the toglee (actually simply the CSS toglee selector, still doesn't support multiple targets)
		If the toglee is to be closed by default, give it a class of 'close'
		The toggler will toggle between having class 'open' and not.
	
	Example:
		<a class="soc" rel="Open me" href="#banana">Close me</a>
		<a class="soc" href="#banana">toggle openosity</a>
		... arbitrary content ...
		<div id="banana">
			Now you can see me
			<ul>
				<li>1</li>
				<li>2</li>
			</ul>
		</div>
	
	It is usually required that the target(s) have overflow hidden via CSS
*/
var simpleOpenClose = new Class({
	
	initialize: function(options){
		this.selector = 'a.soc';
		this.start();
	},
	
	start: function(){
		var togglers = $$(this.selector);
		togglers.each(function(toggler, i){
			toggler.addEvents({
				click: function(e){
					if (e) e.stop();
					var target = $$(toggler.get('href'))[0];
					if (!target.retrieve('original-height')) {
						target.store('original-height', target.getSize().y)
					}
					var morphTo = {
						'display': 'block',
						'height': target.getSize().y.toInt() === 0 ? target.retrieve('original-height') : '0px'
					};
					if (e) {
						target.morph(morphTo);
					} else {
						target.setStyles(morphTo);
					}
					toggler.toggleClass('open');
					if (toggler.get('rel')) {
						var temp = toggler.get('text');
						toggler.set('text', toggler.get('rel'));
						toggler.set('rel', temp);
					}
					return false;
				}
			});
			var target = $$(toggler.get('href'))[0];
			if (target.hasClass('close')) {
				toggler.fireEvent('click', false);
			}
		});
	}
});
