/**
 * ithtools.sprollover
 * 
 * Copyright (c) 2010 itec hankyu hanshin co.,ltd
 *
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) {
	
	$.ithLib = $.ithLib || {};
	
	$.ithLib.hScrollView = {
		conf : {
			next: ".next",
			prev: ".prev",
			items: ".inner",
			time: 600,
			scrollCount: 1,
			transition: "linear",
			autoHeight: true
		}
	};
	
	$.fn.hScrollView = function( conf ) {
		
		var _conf = $.extend({}, $.ithLib.hScrollView.conf, conf);
		
		var _this = $(this);
		var _inner = _this.find( _conf.items );
		var _mask = _inner.parent();
		var _items = _inner.children();
		var _length = _items.length;
		var _next = _this.find( _conf.next );
		var _prev = _this.find( _conf.prev );
		
		var _height = 0;
		var _posAry = [];
		var _count = 0;
		
		//
		var _itemWidth = 0;
		_items.each(function(){
			_itemWidth += $(this).width();
			_posAry.push( $(this).position().left );
			if ( _height < $(this).height() ) _height = $(this).height(); 
		});
		
		if ( _conf.autoHeight ) _mask.height( _height );
		
		//
		if ( _mask.width() > _itemWidth ) return this;
		
		//
		_prev.click( function(){
			move( _count - _conf.scrollCount );
		});
		
		//
		_next.click( function(){
			if ( ( _itemWidth - _posAry[_count] ) <= _mask.width() ) return;
			move( _count + _conf.scrollCount );
		});
		
		var move = function( n ) { 
			if ( n < _length && n >= 0 ) {
				_inner.animate({ left: -_posAry[n] }, _conf.time, _conf.transition );
				_count = n;
			}
		}
		
		return this;
		
	}
	
})(jQuery);


