﻿(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 offset = 0;
    if (options.startPosition != 'middle') {
        offset = (options.startPosition * options.itemWidth);
    }
    var itemWidth = options.itemWidth;
    var itemHeight = options.itemHeight;
    var itemDisplayCount = 0;
    var speed = options.speed;
    var totalItems = 0;
    var currentItem = 0;
    var shownItems = 0;
    if (options.startPosition > 0) {
        currentItem = options.startPosition;
    }    
    
    //get total width and load first 5 items
    $(".simpler-scroller-item").each(function (i) {
        obj = $(this);
        totalItems += 1;
        var upperbound = options.startPosition + 4;
        var lowerbound = options.startPosition - 4;
        if (i >= lowerbound && i <= upperbound) {
            shownItems += 1;
            $(this).find('a').append('<img src="/site-assets/utilities/retrieve-image.aspx?id=' + $(this).attr('id') + '" alt="Click to view recipe" class="small-item" />');
        }
    });
    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').css("marginLeft",currentPosition);
        }
        else {
            currentPosition = -(totalWidth - (itemWidth * itemDisplayCount));
        }
        $('#simpler-scroller ul').css("margin-left",currentPosition);
    }
    
    
   
    //perform action
    $('#simpler-scroller-left').click(function() {
        currentPosition += itemWidth;
        currentItem -= 1;
        if (currentItem > totalItems) {
            currentItem = totalItems;
        }
        if (currentPosition < totalWidth) {
            if (currentPosition > 0) {
                currentPosition = 0;
            }
            else {
                $('#simpler-scroller ul').animate({marginLeft:currentPosition}, speed);
                $(".simpler-scroller-item").each(function (i) {
                    if (i == currentItem - 4) {
                        if ($(this).find('a').find('img').length) {}
                        else {
                            $(this).find('a').append('<img src="/site-assets/utilities/retrieve-image.aspx?id=' + $(this).attr('id') + '" alt="Click to view recipe" class="small-item" />');
                        }
                    }
                });
            }
        }
        return false;
    });
    $('#simpler-scroller-right').click(function() {
        currentPosition -= itemWidth;
        currentItem += 1;
        if (currentItem < 0) {
            currentItem = 0;
        }
        if (Math.abs(currentPosition) < (totalWidth - (itemWidth * (itemDisplayCount - 1)))) {
            $('#simpler-scroller ul').animate({marginLeft:currentPosition}, speed);
            $(".simpler-scroller-item").each(function (i) {
                if (i == currentItem + 4) {
                    if ($(this).find('a').find('img').length) {}
                    else {
                        $(this).find('a').append('<img src="/site-assets/utilities/retrieve-image.aspx?id=' + $(this).attr('id') + '" alt="Click to view recipe" class="small-item" />');
                    }
                }
            });
        }
        else {
            currentPosition = -(totalWidth - (itemWidth * itemDisplayCount));
        }
        return false;
    });
    
  }); 
 };  
})(jQuery); 
