$(document).ready(function(){

	//pretty url fix
	var window_loc = String(window.location);
	var windowAnchorIdx = window_loc.indexOf('#',0);
	if (windowAnchorIdx > -1){
		window_loc = window_loc.substring(0,windowAnchorIdx);
	}	
	if (window_loc.charAt(window_loc.length-1) == '/'){
		window_loc = window_loc.substring(0,window_loc.length-1);	
	}
	
	
	function getRootURL()
	{
		var baseURL = location.href;
		var rootURL = baseURL.substring(0, baseURL.indexOf('index.php', 0));
		return rootURL;
	}

	function getAnchorPrefix(curAnchor)
	{
		var anchor_prefix = curAnchor.substring(0, curAnchor.indexOf('#', 0));
		return anchor_prefix;
	}

	//runs a regular expression on all hrefs beginning with a #
	//if the href already has a prefix ie it is inlinking to an anchor in another page or pretty urls are being used (do not apply the fix)	
	$("a:regex([href,/\#/])").each(function(){		
		var hrefAnchorElement = $(this);		
		var curAnchorLink = hrefAnchorElement.attr("href");	
		var anchor_prefix = getAnchorPrefix(curAnchorLink);
		
		
		//if the anchor_prefix consists of the root url remove it
		if (anchor_prefix.indexOf(getRootURL() +"#") > 0){
			curAnchorLink = curAnchorLink.replace(getRootURL(), '');
			hrefAnchorElement.attr("href", window_loc + curAnchorLink);
		}
		//if the anchor_prefix is null (ie there is nothing precedding the anchor)
		//add the reference to the index page
		if (anchor_prefix == ""){
			hrefAnchorElement.attr("href", window_loc + curAnchorLink);
		}
		
					
	});	
});
