/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
*
* Version: 3.0.3-pre
* 
* Requires: 1.2.2+
*/

(function($) {

    var types = ['DOMMouseScroll', 'mousewheel'];

    $.event.special.mousewheel = {
        setup: function() {
            if (this.addEventListener)
                for (var i = types.length; i; )
                this.addEventListener(types[--i], handler, false);
            else
                this.onmousewheel = handler;
        },

        teardown: function() {
            if (this.removeEventListener)
                for (var i = types.length; i; )
                this.removeEventListener(types[--i], handler, false);
            else
                this.onmousewheel = null;
        }
    };

    $.fn.extend({
        mousewheel: function(fn) {
            return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
        },

        unmousewheel: function(fn) {
            return this.unbind("mousewheel", fn);
        }
    });


    function handler(event) {
        var args = [].slice.call(arguments, 1), delta = 0, returnValue = true;

        event = $.event.fix(event || window.event);
        event.type = "mousewheel";

        if (event.wheelDelta) delta = event.wheelDelta / 120;
        if (event.detail) delta = -event.detail / 3;

        // Add event and delta to the front of the arguments
        args.unshift(event, delta);

        return $.event.handle.apply(this, args);
    }

})(jQuery);


/* jquery.fbis.scrollPane.js - (C) 2010 www.fruitbatscode.com    */
/* Simple plugin to convert a div with overflow:auto into a pane */
/* with up / down markup. It keeps the widht and height of the   */
/* original parent, wraps the content and scrolls it on click.    */

(function($) {
    $.fn.fbisScrollPane = function(settings) {
        var config = {
            upText: 'up'
		, downText: 'down'
		, scrollSpeed: 800
		, pixelOffset: 20
        };

        if (settings) $.extend(config, settings);

        var ie7 = ($.browser.msie && parseInt($.browser.version.substr(0, 1)) < 8);
        if (!ie7) {
            this.each(function() {
                var $wrap = $('<div class="scrollPane"></div>');
                var $this = $(this);
                //get container size
                var h = $this.outerHeight();
                var w = $this.outerWidth();
                var currentY = 0;
                //Add the up down, markup and wrap the content
                $this
			.css({ overflow: 'visible', height: 'auto', position: 'relative', top: 0 })
			.addClass('content')
			.wrap($wrap)
			.parent().css({ width: w, height: h }) //parent to get at the wrap object
			.before('<div class="up">' + config.upText + '</div>')
			.after('<div class="down">' + config.downText + '</div>');
                //get content height
                var $content = $this;
                var $up = $content.parent().prev();
                var $down = $content.parent().next();
                var contentH = $this.innerHeight();
                var mouseScroll = 0;
                //$content.find('div.scrollPane').height(contentH - 20).css({border:'solid 1px green'});

                $content.bind('mousewheel', function(e, delta) {
                    mouseScroll = 150;
                    switch (delta) {
                        case -1:
                            $down.click();
                            break;
                        case 1:
                            $up.click();
                            break;
                    }
                    e.preventDefault();
                    return false;
                })

                //bind up click
                $down.click(function(e) {
                    var top = parseInt($content.css('top'));
                    if (!$down.hasClass('disabled')) {
                        if (top > (0 - $content.height()) + h) {
                            var newTop = top - (h - config.pixelOffset);
                            if (mouseScroll != 0) {
                                newTop = top - mouseScroll;
                            }
                            //if we scrolling past the end of the content just scroll to end of content
                            if (h + Math.abs(newTop) > contentH) newTop = 0 - (contentH - h);

                            if ($content.queue("fx").length == 0) {
                                $content.animate({
                                    top: newTop,
                                    queue: true
                                }, calcSpeed(top, newTop))
                            } else {
                                //$content.clearQueue();
                                $content.stop(true);
                                $content.animate({ top: newTop });
                            };
                            mouseScroll = 0;
                        }
                    }
                    UpdateStatus(newTop);
                });
                //bind down click
                $up.click(function(e) {
                    var top = parseInt($content.css('top'));
                    if (!$up.hasClass('disabled')) {
                        if (top < 0) {
                            var newTop = top + (h - config.pixelOffset);
                            if (mouseScroll != 0) {
                                newTop = top + mouseScroll;
                                mouseScroll = 0;
                            }
                            if (newTop > 0) newTop = 0;
                            if ($content.queue("fx").length == 0) {
                                $content.animate({
                                    top: newTop,
                                    queue: true
                                }, calcSpeed(top, newTop));

                                //console.log(top, newTop);
                            } else {
                                //$content.clearQueue();
                                $content.stop(true);
                                $content.animate({ top: newTop });
                            };
                        }
                        UpdateStatus(newTop);
                    }

                });

                UpdateStatus(parseInt($content.css('top')));

                //turn icons on and off
                function UpdateStatus(top) {
                    if (top == 0) {
                        $up.addClass('disabled');
                    } else {
                        $up.removeClass('disabled');
                    }
                    if (!(top > (0 - $content.height()) + h)) {
                        $down.addClass('disabled');
                    } else {
                        $down.removeClass('disabled');
                    }
                }
                //calculate the relative speed so small and large movements are the same
                function calcSpeed(top, newtop) {
                    return Math.abs(((Math.abs(top) - Math.abs(newtop)) / config.scrollSpeed) * 1000);
                }

            });
            return this;
        }
    };
})(jQuery);
