// 
//  jquery.fixed_sidebar.js
//  spot
//  
//  Created by John van Dijk on 2011-09-16.
//  Copyright 2011 Ruby Libre. All rights reserved.
// 

(function($) {
  // OO Plugin structure
  var FixedSidebar = function(element, options) {
    var el = $(element);
    var obj = this;
    var settings = $.extend({
      threshold: 250
    }, options || {});
    
    var _document,
        _window,
        _elementOffset,
        _state;
    
    var initialize = function() {
      _document = $(document);
      _window   = $(window);
      
      // start when resources loaded
      _window.load(onLoad);
    }
    
    var onLoad = function(e) {
      _window.resize(onResize).scroll(onScroll);
       onResize();
    }
    
    var onResize = function(e) {
      // set element tot static for measurements
      if (_state != 'static') el.css('position', 'static');
      
      // take the measurement
      _elementOffset = el.offset();
      
      // set state
      _state = null;
      onScroll();
    }
    
    var onScroll = function(e) {
      var scrollOffsetTop = _window.scrollTop();
      var documentHeight  = _document.height();
      var elementHeight   = el.height();
      var elementBottom   = scrollOffsetTop + elementHeight;
      var thresholdBottom = documentHeight - settings.threshold;
      //var elementTop      = thresholdBottom - (elementHeight + _elementOffset.top);
      
      // if scroll offset higher than elements offset
      if (scrollOffsetTop >= _elementOffset.top) {
        // if elements bottom is above thresholds bottom
        if (elementBottom < thresholdBottom) {
          setFixed();
        } else {
          // setRelative(elementTop);
          setRelative();
        }
      } else {
        setStatic();
      }
    }
    
    var setFixed = function() {
      if (_state == 'fixed') return;
      el.css({ 
        position: 'fixed', 
        top: 0,
        right: 'auto',
        bottom: 'auto',
        left: _elementOffset.left
      }); 
      _state = 'fixed';
    }
    
    var setRelative = function() {
      if (_state == 'relative') return;
      el.css({ 
        // position: 'relative', 
        // top: elementTop,
        // left: 0
        position: 'absolute',
        top: 'auto',
        right: 25,
        bottom: settings.threshold,
        left: 'auto'
      }); 
      _state = 'relative';
    }
    
    var setStatic = function() {
      if (_state == 'static') return;
      el.css({ 
        position: 'static', 
        top: 'auto',
        right: 'auto',
        bottom: 'auto',
        left: 'auto'
      }); 
      _state = 'static';
    }
    
    initialize();
  }
  
  $.fn.fixedSidebar = function(options) {
    return this.each(function() {
      var element = $(this);
      
      // Return early if this element already has a plugin instance
      if (element.data('fixedSidebar')) return;
      
      // pass options to plugin constructor
      var fixedSidebar = new FixedSidebar(this, options);
      
      // Store plugin object in this element's data
      element.data('fixedSidebar', fixedSidebar);
    })
  };
})(jQuery);
