/* 
	* simpleSlide plugin.
	panelCount: 0,
  loop: true,
  panelWidth: 0,
  slider: null,
  sliderWrapper: null,
*/

(function($) {

  $.simpleSlide = {
    groups: {},
    actions: {
      updateArrows: function($slideWrapper){
          $current = $slideWrapper.find(".slide-panel").eq($slideWrapper.data("current-panel"));
          if ($current.next().length == 0) { $slideWrapper.find(".next").hide(); } else { $slideWrapper.find(".next").show(); }
          if ($current.prev().length == 0) { $slideWrapper.find(".prev").hide(); } else { $slideWrapper.find(".prev").show(); }                  
      },
      showNext: function($slideWrapper, width, count, loop){
        left = null;
        slider = $slideWrapper.find(".slider");
        if ($slideWrapper.data("current-panel") != (count - 1)) {
          left = width * ($slideWrapper.data("current-panel") + 1);
          $slideWrapper.data("current-panel", $slideWrapper.data("current-panel") + 1);
        } else {
          if (loop) {
            left = 0;        
            $slideWrapper.data("current-panel", 0);
          }
        }
        if (left != null) $.simpleSlide.actions.slideTo(slider, left);
      },
      showPrev: function($slideWrapper, width, count, loop){
        left = null;
        slider = $slideWrapper.find(".slider");
        if ($slideWrapper.data("current-panel") != 0) {
          left = width * ($slideWrapper.data("current-panel") - 1);
          $slideWrapper.data("current-panel", $slideWrapper.data("current-panel") - 1);
        } else { 
          if (loop) {
            left = ((count - 1) * width);
            $slideWrapper.data("current-panel", count-1);
          }
        }
        if (left != null) $.simpleSlide.actions.slideTo(slider, left);
      },
      slideTo: function(slider, left) {
        $(slider).stop().animate({left: "-"+left}, 1500, "easeOutQuart");
      }
    }
     
  };

  $.fn.simpleSlide = function(options) {
    var opts = $.extend({}, $.fn.simpleSlide.defaults, options);
    if (this.length == 0) return false;    
    return this.each(function() {      
      
      var $panels = $(this).find(".slide-panel").nextAll().andSelf();
      $panels.addClass("slide-panel");
            
      var width = opts.panelWidth ? opts.panelWidth : $(this).find(".slide-panel:first").outerWidth();
      var containerWidth = opts.containerWidth ? opts.containerWidth : width;
      
      var sliderWrapper = $(this);
      var count = $panels.length;
      var loop = opts.loop || false;
      var start = opts.start;
      var totalWidth = 0;
      
      $(this).find(".slide-panel").each(function(){ totalWidth += $(this).outerWidth(); });
      if (opts.panelWidth) totalWidth = opts.panelWidth * count;
      
      sliderWrapper.data("current-panel", 0);
      $(this)
      .find(".slider").css("width", totalWidth+"px").css("position", "relative")
      .wrap('<div class="simple-slide-wrapper" style="overflow:hidden;width:'+containerWidth+'px;"></div>');
      
      $(this).find(".prev").click(function(){ 
        $.simpleSlide.actions.showPrev(sliderWrapper, width, count, loop);
        if (!loop) $.simpleSlide.actions.updateArrows(sliderWrapper);
        return false;
      });
      
      $(this).find(".next").click(function(){
        $.simpleSlide.actions.showNext(sliderWrapper, width, count, loop);
        if (!loop) $.simpleSlide.actions.updateArrows(sliderWrapper);
        return false;
      });

      // Initialize the arrows.
      if (!loop) $.simpleSlide.actions.updateArrows(sliderWrapper);      
    });
  };
  $.fn.simpleSlide.defaults = {};
	$(function(){ $('.simple-slide').simpleSlide(); });
})(jQuery);

/*
GROUP STUFF. ALTERNATIVE SOLUTION TO PASSING LOTS OF PARAMETERS.
i = $.simpleSlide.groups.length || 0;
$.simpleSlide.groups[i] = {};
$.simpleSlide.groups[i].panelCount = $panels.length;

$.simpleSlide.groups[i].sliderWrapper = $(this);
$.simpleSlide.groups[i].slider = $(this).find(".slider");
$.simpleSlide.groups[i].panelWidth = opts.width || $(this).find(".current").outerWidth();
$.simpleSlide.groups[i].slider.width($.simpleSlide.panelCount * $.simpleSlide.panelWidth);
*/