$(document).ready(function() {

		    $('#bodyArea a.btn.underground').attr('href', '');

		    if (currentUrlPath == '/') {
			var bottom = '130px';
		        $('#bodyArea a.underground').attr('style', 'bottom:' + bottom);
		    }
    $('#bodyArea a.btn.underground').css('top', ($('#wrapper').height() - ($('#footer').height() + $('#header') + 1000)));

		$("a.btn.underground").click(function(){
					       //alert('foo');
					       //					       document.getElementById
					  $(this).attr('href', '');

					    if ($(this).hasClass('surface')) { // scroll to top
					        window.scrollTo(0,0);
					             
						$(this).removeClass('surface');    
						$(this).attr('style', '');
					      } else { // scroll to bottom
					        window.scrollTo(0, 99999);
						//console.log('went to bottom');

						
						// Distance from right
						var distanceDesired = ($('#bodyArea').position().left - 30);
						var distanceDesired = '-' + distanceDesired + 'px';
						
						// Distance from bottom
						var distanceFromBottom = ($('#footer').height() + 50);
						var distanceFromBottom = distanceFromBottom + 'px';
						
						if (currentUrlPath == '/') {
						  distanceFromBottom = '130px'; 
						}


						$(this).css('bottom', distanceFromBottom);
						$(this).css('position', 'absolute');
						$(this).css('right', distanceDesired);
						
						// Add surface class to button
												var newClassSet = jQuery(this).attr('class') + ' surface';
						jQuery(this).attr('class', newClassSet);
					      }
    
					      return false;
					      });
		  });

function getDocHeight() {
  var D = document;
  return 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)
		  );
}


$(window).scroll(function() {
		   var wInnerHeight = document.documentElement.clientHeight;

		   if (window.pageYOffset == 0) {
		     		       $('#bodyArea a.underground').css('bottom', '130px');
		   }

		   // Don't use jQuery .position() - IE will fail.
	      	   var currentTopPosition = window.pageYOffset;

			       var reachedFooter = (getDocHeight() - ( $('#footer').height() + wInnerHeight     )    );


			       // The position that indicates the user is AT the footer
			       var atFooter = currentTopPosition > reachedFooter;
			       
			       if (currentUrlPath == '/') {
				 var atFooter = currentTopPosition > 100; 
			       }
			       

			       // console.log(atFooter);

	       if (atFooter) { // reached footer		 
		 /* if (typeof usingIe != 'undefined' && usingIe) {
		   console.log(  document.getElementById('bodyArea')  );
		   var distanceDesired = (document.getElementById('bodyArea').offsetLeft - 30);
		 } else {
		   console.log(   $('#bodyArea').position().left  );
		   var distanceDesired = ($('#bodyArea').position().left - 30);
		 }*/
		 var distanceDesired = ($('#bodyArea').position().left - 30);
                     var distanceDesired = '-' + distanceDesired + 'px';
									
		   var distanceFromBottom = ($('#footer').height() + 50);
		     var distanceFromBottom = distanceFromBottom + 'px';

		     $('#bodyArea a.btn.underground').css('bottom', distanceFromBottom);
		     $('#bodyArea a.btn.underground').css('position', 'absolute');
		     $('#bodyArea a.btn.underground').css('right', distanceDesired);


		     $('#bodyArea a.btn.underground').addClass('surface');
		   } else {
		
                
		     if (currentUrlPath == '/') {
                        var bottom = 'bottom:130px';
                     } else {
		       var bottom = 'bottom: 50px';
		     }
                
		   $('#bodyArea a.underground').attr('style', '');
		        $('#bodyArea a.underground').attr('style', bottom); 
		
	       
		     $('#bodyArea a.btn.underground').removeClass('surface');
	       }
});
