function getElementYpos(el){
	var y = 0;
	while(el.offsetParent){
		y += el.offsetTop
		el = el.offsetParent;
	}
	return y;
}

function scrollUpByHeader() {
	$(window).unbind('scroll', scrollUpByHeader);
	//confirm("Called scrollUpByHeader");
	var header = document.getElementById("header-image");
	var headerHeight = 140;
	targetObject = document.getElementById(document.location.hash.substr(1));
	if (targetObject) {
		objectPos = getElementYpos(targetObject);
		setTimeout(function() {
			//alert("Scrolling... to ");
			window.scrollTo(0, objectPos-headerHeight);
			//alert("Done");
		}, 10);
	}
	return false;
}

if (window.location.hash) {
	$(window).scroll(scrollUpByHeader);
}


function insertEmail(name, host, ext, isLink, linkContent) {
	var email = name + "@" + host + "." + ext;
	if (document.write) {
		if (isLink) {
			linkContent = linkContent ? linkContent : email;
			document.write('<a href="mailto:'+email+'">'+linkContent+'</a>');
		} else {
			document.write(email);
		}
	}
}


// Functions for defining CSS styles

function createStyleNode() {
	// Returns a reference object that can be passed to the other functions
	
	// DOM-based browsers
	var styleNode = document.createElement("style");
	styleNode.setAttribute("type", "text/css");
	styleNode.setAttribute("media", "screen"); 
	document.getElementsByTagName("head")[0].appendChild(styleNode);
	
	// IE workaround
	var styleNodeIE = null;
	if 	(	document.styleSheets 
			&& document.styleSheets.length > 0
			&& document.styleSheets[0]
			&& typeof(document.styleSheets[0].addRule) == "object"
		) {
		styleNodeIE = document.styleSheets[document.styleSheets.length - 1];
	}
	
	return { DOM: styleNode, IE: styleNodeIE };
}

function clearStyleNode(styleRef) {
	// DOM-based browsers
	while (styleRef.DOM.firstChild)
		styleRef.DOM.removeChild(styleRef.DOM.firstChild);
	
	// IE
	if (styleRef.IE) {
		while (styleRef.IE.rules.length) {
			styleRef.IE.removeRule();
		}
	}
}

function appendCSSRuleToStyle(styleRef, selector, declaration) {
	if (!styleRef.IE) {
		// DOM-based browsers
		styleRef.DOM.appendChild(document.createTextNode(selector + " {" + declaration + "}"));
	} else {
		// IE, who else?
		styleRef.IE.addRule(selector, declaration);
	}	
}
