$(document).ready(function() {
	$.keepInView({ 
		divs: ['right-mover'],
		scrollSpeed: 400
		})
});


/**
	usage:
	
	$.keepInView({ 
		divs: ['inView01','inView02'], //the id's of the divs to apply this to. exclude the # character.
		maxScroll:, 800 	//the limit of how much this can scroll by. don't set or set to -1 for this to be ignored
		scrollSpeed: 500, 	//the scroll speed in miliseconds
		safeOffset: 5		//if the div is scrolling past the height of its parent then increase this number (in pixels). else don't include or set to 0
	})
	
**/
(function( $ ){
	
	
	$keepInView = $.keepInView = $.kiv = $kiv = function(settings){
		
		if(settings.divs != undefined){
			$kiv.vars.divs = settings.divs;
		}
		
		if(settings.maxScroll != undefined){
			$kiv.vars.maxScroll = settings.maxScroll;
		}
		
		if(settings.scrollSpeed != undefined){
			$kiv.vars.scrollSpeed = settings.scrollSpeed;
		}
		
		if(settings.safeOffset != undefined){
			$kiv.vars.safeOffset = settings.safeOffset;
		}
		
		$kiv.processElements();
		$kiv.initialize();
	}
	
	$kiv.vars = {
		divs: 		[''], //the id's of the divs that will scroll
		divsString: '', //a string representation of the divs array
		maxScroll: -1,
		scrollSpeed: 0,
		safeOffset: 0
	}
	
	$kiv.initialize = function(){
		
		$(window).scroll(function() {
		
				
				$($kiv.vars.divsString).each(function(){
					
					var thisID = $(this).prop('id');

					
					$(this)
					.stop()
					.animate({"margin-top":$kiv.getMarginPX(thisID)}, $kiv.vars.scrollSpeed);
				});
		});
		
	}
	
	$kiv.getMarginPX = function(theID){
		return $kiv.getMargin(theID) + 'px';
	}
	
	/**
		Gets the new top margin for the divs that we want to stay in view. Calculates max values based on the parent size.
	**/
	$kiv.getMargin = function(theID){
	
		var offset = $('#'+theID).parent().offset().top;
		var ScrollDist = $(window).scrollTop();
		var scrollby = ScrollDist-offset;
		//distance from the top of the page
		var childPosition = Math.ceil($('#'+theID).offset().top - $('#'+theID).css('margin-top').replace("px", "")); 
		//distance from the top of the page
		var parentPosition = Math.ceil($('#'+theID).parent().offset().top - $('#'+theID).parent().css('margin-top').replace("px", ""));
		
		var pcOffset =  childPosition - parentPosition - $kiv.vars.safeOffset; //the offset between the parent anc child


		theMax = $('#'+theID).parent().height() - $('#'+theID).height() - pcOffset;
		//alert( pcOffset);
		if(scrollby <= 0){
			return 0;
		}else if($kiv.vars.maxScroll >= 0 && scrollby >= $kiv.vars.maxScroll){
			return $kiv.vars.maxScroll;
		}else if(scrollby >= theMax){
			return theMax;
		}else{
			return scrollby;
		}

	}
	
	/**
		Takes the divs array and makes it into a string that can be used in
		jQuery calls. e.g. $('#id1, #id2, #id3).something();
	**/
	$kiv.processElements = function(){
		
		var o = '';
		for(var i = 0; i < $kiv.vars.divs.length; i++){
		
			o = o + '#' + $kiv.vars.divs[i];
			
			if(i < $kiv.vars.divs.length -1){
				o =  o + ', ';
			}
		}
		$kiv.vars.divsString = o;
	
		
	}
	
	
	
	
})( jQuery );
