// the following code generously offered by Richard Cornford at bytes.com
var windowState = (function() {
	var readScroll = {scrollLeft:0,scrollTop:0};
	var readSize = {clientWidth:0,clientHeight:0};
	var readScrollX = 'scrollLeft';
	var readScrollY = 'scrollTop';
	var readWidth = 'clientWidth';
	var readHeight = 'clientHeight';
	
	function otherWindowTest(obj) {
		if((document.compatMode) && (document.compatMode == 'CSS1Compat') && (document.documentElement)) {
			return document.documentElement;
		} else if (document.body) {
			return document.body;
		} else {
			return obj;
		}
	};
	
	if ((typeof this.innerHeight == 'number') && (typeof this.innerWidth == 'number')) {
		readSize = this;
		readWidth = 'innerWidth';
		readHeight = 'innerHeight';
	} else {
		readSize = otherWindowTest(readSize);
	}
	if ((typeof this.pageYOffset == 'number') && (typeof this.pageXOffset == 'number')) {
		readScroll = this;
		readScrollY = 'pageYOffset';
		readScrollX = 'pageXOffset';
	} else {
		readScroll = otherWindowTest(readScroll);
	}
	
	return {
		getScrollX:function() {
			return (readScroll[readScrollX]||0);
		},
		getScrollY:function(){
			return (readScroll[readScrollY]||0);
		},
		getWidth:function(){
			return (readSize[readWidth]||0);
		},
		getHeight:function(){
			return (readSize[readHeight]||0);
		}
	};
})();

function getCenteredCoords(divWidth, divHeight) {
	// center alignment
	var viewPortWidth = windowState.getWidth();
	var viewPortHeight = windowState.getHeight();
	var horizontalScroll = windowState.getScrollX();
	var verticalScroll = windowState.getScrollY();
	var hPos = Math.round(horizontalScroll+((viewPortWidth-divWidth)/2));
	var vPos = Math.round(verticalScroll+((viewPortHeight-divHeight)/2));
	
	var coords = [hPos, vPos];
	
	return coords;
}

function showTermsBox(id) {
	var obj = document.getElementById(id);
	
	if (!obj) {
		alert("The information could not be displayed. Please email us at info@allemana.com for the appropriate information.");
	} else {
		centerBox(obj);
	}
}

function centerBox(obj) {
	if (obj) {
		obj.style.display = 'block';
		
		var coords = getCenteredCoords(obj.offsetWidth, obj.offsetHeight);
		var hPos = coords[0];
		var vPos = coords[1];
		
		hPos = (hPos < 0) ? 0 : hPos;
		vPos = (vPos < 0) ? 0 : vPos;
//	alert(hPos+" - "+vPos);
		obj.style.left = hPos+"px";
		obj.style.top = vPos+"px";
		obj.style.visibility = 'visible';
	}
}

function closeBox(id) {
	var obj = document.getElementById(id);
	
	if (obj) {
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
	}
}

function openTermsWin(page) {
	var url = '/';
	url += (page == 'privacy') ? 'privacysmall.php' : 'termssmall.php';
	
	var w = 600;
	var h = 600;
	
	var hPos = Math.round(screen.width/2)-Math.round(w/2);
	var vPos = Math.round(screen.height/2)-Math.round(h/2);
	
	var newwin = window.open(url, 'termswin', 'width='+w+', height='+h+', top='+vPos+', left='+hPos+', scrollbars=yes, status=no, menubar=no, location=no, toolbar=no, resizable=yes, dependent=yes');
	
}

function setRegFieldFocus(obj) {
	if (obj) {
		obj.className = 'regfieldhi';
	}
}

function setRegFieldBlur(obj) {
	if (obj) {
		obj.className = 'regfield';
	}
}

function toggleProdDesc(id) {
	var obj = document.getElementById('prod'+id);
	
	if (obj) {
		flag = obj.style.display;
		obj.style.display = (flag == 'block') ? 'none' : 'block';
		
		var rmobj = document.getElementById('rm'+id);
		
		if (rmobj) {
			rmobj.innerHTML = (flag == 'block') ? 'Read More' : 'Close';
		}
	}
}

function showHelp(page) {
	var url = '/showhelp.php?p='+unescape(page);
	
	var w = 560;
	var h = 400;
	
	var hPos = Math.round(screen.width/2)-Math.round(w/2);
	var vPos = Math.round(screen.height/2)-Math.round(h/2);
	
	var newwin = window.open(url, 'helpwin', 'width='+w+', height='+h+', top='+vPos+', left='+hPos+', scrollbars=yes, status=no, menubar=no, location=no, toolbar=no, resizable=yes, dependent=yes');
	
}

gTimer = null;
gNavID = 'subnav-empowered'; // hard coded for now
function setNavOver(id) {
	var obj = document.getElementById(id);
	
	if (obj) {
		if (gTimer) {
			clearTimeout(gTimer);
		}
		
		obj.style.visibility = 'visible';
	}
}

function setNavOut(id) {
	//gNavID = id;
	gTimer = setTimeout("hideNav()",800);
}

function hideNav() {
	var obj = document.getElementById(gNavID);
	
	if (obj) {
		obj.style.visibility = 'hidden';
	}
}

function showFeatures() {
	showTermsBox('features');
}

function showImage(imgname,w,h) {
	if (!w || isNaN(w)) {
		w = 500;
	}
	
	if (!h || isNaN(h)) {
		h = 500;
	}
	
	var url = "/popupimg.php?img="+escape(imgname);
	
	var hPos = Math.round(screen.width/2)-Math.round(w/2);
	var vPos = Math.round(screen.height/2)-Math.round(h/2);
	
	var imgwin = window.open(url, 'imgwin', 'width='+w+', height='+h+', top='+vPos+', left='+hPos+', scrollbars=yes, status=no, menubar=no, location=no, toolbar=no, resizable=yes, dependent=yes');
	
}