/*	
 *	jQuery carouFredSel 1.1.2
 *	www.frebsite.nl
 *	Copyright (c) 2009 Fred Heusschen
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 */


(function($) {
	$.fn.carouFredSel = function(options) {
		return this.each(function() {
			var opts 			= $.extend({}, $.fn.carouFredSel.defaults, options),
				$ul 			= $(this),
				$items 			= $("li", $ul),
				totalItems		= $items.length,
				lastItem		= opts.visibleItems,
				firstItem		= totalItems-1,
				itemWidth		= $items.outerWidth(),
				itemHeight		= $items.outerHeight(),
				autoInterval	= null,
				direction		= (opts.direction == "up" || opts.direction == "right") ? "next" : "prev";


			if (opts.direction == "right" ||
				opts.direction == "left"
			) {
				var css = {
					width	: itemWidth * opts.visibleItems,
					height	: $ul.outerHeight() || itemHeight
				}
			} else {
				var css = {
					height	: itemHeight * opts.visibleItems,
					width	: $ul.outerWidth() || itemWidth
				}
			}
			
			$ul.wrap('<div class="caroufredsel_wrapper" />').css({
				position	: "absolute"
			}).parent().css(css).css({ 
				position	: "relative",
				overflow	: "hidden"
			});

			if (opts.scrollItems 		== 0)	opts.scrollItems		= opts.visibleItems;
			if (opts.scrollItemsButtons == 0)	opts.scrollItemsButtons = opts.scrollItems;

			if (opts.visibleItems >= totalItems) return false;

			$items.filter(":gt("+(opts.visibleItems-1)+")").remove();
			$ul
				.bind("pause", function() {
					if (autoInterval != null) {
						clearTimeout(autoInterval);
					}
				})
				.bind("play", function(e, d) {
					if (opts.pauseDuration > 0) {
						if (d == null	||
							d == '' 	||
							typeof(d)	|| 'undefined'
						) {
							d = direction;
						}
						
						autoInterval = setTimeout(function() {
							$ul.trigger(d);
						}, opts.pauseDuration);
					}
				})
				.bind("next", function(e, b) {
				 	if(lastItem > 0)
					{
						if ($ul.is(":animated")) return false;
	
						var numItems = (b) ? opts.scrollItemsButtons : opts.scrollItems;
	
						for (var a = 0; a < numItems; a++) {
							$ul.append($($items[lastItem]).clone());
							if (++lastItem  >= totalItems) lastItem  = 0;
							if (++firstItem >= totalItems) firstItem = 0;
						}
						if (opts.direction == "right" ||
							opts.direction == "left"
						) {
							var pos = 'left',
								mea = 'width',
								siz = itemWidth;
						} else {
							var pos = 'top',
								mea = 'height',
								siz = itemHeight;
						}
						var css = {},
							ani = {},
							cal = {};
						
						css[mea] = (siz * 0.1) + (siz * $("li", $ul).length);
						ani[pos] = -(siz * numItems);
						cal[pos] = 0;
	
						$ul.css(css)
							.animate(ani, { 
								duration: opts.scrollSpeed, 
								easing	: opts.scrollEffect, 
								complete: function() {
									$ul.css(cal).find("li:lt("+numItems+")").remove();
								}
							}
						);
						$('#peopleCarosel li img').tipTip({defaultPosition : "top"});
						//	auto-play
						$ul.trigger("pause").trigger("play", "next");
					}
				})
				.bind("prev", function(e, b) {
					if(firstItem<(totalItems-1))
					{
						if ($ul.is(":animated")) return false;
	
						var numItems = (b) ? opts.scrollItemsButtons : opts.scrollItems;
	
						for (var a = 0; a < numItems; a++) {	
							$ul.prepend($($items[firstItem]).clone());
							if (--firstItem < 0) firstItem = totalItems-1;
							if (--lastItem  < 0) lastItem  = totalItems-1;
						}
						if (opts.direction == "right" ||
							opts.direction == "left"
						) {
							var pos = 'left',
								mea = 'width',
								siz = itemWidth;
						} else {
							var pos = 'top',
								mea = 'height',
								siz = itemHeight;
						}
	
						var css = {},
							ani = {};
					
						css[pos] = -(siz * numItems);
						css[mea] = (siz * 0.1) + (siz * $("li", $ul).length);
						ani[pos] = 0;
					
					
						$ul.css(css)
							.animate(ani, { 
								duration: opts.scrollSpeed, 
								easing	: opts.scrollEffect, 
								complete: function() {
									$ul.find("li:gt("+(opts.visibleItems-1)+")").remove();
								}
							}
						);
					}
					$('#peopleCarosel li img').tipTip({defaultPosition : "top"});
					//	auto-play
					$ul.trigger("pause").trigger("play", "prev");
				});

			if (opts.pauseOnHover) {
				$ul.hover(
					function() { $ul.trigger("pause"); },
					function() { $ul.trigger("play", direction); }
				);
			}

			//	via prev- en/of next-buttons
			if (opts.next != null || opts.prev != null) {
				if (opts.next != null) {
					opts.next.click(function() {
						$ul.trigger("next", true);
						return false;
					});
				}
				if (opts.prev != null) {
					opts.prev.click(function() {
						$ul.trigger("prev", true);
						return false;
					});
				}
			
			//	alleen via auto-play
			} else {
				if (opts.pauseDuration == 0) opts.pauseDuration = 2500;
			}
						
			//	via auto-play
			$ul.trigger("play", direction);
		});
	}
	$.fn.carouFredSel.defaults = {
		visibleItems		: 4,
		scrollItems			: 0,
		scrollItemsButtons	: 0,
		scrollEffect		: 'swing',
		scrollSpeed			: 500,
		next				: null,
		prev				: null,
		direction			: "right",
		pauseDuration		: 0,
		pauseOnHover		: false
	}
})(jQuery);
