/**
 * Suite of functions and events to delegate document height
 */
jQuery.documentHeight = function() {
	var D = document;
	var documentHeight = Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
	return documentHeight;
};
jQuery.fn.documentHeight = function(updateOnWindowResize) {
	var $this = jQuery(this);
	if($this.hasClass('resized-to-doc')) {
		$this.height('auto');
		jQuery(window).unbind('resize.docResize');
	}
	var initHeight = $this.height();
	if(updateOnWindowResize == true) {
		jQuery(window).bind('resize.docResize', function(){
			$this.height('100%');
			var newHeight = jQuery.documentHeight();
			if(newHeight > initHeight) $this.height(newHeight);
		}).trigger('resize.docResize');
	};
	$this.addClass('resized-to-doc', 'true');
	return $this;
};

jQuery.fn.keepContentInViewPort = function(settings) {
	var $this = jQuery(this);
	if(!$this.length) return;
	var options = jQuery.extend({
		offset:20,
		delay:200,
		animate:true,
		animationSpeed:'slow'
	}, settings);
	var $document = jQuery(document);
	var offset = options.offset;
	var timeout;
	$document.scroll(function(){
		clearTimeout(timeout);
		timeout = setTimeout(function() {
			var paddingTop = Math.round($document.scrollTop()-$this.offset().top);
			if(paddingTop <= 0) paddingTop = 0;
			if(paddingTop > 0 && offset) paddingTop += offset;
			if(settings.max) {
				var max = settings.max-$this.height()
				if(paddingTop > max) paddingTop = max+options.offset;
			}
			if(!options.animate) {
				$this.css('paddingTop', paddingTop+'px');
			} else {
				$this.stop().animate({paddingTop:paddingTop+'px'}, options.animationSpeed);
			}
		}, options.delay);
	});
};

/**
 * Some functionality
 *
 */
(function($) {
  $(function() {
	  // Init Matching Document Height
	  $('body, #BG').documentHeight(true);
	  // Right Column Stay on screen
	  $('.region-sidebar-second').keepContentInViewPort({max:$('.main-inner-container').height()});
	  
	  /*$('.view-display-id-block_1 .view-footer').hover(function(){
		  $(this).find('.subscription-form').
	  });*/
	});
}(jQuery));
;

