document.getElementsByClassName = function(className) {
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
  
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }
  
  return elements;
}

function popitup(url, w, h, scrolling, wtitle)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	
    newwindow=window.open(url, wtitle,'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrolling);
    if (window.focus) {newwindow.focus()}
    return false;
}

function newImage(src)
{
	if (!document.createElement) return false;
	var img = document.createElement('img');
	img.setAttribute('src', src);
	return img;
}

function swap()
{
	if (this.getAttribute('src') != this.mouseover.getAttribute('src'))
	{
		this.oldsrc = this.getAttribute('src');
		this.setAttribute('src', this.mouseover.getAttribute('src'));
	}
	else this.setAttribute('src', this.oldsrc);
}

function init()
{
	if (!document.createElement) return;
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	
	var lists = document.getElementsByClassName('omo');
	
	for (var j = 0; j < lists.length; j++)
	{
		var items = lists[j].getElementsByTagName('li');
		
		for (var i = 0; i < items.length; i++)
		{
			// skip list items which do contain a nav item image
			if (items[i].getElementsByTagName('img').length != 1) continue;
			
			// find image
			var img = items[i].getElementsByTagName('img')[0];
			
			// skip active
			if (img.src.indexOf('_active.png') != -1) continue;
			
			// get filename
			var filename = img.src;
			
			var suffix = filename.substring(filename.lastIndexOf('.'));
			var preFilename = filename.substring(0,filename.lastIndexOf('.')) + "_active" + suffix;
			
			// create preload rollover property
			img.mouseover = newImage(preFilename);
			
			// attach mouse over events
			img.onmouseover = swap;
			img.onmouseout = swap;
		}
	}
	
	
	var contactimg = document.getElementById('contactimg');
	
	if (contactimg)
	{
		//
		var wrap = document.getElementById('contactwrap');
		var alink = contactimg.parentNode;
		var href = alink.href;
		
		var section = document.body.getAttribute('id');
		
		href = href + "&section=" + section;
		
		contactimg.onclick = function() { popitup(href, 535, 740, 'no', 'contact'); }
		
		wrap.removeChild(alink);
		wrap.appendChild(contactimg);
	}
	
	var engrow = document.getElementById('engrow-link');
	
	if (engrow)
	{
		var href = engrow.href;
		var html = engrow.firstChild.nodeValue;
		
		var newlink = document.createElement('a');
		var txtnode = document.createTextNode(html);
		
		newlink.appendChild(txtnode);
		newlink.className = 'popup-link';
		
		newlink.onclick = function() { popitup(href, 569, 485, 'no', 'engrow'); }
		
		engrow.parentNode.replaceChild(newlink, engrow);
		
	}
	
}

window.onload = init;