(function($)
{
	$.fn.backgroundFullPageScroll = function (options) {
	
		var settings = {
      leftPosition: 'center', 
      anchorElement: 'body'
		};
		
		$.extend(settings, options);
		
		return this.each(function () {
      var $anchor_element, $target_element, target_element, $window, $document, background_image_height = 0, document_height, window_height;
      
      // figure out how big our bg is.
      var img = new Image();
      img.onload = function() {
        background_image_height = this.height;
        setBackgroundPosition();
      }      
      // this could be cleaner but will do the job for now
      img.src = $(this).css('backgroundImage').replace(/url\(/g, '').replace(/['"]/g,'').replace(/\)/g, '');

      $anchor_element = $(settings.anchorElement);
      $target_element = $(this);
      if ($target_element.size() > 0) target_element = $target_element.get(0); else return;
      target_height = $anchor_element.height();
      $window = $(window);
      $document = $(document);
      document_height = $document.height();
      window_height = $window.height();

      function setBackgroundPosition(){        
        var top, target_height, scroll_top;        
        
        if (background_image_height <= $(window).height())
        {          
          top = 0; // image is shorter than page.
        } else {          
          top = sineEasing($window.scrollTop() / document_height) * (background_image_height - window_height) * -1;
        }
        target_element.style.backgroundPosition = settings.leftPosition + " " + top + "px";
      }

      function sineEasing(p){
        return 1 - Math.cos(p * Math.PI / 2);
      }
			
			$window.scroll(setBackgroundPosition);
      $window.resize(function(){
        document_height = $document.height();
        window_height = $window.height();
        setBackgroundPosition()
      });
      
      // init
      setBackgroundPosition();
		});
	};
})(jQuery);
