// Copyright ©2009 Aaron Vanderzwan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



(function($) {
    $.fn.slides = function(options) {


    // Util function that converts a ('11px') string to an (11) number
    function px_to_i($i) {
        var i = $i.replace('px', ''),
            last = parseInt(i,10);
        return last;
    }

    // var opts = $.extend({}, $.fn.slides.defaults, options);
        var opts = $.extend({
            containerWidth: 646,
            containerHeight: 124,
            slideWidth: 145,
            marginWidth: 22,
            numOfSlideJump: 3
        }, options);

        return this.each(function() {
            var ul = this;
            var inProcess = false;
            var movement = opts.numOfSlideJump * (opts.marginWidth + opts.slideWidth);
            // debug('numOfSlideJump = '+opts.numOfSlideJump+'\nmarginWidth = ' + opts.marginWidth + '\nslideWidth = '+ opts.slideWidth)
            var fullWidth = $(ul).find('li').length * (opts.marginWidth + opts.slideWidth);

            $(this).wrap('<div class="slideContainer"></div>');
            $('.slideContainer').css({ 'overflow': 'hidden', 'position': 'relative', 'height': opts.containerHeight, 'width': opts.containerWidth });
            $(this).css({ 'position': 'absolute', 'left': 0, 'width': fullWidth + 'px' });
            $('.slideContainer').parent().addClass('prevDisabled');

            // If slider width is less then container width, disable next button
            if ((fullWidth - opts.marginWidth) <= opts.containerWidth) { $('.slideContainer').parent().addClass('nextDisabled'); $(ul).parent().parent().addClass('noSlide'); }

            function animateSlider(a) {
                $(ul).animate({ left: a }, 1000, 'easeOutQuint', function() {
                    inProcess = false;
                });
            }

            // Disable buttons if there are no more left or right
            function checkEnd(a) {
                // Disable clicking
                inProcess = true;

                // Get what left will be after movement
                var left = px_to_i($(ul).css('left')) + a;

                // Else statement for prev and next buttons
                $('.slideContainer').parent().removeClass('prevDisabled').removeClass('nextDisabled');

                if (left <= (-fullWidth + opts.containerWidth + opts.marginWidth)) {
                    $('.slideContainer').parent().addClass('nextDisabled');
                    left = (-fullWidth + opts.containerWidth) + opts.marginWidth;
                } else if (left >= 0) {
                    $('.slideContainer').parent().addClass('prevDisabled');
                    left = 0;
                }

                animateSlider(left);
            }

            // Click = run animation and check if the buttons should be disabled
            $(ul).parent().parent().find('.nextButton').click(function() { if (inProcess || $(this).parent().hasClass('nextDisabled')) { return false; } else { checkEnd(-movement); return false; } });
            $(ul).parent().parent().find('.prevButton').click(function() { if (inProcess || $(this).parent().hasClass('prevDisabled')) { return false; } else { checkEnd(movement); return false; } });
        });


    };


})(jQuery);