﻿(function($) {

    $.fn.slideDeck = function(options) {

        var _this = this;
        var slideInterval = 0;
        var slides = null;
        var defaults = { current: 0, moveBy: 100, speed: 'slow', pause: 4500, randomStart: false, slideSelector: '.slide', itemsPerSlide: 1, direction: 'left', showNavigation: true, debug: true };
        var settings = $.extend({}, defaults, options);
        var resetTimer = null;
        var defaultDirection = null;

        this.intialize = function() {
            try {
                $(this).css({ 'overflow': 'hidden', 'position': 'relative' });
                $(this).wrapChildren({ childElem: 'a', sets: settings.itemsPerSlide, wrapperClass: String(settings.slideSelector).replace('.', '') });

                slides = $(settings.slideSelector, this);

                var current = (settings.randomStart) ? Math.floor(Math.random() * slides.length) : settings.current;
                var extras = { current: current, moveBy: $(slides[settings.current]).innerWidth() };

                jQuery.extend(settings, extras);

                slides.css($.evalJSON('{ "position": "absolute", "top": "0", "left" : "0" }'));
                var dir = (settings.direction == "left") ? "" : "-";
                slides.not($(slides[settings.current])).css({ 'left': '' + dir + settings.moveBy + 'px' });

                if (settings.showNavigation) { this.buildNavigation(); }
                defaultDirection = settings.direction;
                this.autoRun();

            }
            catch (e) {
                log(e);
            }

            return this;
        };
        this.buildNavigation = function() {
            var nav = $('<div />').addClass('actions');
            var prev = $('<a />').addClass('move').addClass('previous').attr('title', 'previous').attr('rel', 'previous').html('&lt;');
            var next = $('<a />').addClass('move').addClass('next').attr('title', 'next').attr('rel', 'next').html('&gt;');
            var more = $('<a />').attr('id', 'more_activities').attr('href','/Activities/').html('more');
            
            nav.append(prev).append(next).append(more);
            $(this).after(nav);
            $('a[rel]', nav).css({ 'cursor': 'pointer' }).click(function() {
                var func = eval('_this.' + $(this).attr('rel'));
                func();
            });
        };
        this.autoRun = function() {
            if (slideInterval) { clearInterval(slideInterval); }
            slideInterval = setInterval(this.slide, settings.pause);
        };
        this.reset = function() {
            if (resetTimer) { clearTimeout(resetTimer); }
            settings.direction = defaultDirection;
            resetTimer = setTimeout(function() { _this.autoRun() }, settings.pause / 2);
        };
        this.reverse = function() {
            settings.direction = settings.direction == "left" ? "right" : "left";
        };
        this.previous = function() {
            settings.direction = "right"; clearInterval(slideInterval); _this.slide(); _this.reset();
        };
        this.next = function() {
            settings.direction = "left"; clearInterval(slideInterval); _this.slide(); _this.reset();
        };
        this.slide = function() {
            try {
                var dir = "-";
                var rDir = "";
                var next = settings.current + 1;
                if (next == slides.length) {
                    next = 0;
                }

                if (settings.direction != "left") {
                    dir = "";
                    rDir = "-";
                    next = settings.current - 1;
                    if (next < 0) {
                        next = slides.length - 1;
                    }
                }


                $(slides[next]).css({ 'left': '' + rDir + settings.moveBy + 'px' });

                $(slides[settings.current]).animate({ 'left': '' + dir + settings.moveBy + 'px' }, {
                    duration: settings.speed,
                    complete: function(e) {
                        $(this).css({ 'left': '' + rDir + settings.moveBy + 'px' });
                    }
                });


                $(slides[next]).animate({ 'left': '0px' }, settings.speed);

                var options = { current: next };
                jQuery.extend(settings, options);
            }
            catch (e) {
                log(e);
            }
        };

        /**
        * Debug Logging
        * @param {Object} data
        */

        var log = function(data) {
            if (settings.debug) {
                try {
                    console.log("%o: %o", data, this);
                } catch (e) {
                    $('BODY').append($("<div class='error'>" + data + "</div>"));
                }
            }
        };

        return this.intialize();

    };

    $.fn.wrapChildren = function(options) {

        var options = $.extend({ childElem: undefined, sets: 1, wrapper: 'div', wrapperClass: 'wrapper' }, options || {});
        if (options.childElem === undefined) return this;

        return this.each(function() {
            var elems = $(this).children(options.childElem);
            var arr = [];

            elems.each(function(i, value) {
                arr.push(value);
                if (((i + 1) % options.sets === 0) || (i === elems.length - 1)) {
                    var set = $(arr);
                    arr = [];
                    set.wrapAll($('<' + options.wrapper + ' class="' + options.wrapperClass + '">'));
                }
            });
        });
    };

})(jQuery);

