$(document).ready(function(){	
	//Assign position dynamic position for bookmark when HTML loaded
	/*
		Calculation for dynamic positioning
		position = (window's height - (bookmark div's height * 4.5))
	*/	
	
	hideBookmark();
	//Pre-define bookmark position
	$("#bookmark").css("top", ($("#masterContentBox").height()-30));
	
	$(window).bind("load", function(){		
		//Hide Anchor if content is short
		var isExceed = hideBookmark();
		
		if(isExceed){
			//Trigger bookmarkPositionAllocation on load complete
			bookmarkPositionAllocation(); 
		
			//Trigger bookmarkPositionAllocation if window resize
			$(window).resize(function(){
				hideBookmark();
				bookmarkPositionAllocation();
			});
			
			//Trigger bookmarkPositionAllocation if scroll-bar scrolling
			$(window).scroll(function(){		
				bookmarkPositionAllocation();
			});
		}
	});
	
	//Assign bookmark position follow scroll bar
	function bookmarkPositionAllocation(){
		var currentContentHeight = $("#masterContentBox").height();		
		var currentScrollValue = $(window).scrollTop();
		var extendHeight = ($(window).height() - ($("#bookmark").height() * 4.5));
		
		if((currentScrollValue+extendHeight) < 130){
			$("#bookmark").stop().animate({
				top: "130px"
			});
		}
		else{
			if(currentScrollValue == 0){
				$("#bookmark").stop().animate({
					top: extendHeight+"px"
				});
			}
			else if((currentScrollValue+extendHeight) < (currentContentHeight-30)){			
				$("#bookmark").stop().animate({
					top: ($(window).scrollTop()+extendHeight)+"px"
				}, 1000);
			}		
			else{			
				$("#bookmark").stop().animate({
					top: (currentContentHeight-30)
				});
			}	
		}
	}
	
	//Hide bookmark
	function hideBookmark(){
		var thisContent = "";
		var name = "content";	
		var urlParameter = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);	

		if(urlParameter != null){
			var thisContent = urlParameter[1];
		}
		
		if($("#masterContentBox").height() < $(window).height()){
			$("#bookmark").css("display", "none");
			return false;
		}
		else if(thisContent == "activities"){ //Hide the anchor boomarks, because overlay iframe are not compatible in crossbrowser at present.
			$("#bookmark").css("display", "none");
			return false;
		}
		else{
			$("#bookmark").css("display", "block");
			return true;
		}
	}
});
