$(document).ready(function() {
						   
	// Added variable to detect height of given DIV			   
	var headersize = $('#nww_header').height();
	var initial_top;
					
	if (headersize != 0) {							   
	//sets the top of the sidecar to line up with the top of #sharetop	(email, comments, print, facebook like)
		initial_top = $('#sharetop').height() + parseFloat($('#sharetop').css('marginBottom')) + $('#blogs_entry_meta h1.mgtitle').height() + parseFloat($('#blogs_entry_meta h1.mgtitle').css('marginBottom')) + $('#blogs_entry_meta h2.blogdeck').height() + parseFloat($('#blogs_entry_meta h2.blogdeck').css('marginBottom')) + $('#blogs_prevnext').height() + parseFloat($('#blogs_prevnext').css('marginBottom')) + 300;
	} else {
		initial_top = 340;	
	}
	
	$('#floating_tools').css('top',initial_top);
	$('#floating_tools').show();	
 
	var iecatch = $.browser.msie + $.browser.version;
	
	var top;
	
	if (headersize != 0) {
		top = $('#floating_tools').offset().top - parseFloat($('#floating_tools').css('marginTop').replace(/auto/, 0));
	} else {
		top = 340;	
	}
	
	//just in case we get a 3-line story again...
	if ($('#blogs_entry').height() < 584) {
			$('#blogs_entry').css('min-height', '584px');
			if (iecatch.indexOf("true6") != -1) {
				$('#blogs_entry').css('height', '584px');
			}
	}
	
	$(window).scroll(function (event) {
		// what the y position of the scroll is
		var y = $(this).scrollTop();
		var floating_tools_btm = y + $('#floating_tools').height() + parseFloat($('#floating_tools').css('paddingTop').replace(/auto/, 0));
		var leftcol_btm = $('#blogs_entry').offset().top + $('#blogs_entry').height();
	
		if (y >= top && leftcol_btm >= floating_tools_btm) {
		  
		   // if so, add the fixed class
			if (iecatch.indexOf("true6") != -1 || iecatch.indexOf("true7") != -1) {
			 $('#floating_tools').css('top', (y - $('#nww_container').offset().top) +'px');
			}
			
			else {
			 $('#floating_tools').addClass('fixed');
			 $('#floating_tools').css('top', '0');	
			}
		} 
		
		else if (leftcol_btm < floating_tools_btm) {
		  // otherwise remove it and keep the toolbar at the bottom
		  $('#floating_tools').removeClass('fixed');
		  
		  //set the top of the toolbar such that it sticks at the bottom of the blogs_entry div
		 $('#floating_tools').css('top', ($('#blogs_entry').height() - $('#floating_tools').height() +$('#blogs_entry_meta').height() + $('#sharetop').height() + parseFloat($('#sharetop').css('paddingBottom')) + parseFloat($('#floating_tools').css('marginTop'))) + $('#blogs_prevnext').height() + 90 + 'px');
		} 
		
		else {
		  // otherwise remove it
		  $('#floating_tools').removeClass('fixed');
		  $('#floating_tools').css('top', initial_top);
		}
	}); 
});
