/**
 * 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 = 55;
 
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);
			//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)+"px";
				}else if (navigator.userAgent.indexOf('MSIE 8') != -1) {
					ctArea.style.paddingTop = (fixHeight-55)+"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)+"px";
				}else if (navigator.userAgent.indexOf('MSIE 8') != -1) {
					ctArea.style.paddingTop = (fixHeight-55)+"px";
				}
			}
		}
	}catch(e) {}
}

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


