/*	
 *	jQuery carouFredSel 1.1.1
 *	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,
				nextItem = opts.visibleItems,
				prevItem = totalItems - 1,
				onPause = false,
				itemWidth = $items.outerWidth(),
				itemHeight = $items.outerHeight(),
				direction = (opts.direction == "up" || opts.direction == "right") ? "forward" : "rewind",
				onMove = function() { };

            if (opts.direction == "right" ||
				opts.direction == "left"
			) {
                var css = {
                    height: $ul.outerHeight() || itemHeight,
                    width: itemWidth * opts.visibleItems
                }
            } 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"
            });

            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) return false;
				    if ($ul.is(':animated')) return false;

				    for (var a = 0; a < opts.scrollItems; a++) {
				        $ul.append($($items[nextItem]).clone());
				        if (++nextItem >= totalItems) nextItem = 0;
				        if (++prevItem >= totalItems) prevItem = 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);
						        //opts.onMove();
						        //alert(opts.onMove);
						    }
						}
					);



				    //	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) return false;
				    if ($ul.is(':animated')) return false;

				    for (var a = 0; a < opts.scrollItems; a++) {
				        $ul.prepend($($items[prevItem]).clone());
				        if (--prevItem < 0) prevItem = totalItems - 1;
				        if (--nextItem < 0) nextItem = 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 - 1) + ")").remove();
						        //opts.onMove();

						    }
						}
					);

				    //	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);
            }

            opts.onMove();
            
        });
    }
    $.fn.carouFredSel.defaults = {
        visibleItems: 4,
        scrollItems: 0,
        scrollEffect: 'swing',
        scrollSpeed: 500,
        next: null,
        prev: null,
        direction: "right",
        pauseDuration: 2500,
        pauseOnHover: false,
        onMove: function() { }
    }
})(jQuery);