﻿/*
* 	Adapted from Easy Slider 1.7 - jQuery plugin
*	written by Alen Grakalic	
*	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
*
*	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*
*	Built for jQuery library
*	http://jquery.com
*
*/

(function($) {

    $.fn.easySlider = function(options) {

        var defaults = {
            prevId: 'prevBtn',
            nextId: 'nextBtn',
            continuous: true
        };

        var options = $.extend(defaults, options);

        this.each(function() {
            var obj = $(this);
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            var clickable = true;
            var ts = s - 1;
            var t = 0;
            // Adds li's for continuous rotation
            $("ul", obj).append($("ul li:nth-child(1)", obj).clone());
            $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
            $("ul", obj).append($("ul li:nth-child(3)", obj).clone());
            $("ul", obj).append($("ul li:nth-child(4)", obj).clone());
            $("ul", obj).prepend($("ul li:nth-child(" + (ts + 1) + ")", obj).clone().css("margin-top", "-" + h + "px"));
            $("ul", obj).prepend($("ul li:nth-child(" + (ts + 1) + ")", obj).clone().css("margin-top", "-" + (2 * h) + "px"));
            $("ul", obj).prepend($("ul li:nth-child(" + (ts + 1) + ")", obj).clone().css("margin-top", "-" + (3 * h) + "px"));
            $("ul", obj).prepend($("ul li:nth-child(" + (ts + 1) + ")", obj).clone().css("margin-top", "-" + (3 * h) + "px"));
            // Next line is to add room to ul to account for cloned items //
            $("ul", obj).css('height', (s + 4) * h);
            // finds the next button in the scope of splider, so make sure buttons are children of selected in called function //
            // then sets next to true //
            //alert(options.nextId);
            $("#" + options.nextId).click(function() {
                animate("next", true);
            });
            $("#" + options.prevId).click(function() {
                animate("prev", true);
            });

            //sets a counter t for each li slide, goes back to zero when the counter is greater than the net of the slides
            function adjust() {
                if (t > ts) t = 0;
                if (t < 0) t = ts;
                $("ul", obj).css("margin-top", (t * h * -1));
                clickable = true;
            };

            function animate(dir, clicked) {
                if (clickable) {

                    // stops trigger build up //
                    clickable = false;
                    var ot = t;
                    switch (dir) {
                        case "next":
                            // + 3 is the incremental movement //
                            t = (ot >= ts) ? (options.continuous ? t + 1 : ts) : t + 3;
                            break;
                        case "prev":
                            t = (t <= 0) ? (options.continuous ? t - 1 : 0) : t - 3;
                            break;
                        default:
                            t = dir;
                            break;
                    };
                    p = (t * h * -1);
                    //alert(h);
                    // duration is the speed //
                    $("ul", obj).animate(
    				    { marginTop: p },
    				    { queue: false, duration: 800, complete: adjust }
    				);
                };

            };

            // Animation on load //
            $(document).ready(function() {
                $("ul", obj).animate(
			    { marginTop: (s * h * -1) },
			    { queue: false, duration: 4000, complete: adjust }
			);
            });
        });
    };

})(jQuery);
