﻿/* Queue the Site Load Events
===================================================================================== */

addLoadEvent(initMenu) ;
addLoadEvent(initHelp) ;
addLoadEvent(layoutWin);

/* Window Size Layout
===================================================================================== */
window.onresize = layoutWin;

function layoutWin(){
  var width;
	if (self.innerWidth) // all except Explorer
	{
		width = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
		// Explorer 6 Strict Mode
	{
		width = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		width = document.body.clientWidth;
	}

  var banners = getElementsByClass('bannersHorz', null, 'div') ;
  if (banners == '') {
		banners = getElementsByClass('bannersVert', null, 'div') ;
	}
  if (banners == '') return ;
  banners = banners[0] ;
  
  if (width < 950){
    if (banners.className != 'bannersHorz') {
      banners.className = 'bannersHorz'
    }
  } else{
    if (banners.className != 'bannersVert') {
      banners.className = 'bannersVert'
    }
  }
}

/* Search help tooltips
===================================================================================== */
function initHelp() {
	var tts = getElementsByClass('tooltip', null, 'label') ;
	if (tts == '') return ;
	for (i = 0; i < tts.length; i++) {
		var tt = tts[i] ;
		var tti = nextElement(tt) ;
		if (tt.parentNode.className != 'DataList') {
			tti.style.width = '160px' ;
		}
		var tip = tt.getElementsByTagName('em')[0] ;
		var img = document.createElement('img') ;
		img.src = '/Images/help.gif' ;
		img.className = 'help' ;
		img.tip = tip ;
		img.on = false ;
		insertAfter(tt.parentNode, img, tti) ; 
    img.onmouseover = toggleTip ;
    img.onmouseout = toggleTip ;
    tip.onmouseover = toggleTip ;
    tip.onmouseout = toggleTip ;
  }
}
var t ;
var curTip ;
function toggleTip(e) {
	if (!e) e = window.event;
	if (this.tip) {
		if (this.on == true) {
	// Hide Tip if mouse out of img
			curTip = this.tip
			t = setTimeout('animTip("none")', 500) ;
			this.on = false ;
		} else {
	// Show Tip if mouse over img
			clearTimeout(t) ;
			curTip = this.tip
			t = setTimeout('animTip("block")', 100) ;
			this.on = true ;
		}
	} else {
	// Dont hide tip if mouse over tip
		if (e.type == 'mouseover') {
			clearTimeout(t) ;
		} else {
	// Hide tip if mouse out of tip
			curTip = this
			t = setTimeout('animTip("none")', 500) ;
		}
	}
}
function animTip(display) {
	curTip.style.display = display ;
}
function nextElement(obj) {
	while (obj.nextSibling.nodeType != '1') {
		obj = obj.nextSibling;
	}
	return obj.nextSibling;
}
function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}

/* Build the menu
===================================================================================== */
var menu ;
function initMenu() {
	menu = getElementsByClass('BrecklandMenu', null, 'ul') ;
	if (menu == '') return ;
	var lis = menu[0].getElementsByTagName('li') ;
	var t = lis.length ;
	for (i = 0; i < lis.length; i++) {
		lis[i].firstChild.onclick = toggleMenu ;
		lis[i].style.zIndex = t - i
	}
}
function toggleMenu() {
	sub = this.parentNode.getElementsByTagName('ul') ;
	if (sub.length == 0) {
		return true ;
	}	else {
		sub[0].style.left = (parseInt(sub[0].style.left) == '0' ? '-8888px' : '0') ;
		if ((menu.open != null) && (menu.open != sub[0])) {
			menu.open.style.left = '-8888px' ;
			appendIframe(menu.open)	;
		}
		appendIframe(sub[0])	;
		menu.open = sub[0] ;
		return false ;
	}
}

/* Hack for IE selects above all over elements
===================================================================================== */
function appendIframe(e) {
	if (e.parentNode.getElementsByTagName('iframe').length == 0) {
		var ieMat=document.createElement('iframe');
		if(document.location.protocol == "https:")
			ieMat.src="//0";
		else if(window.opera != "undefined")
			ieMat.src="";
		else
		ieMat.src="javascript:false";
		ieMat.scrolling="no";
		ieMat.frameBorder="0";
		ieMat.style.width=e.offsetWidth +"px";
		ieMat.style.height=e.offsetHeight +"px";
		ieMat.style.zIndex="-1";
		ieMat.style.position="absolute";
		ieMat.style.left=e.offsetLeft+"px";;
		ieMat.style.top=e.offsetTop+"px";;
		e.parentNode.appendChild(ieMat);
		e.style.zIndex="2";
	}	else {
		e.parentNode.removeChild(e.parentNode.getElementsByTagName('iframe')[0]) ;
	}
}
