/*
 * @file overview: focus img
 * @author LiJingYu
 * @version v1
 * @build 2013-06-17
*/

(function($){
	$.fn.focusImg = function(settings){
		//speed:图片切换时间，time:定时器时间
		var default_settings = {speed:300, time:3000};
		var settings = jQuery.extend(default_settings, settings);

		var that = $(this);
		if ($('.focus-btn'))
		{
			var oList = that.find('.focus-btn');
			var oBd = that.find('.focus-bd');
		}else{
		  return;
		}
		
		var iNow = 0;
		var timer;
		
		//通过图片数量 自动输出按钮个数
		var _html = ''
		var _length = oBd.find('li').length;


		if (_length > 1){
			for (var i=1; i<_length+1; i++ )
			{
				_html += '<li>'+i+'</li>';
			}
			oList.html(_html);
		}

		var oBtn = that.find('.focus-btn>li');
		oBtn.eq(0).addClass('on');

		//自动计算宽度
		var oCont = oBd.find('li');
		var _width = oCont.length * oCont.eq(0).width();
		oBd.css('width' , _width);

		//点击时滚动
		oBtn.mouseenter(function (){
			var that = $(this);
			iNow = that.index();
			doMove(iNow);
		});

		//自动滚动
		function starMove(){
			if (iNow == oCont.length-1)
			{
				iNow = 0;
			}else{
				iNow++;
			}
			doMove(iNow);
		}
		timer = setInterval(starMove, settings.time);

		function doMove(iNow){
            //fix ie6 layout bug zhe.zhu
            if(oCont.css('display') == 'inline-block'){
                oCont.css('display','inline');
            }else{
                oCont.css('display','inline-block');
            }

            oBtn.eq(iNow).addClass('on').siblings().removeClass('on');
			oBd.stop().animate({ 
				left : -oCont.eq(0).width() * iNow
			}, settings.speed);
		}
		
		//清除定时器
		that.mouseover(function(){
			timer = clearInterval(timer);
		}).mouseout(function(){
			timer = clearInterval(timer);
			timer = setInterval(starMove, settings.time);
		});

		return this;
	}
})(jQuery);