function pageInit() {
	searchBoxFocusBlurHandler();
	featureTableHandler();
}

function resizeBlogPageContent() {
	var innerContent = document.getElementById('inner-content');
	if (innerContent) {
		var innerContentMain = innerContent.getElementsByTagName('div')[0];
		if (innerContentMain) innerContent.style.height = innerContentMain.offsetHeight+'px';
	}
}

function TopMenu(id) {
	this._init = false;
	this.node = document.getElementById(id);
	var m = this;
	NG.addEventListener(this.node, 'mouseover', function (e) { m.open(e); });
}

TopMenu.prototype.open = function (event) {
	if (!this._init) {
		var m = this;
		NG.addEventListener(document, 'mouseover', function () { m.close(); });
		this._init = true;
	}
	var li = (event.target ? event.target : event.srcElement);
	while (li && li.nodeName != 'LI') li = li.parentNode;
	if (!li) { throw('Couldn\'t find <li>'); return; }
	this.close(li.parentNode);
	li.className += ' sel';
	if (!NG.hasClass(li, 'widthset')) {
		setSubLiWidth(li);
		li.className += '  widthset  ';
	}

	//fix for extra margin for IE6/7 by adding a new class. CSS will handle it later on 
	var prevSibling = li.previousSibling;
	if (prevSibling && NG.hasClass(li, 'parent-menu')) {
		li.className += (NG.hasClass(prevSibling, 'first')) ? '  first-remove-bottom-margin  ' : '  remove-bottom-margin  ';
	}

	NG.stopPropagation(event);
}

TopMenu.prototype.close = function (node) {
	if (!node) node = this.node;
	var l = node.getElementsByTagName('li');
	for (var i = 0; i < l.length; i++) {
		l[i].className = l[i].className.replace(/(^| )sel( |$)/, '');
		l[i].className = l[i].className.replace(/(^| )remove-bottom-margin( |$)/, '');
		l[i].className = l[i].className.replace(/(^| )first-remove-bottom-margin( |$)/, '');
	}
}

function SelectLocation(select, num_head_options, errmsg) {
	if (arguments.length <= 1) num_head_options = 0;
	if (select.selectedIndex >= num_head_options) {
		window.location = select.options[select.selectedIndex].value;
	} else {
		alert(errmsg);
	}
}

//set the li width to be the same as the longest sibling. Fix for IE7
function setSubLiWidth(parentLiElem) {
	try {
		var uls = parentLiElem.getElementsByTagName('ul');
		if (uls && uls.length > 0) {
			var ul = uls[0];
			var maxSubLiWidth = 0;
			for (var liIndex = 0; liIndex < ul.childNodes.length; liIndex++) {
				var subLi = ul.childNodes[liIndex];
				maxSubLiWidth = (subLi.offsetWidth > maxSubLiWidth) ? subLi.offsetWidth : maxSubLiWidth;
			}
			if (maxSubLiWidth > 0) {
				for (var liIndex = 0; liIndex < ul.childNodes.length; liIndex++) {
					var subLi = ul.childNodes[liIndex];
					subLi.style.width = maxSubLiWidth + 'px';
				}
			}
		}
	} catch(e) {}
}


function searchBoxFocusBlurHandler() {
	try {
		var b = document.getElementById('searchBox');
		NG.addEventListener(b, 'focus', function (e) {  
			if (b.value == 'Search our Site') b.value = '';
		});
		NG.addEventListener(b, 'blur', function (e) {  
			if (b.value == '') b.value = 'Search our Site';
		});
	}
	catch(e){}
}

function goToOtherFeatures(elem) {
	if (elem.selectedIndex > -1) {
		var url = elem.options[elem.selectedIndex].value;
		if (url != '0') {
			window.location = url;
		}
	}
}


function Noggin() {}

Noggin.Comment = {};
Noggin.Comment.maxReplyConfirmationLength = 30;

Noggin.Comment.Init = function() {
	this.CommentReplyHandler();
}

Noggin.Comment.CommentReplyHandler = function() {
	maxTextLength = this.maxReplyConfirmationLength;
	var commentID;

	var setReplyToBlog = function(elem) {
		try {
			document.getElementById('reply_confirmation').innerHTML = '';
			var formElem = document.getElementById('comment_reply_form');
			if (formElem != null && commentID != '') {
				formElem.parentid.value = '';
			}

			var url = window.location.href;
			if (url.substring(url.length - 6) != '#reply') {
				url += '#reply';
			}
			window.location.href = url;
			
		}
		catch(e){}
	}	

	var setReplyToComment = function(elem) {
		try {
			var commentID = '';
			var siblings = elem.parentNode.getElementsByTagName('input');
			if (siblings.length > 0 && siblings[0].name == 'commentid') {
				commentID = siblings[0].value;
			}

			var commentText = '';
			var pSiblings = elem.parentNode.getElementsByTagName('p');
			for (var i = 0; i < pSiblings.length; i++) {
				var pElem = pSiblings[i];
				if (pElem.className == 'comment_post_content' && commentText == '') {
					if (pElem.innerHTML.length > maxTextLength) {
						commentText = pElem.innerHTML.substring(0, maxTextLength) + ' ...';
					} else {
						commentText = pElem.innerHTML;
					}
				}
			}
			var replyConf = 'You are replying to comment  <i>' + commentText + '</i>' +
					'<br>Click <a href="#reply" id="reply_to_blog">here</a> if you want to reply to the blog instead';
			
			document.getElementById('reply_confirmation').innerHTML = replyConf;
			var replyLink = document.getElementById('reply_to_blog');
			if (replyLink != null) {
				NG.addEventListener(replyLink, 'click', function() { setReplyToBlog(this); } );
			}

			var formElem = document.getElementById('comment_reply_form');
			if (formElem != null && commentID != '') {
				formElem.parentid.value = commentID;

				var parentCommentNode = elem.parentNode.parentNode;
				if (parentCommentNode.parentNode.lastElementChild == parentCommentNode) {
					formElem.lastcomment.value = '1';
				} else {
					formElem.lastcomment.value = '';
				}
			}
			
				}
		catch(e){alert(e);}
			
	}
	var replyLinks = document.getElementsByTagName('a');
	for(var i = 0; i < replyLinks.length; i++) {
		if (replyLinks[i].className == 'reply_link') {
			NG.addEventListener(replyLinks[i], 'click', function() { setReplyToComment(this); } );
		}
	}

	var postCommentButton = document.getElementById('post_comment_button');
	if (postCommentButton != null) {
		NG.addEventListener(postCommentButton, 'click', function() { setReplyToBlog(this); } );
	}
}

Noggin.Comment.AdjustCommentBoxWidth = function() {
	var padding = 15;
	try {
		var c = document.getElementById('comment-comments');
		for (var nodeIndex = 0; nodeIndex < c.childNodes.length; nodeIndex++) {
			var node = c.childNodes[nodeIndex];
			if (node.className == 'comment_node') {
				var nodeWidth = node.offsetWidth;
				var indenterWidth = null;
				var commentNode = null;
				
				for (var subNodeIndex = 0; subNodeIndex < node.childNodes.length; subNodeIndex++) {
					var subNode = node.childNodes[subNodeIndex];
					if (subNode.className == 'comment_indenter') {
						indenterWidth = subNode.offsetWidth;
					} else if (subNode.className == 'comment') {
						commentNode = subNode;
					}
				}
				if (indenterWidth != null && commentNode != null) {
					commentNode.style.width = (nodeWidth - indenterWidth - (2*padding)) + 'px';
				}
			}
		}
	}
	catch(e) {}
}

function featureTableHandler() {
	var m = document.getElementById('ngcms-region-main');
	var tables = m.getElementsByTagName('table');
	for (var tIndex = 0; tIndex < tables.length; tIndex++) {
		if (NG.hasClass(tables[tIndex], 'feature-table')) {
			var links = tables[tIndex].getElementsByTagName('span');
			for (var lIndex = 0; lIndex < links.length; lIndex++) {
				var linkElem = links[lIndex];
				if (NG.hasClass(linkElem, 'expand')) {
					NG.addEventListener(linkElem, 'click', function(e){ hideShowFeatureTableDetail(e, true);});
					/*
					NG.addEventListener(linkElem, 'click', function(e){
						var targ = (e.target) ? e.target : e.srcElement;
						NG.delClass(targ.parentNode, /^feature-detail-container$/);
						NG.addClass(targ.parentNode, 'feature-detail-container-expanded');
					});
					*/
				} else if (NG.hasClass(linkElem, 'collapse')) {
					NG.addEventListener(linkElem, 'click', function(e){ hideShowFeatureTableDetail(e, false);});
					/*
					NG.addEventListener(linkElem, 'click', function(e){
						var targ = (e.target) ? e.target : e.srcElement;
						NG.delClass(targ.parentNode, /^feature-detail-container-expanded$/);
						NG.addClass(targ.parentNode, 'feature-detail-container');
					});
					*/
				}		
			}
		}
	}
}

function hideShowFeatureTableDetail(e, toExpand) {
	var targ = (e.target) ? e.target : e.srcElement;
	var parentDiv = (targ.tagName == 'SPAN') ? targ.parentNode : targ.parentNode.parentNode;
	if (toExpand) {
		NG.delClass(parentDiv, /^feature-detail-container$/);
		NG.addClass(parentDiv, 'feature-detail-container-expanded');
	} else {
		NG.delClass(parentDiv, /^feature-detail-container-expanded$/);
		NG.addClass(parentDiv, 'feature-detail-container');
	}
}

function adjustHomeRcolSpacing() {
	var m = document.getElementById('ngcms-region-main');
	var s = document.getElementById('ngcms-region-side');

	var learnAboutBox = null;
	for (var i = 0; i < m.childNodes.length && learnAboutBox == null; i++) {
		if (m.childNodes[i].tagName == 'DIV' && NG.hasClass(m.childNodes[i], 'has-border')) {
			learnAboutBox = m.childNodes[i];
		}
	}

	var recentWorkBox = null;
	var newsViewsBox = null;
	var infoBox = null;
	for (var i = 0; i < s.childNodes.length; i++) {
		if (s.childNodes[i].tagName == 'DIV') {
			if (NG.hasClass(s.childNodes[i], 'recent-work')) {
				recentWorkBox = s.childNodes[i];
			} else if (NG.hasClass(s.childNodes[i], 'news-views')) {
				newsViewsBox = s.childNodes[i];
			} else if (NG.hasClass(s.childNodes[i], 'info')) {
				infoBox = s.childNodes[i];
			}
		}
	}

	if (
			learnAboutBox != null && 
			infoBox != null &&
			recentWorkBox != null &&
			newsViewsBox != null &&
			learnAboutBox.offsetTop > infoBox.offsetTop
	) {
		var d = learnAboutBox.offsetTop - recentWorkBox.offsetHeight - newsViewsBox.offsetHeight;
		var recentWorkBottomMargin = (d % 2 == 0) ? (d/2) : Math.floor(d/2)+1;
		var newsViewsBottomMargin = Math.floor(d/2);
		recentWorkBox.style.marginBottom = recentWorkBottomMargin + 'px';
		newsViewsBox.style.marginBottom = newsViewsBottomMargin + 'px';
	}
}

