/**
 * This fixes the cutting off of the header when a hash is in the url.
 * @author: Tizian Schmidlin <st@cabag.ch>
 */

 // Set the height to fix. This might change from website to website.
var fixHeight = 30;
try {
	jQuery.noConflict();
}catch(e){}
function fixHash(href) {
	// As some browsers may not support regular expressions, catch it if an error is thrown, else it could kill everything else
	try {
		if(!href) {
			href = window.location.href;
		}
			// Check if there is a # in the href and if it is a non IE or IE >= 8
		if(/[^#]*#.*/.test(href) && (navigator.appName != 'Microsoft Internet Explorer'  || navigator.userAgent.indexOf('MSIE 8') != -1)) {
			
				// now get the id or anchor name
			hashEl = href.substring(window.location.href.indexOf('#')+1);
			
			// SS: 2011-10-11 - check if it is a indexed_search link where no margin modification is needed then a anchor id could not be found at this point
			indexedSearchSwitch = document.getElementById('linktoothermethoode');
			if(!indexedSearchSwitch) {
			
				//window.alert($$('a [name="'+hashEl+'"]'));
					// if it is an anchor
				if($$('a [name='+hashEl+']')) {
					ctArea = $$('#midCol .ctArea')[0];
					// Remove all top margins
					ctArea.style.marginTop="0px";
					// Replace it by some top padding
					ctArea.style.paddingTop=fixHeight+"px";
					if(navigator.vendor == 'Apple Computer, Inc.') {
						ctArea.style.paddingTop=(fixHeight-1)+"px";
					}else if (navigator.userAgent.indexOf('MSIE 8') != -1) {
						ctArea.style.paddingTop = (fixHeight-11)+"px";
					}
					// As the anchor check sometimes doesn't work in IE 8, do this
				}else if(document.getElementById(hashEl) && !$$('a [name='+hashEl+']')) {
					ctArea = $$('#midCol .ctArea')[0];
					
					// Remove all top margins
					ctArea.style.marginTop="0px";
					
					// Replace it by some top padding
					ctArea.style.paddingTop=fixHeight+"px";
						// Actually in Safari, the space isn't the same by 2px
					if(navigator.vendor == 'Apple Computer, Inc.') {
						ctArea.style.paddingTop=(fixHeight-1)+"px";
					}else if (navigator.userAgent.indexOf('MSIE 8') != -1) {
						ctArea.style.paddingTop = (fixHeight-11)+"px";
					}
				}
			}
		}
	}catch(e) {}
}

document.observe('dom:loaded', 
	function(e) {
		fixHash();
		if($$('#midCol .ctArea a')) {
			$$('#midCol .ctArea a').each(
				function(el) {
					if(/#/.test(el.href)) {
						$(el).observe('mouseup',
							function(ev) {
								fixHash(el.href);
							}
						);
					}
				}
			);
		}
	}
);


