function cssCut(val)
{
	if (val.indexOf('px') == -1)
		return 0;
	return parseInt(val.substr(0, val.indexOf('px')));

}

$.fn.hyhscrollable = function(a_options)
{
	if ($(this).length == 0)
	{
		return;
	}
	var Object = $(this);

	var options = { 
					itemHeight:'auto',
					itemWidth: 'auto',
					duration: 500,
					elemNext: null,
					elemPrev: null,
					disabledClass: 'disabled',
					startFrom: 0
				};

	if (typeof(a_options) != 'undefined') {
		for (var key in a_options) {
			options[key] = a_options[key];
		}
	}

	var Items = $('> *', Object);
	if (Items.length == 0) 
		return;
	if (options.itemWidth == 'auto') {
		options.itemWidth = Items.eq(0).width() +
			cssCut(Items.eq(0).css('marginLeft')) + cssCut(Items.eq(0).css('marginRight')) +
			cssCut(Items.eq(0).css('paddingLeft')) + cssCut(Items.eq(0).css('paddingRight')) +
			cssCut(Items.eq(0).css('borderLeftWidth')) + cssCut(Items.eq(0).css('borderRightWidth'));
	}

	Object.css('overflow', 'hidden');
	var wrapper = $(document.createElement('div')).css({width: (options.itemWidth*Items.length)+'px'});
	if (options.itemHeight != 'auto') {
		wrapper.css('height', options.itemHeight+'px');
	}
	Items.wrapAll(wrapper);
	wrapper = Items.parents('div').eq(0);
	var onPage = Math.floor(cssCut(Object.css('width'))/options.itemWidth);

	var now = {onPage: onPage, minPage:0, maxPage:Math.max(Items.length-onPage, 0), now:0};
	
	function moveTo(page) {
		if(typeof(page) == 'undefined')
			page = now.now;
		wrapper.animate({marginLeft:'-'+(options.itemWidth*page)+'px'}, options.duration);
	}

	function next(count) {
		if(typeof(count) == 'undefined')
			count = 1;
		now.now = Math.min(now.now+count, now.maxPage);
		if (now.now == now.maxPage) {
			options.elemNext.addClass(options.disabledClass);
		}
		options.elemPrev.removeClass(options.disabledClass);
		moveTo();
	}
	function prev(count) {
		if(typeof(count) == 'undefined')
			count = 1;
		now.now = Math.max(now.now-count, now.minPage);
		if (now.now == now.minPage) {
			options.elemPrev.addClass(options.disabledClass);
		}
		options.elemNext.removeClass(options.disabledClass);
		moveTo();
	}

	if (options.elemNext == null) {
		options.elemNext = Object.parents().eq(0).find('.next');
	}

	if (options.elemPrev == null) {
		options.elemPrev = Object.parents().eq(0).find('.prev');
	}

	if (now.now == now.maxPage) {
		options.elemNext.addClass(options.disabledClass);
	}
	if (now.now == now.minPage) {
		options.elemPrev.addClass(options.disabledClass);
	}
	if (options.startFrom) {
		next(options.startFrom);
	}

	options.elemNext.click(function(){next(); return false;});
	options.elemPrev.click(function(){prev(); return false;});
}
