﻿(function ($) {
    $.s80imagestrip = function (el, options) {
        var base = this; // avoid scoping issues- reference $(this) as base internally
        base.$el = $(el); // assign $ and DOM versions of element to the plugin
        base.el = el;
        base.$el.data("imagestrip", base); // Add a reverse reference to the DOM object
        base.init = function () {
            base.options = $.extend({}, $.s80imagestrip.defaultOptions, options);
            base.options.current = 0;
            base.length = $(base.options.itemselector, base.$el).length;
            base.$wrapper = $(base.options.wrapselector, base.$el);
            if (base.length > 0) {
                // add prev/next
                base.$wrapper.before($("<a/>").addClass("left").attr("title", "scroll left").click(function (e) {
                    e.preventDefault()
                    base.scrollToItem("-")
                }))
                base.$wrapper.after($("<a/>").addClass("right").attr("title", "scroll right").click(function (e) {
                    e.preventDefault()
                    base.scrollToItem("+")
                }))
            }
            $('.wrapper ul', base.$el).css({
                width: (base.length * 335)
            });
            //$('.wrapper ul a', base.$el).prettyPhoto();
        };
        base.scrollToItem = function (dir) {
            var index = base.options.current
            // kick out if we're out of range
            if (((index == 0) && dir == "-") || (((index + base.options.steps) > base.length) && dir == "+")) {
                return
            }
            //check and set index in range
            if (dir == '-') {
                if ((index - base.options.steps) < 0) {
                    index = 0;
                } else {
                    index -= base.options.steps
                }
            } else {
                if ((index + base.options.steps) >= base.length) {
                    index = base.length
                } else {
                    index += base.options.steps
                }
            }
            //run animation and set current property
            if ((index >= 0) && (index <= (base.length))) {
                $(".wrapper ul", base.$el).stop().animate({
                    right: dir + "=" + (base.options.steps * 316)
                }, "slow")
                base.options.current = index
            }
        }
        // Run initializer
        base.init();
    };
    $.s80imagestrip.defaultOptions = {
        steps: 3,
        wrapselector: '.wrapper',
        itemselector: '.wrapper li'
    };
    $.fn.s80imagestrip = function (options) {
        return this.each(function () {
            (new $.s80imagestrip(this, options));
        });
    };
})(jQuery);
