(function($) {
  var defaults = {
     visibleElements: 1,
     has_indicator: true,
     slide_interval: 0
  };
  
  $.fn.mikos_slider = function(options) {
    this.each((function() {
      var auto = false;
      
      init = function() {
         var slider = $this;
         if(slider.opts.has_indicator) init_indicator();
         $(slider).find('ul.s_container').css({ width: ($this.find('.slide_element').outerWidth(true) * $this.find('.slide_element').length) });
         $(slider).find('a.next').bind('click', slide_next);
         $(slider).find('a.prev').bind('click', function() {
           $(slider).find('a.prev').unbind('click').bind('click', function() { return false; });
           $(slider).find('ul.s_container').stop().animate({ left: '+=' + ($(slider).find('.slide_element').outerWidth(true) * slider.opts.visibleElements) }, function() {
             setup_animation(slider, slider.opts.slide_interval);
             position_check(slider, slider.opts.visibleElements);
           });

           return false;
         });
         position_check(slider, slider.opts.visibleElements);
         setup_animation(slider, slider.opts.slide_interval);
      };
      
      setup_animation = function(slider, interval) {
        if(interval == 0) return;
        
        clearInterval(auto);
        
        auto = setInterval(function() {
            var reset = position_check(slider, slider.opts.visibleElements, 'auto');
            var move = reset ? 0 : '-=' + ($(slider).find('.slide_element').outerWidth(true) * slider.opts.visibleElements);
            $(slider).find('a.next').unbind('click').bind('click', function() { return false; });
            $(slider).find('ul.s_container').stop().animate({ left: move }, function() {
              position_check(slider, slider.opts.visibleElements);
            });
          
            return false;
        }, interval);
        
      }

      init_indicator = function() {
         var indicator = $('<ul />').addClass('indicator');

         for(var i = 0; i < ($this.find('.slide_element').length / $this.opts.visibleElements); i++) 
            indicator.append($('<li />').append($('<a />')));

         indicator.appendTo($this);
      }

      position_check = function(slider, visibleElements, caller) {
         if($(slider).find('.slide_element').length > visibleElements) {
           var current_slider_position = parseInt($(slider).find('.s_container').css('left').slice(0, -2));
           var slide_element_width = $(slider).find('.slide_element').outerWidth(true);
           var viewable_area_width = slide_element_width * visibleElements;
           var current_slider_position_index = -(current_slider_position) / viewable_area_width;
           var total_slider_length = Math.ceil((slide_element_width * $(slider).find('.slide_element').length) / viewable_area_width) - 1;

            switch(current_slider_position_index) {
               case 0 :
                  if(caller == 'auto') return false;
                  $(slider).find('a.prev').css('opacity', 0.6).unbind('click').bind('click', function() { return false; });
                  $(slider).find('a.next').css('opacity', 1.0).unbind('click').bind('click', function() {
                    $(slider).find('a.next').unbind('click').bind('click', function() { return false; });
                    $(slider).find('ul.s_container').stop().animate({ left: '-=' + ($(slider).find('.slide_element').outerWidth(true) * visibleElements) }, function() {
                      setup_animation(slider, slider.opts.slide_interval);
                      position_check(slider, visibleElements);
                    });
                  
                    return false;
                  });
                  // $(slider).find('a.next').css('opacity', 1.0).unbind('click').bind('click', slide_next);
                  
                  break;

               case (total_slider_length) :
                  if(caller == 'auto') return true;
                  $(slider).find('a.prev').css('opacity', 1.0).unbind('click').bind('click', function() {
                    $(slider).find('a.prev').unbind('click').bind('click', function() { return false; });
                    $(slider).find('ul.s_container').stop().animate({ left: '+=' + ($(slider).find('.slide_element').outerWidth(true) * visibleElements) }, function() {
                      setup_animation(slider, slider.opts.slide_interval);
                      position_check(slider, visibleElements);
                    });

                    return false;
                  });
                  $(slider).find('a.next').css('opacity', 0.6).unbind('click').bind('click', function() { return false; });
                  break;

               default :
                  if(caller == 'auto') return false;
                  $(slider).find('a.nav').css('opacity', 1.0);
                  $(slider).find('a.prev').bind('click', function() {
                    $(slider).find('a.prev').unbind('click').bind('click', function() { return false; });
                    $(slider).find('ul.s_container').stop().animate({ left: '+=' + ($(slider).find('.slide_element').outerWidth(true) * visibleElements) }, function() {
                      setup_animation(slider, slider.opts.slide_interval);
                      position_check(slider, visibleElements);
                    });

                    return false;
                  });
                  $(slider).find('a.next').bind('click', function() {
                    $(slider).find('a.next').unbind('click').bind('click', function() { return false; });
                    $(slider).find('ul.s_container').stop().animate({ left: '-=' + ($(slider).find('.slide_element').outerWidth(true) * visibleElements) }, function() {
                      setup_animation(slider, slider.opts.slide_interval);
                      position_check(slider, visibleElements);
                    });

                    return false;
                  });
                  break;
            }
         } else {
            $(slider).find('a.nav').css('opacity', 0.6).unbind('click').bind('click', function() { return false; });
         }

         slider.find('.indicator li').removeClass('on');
         slider.find('.indicator li:eq('+ current_slider_position_index +')').addClass('on');
      }
      
      slide_next = function(slider) {
        $(slider).find('a.next').unbind('click').bind('click', function() { return false; });
        $(slider).find('ul.s_container').stop().animate({ left: '-=' + ($(slider).find('.slide_element').outerWidth(true) * slider.opts.visibleElements) }, function() {
          position_check(slider, slider.opts.visibleElements);
        });

        return false;
      }
      
      slide_prev = function(slider) {
        console.debug($this);
        
        $(slider).find('a.prev').unbind('click').bind('click', function() { return false; });
        $(slider).find('ul.s_container').stop().animate({ left: '+=' + ($(slider).find('.slide_element').outerWidth(true) * slider.opts.visibleElements) }, function() {
          position_check(slider, slider.opts.visibleElements);
        });

        return false;
      }
      
      return function() {
        $this = $(this);
        $this.opts = $.extend({}, defaults, options);
        init();
      };
    })());
  };

})(jQuery);
