/*
  Name     : jQuery.simpleLoopSlider.js
  Required : jQuery.js
  Author   : Matori/ub-pnr (pnr.matori@gmail.com)
  Copyright: Unformed Building (http://unformedbuilding.com/)
  Lisence  : MIT (http://www.opensource.org/licenses/mit-license.php)
  Link     : http://unformedbuilding.com/articles/jquery-simple-loop-slider-js/
  Modified : 2011-03-10 (v0.1)
*/

(function ($) {
    $.fn.simpleLoopSlider = function (options) {
        var defaults = {
            controller: true,
            pagination: false,
            autoSlide: true,
            interval: 3500,
            duration: 500,
            easing: 'linear'
        };

        var o = $.extend(defaults, options);

        this.each(function () {
            var $slider = $(this),
                $view = $slider.children('.sls-view'),
                $container = $view.children('.sls-container'),
                $contents = $container.children().addClass('sls-content');

            var size = {
                width: $view.width(),
                height: $view.height()
            };

            var count = {
                min: 0,
                max: $contents.length,
                current: 0
            };

            $container.css({
                width: size.width * ($contents.length + 2),
                marginLeft: -size.width,
                paddingLeft: size.width
            });

            // slider
            var slide = {
                next: function () { fnc.scrollNext(); },
                prev: function () { fnc.scrollPrev(); }
            };

            var fnc = {
                scrollNext: function () {
                    var children = $container.children();
                    var elm = children.first().get(0);
                    $container.append(children.first().clone().hover(
                        function () { clearInterval(start); },
                        function () { play(); }
                    ));
                    $(elm).stop(true, false).animate({'margin-left': '-220px'}, o.duration, o.easing, function() {
                        elm.parentNode.removeChild(elm);
                    });
                },

                scrollPrev: function () {
                    var children = $container.children();
                    var item = children.last().clone().hover(
                        function () { clearInterval(start); },
                        function () { play(); }
                    );
                    var elm = children.last().get(0);
                    elm.parentNode.removeChild(elm);
                    $(item.css({'margin-left': '-220px'}).stop(true, false).animate({'margin-left': '0px'}, o.duration, o.easing)).insertBefore(children.first());
                }
            };

            // create next/prev controller
            if(o.controller) {
                $('<a/>').attr('href', '#')
                .addClass('sls-next sls-controller')
                .text('\u00BB')
                .appendTo($slider)
                .click(function (e) {
                    e.preventDefault();
                    if (o.autoSlide) { clearInterval(start); }
                    slide.next();
                    if (o.autoSlide) { play(); }
                });
                $('<a/>').attr('href', '#')
                .addClass('sls-prev sls-controller')
                .text('\u00AB')
                .appendTo($slider)
                .click(function (e) {
                    e.preventDefault();
                    if (o.autoSlide) { clearInterval(start); }
                    slide.prev();
                    if (o.autoSlide) { play(); }
                });
            }

            // autoslide
            if(o.autoSlide) {
                var play, start;
                play = function () {
                    start = setInterval(function () { slide.next(); }, o.interval);
                };

                // hover event (stop)
                $contents.hover(
                    function () {
                        clearInterval(start);
                    },
                    function () {
                        play();
                    }
                );
                play();
            }
        });
        return this;
    };
})(jQuery);

