function initFormElements() {	
	var tagNames = ["INPUT", "TEXTAREA"];
	for (var j = 0; j < tagNames.length; j++) {
	    var formElements = document.getElementsByTagName(tagNames[j]);
	    for (var i = 0; i < formElements.length; i++) {
	    	if (formElements[i].type && formElements[i].type != "submit" &&
	    		 formElements[i].type != "checkbox") {
			    formElements[i].onfocus = function(e) {
			    	if (!e) {
			    		var e = window.event;	
			    	}
			    	var sender = (e.target?e.target:e.srcElement);
			        sender.className = "focused";
			    }     
			    formElements[i].onblur = function(e) {
			    	if (!e) {
			    		var e = window.event;	
			    	}	    	
			    	var sender = (e.target?e.target:e.srcElement);
			        sender.className = "";
			    }
	    	}
	    }
	}
}

function initEmailLinks() {
    var aElements = document.getElementsByTagName("A");
    for (var i = 0; i < aElements.length; i++) {
        if (aElements[i].className == "email-link") {
            aElements[i].target = "";
            var decodedEmailAddress = aElements[i].href.replace(/_dot_/g, ".");
            decodedEmailAddress = decodedEmailAddress.replace(/_at_/g, "@");
            decodedEmailAddress = decodedEmailAddress.substring(0, decodedEmailAddress.indexOf("?"));
            aElements[i].href = decodedEmailAddress;
            aElements[i].title = "Send an email to " + decodedEmailAddress.substring(7);
        } else if (aElements[i].className == "comment-email-link") {
            var decodedEmailAddress = aElements[i].href.replace(/_dot_/g, ".");
            decodedEmailAddress = decodedEmailAddress.replace(/_at_/g, "@");
            aElements[i].href = decodedEmailAddress;
			aElements[i].firstChild.nodeValue = decodedEmailAddress.replace(/mailto:/g, "");            
        }
    }
}

function initCodeBlocks() {
	var codeElements = document.getElementsByTagName("CODE");
	for (var i = 0; i < codeElements.length; i++) {
		codeElements[i].onclick = function(e) {
			if (!e) { var e = window.event; }
			var sender = (e.target?e.target:e.srcElement);
			while (sender.tagName != "CODE") {
				sender = sender.parentNode;
			}
			sender.className = "selectable";
			sender.onclick = null;
			var spanElements = sender.getElementsByTagName("SPAN");
			for (var i = spanElements.length - 1; i >= 0; i--) {
				if (spanElements[i].className == "line") {
				sender.removeChild(spanElements[i]);
				}
			}
			if (document.all) {
				var range = document.body.createTextRange();
				range.moveToElementText(sender);
				range.select();
			} else { 
				var range = document.createRange();
				range.selectNode(sender);
				var selection = window.getSelection();
				selection.removeAllRanges();
				selection.addRange(range);
	  		}
		}
	}
}

function initAutoGrow() {
	var commentTextElement = document.getElementById("comment_text");
	if (!commentTextElement) {
		return;
	}	
	
	commentTextElement.onkeydown = function() {
		if (commentTextElement.value.length > 250) {
			commentTextElement.style.width = "600px";
			commentTextElement.style.height = "300px";
			commentTextElement.onkeydown = null;
		}
	}
}



var menuVisible = false;
var menuHideTimer = null;
var activeMenu = null;

function hideMenu() {
	activeMenu.style.display = "none";	
	menuVisible = false;
}

function hideMenuDelayed() {
    window.clearTimeout(menuHideTimer);
    menuHideTimer = window.setTimeout(hideMenu, 100);
}

function clearMenuTimer() {
    window.clearTimeout(menuHideTimer);
}

function displayMenu(menu) {
	if (menuVisible) {
	    window.clearTimeout(menuHideTimer);
		hideMenu();
	}
	
	menuVisible = true;
	
	activeMenu = menu;
	
	var innerSize = {};
	var scrollPos = {};
	var linkPos = {};
	
	if (self.innerHeight) {
		innerSize.width = self.innerWidth;
		innerSize.height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		innerSize.width = document.documentElement.clientWidth;
		innerSize.height = document.documentElement.clientHeight;
	}
  
	if (self.pageYOffset) {
		scrollPos.x = self.pageXOffset;
		scrollPos.y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrollPos.x = document.documentElement.scrollLeft;
		scrollPos.y = document.documentElement.scrollTop;
	}

	linkPos.x = 0;
	linkPos.y = 0;
	var tempObj = this;
	while (tempObj.offsetParent) {
		linkPos.x += tempObj.offsetLeft;
		linkPos.y += tempObj.offsetTop;
		tempObj = tempObj.offsetParent;
	} 
  
  	menu.style.left = linkPos.x + "px";
  	menu.style.top = linkPos.y + this.offsetHeight + "px";
    
	menu.style.display = "block";    

    if (linkPos.y + menu.offsetHeight - scrollPos.y > innerSize.height) {
		menu.style.top = (linkPos.y - menu.offsetHeight) + 'px';      
    }    
}

function intCommentUserLinks() {
    var aElements = document.getElementsByTagName("A");

	var clickHandler = function(url) {
		return function() {
			if (!window.confirm("Your are now leaving this website. The author of this website is not responsible for the contents of the site you are about to see.\r\n\r\n" +
				"The url of the website is: " + url + "\r\n\r\nWould you like to continue?")) {
				return false;
			}
		} 
	}

    for (var i = 0; i < aElements.length; i++) {
        if (aElements[i].className == "comment-user-link") {
            aElements[i].onclick = clickHandler(aElements[i].href);
        }
    }	
}

function initMenus() {
    var menuLinks = document.getElementsByTagName("A");

    for (var i = 0; i < menuLinks.length; i++) {
        if (menuLinks[i].className == "bookmark" || menuLinks[i].className == "comment") {
		    var menu = menuLinks[i];
		    while (menu != null && menu.className != "bookmark-menu" && menu.className != "comment-menu") {
		    	menu = menu.nextSibling;
		    }
		
			if (menu != null && (menu.className == "bookmark-menu" || menu.className == "comment-menu")) {
	            menuLinks[i].onmouseover = (function genHandler(menu) { 
	    			return function() {
	    				displayMenu.call(this, menu);
	    			}
	    		})(menu);
			    
	    		menu.onmouseover = clearMenuTimer;
	    		menu.onmouseout = hideMenuDelayed;

	            menuLinks[i].onmouseout = hideMenuDelayed;
			}
        }
    }
}

window.onload = function() {
    initEmailLinks();
    initFormElements();
    initCodeBlocks();
    initAutoGrow();
    intCommentUserLinks();
    initMenus();
}