jQuery(document).ready( function() {
	
	var topLinkContainer = jQuery("<div id='topLinkContainer'/>");
	
	var bubble = jQuery("<p class='bubble bubbleRight clearfix' />");
	
	var span = jQuery("<span />");
	
	var link = jQuery("<a href='#' class='top' />");
	link.html("top");
	
	span.append( link );
	bubble.append( span );
	
	topLinkContainer.append( bubble );


	jQuery("#left").append( topLinkContainer );
	
	
	toBottom( jQuery("#topLinkContainer") );	
	
	jQuery(window).resize( function() {
		toBottom( jQuery("#topLinkContainer") );			   
	});
	
			
	jQuery(window).scroll(function () {
		visibility( jQuery("#topLinkContainer") );
	});
	
	
	jQuery("#topLinkContainer a.top").click( function() {
		jQuery('html, body').animate({scrollTop:0});
		return false;
	});
	
});
							   
							   
function toBottom(selector) {
			
	var newTop =   jQuery(window).height() - jQuery(selector).height();
    jQuery(selector).css({'top': newTop});
	
}

function visibility(selector) {

	var hContent = jQuery("body").height(); 
	var hWindow = jQuery(window).height();
	
	var scrollPosition = jQuery(window).scrollTop();
	
	if( hContent>hWindow && scrollPosition > 0 ) {
		jQuery(selector).fadeIn(250);
	}
	else {
		jQuery(selector).fadeOut(250);
	}
	
}
