﻿(function($){  
 $.fn.simplerscroller = function(options) {  
  
      //set defaults
      var defaults = {  
          width: 400,
          height: 100,
          orientation: 'horizontal',
          animation: '',
          speed: 'slow',
          itemWidth: 100,
          itemHeight: 100,
          startPosition: 'middle'
      };  
      var options = $.extend(defaults, options);
      
    return this.each(function() {
    obj = $(this);
    var body = obj.html();
    
    //setup variables
    var scrollerWidth = options.width;
    var scrollerHeight = options.height;
    var orientation = options.orientation;
    var currentPosition = 0; //options.startPosition; //start the position at 0
    var totalWidth = 0;
    var totalHeight = 0;
    var itemWidth = options.itemWidth;
    var itemHeight = options.itemHeight;
    var itemDisplayCount = 0;
    var speed = options.speed;
    var totalItems = 0;
    var id = '#' + $(this).attr('id');
   
    //handle orientation
    if (orientation == 'vertical') {
        //get total height
        $(".simpler-scroller-item").each(function (i) {
            obj = $(this);
            totalItems += 1;
        });
        totalHeight = totalItems * itemHeight;
        
        //calculate item display count
        itemDisplayCount = scrollerHeight / itemHeight;
        
        if (currentPosition == 'middle') {
            currentPosition = totalItems / 2;
        }
        
        //handle css
        $(this).find('ul').attr({style: "height:" + totalHeight + "px;margin:0;padding:0;list-style:none"});
        $(this).find('li').attr({style: "margin:0;padding:0;list-style:none;"});
        $(this).attr({style: "width:" + scrollerWidth + "px;height:" + scrollerHeight + "px;overflow:hidden"});
        $(this).find('.simpler-scroller-item').attr({style: "width:" + itemWidth + "px;height:" + itemHeight + "px;"});       
        
        //perform action
        $('#simpler-scroller-up').click(function() {
            currentPosition -= itemHeight;
            if (Math.abs(currentPosition) < (totalHeight - (itemHeight * (itemDisplayCount - 1)))) {
                $('#simpler-scroller ul').animate({marginTop:currentPosition}, speed);
            }
            else {
                currentPosition = -(totalHeight - (itemHeight * itemDisplayCount));
            }
            return false;
        });
        $('#simpler-scroller-down').click(function() {
            currentPosition += itemHeight;
            if (currentPosition < totalHeight) {
                if (currentPosition > 0) {
                    currentPosition = 0;
                }
                else {
                    $('#simpler-scroller ul').animate({marginTop:currentPosition}, speed);
                }
            }
            return false;
        });
    }
    else {
        //get total width   
        $(this).find(".simpler-scroller-item").each(function (i) {
            obj = $(this);
            totalItems += 1;
        });
        totalWidth = totalItems * itemWidth;
        
        //calculate item display count
        itemDisplayCount = scrollerWidth / itemWidth;
        
        //handle css
        $(id + ' ul').attr({style: "width:" + totalWidth + "px;margin:0;padding:0;list-style:none"});
        $(id + ' li').attr({style: "margin:0;padding:0;list-style:none;float:left;"});
        $(id).attr({style: "width:" + scrollerWidth + "px;height:" + scrollerHeight + "px;float:left;overflow:hidden"});
        $(id + ' .simpler-scroller-item').attr({style: "width:" + itemWidth + "px;height:" + itemHeight + "px;float:left"});       
    
        //perform action
        $(this).prev().click(function() {
            currentPosition += itemWidth;
            if (currentPosition < totalWidth) {
                if (currentPosition > 0) {
                    currentPosition = 0;
                }
                else {
                    $(id + ' ul').animate({marginLeft:currentPosition}, speed);
                }
            }
            return false;
        });
        $(this).next().click(function() {
            currentPosition -= itemWidth;
            if (Math.abs(currentPosition) < (totalWidth - (itemWidth * (itemDisplayCount - 1)))) {
                $(id + ' ul').animate({marginLeft:currentPosition}, speed);
            }
            else {
                currentPosition = -(totalWidth - (itemWidth * itemDisplayCount));
            }
            return false;
        });
    }
    
  }); 
 };  
})(jQuery);  