/*

	  Author: Cy Morris
	 Created: April, 2009
Dependencies: jQuery

To create a slider box you need a containing element with the class "slider".
Inside that you need an element with class "top" and a containing element with class "content".
Inside "top" goes the title; inside "content" goes the sliding content
If you want the slider closed initially, add "closed" to the slider class

If you want several sliders but only want one to be open at a time, 
add the class "slider-single-id" to each slider, where "id" is any string [a-zA-Z0-9_-] no spaces

EXAMPLE:
<div class="slider slider-single-leftnav">
	<h4 class="top">TITLE</h4>
	<div class="content">
		text text text text text text 
	</div>
</div>

CSS classes are:
	slider
	slider.open
	slider.closed
	slider.animating
	slider .top
	slider .content

*/

var slider = {
	
	alertErrors: true,
	singleRegExp: new RegExp('(^| )(slider-single-[\\w\\d-_]*)( |$)','i'),
	
	toggleSlide: function() {
		var objParent = $(this).parent();
		// find the true parent
		do {
			if(objParent.hasClass('slider')) break;
			else objParent = objParent.parent();
		} while (objParent);
		if(!objParent) return true;
		
		// find the content
		var slideContent = objParent.find('.content:first');

		// if there is no actual content, return true (follow the link that was clicked, if applicable)
		if(slideContent.children().length==0 || ( slideContent.find('ul').length > 0 && slideContent.find('ul').children().length == 0 )) return true;
		
		if(slideContent.is(':hidden')) {
			// check for the slider-single-id class and if it exists, hide all the sliders with this class
			matches = slider.singleRegExp.exec( objParent.attr('class') );
			if(matches) $('.'+matches[2]).each( function() { slider.hideSlide(this) } );
		}
		
		// add the animating class
		objParent.addClass('animating');

		// show/hide the slider content
		slideContent.animate({'height':'toggle'/*, 'opacity':'toggle'*/}, 'slow', 'swing', function() { slider.toggleSliderClass(slideContent, objParent); });
		
		// return false so the link that was clicked is not activated (if applicable)
		return false;
	},
	
	hideSlide: function(parent) {
		var objParent = $(parent);
		var slideContent = objParent.find('.content');
		slideContent.animate({'height':'hide'/*, 'opacity':'hide'*/}, 'slow', 'swing', function() { slider.toggleSliderClass(slideContent, objParent); });
		return false;
	},
	
	toggleSliderClass: function(slideContent, objParent) {
		// remove the animating class
		objParent.removeClass('animating');
		// add/remove open and closed classes
		if(slideContent.is(':hidden')) {
			objParent.removeClass('open');
			objParent.addClass('closed');
		} else {
			objParent.addClass('open');
			objParent.removeClass('closed');
		}
		
		// NOTE: can't use objParent.toggleClass() because it messes up in IE
	},
	
	init: function() {
		$('.slider').each( function() {
			// if the slider is not set to closed, add the open class
			if(!$(this).hasClass('closed') && !$(this).hasClass('open')) $(this).addClass('open');
			slideLink = $(this).find('.top');
			slideLink.css('cursor','pointer');
			slideLink.click( slider.toggleSlide );
		} );
	}
	
};

if(window.jQuery) $( slider.init );
else if(slider.alertErrors) alert('Slider class requires jQuery to be loaded.');
