﻿// Simple Slider
// Slide Content Simply
// Inspired by Easy Slider
    /*
    * 	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
    *
    */
// Copyright (c) 2011 Chris Hehn (http://reachingnexus.com)
// Dual licensed under the MIT (MIT-LICENSE.txt)
// and GPL (GPL-LICENSE.txt) licenses.

// Sample markup for $('#slider').simpleSlider();
//<div id="slider'>
// <ul>
//   <li><anything/></li>
//   <li><anything/></li>
// <ul>
//</div>

(function ($) {
    $.fn.simpleSlider = function (optionsSet) {
        $(this).each(function () {
            var defaults = {
                continuous: true,
                itemsPer: 1,
                defaultdirection: 'NEXT',
                speed: 4000,
                timeout: 0,
                prevId: 'prevBtn',
                prevClass: 'prevBtn',
                prevText: 'Previous',
                nextId: 'nextBtn',
                nextClass: 'nextBtn',
                nextText: 'Next',
                controlsShow: true,
                controlsBefore: '',
                controlsAfter: ''
            }

            var options = $.extend(defaults, optionsSet);

            var obj = $(this);
            var list = $('ul:first', obj);
            var fullWidth = 0;
            $('li', list).each(function () {
                fullWidth += $(this).width();
            });
            list.width(fullWidth);
            list.hover(function () { clearTimeout(list.attr('data-timeout')) }, function () { startRotation(); });
            startRotation();

            function startRotation() {
                if (options.continuous == true) {
                    options.timeout = setTimeout(function () { rotate(options.defaultdirection); }, options.speed);
                    list.attr('data-timeout', options.timeout).attr('data-direction', options.defaultdirection);
                }
            }

            function rotate(direction, clicked) {
                if (direction == 'NEXT') {
                    for (i = 0; i < options.itemsPer; i++) {
                        list.children('li:not(.locked):first').addClass('locked').animate({ width: 'toggle', padding:'toggle', 
                        opacity: 'toggle' }, (options.speed * .25), function () {
                            $(this).appendTo(list);
                            $(this).animate({ width: 'toggle', padding: 'toggle', 
                            opacity: 'toggle' }, 0, function () { $(this).removeClass('locked'); });
                        });
                    }
                } else if (direction == 'PREV') {
                    for (i = 0; i < options.itemsPer; i++) {
                        list.children('li:not(.locked):last').addClass('locked').animate({ width: 'toggle', padding:'toggle', 
                        opacity:'toggle' }, 0, function () {
                            $(this).prependTo(list);
                            $(this).animate({ width: 'toggle',padding:'toggle', 
                            opacity:'toggle'}, (options.speed * .25), function () { $(this).removeClass('locked'); });
                        });
                    }
                }
                if (list.attr('data-timeout') == options.timeout && options.continuous == true && !clicked) {
                    options.timeout = setTimeout(function () { rotate(direction); }, options.speed);
                    list.attr('data-timeout', options.timeout);
                }
            }
            if (options.controlsShow) {
                var html = options.controlsBefore;
                if (!($('#' + options.prevId).length)) html += ' <span id="' + options.prevId + obj.attr('id') + '" class="' + options.prevClass + '"><a href=\"javascript:void(0);\">' + options.prevText + '</a></span>';
                if (!($('#' + options.nextId).length)) html += ' <span id="' + options.nextId + obj.attr('id') + '" class="' + options.nextClass + '"><a href=\"javascript:void(0);\">' + options.nextText + '</a></span>';
                html += options.controlsAfter;
                //$(list).after(html);
                $(obj).after(html);
                $("a", "#" + options.prevId + obj.attr('id')).click(function () {
                    clearTimeout(list.attr('data-timeout'));
                    rotate('PREV', true);
                });
                $("a", "#" + options.nextId + obj.attr('id')).click(function () {
                    clearTimeout(list.attr('data-timeout'));
                    rotate('NEXT', true);
                });
            }
        });
    }

})(jQuery);
