(function($) {
	try{
	$.fn.opRolling = function(options) {
		
		options = jQuery.extend({
			'isScrollBar'		: true,
			'displayTime'		: 4,
			'moveElementCount'	: 1, 
			'animateSpeed'		: 1,
			'mode'				: 'up',			//up, down, left, right,
			'visible'			: 5,
			'startButtonClass'	: null,
			'stopButtonClass'	: null,
			'nextButtonClass'	: null,
			'prevButtonClass'	: null,
			'autoStart'			: true,
			'prevNextSpeed'		: 0.5,
			'scrollber_css'		: 	{	
										'scrollbar-face-color' 			: '#f6f6f6',
										'scrollbar-highlight-color' 	: '#dfdfdf',
										'scrollbar-shadow-color'		: '#aaa',
										'scrollbar-3dlight-color'		: '#ffffff',
										'scrollbar-track-color'			: '#fff',
										'scrollbar-darkshadow-color'	: '#fff'
									}
		}, options);
		
		init = function(_obj, option) {
			
			var isRun = option.autoStart;
			var _this = $(_obj).css({
				'position'	: 'relative',
				'overflow'	: 'hidden',
				'height'	: '100%',
				'width'		: '100%'
				
			});
	
			var rollingArea = $('> ul', _this).css({
				'position'	: 'absolute',
				'overflow'	: 'visible',
				'z-index'	: 255
			});
			
			if (option.visible < $('>li', $(rollingArea)).size()) {
				$(_this).attr('rolling_id', '');
				if (option.mode == 'left' || option.mode == 'right') {
					width = 0;
					$('> li', $(rollingArea)).each(function(){
						width += parseInt($(this).outerWidth());
					});
					$(rollingArea).css({'width'		: width + 'px'});
					$('> li', $(rollingArea)).css({'float' : option.mode});
				} else {
					height = 0;
					$('> li', $(rollingArea)).each(function(){
						height += parseInt($(this).outerHeight());
					});
					$(rollingArea).css({'height' : height+'px'});
				}
				
				if (option.isScrollBar) {
					if (option.scrollber_css) {
						$(_this).css(option.scrollber_css);
					}
					$(_this).css({'overflow' : 'auto'});
					if (option.mode == 'up' || options.mode == 'down') {
						$(_this).css({'overflow-y' : 'scroll'});
					} else {
						$(_this).css({'overflow-x' : 'scroll'});
					}
	
					if(isRun) {
						$(_this).bind('mouseleave focusout',function(){
							$(this).scrollTop(0);
						});
					}
				}
				
				$(_this).bind('mouseenter focusin', function(){
					rollingStop(_this);
				});
	
				$(_this).bind('mouseleave focusout',function(){
					rollingStart(_this, option, isRun, option.mode);
				});
	
				if(option.startButtonClass) {
					$('.' + option.startButtonClass).bind('click',function(){
						if (!isRun) {
							isRun = true;

							if (option.isScrollBar) {
								$(_this).scrollTop(0);
								$(_this).bind('mouseleave focusout',function(){
									$(this).scrollTop(0);
								});
							}
							
							rollingStart(_this, option, isRun, option.mode);
							
						}
					});
				}
	
				if(option.stopButtonClass) {
					$('.' + option.stopButtonClass).bind('click',function(){
						isRun = false;
						rollingStop(_this);
	
						if (option.isScrollBar) {
							$(_this).unbind('mouseleave focusout');
						}
					});
				}
	
				if(option.nextButtonClass) {
					$('.' + option.nextButtonClass).bind('click', function(){
						if ($(':animated', _this).size() == 0) {
							isRun = true;
							rollingStop(_this);
							var tmpMode = option.mode == 'up' || option.mode == 'down' ? 'down' : 'right';
							run(rollingArea, option, isRun, tmpMode, (1000 * option.prevNextSpeed));
						}
					});
				}
	
				if(option.prevButtonClass) {
					$('.' + option.prevButtonClass).bind('click', function(){
						if ($(':animated', _this).size() == 0) {
							
							isRun = true;
							rollingStop(_this);
							var tmpMode = option.mode == 'up' || option.mode == 'down' ? 'up' : 'left';
							run(rollingArea, option, isRun, tmpMode, (1000 * option.prevNextSpeed));
						}
					});
				}
	
				if(isRun) {
					rollingStart(_this, option, isRun, option.mode);
				}
			} else if (option.visible == $('>li', $(rollingArea)).size()) {
				if (option.mode == 'left' || option.mode == 'right') {
					width = 0;
					$('> li', $(rollingArea)).each(function(){
						width += parseInt($(this).outerWidth());
					});
					
					if (width > $(_this).outherWidth()) {
						if (option.scrollber_css) {
							$(_this).css(option.scrollber_css);
						}
						$(_this).css({'overflow' : 'auto', 'overflow-x' : 'scroll'});
					}
				} else {
					height = 0;
					$('> li', $(rollingArea)).each(function(){
						height += parseInt($(this).outerHeight());
					});
					
					if (height > $(_this).outerHeight()) {
						if (option.scrollber_css) {
							$(_this).css(option.scrollber_css);
						}
						$(_this).css({'overflow' : 'auto', 'overflow-y' : 'scroll'});
					}
				}
			};
		};
	
		run = function(obj, option, isRun, mode, speed){
			if(isRun) {
				var rollingArea =  $('> ul', obj);
				if($(obj).attr('rolling_id')) {
					var item = $('> li:' + (mode == 'up' || mode == 'left' ? 'first-child' : 'last-child'), $(rollingArea));
					
					if(mode == 'up' || mode == 'down') {
						if (mode == 'up') {
							$(item).clone(true).appendTo($(rollingArea));
						} else {
							$(item).clone(true).prependTo($(rollingArea));
							$(rollingArea).css({top : '-' + parseInt($(item).outerHeight()) + 'px'});
						}
						var move = (mode == 'up' ? '-' : '+') + '=' + parseInt($('> li:first-child', $(rollingArea)).outerHeight()) + 'px';
						$(rollingArea).stop(true).animate({'top' : move}, speed, function(){
							$(rollingArea).css({'top' : '0px'});
							$(item).remove();
						});
					} else {
	
						$(rollingArea).css('width', parseInt($(rollingArea).css('width')) + parseInt($(item).outerWidth()));
						if (mode == 'left') {
							$(item).clone(true).appendTo($(rollingArea));
						} else {
							$(rollingArea).css({left : '-' + parseInt($(item).outerWidth()) + 'px'});
							$(item).clone(true).prependTo($(rollingArea));
						}
	
						var move = (mode == 'left' ? '-' : '+') + '=' + parseInt($('> li:first-child', $(rollingArea)).outerWidth()) + 'px';
						$(rollingArea).stop(true).animate({'left' : move}, speed, function(){
							$(rollingArea).css({'left' : '0px'});
							$(rollingArea).css('width', parseInt($(rollingArea).css('width')) - parseInt($(item).outerWidth()));
							$(item).remove();
						});
					}
				}
			} else {
				rollingStop(obj);
			}
		};
	
		rollingStop = function(obj){
			clearInterval($(obj).attr('rolling_id'));
			$(obj).attr('rolling_id', '');
		};
	
		rollingStart = function(obj, option, isRun, mode){
			if($(obj).attr('rolling_id') == '') {
				var timer = setInterval(function(){ run(obj, option, isRun, mode, (1000 * option.animateSpeed)); }, (1000 * option.displayTime));
				$(obj).attr('rolling_id', timer);
			}
		};
	
		init(this, options);
	};
	} catch (e) {
		alert(e.message);
	}
})(jQuery);

