/*example : $(function(){$('#example').faiSc();*/
$.fn.extend({
	mousewheel:function(Func){
		return this.each(function(){
			var _self = this;
			_self.D = 0;
			if($.browser.msie||$.browser.safari){
			   _self.onmousewheel=function(){_self.D = event.wheelDelta;event.returnValue = false;Func && Func.call(_self);};
			}else{
			   _self.addEventListener("DOMMouseScroll",function(e){
					_self.D = e.detail>0?-1:1;
					e.preventDefault();
					Func && Func.call(_self);
			   },false); 
			}
		});
	}
});
$.fn.faiSc = function(s){
	return this.each(function(){				  
		var s_statue = false;
		var s_x,init_X;
		var $self = $(this);
		var $sw = $self.find('ul');
		var $li = $self.find('li');
		var lisize = $li.size();
		var liW = $li.outerWidth(true);
		var msgW = liW * lisize;
		var wrapW = $self.width();
		var newX = 0;
		
		function setW(){
			if(newX <= 0) newX = 0;
			if(newX >= sw) newX = sw;
			var scDis = -newX*((msgW-sw)/sw);
			try{
				$el.css('left',newX);
				$sw.stop().animate({'margin-left':scDis},{duration:800,easing:'easeOutExpo'});
			}catch(e){}
		}
		
		if(msgW > wrapW){
			$self.append("<div id=scroll_bg></div><div id=scroll_btn></div>");
			var $el = $('#scroll_btn');
			var btnW = $el.width();
			var sw = wrapW - btnW;
			var mwW =wrapW*wrapW/(msgW*3);
		
			$el.css({'-moz-user-select':'none'}).mousedown(function(e){
				s_x = e.clientX;
				init_X = parseInt($el.css('left'));
				s_statue = true;
			});	
			$(document).mousemove(function(e){
				if(s_statue){
					newX = e.clientX - s_x + init_X;
					$(this).bind('selectstart',function(){return false;});
					setW();
				}						   
			}).mouseup(function(){
				$(this).unbind('selectstart');
				s_statue = false;
			});
			$self.mousewheel(function(){						   
				if(this.D > 0) newX -= mwW; 
				else newX += mwW;
				setW();
			});
		}
	});
}
