(function($) {
	$.tiny = $.tiny || {};
	$.tiny.scrollable = {
		options : {
			axis : 'x',
			size : 1,
			speed : 300,
			appendTab : false,
			nextSize : 1
		}
	};
	$.fn.tinyscrollable = function(options) {
		var options = $.extend( {}, $.tiny.scrollable.options, options);
		new Scrollable($(this), options);
		return this;
	};
	function Scrollable(root, options) {
		var oSelf = this;
		var oWrapper = root;

		var oTab = $('.tab', root);

		var oWrapContent = $('.wrap_content', root);

		var listTab = $('a', oTab);

		var oNext = $('.next', root);

		var oPrev = $('.prev', root);

		var oContent = $('.content', root);

		var oItems = $('.item', oContent);

		var sAxis = options.axis == 'x', sDirection = sAxis ? 'left' : 'top', sSize = sAxis ? 'Width'
				: 'Height';
		var nextSize = options.nextSize;
		var itemSize = 0;
		if (sDirection == 'left') {
			itemSize = $(oItems[0]).outerWidth(true);
		} else {
			itemSize = $(oItems[0]).outerHeight(true);
		}

		var indexItem = 0;
		function initialize() {
			update();
			return oSelf;
		}
		function update() {
			if(oItems.size()<=options.size){
				oNext.css('visibility','hidden');
				oPrev.css('visibility','hidden');
				return;
			}
			oWrapContent.css( {
				'overflow' : 'hidden',
				'position' : 'relative'
			});
			if (oContent[0] != null) {
				oContent.css(sDirection, 0);
				oWrapContent.css(sSize.toLowerCase(), itemSize * options.size);
				oItems.page = Math.ceil(oItems.size() / nextSize);
				oItems.page = oItems.page - Math.floor(options.size / nextSize);
				oContent.css( {
					'position' : 'absolute',
					'width' : itemSize * oItems.size(),
					'height' : itemSize * oItems.size()
				});
				oContent.nextmax = oContent[0]['offset' + sSize]
						- oWrapContent[0]['offset' + sSize];
				oContent.next = 0;
				if (options.appendTab == true) {
					var s = oItems.page + 1;
					for ( var i = 0; i < s; i++) {
						oTab.append("<a class='tabItem'></a>");
					}
					listTab = $('a', oTab);
					if (listTab.size() > 0) {
						$(listTab.get(0)).addClass("tabItemSelect");
					}
				}
				setEvent();
			}
		}

		function setEvent() {
			listTab.click(function() {
				indexItem = listTab.index(this);
				run(indexItem);
			});

			if (options.size < oItems.size()) {
				oNext.click(function() {
					indexItem++;
					if (indexItem > oItems.page
							|| oContent.nextmax == oContent.next)
						indexItem = 0
					run(indexItem);
				});
				oPrev.click(function() {
					indexItem--;
					if (indexItem < 0 || oContent.next == 0)
						indexItem = oItems.page;
					run(indexItem);
				});
			}
		}
		;

		function run(index) {
			listTab.removeClass("tabItemSelect");
			$(listTab.get(index)).addClass("tabItemSelect");
			oContent.next = Math.min(oContent.nextmax, (itemSize * nextSize)
					* (index));
			if (sDirection == 'left') {
				oContent.stop().animate( {
					left : -oContent.next + 'px'
				}, options.speed);
			} else {
				oContent.stop().animate( {
					top : -oContent.next + 'px'
				}, options.speed);
			}
		}
		;

		return initialize();
	}
	;
})(jQuery);
