(function($) {
$.fn.jSlider = function(o) {
    var o = $.extend({
        item: null,
        container: null,
        leftButton: null,
        rightButton: null,
        pauseButton: null
    }, o || {});

    return this.each(function() {
		var self = $(this);
		var container = $(o.container, self);
		var panels = $(o.item, container);

	    var speed               = 1;   //22
	    var speedTime           = 15;

	    var curStart            = "0px";

	    var timeOut;
	    var countPanel          = panels.length;
	    var goDir               = true;
	    var paused = false;

    	if (countPanel == 0)
    		return;

		var movingDistance = panels.eq(0).outerWidth(true);
		var containerWidth = container.outerWidth(true);

		if (containerWidth <= 0 || movingDistance <= 0)
			return;

		var panelSize = Math.ceil(containerWidth / movingDistance);
		var panelStart = 1;

		if (countPanel <= panelSize) {
			return;
		}

		if (countPanel / panelSize >= 2)
			panelStart = panelSize;
		else
			panelStart = countPanel % panelSize;

		self.css({'overflow': 'hidden', 'position': 'relative'});
		container.css({'overflow': 'hidden', 'position': 'relative', 'width': (movingDistance * countPanel) + 'px', 'left': 0, 'top': 0});

		function change(dir) {
			clearTimeout(timeOut);

			if (dir!=undefined) { goDir = dir; }

			var mul = goDir ? -1 : 1;

			var cLeft = parseFloat(container.css("left")) + mul * speed;
			var cRight = cLeft + parseFloat(container.css("width"));

			if (goDir) {
				if (cLeft <= -movingDistance * panelStart) {
					cLeft += movingDistance * panelStart;

					var ch = null;

					for (var i = 0; i < panelStart; i++) {
						ch = panels.parent().children().eq(0).detach();
						panels.parent().append(ch);
					}
				}
			} else {
				if (cLeft >= 0) {
					cLeft -= movingDistance * panelStart;

					var ch = null;

					for (var i = 0; i < panelStart; i++) {
						ch = panels.parent().children().eq(-1).detach();
						panels.parent().prepend(ch);
					}
				}
			}

			container.css({"left": cLeft+"px"});

			timeOut = setTimeout(function(){change();},speedTime);
		}

		if (o.leftButton != null)
			$(o.leftButton, self).bind('click', function (event) {event.preventDefault(); paused = false; change(true);});

		if (o.rightButton != null)
			$(o.rightButton, self).bind('click', function (event) {event.preventDefault(); paused = false; change(false);});

		if (o.pauseButton != null)
			$(o.pauseButton, self).bind('click', function (event) {event.preventDefault(); paused = true; clearTimeout(timeOut);});

		container.hover(
			function () {
				if (!paused)
					clearTimeout(timeOut);
			},
			function () {
				if (!paused)
					change();
			}
		);

		timeOut = setTimeout(function() {
			change();
		},
		500);
    });
};
})(jQuery);
