/*	
 *	jQuery carouFredSel
 *	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,
				onPause			= false,
				itemWidth		= $items.outerWidth(),
				itemHeight		= $items.outerHeight(),
				direction		= (opts.direction == "up" || opts.direction == "right") ? "forward" : "rewind";
			

			if (opts.direction == "right" ||
				opts.direction == "left"
			) {
				var css = {
					height	: $ul.outerHeight() || itemHeight,
					width	: itemWidth * opts.visibleItems +70
				}
			} else {
				var css = {
					width	: $ul.outerWidth() || itemWidth,
					height	: itemHeight * opts.visibleItems
				}
			}

			$ul.wrap('<div class="caroufredsel_wrapper" />').css({
				position	: "absolute"
			}).parent().css(css).css({ 
				position	: "relative",
				overflow	: "hidden",
				height		: "122px"
			});

			if (opts.scrollItems == 0) opts.scrollItems = opts.visibleItems;
			if (opts.visibleItems >= totalItems) return;

			$items.filter(":gt("+(opts.visibleItems-1)+")").remove();
			$ul
				.bind("forward", function() {
					if (!onPause) {
						for (var a = 0; a < opts.scrollItems; a++) {	
							$ul.append($($items[lastItem]).clone());
							if (++lastItem >= totalItems) lastItem = 0;
						}
						if (opts.direction == "right" ||
							opts.direction == "left"
						) {
							var css = {width: (itemWidth * 0.1) + (itemWidth * $("li", $ul).length)},
								ani = {left: -(itemWidth * opts.scrollItems)},
								cal = {left: 0}

						} else {
							var css = {height: (itemHeight * 0.1) + (itemHeight * $("li", $ul).length)},
								ani = {top: -(itemHeight * opts.scrollItems)},
								cal = {top: 0}
						}
						$ul.css(css)
							.animate(ani, { 
								duration: opts.scrollSpeed, 
								easing	: opts.scrollEffect, 
								complete: function() {
									$ul.find("li").filter(":lt("+opts.scrollItems+")").remove();
									$(this).css(cal);
								}
							}
						);							
					}
					
					//	alleen auto-play als geen prev- en/of next-buttons
					if (opts.next == null && opts.prev == null) {
						setTimeout(function() {
							$ul.trigger("forward");
						}, opts.pauseDuration);
					}
				})
				.bind("rewind", function() {
					if (!onPause) {
						for (var a = 0; a < opts.scrollItems; a++) {	
							$ul.prepend($($items[firstItem]).clone());
							if (--firstItem < 0) firstItem = totalItems-1;
						}
						if (opts.direction == "right" ||
							opts.direction == "left"
						) {
							var css = {left: -(itemWidth * opts.scrollItems), width: (itemWidth * 0.1) + (itemWidth * $("li", $ul).length)},
								ani = {left: 0 }

						} else {
							var css = {top: -(itemHeight * opts.scrollItems), height: (itemHeight * 0.1) + (itemHeight * $("li", $ul).length)},
								ani = {top: 0}
						}
						$ul.css(css)
							.animate(ani, { 
								duration: opts.scrollSpeed, 
								easing	: opts.scrollEffect, 
								complete: function() {
									$ul.find("li").filter(":gt("+opts.visibleItems+")").remove();
								}
							}
						);							
					}
					
					//	alleen auto-play als geen prev- en/of next-buttons
					if (opts.next == null && opts.prev == null) {
						setTimeout(function() {
							$ul.trigger("rewind");
						}, opts.pauseDuration);
					}
				})
				.bind("pause", function() {
					onPause = true;
				})
				.bind("play", function() {
					onPause = false;
				});

			if (opts.pauseOnHover) {
				$ul.hover(
					function() { $ul.trigger("pause"); },
					function() { $ul.trigger("play"); }
				);
			}

			//	of via prev- en/of next-buttons
			if (opts.next != null || opts.prev != null) {
				if (opts.next != null) {
					opts.next.click(function() {
						$ul.trigger("play").trigger("forward").trigger("pause");
						return false;
					});
				}
				if (opts.prev != null) {
					opts.prev.click(function() {
						$ul.trigger("play").trigger("rewind").trigger("pause");
						return false;
					});
				}

			//	of via auto-play
			} else if (opts.pauseDuration > 0) {
				setTimeout(function() {
					$ul.trigger(direction);
				}, opts.pauseDuration);							
			}					
		});
	}
	$.fn.carouFredSel.defaults = {
		visibleItems	: 3,
		scrollItems		: 1,
		scrollEffect	: 'swing',
		scrollSpeed		: 500,
		next			:  null,
		prev			: null,
		direction		: "right",
		pauseDuration	: 2500,
		pauseOnHover	: false
	}
})(jQuery);
