//Hover function ie
var siteBaseUrl = '/';
var onloadEvents = new Array();

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() {
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


if (document.all && window.attachEvent){
	addToOnload('attachButtonHover()');	
}

var attachButtonHover = function(){
	var list = document.getElementsByTagName('button');
	for (var i=0; i<list.length; i++) {
		list[i].onmouseover=function() {
			this.className+=" buttonHover";
		}
		list[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" buttonHover\\b"), "");
		}
	}
}



var HeaderReplacement = {
	//List of images
	list : [],
		
	add : function(cnt, val){
		HeaderReplacement.list[cnt] = val;	
		var header = document.getElementById(val.id);	
		if (!header){
			return 0;
		}
		var bits = header.id.split('_');
		var param = HeaderReplacement.list[Number(bits[1])];
		if(!param) return 1;
		//If height is > than 37 give it another class name
		header.className = header.className+' imageHeading';
		var bg = param.url;
		var bgString = "url('"+bg+"') no-repeat 0 0";
		header.style.background = "url('"+bg+"') no-repeat 0 0";
		header.style.height = param.imgHeight+'px';
		
		return 1;
	}
}

function setBaseUrl(baseUrl){
	siteBaseUrl = baseUrl;
}

function closePopup() {
	e = document.getElementById('imgPopup');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function popupKeyListener() {document.onkeypress = processKeyPress; }

function processKeyPress(e){
	var e = window.event  || e;
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
}

function attachOnClickImage(id, imgPath , altText, imgWidth, imgHeight){
	element = document.getElementById(id);
	element.style.cursor = 'pointer';
	var imageText = altText || '';		
	var imgWidth = Number(imgWidth);
	var imgHeight = Number(imgHeight);		
	element.removeAttribute("href");
	element.onclick = function (){
		//check if this element exists.
		e = document.getElementById('imgPopup');
		if (!e){
			popupKeyListener();
			var myDiv = document.createElement("DIV");
			myDiv.id = "imgPopup";
			myIframe = document.createElement('iframe');
			myIframe.src = siteBaseUrl+'blank.php?image='+encodeURIComponent(imgPath)+'&altText='+encodeURIComponent(imageText);			
			myIframe.scrolling = 'no';
			myIframe.width = imgWidth;
			myIframe.height = imgHeight+45;					
			myIframe.frameBorder = "0";
			//Append
			myDiv.appendChild(myIframe);
			myDimensions = getWindowDimensions();
			myWidth = myDimensions[0];
			myDiv.style.width = imgWidth;
			//calc left 
			var boxwidth = imgWidth+18;
			myDiv.style.left = (myWidth / 2) - (boxwidth / 2) + "px";
			document.body.appendChild(myDiv);
		}
		return false;
	}
}
function getWindowDimensions(){
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

var dotdash = {	
	
	feedbackAnimation : function(element, state){
		var eList = element.split(" ");
		
		for (var y = 0; y < eList.length ; y++){
			var e = document.getElementById(eList[y]);
			if (e){
				if (state == 'processing'){
					dotdash.displayBusySignal(e);	
				} else if (state == 'ready'){
					dotdash.clearBusySignal(e);	
				}
			}
		}
	},displayBusySignal: function(pNode){
		
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';
		
		var containerWidth = pNode.offsetWidth;
		var containerHeight = pNode.offsetHeight;
		
		var busySignal = document.getElementById(idPrefix+'busySignal');
		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
		
		if (!busySignal){
			var busySignalContainer = document.createElement('div');
			busySignalContainer.id = idPrefix+'busySignalContainer';
			
			var busySignal = document.createElement('div');
			busySignal.id = idPrefix+"busySignal";
			busySignal.className = "busySignal";
			busySignal.style.paddingBottom = '10px';

			pNode.parentNode.insertBefore(busySignalContainer, pNode);
			var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
			busySignalContainer.appendChild(busySignal);
		}
		busySignal.style.filter = 'alpha(opacity=50)';
		busySignal.style.opacity = '.5';
		busySignal.style.height = containerHeight + "px";
		busySignal.style.width = containerWidth+'px';
	},
	clearBusySignal: function (pNode){
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';
		
		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');	
		var busySignal = document.getElementById(idPrefix+'busySignal');
		busySignal.parentNode.removeChild(busySignal);
		busySignalContainer.parentNode.removeChild(busySignalContainer);
	}
}