﻿/*
* SideScroll jQuery Plug-in
* http://phdcon.com/sidescroll
* Simple Inventory Thumbnail Scroller.
*
* Copyright (c) 2011 PHD Consulting & Samuel Rouse
* Licensed under the MIT License.
* Requires jQuery 1.4+, tested with 1.5.2
*
* Date: 2011-08-05
* Revision: 1
* Options: 
*	- scroll_distance: the amount moved up or down when a scroll button is clicked.
*/
(function ($) {
    $.fn.sidescroll = function (options) {

        // Set the options.
        options = $.extend({}, $.fn.sidescroll.defaults, options);
        $.fn.sidescroll.options = options;

        this.each(function (idx, el) {
            $this = $(el);
            $this.wrapInner('<div class="thumb-wrapper-inner" />');
            $this.wrapInner('<div class="thumb-wrapper" />');
            $inner = $('div.photo-select .thumb-wrapper');
            $inner.attr('max', $('div.photo-select .thumb-wrapper-inner').height());
            if ($inner.attr('max') > $this.height()) {
                $this.addClass('has-scroll');
                $inner.height($this.height());
                $this.prepend('<a href="" class="scrolluplink"><img src="/images/inventory/arrow_up.png" class="arrow scrollup" width="30" height="30" alt="Up" /></a>');
                $this.append('<a href="" class="scrolldownlink"><img src="/images/inventory/arrow_down.png" class="arrow scrolldown" width="30" height="30" alt="Up" /></a>');
                $this.find('.scrolluplink').click(function (evt) {
                    evt.preventDefault();
                    evt.stopPropagation();
                    $.fn.sidescroll.scrollup($(this));
                });
                $this.find('.scrolldownlink').click(function (evt) {
                    evt.preventDefault();
                    evt.stopPropagation();
                    $.fn.sidescroll.scrolldown($(this));
                });
            }
        });
    };

    $.fn.sidescroll.scrollup = function ($button) {
        $this = $button.parent().find('.thumb-wrapper-inner');
        cur_top = parseInt($this.css("top").replace('px', ''));
        cur_top += $.fn.sidescroll.options.scroll_distance;
        if (cur_top > 0) { cur_top = 0; }
        $this.animate({ top: cur_top }, 'slow');
    };

    $.fn.sidescroll.scrolldown = function ($button) {
        $this = $button.parent().find('.thumb-wrapper-inner');
        min_top = $this.parent().height() - $this.parent().attr("max");
        cur_top = parseInt($this.css("top").replace('px', ''));
        cur_top -= $.fn.sidescroll.options.scroll_distance;
        if (cur_top < min_top) { cur_top = min_top; }
        $this.animate({ top: cur_top }, 'slow');
    };

    // Public Values
    $.fn.sidescroll.defaults = {
        scroll_distance: 79
    };
    $.fn.sidescroll.options = "";
    // Public Functions	

})(jQuery);
