/*

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

Changes
	v1.1 - added slideTypes functionality

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
Optionally add type-N to the class where N is the number of the slideType you want (starting at 0)

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 type2 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'),
	slideTypeExp: new RegExp('type-(\\d+)','i'),
	
	slideTypes: Array( 
		/* 01 */ {'height':'toggle'},
		/* 02 */ {'height':'toggle', 'width':'toggle', 'opacity':'toggle'}
	),
	
	toggleFind: function(srcElm) {
		var objParent = $(srcElm);
		while(objParent && !objParent.hasClass('slider')) {
			objParent = objParent.parent();
		}
		if(!objParent) return;
		slideTop = objParent.find('.top');
		slideTop.click();
	},
	
	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');

		matches = slider.slideTypeExp.exec( objParent.attr('class') );
		sType = matches && slider.slideTypes[ parseInt(matches[1]) ] ? parseInt(matches[1]) : 0;
		sType = slider.slideTypes[sType];

		// show/hide the slider content
		slideContent.animate(sType, '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);
		objParent.addClass('animating');
		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() {
			/* ******** CUSTOM ******** */
			isAdvSearchPage = $(this).hasClass('adv-search-slider') && location.search.toLowerCase().indexOf('p=advanced_search') > 0;

			if(isAdvSearchPage) {
				$(this).removeClass('closed');
				$(this).addClass('open');
			} else
			/* ******** END CUSTOM ******** */
			
			// if the slider is not set to closed, add the open class
			if(!$(this).hasClass('closed') && !$(this).hasClass('open')) $(this).addClass('open');
			if($(this).hasClass('closed')) $('.content',this).css('display','none');
			
			slideLink = $(this).find('.top:first');
			slideLink.css('cursor','pointer');
			slideLink.click( slider.toggleSlide );
		} );
	}
	
};

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

