(function ($) {
   
  $.fn.rfScroller= function( method, options ) {
      return this.each(function() {        
      // If options exist, lets merge them
      // with our default settings

      var settings = {
        'sliderwidth' : 200,
        'sliderheight' : 130,
        'holder' : '.twitter',
        'slidetime':2000,
        'slideinterval':6000,
        'slidetransition':'wipe', // fade or wipe
        'transition':true // fade or wipe
      };
        var slidetimer = null;
        var ismoving= false;
      var currentpos = 0;
        var currentindex = 0;
      var methods = {
      init: function () {
        var holder = settings.holder;
          $(holder).css('width', settings.sliderwidth+ 'px');
          $(holder).css('height', settings.sliderheight+ 'px');
          $(holder+" .tweets").css('width', settings.sliderwidth+ 'px');
          $(holder+" .tweets").css('height', settings.sliderheight+ 'px');
          $(holder+" .tweets .tweet").css('width', settings.sliderwidth+ 'px');
          $(holder+" .tweets .tweet").css('height', settings.sliderheight+ 'px');
          methods.checkbuttons(settings.holder+" .tweets", 0);
      },
      slide: function (selector, slidedistance) {
        
        ismoving= true;
        var $mover = $(".tweets");
        slidedistance =parseInt($mover.css('margin-top'), 10)-$(".tweets .tweet:eq("+currentindex+")").height();

          
         $mover.animate({
               "marginTop": slidedistance+"px"          }, settings.slidetime,function() {
            ismoving= false;currentindex++;try  { console.log($(selector).css("margin-top"));  }catch(err)  {  }
});

      },
      playprev: function () {
       if(!ismoving){
        var holder = settings.holder+" .tweets";
          currentpos = parseInt($(holder).css('margin-top'), 10) + settings.sliderheight;
          methods.checkbuttons(holder, currentpos);
          methods.slide(holder, currentpos);
        }
      },
      playnext: function () {
        if(!ismoving){
          var holder = settings.holder+" .tweets";
          currentpos = parseInt($(holder).css('margin-top'), 10) - settings.sliderheight;
          methods.checkbuttons(holder, currentpos);
          methods.slide(holder, currentpos);
        }
      },
       resetslider: function (holder) {
        if(!ismoving){
          ismoving = true;
          $(holder).parent().fadeOut('slow', function() {$(holder).css('margin-top','0px');
             currentindex=0;
            $(holder).parent().fadeIn('slow', function() {methods.checkbuttons(holder, 0);ismoving = false;} );
          });
        }
      },
      checkbuttons: function (holder, currentpos) {
        var that = this;
        var totalheight = $(holder + " > .tweet").size() * settings.sliderheight;

        clearTimeout(slidetimer);
          if (totalheight <= settings.sliderheight) {
              $(holder).css('height', settings.sliderheight+ 'px');
          } else {
              $(holder).css('height', totalheight + 'px');
              try  { console.log(currentpos);  }catch(err)  {  }
              if (currentpos == 0) {
                  slidetimer  = setTimeout(function(){that.playnext();},settings.slideinterval);
              } else if (totalheight <= Math.abs(currentpos) + settings.sliderheight) {
                  slidetimer = setTimeout(function(){methods.resetslider(holder);},settings.slideinterval);
              } else {
                  slidetimer = setTimeout(function(){that.playnext();},settings.slideinterval);
              }
          }
      } 
    }; //end methods

      if ( options ) { 
        $.extend( settings, options );
      }
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.RocketfuelSlider' );
    } 

    });
  };

})(jQuery);
