(function($){
  var animateBox = function(items, options){
    var
    defaults = {
      disableOnMousehover : true,
      nextbutton : null,
      prevbutton : null,
      Autoanimate : false,
      TimeOut : 5000
    },
    Data = $(items),
    S = $.extend({}, defaults, options),
    Current = 0,
    TimeOut = null,
    Autoanimate = false,
    DataLength = Data.length,
    OnAnimationEnd = null,

    Set_TimeOut = function () {
      if (TimeOut)
        clearTimeout(TimeOut);
      TimeOut = setTimeout(function(){
        AnimateTo(Current + 1 >= DataLength? 0 : Current + 1)
      }, S.TimeOut);
    },

    SetAutoanimate = function(set) {
      if (set) {
        Autoanimate = true;
        Set_TimeOut();
      } else {
        Autoanimate = false;
        clearTimeout(TimeOut);
      }
    },

    AnimateTo = function(index) {
      $(Data).css('display' , 'none');
      $(Data.get(index)).css('display' , '');
      Current = index;
      if (OnAnimationEnd) {
        OnAnimationEnd.call(items, Current);
      }
      if (Autoanimate) {
        Set_TimeOut();
      }
    };

    if (DataLength > 1) {
      if (S.Autoanimate) {
        SetAutoanimate(true);
        if (S.disableOnMousehover) {
          Data.parent()
          .mouseenter(function(){
            SetAutoanimate(false);
          })
          .mouseleave(function(){
            SetAutoanimate(true);
          });
        }
      }
    }

    return {
      navigate : function(to) {
        if (to >= 0 && to < DataLength && to != Current) {
          AnimateTo(to);
        }
        return this;
      },
      animated : function(callback) {
        if (callback && $.isFunction(callback))
          OnAnimationEnd = callback;
        return this;
      },
      Autoanimate : function(set) {
        SetAutoanimate(set? true : false);
        return this;
      }
    }
  };
  $.fn.bannerslider = function(options){
    if (!this.animateBox)
      this.animateBox = new animateBox(this, options);
    return this.animateBox;
  };

})(jQuery);
