﻿
(function($){
    $.fn.easySlider = function(options){
        var options = $.extend(defaults, options);
        this.each(function(){
            var obj = $(this);
            var curr = 1; //当前索引
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            obj.css("position", "relative");
            if (!options.vertical) {
                $("li", obj).css('float', 'left')
            	$("ul", obj).css('width', s * w);
            }
            else {
                $("li", obj).css('display', 'block')
            	$("ul", obj).css('height', s * h);
            };
			$("li img", obj).mouseover( function(){animate(curr+1, true);});
			$("li img", obj).mouseout( function(){animate((curr+1), false);});
            if (options.controlsShow) {
                var html = '<div id="'+options.numericId +'"><div>';
                
                obj.append(html);
                for (var i = 0; i < s; i++) {
                    $(document.createElement("span")).attr('id', options.numericId + (i + 1))
													.html(i + 1)
													.appendTo($("#" + options.numericId))
													.click(function(){
								                        animate($(this).html(), true);
								                    });
                };
                $("#controls span", obj).hover(function(){
                    var num = $(this).html();
                    animate(num, true);
                }, function(){
                    timeout = setTimeout(function(){
                        animate((curr + 1), false);
                    }, options.pause / 4);
                });
            };
            function setcurrnum(index){
                obj.find("#controls span").eq(index).addClass('current').siblings().removeClass('current');
            }
			function adjust(){
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(curr*w*-1));
				} else {
					$("ul",obj).css("margin-left",(curr*h*-1));
				}
				setCurrent(t);
			};
            function animate(index, clicked){
                $("ul", obj).stop();
                var next = index == s ? 1 : index + 1;
                curr = index - 1;
                setcurrnum((index - 1));
                if (!options.vertical) {
                    p = ((index - 1) * w * -1);
                    $("ul", obj).animate({
                        marginLeft: p
                    }, {queue:false, duration:options.speed});
                }
                else {
                    p = ((index - 1) * h * -1);
                    $("ul", obj).animate({
                        marginTop: p
                    }, {queue:false, duration:options.speed});
                };
                if (clicked) {
                    clearTimeout(timeout);
                };
                if (options.auto && !clicked) {
                    timeout = setTimeout(function(){
                        animate(next, false);
                    }, options.speed + options.pause);
                };
                            }
            var timeout;
            //初始化
            setcurrnum(0);
            if (options.auto) {
                timeout = setTimeout(function(){
                    animate(2, false);
                }, options.pause);
            };
                    });
    };
    //默认值
    var defaults = {
        controlsShow: true, //是否显示数字导航
        vertical: false, //纵向还是横向滑动
        speed: 800, //滑动速度
        auto: true, //是否自定滑动
        pause: 2000, //两次滑动暂停时间
        easing: "swing", //easing效果，默认swing，更多效果需要easing插件支持
        numericId:"controls"
    }
})(jQuery);

