/**Province list plugins
*Sample: jQuery('#scroll').mouseSlider() ;
*Author: Shi Yaxiong
*Date: 2010-9-22
*/

jQuery.fn.mouseSlider = function()
{
	var old_pos = 0 ;
    var obj = jQuery(this) ;
	var number = obj.find('ul li').size() ;
	var visual_width = obj.width() ;
	var total_width = number * (obj.find('ul li:eq(0)').width()+strToInt(obj.find('ul li:eq(0)').css('margin-left'))+strToInt(obj.find('ul li:eq(0)').css('margin-right'))) ;

	var move_width = total_width - visual_width ;
	var obj_pos = obj.offset().left ;
	
	obj.children('ul').css({position:'relative',left:'0px',top:'0px'}) ;
	obj.children('ul').width(total_width) ;
    obj.css({position:'relative',overflow:'hidden'}) ;

	obj.mousemove (function(e) {
		if(old_pos <= 0){
			old_pos = e.pageX ;
		}
		
		if(e.pageX > old_pos){//left
			var tmp_move = parseInt(((e.pageX-old_pos)*move_width)/(obj_pos+visual_width-e.pageX)) ;
			if(parseInt(obj.children('ul').css('left')) > visual_width - total_width && tmp_move <= move_width){
				obj.children('ul').animate({left:'-='+tmp_move}, {duration:0, complete:function(){
																							if(parseInt(obj.children('ul').css('left')) < visual_width - total_width){
																								obj.children('ul').css({'left':visual_width - total_width}) ;
																							}
																						}}) ;
			}
		}else if(e.pageX < old_pos){//right
			var tmp_move = parseInt(((old_pos-e.pageX)*move_width)/(e.pageX-obj_pos)) ;
			if(parseInt(obj.children('ul').css('left')) < 0 && tmp_move <= move_width){
				obj.children('ul').animate({left:'+='+tmp_move}, {duration:0, complete:function(){
																							if(parseInt(obj.children('ul').css('left')) > 0){
																								obj.children('ul').css({'left':0}) ;
																							}
																						}}) ;
			}
		}
		
		old_pos = e.pageX ;
	});
	
	obj.mouseenter (function(e) {
		old_pos = e.pageX ;
	});
	
	function strToInt(str){
		if(is_empty(str)){
			return 0 ;
		}else{
			if(str.charAt(0).match(/\d+/)){
				return parseInt(str) ;
			}else{
				return 0 ;
			}
		}
	}
}
