/**
 * image and caption switcher plugin
 * 
 * @auther ianbarker
 */

(function($) {

	$.fn.extend({

		switcher : function(options) {

			var defaults = {
				boxId : 'testimonial-box',
				quoteId : 'testimonail-quote',
				creditId : 'testimonail-credit'
			};

			var options = $.extend(defaults, options);

			return this.each(function() {

				var $container = $(this);
				var $slides = $container.children();
				var $box = $('#' + options.boxId);
				var $quote = $('#' + options.quoteId);
				var $credit = $('#' + options.creditId);
				var $slide = $slides.eq(0);
				
				$box.hide();

				$(window).load(function() {

					// prepare the testimonial box with the first quote
					var content = $slide.children('img').attr('title').split('|');

					if (content[0].length > 0 ) {

						$quote.html(content[0]);
						$credit.html(content[1]);

						$box.fadeIn();
					}
					
					if ($slides.length > 1) {
						// start the animation
						setInterval($container.nextSlide, 6000);
					}

				});

				// helper functions
				$container.nextSlide = function() {
					var width = $slide.width() + parseInt($slide.css('border-right-width'));
					$box.fadeOut();
					$container.animate({
						left : '-=' + width
					}, {
						duration : 2000,
						easing : 'easeInOutQuart',
						complete : function() {
							$container.find('li:last').after($container.find('li:first'));
							$container.css('left', '0px');
							$slide = $container.find('li:first');
							var content = $slide.children('img').attr('title').split('|');
							if (content[0].length > 0 ) {
								$quote.html(content[0]);
								$credit.html(content[1]);
								$box.fadeIn();
							}
						}
					});
				};

			});

		}

	});

})(jQuery);
