(function ($) {

    $.fn.gallery = function (options) {

        var defaults = {
            speed : 5,
            minleft : null
        };

        var opts = $.extend(defaults, options);

        var content = null,
            cww     = null,
            crw     = null;

        var interval = null;

        var _left = function () {
            var scroll = content.scrollLeft();
            content.scrollLeft(scroll - opts['speed']);
            console.log(scroll);
/*
            if (left < 0) {
                var newleft = left + opts['speed'];
                if (newleft > 0) {
                    newleft = 0;
                }
                $(content).scrollLeft(newLeft);
            }
*/
        };

        var _right = function () {
//            console.log("Right");
            var scroll = content.scrollLeft();
            content.scrollLeft(scroll + opts['speed']);
            console.log(scroll);
/*
            var left = parseInt($(content).css("left"));
            if (left > -(crw - cww)) {
                var newleft = left - opts.speed;
                if (newleft < opts['minleft']) {
                    newleft = opts['minleft'];
                }
                $(content).scrollLeft(newLeft);
//                $(content).css("left", newleft + "px");
            }
*/
        };

        var _moveLeft = function () {
            interval = window.setInterval(_left, 25);
        };

        var _moveRight = function () {
            interval = window.setInterval(_right, 25);
        };

        var _stop = function () {
            window.clearInterval(interval);
        };

        return this.each(function () {
            content = $(this).find("div.gallery-content");
            cww = parseInt($(this).find("div.gallery-content").css("width"));
            crw = parseInt($(this).find("table.gallery-content").css("width"));

            if (!opts['minleft']) {
                opts['minleft'] = cww - crw;
            }

            $(this).find("td.gallery-left-arrow").hover(_moveLeft, _stop);
            $(this).find("td.gallery-right-arrow").hover(_moveRight, _stop);
        });

    }

})(jQuery);


