/*
 * hscroll v0.1
 * Author: Bryce Looms
 * Requires Jquery
 */
var hscroll = function () {
        var $wrapper = '<div class="hscrollwrapper" />',
            $scroller = '.hscroll',
            $group = 'ul',
            $groupClone = '',
            $holder = '<div />',
            numItems = 0,
            itemWidth = 0,
            groupWidth = 0,
            position = [0],
            currentPos = 0,
            anSpeed = 500,
            interval = 4500,
            animated = false,
            GO = false;
        var config = {
            auto: true
        }

        function constructor(settings) {
            $.extend(config, settings);
            $wrapper = $($wrapper);
            $scroller = $($scroller).css("overflow", "hidden");
            $group = $scroller.find($group);
            $groupClone = $group.clone();
            $holder = $($holder).appendTo($scroller).append($group).append($groupClone);
            numItems = $group.find("> li").length;
            itemWidth = $group.find("> li:first").outerWidth(true);
            groupWidth = numItems * itemWidth;
            for (var i = 0; i < numItems; i++) {
                position.push((i + 1) * itemWidth);
            }
            $holder.css("width", groupWidth * 2);
            var $left = $('<span />').attr({
                'class': 'slide_left',
                'title': 'left'
            }).click(function () {
                MoveRight();
                return false;
            });
            var $right = $('<span />').attr({
                'class': 'slide_right',
                'title': 'Right'
            }).click(function () {
                MoveLeft();
                return false;
            });
            $wrapper.insertAfter($scroller).append($scroller).append($left).append($right);
            $scroller.scrollLeft(position[0]);
            if (config.auto) autoscroll();

			$('#carousel_slide').click(function () {
		        MoveRight();
		    });
        };

        function autoscroll() {
            GO = setInterval(MoveLeft, interval);
        }

        function kill() {
            clearInterval(GO);
        }

        function MoveRight() {
            if (!animated) {
                if (currentPos <= 0) {
                    currentPos = numItems;
                    $scroller.scrollLeft(position[currentPos]);
                }
                currentPos--;
                animated = true;
                $scroller.animate({
                    scrollLeft: position[currentPos]
                }, anSpeed, function () {
                    animated = false;
                });
            }
        };

        function MoveLeft() {
            if (!animated) {
                if (currentPos >= numItems) {
                    currentPos = 0;
                    $scroller.scrollLeft(position[currentPos]);
                }
                currentPos++;
                animated = true;
                $scroller.animate({
                    scrollLeft: position[currentPos]
                }, anSpeed, function () {
                    animated = false;
                });
            }
        };
        return {
            init: constructor,
            play: autoscroll,
            pause: kill
        }
    }();

