﻿(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 offset = 0;
    if (options.startPosition != 'middle') {
        offset = (options.startPosition * options.itemWidth);
    }
    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;
   
    //get total width   
    $(".simpler-scroller-item").each(function (i) {
        obj = $(this);
        totalItems += 1;
    });
    totalWidth = totalItems * itemWidth;
    
    //calculate item display count
    itemDisplayCount = scrollerWidth / itemWidth;
    
    //handle css
    $('#simpler-scroller ul').attr({style: "width:" + totalWidth + "px;margin:0;padding:0;list-style:none"});
    $('#simpler-scroller li').attr({style: "margin:0;padding:0;list-style:none;float:left;"});
    $('#simpler-scroller').attr({style: "width:" + scrollerWidth + "px;height:" + scrollerHeight + "px;float:left;overflow:hidden"});
    $('.simpler-scroller-item').attr({style: "width:" + itemWidth + "px;height:" + itemHeight + "px;float:left"});       
    
    if (offset > 0) {
        //apply offset
        //check if the offset is too much
        currentPosition = -offset;
        if (Math.abs(currentPosition) < (totalWidth - (itemWidth * (itemDisplayCount - 1)))) {
            $('#simpler-scroller ul').animate({marginLeft:currentPosition}, speed);
        }
        else {
            currentPosition = -(totalWidth - (itemWidth * itemDisplayCount));
        }
        $('#simpler-scroller ul').css("margin-left",currentPosition);
    }

    //perform action
    $('#simpler-scroller-left').click(function() {
        currentPosition += itemWidth;
        if (currentPosition < totalWidth) {
            if (currentPosition > 0) {
                currentPosition = 0;
            }
            else {
                $('#simpler-scroller ul').animate({marginLeft:currentPosition}, speed);
            }
        }
        return false;
    });
    $('#simpler-scroller-right').click(function() {
        currentPosition -= itemWidth;
        if (Math.abs(currentPosition) < (totalWidth - (itemWidth * (itemDisplayCount - 1)))) {
            $('#simpler-scroller ul').animate({marginLeft:currentPosition}, speed);
        }
        else {
            currentPosition = -(totalWidth - (itemWidth * itemDisplayCount));
        }
        return false;
    });
    
    $('#work-thumbnails').show();
    
  }); 
 };  
})(jQuery);  