(function($){
  $.fn.slidePic = function(o) {
    o = $.extend({
      show:"x-scroll",  // 默认是水平滚动
      offset:30, //滚动时的抖动值
      speedFirst:5000,  // 默认速度5秒，用于焦点图第一张
      speed:3000  // 默认滚动速度是3秒，用于其他焦点图
    }, o || {});
    return this.each(function(){
      var position = 0;
      var clock;
      var slides = $(".slide");
      var slideWidth = $(".slide").width();
      var slideHeight = $(".slide").height();
      var numberOfSlides = slides.length;
      $(".servicesPic").css("overflow", "hidden");
      $(".servicesBotton span:first").css("background-color", "#bd0a0d");
      $(".servicesBotton span:last").css("border","0");
      slides.wrapAll("<div id=\"slideInner\"></div>").css({
        "float":"left",
        "width":slideWidth
      });

      function doSlideImg(){
        slideImg(position);
        if(position == 1){
          clock = setTimeout(doSlideImg,o.speedFirst);
        }else{
          clock = setTimeout(doSlideImg,o.speed);
        }
      }
      doSlideImg();

      $(".servicesBotton span").bind("click",function () {
        position = $(".servicesBotton span").index(this);
        clearTimeout(clock);
        doSlideImg();
      });

      function slideImg(position) {
        if(o.show == "x-scroll"){
          $("#slideInner").css("width", slideWidth * numberOfSlides);
          $("#slideInner").animate({
            marginLeft:slideWidth * (-position) - o.offset
          },150).animate({
            marginLeft:slideWidth * (-position)
          },100);
        }else if(o.show == "y-scroll"){
          $("#slideInner").css("width", slideWidth);
          $("#slideInner").animate({
            marginTop:slideHeight * (-position) - o.offset
          },150).animate({
            marginTop:slideHeight * (-position)
          },100);
        }
        changeBg();
      }

      function changeBg(){
        $(".servicesBotton span").removeAttr("style");
        $(".servicesBotton span:last").css("border","0");
        $(".servicesBotton span:eq("+position+")").css("background-color", "#bd0a0d");
        position++;
        if (position == numberOfSlides) {
          position = 0;
        }
      }
    });
  };
   
})(jQuery);


