
$(document).ready( function() {
	$('.teaser').teaser( {items: '.testimonials li', nav: null, cycle: true, maxItems: 20, effect: 'fade', interval: 10} );
	$('dl').faq({effect: 'slide'});
});

(function($) {
	$.fn.faq = function(settings) {
		var defaults = {
			q: 'dt',
			a: 'dd',
			qHover: 'faq-question-hover',
			qCurrent: 'faq-question-current',
			aCurrent: 'faq-answer-current',
			effect: null,
		};
		settings = $.extend(defaults, settings);
		return this.each(function(i, v) {
			$(settings.a, v).hide(); // hide all the answers
			var questions = $(settings.q, v).each(function(i) {
				$(this).bind('mouseover',function() { $(this).addClass(settings.qHover); });
				$(this).bind('mouseout',function() { $(this).removeClass(settings.qHover); });
				$(this).bind('click', function() { if($(this).hasClass(settings.qCurrent)) return; hideAnswer('.'+settings.qCurrent); showAnswer(this); });
			});
			function getAnswer(question) {
				return $(question).next(settings.a);
			}
			function hideAnswer(question) {
				var answer = getAnswer($(question));
				
				switch(settings.effect)
				{
					case 'fade' :
						$(answer).fadeOut('slow');
						break;
					case 'slide' :
						$(answer).slideUp('slow');
						break;
					default :
						$(answer).hide();
						break;
				}
					
				$(answer).removeClass(settings.aCurrent);
				$(question).removeClass(settings.qCurrent);
			}
			function showAnswer(question) {
				var answer = getAnswer($(question));
				switch(settings.effect)
				{
					case 'fade' :
						$(answer).fadeIn('fast');
						break;
					case 'slide' :
						$(answer).slideDown('fast');
						break;
					default :
						$(answer).hide();
						break;
				}
					
				$(answer).addClass(settings.aCurrent);
				$(question).addClass(settings.qCurrent);
			}
		});
	};
})(jQuery);

/* 
Teaser
Hides the items in a list
Creates a "dot" based navigation for those items
Clicking the dots show the corresponding item
Shows the first item automatically
Optionally moves to the next item after a specified interval
	
Copyright (c) 2009 Rob Sutherland Consulting, LLC
*/

(function($) {
	$.fn.teaser = function(settings) {
		var defaults = {
			items: '.teaser-contents li',
			nav: '.teaser-header .options',
			maxItems: 4,
			cycle: false,
			interval: 5,
			text: '%i',
			effect: null
		};
		settings = $.extend(defaults, settings);
		return this.each(function(i, v) {
			// iterate over the items
			var current = 0;
			var items = $(settings.items, v).each(function(i) {
				// hide the items
				$(this).hide();
				// skip any that are beyond the max
				if (i > settings.maxItems - 1) return;
				// create the link element and append it to the dot navigation node

				var text = settings.text.replace('%i', i+1);

				$('<a />')
						.attr('class', 'dot')
						.attr('index', i)
						.css('cursor', 'pointer')
						.text(text)
						.click(function() {
							current = $(this).attr('index');
							show(current);
							$(settings.items, v).hide();
							$(settings.items, v).eq($(this).attr('index')).show();
						})
						.appendTo($(settings.nav, v));
						
					if ($(settings.items, v).length == 1) $(settings.nav, v).hide();
				
			});

			function show(index) {
				if(settings.effect == "fade")
					$('.current', v).fadeOut('fast').removeClass('current');
				else
					$('.current', v).removeClass('current');
				
				$(settings.nav + ' a', v).eq(index).addClass('current');
				$(settings.items, v).hide();

				if(settings.effect == "fade")
					$(settings.items, v).eq(index).fadeIn('slow');
				else
					$(settings.items, v).eq(index).show();
			}

			function cycle() {
				current += 1;
				if (current > $(settings.items, v).length - 1) current = 0;
				show(current);
			}

			if (settings.cycle)
				setInterval(cycle, settings.interval * 1000);

			show(0);
		});
	};
})(jQuery);


