﻿function adjustCenteredContent(elementID,styleSheetName) {
	if(!document.styleSheets) { return; }

	var elem = document.getElementById(elementID);
	if(!elem) { return; }

	var styleSheet;
	for(var i = 0; i < document.styleSheets.length; i++) {
	    if (document.styleSheets[i].href == null) { continue; }
        if(document.styleSheets[i].href.indexOf(styleSheetName) != -1) { styleSheet = document.styleSheets[i]; }	}
	if(!styleSheet) { return; }

	var styleRule;
	var rules = styleSheet.cssRules || styleSheet.rules;
	for(var i = 0; i < rules.length; i++) {
		if(rules[i].selectorText == "#" + elementID) { styleRule = rules[i]; }
	}
	if(!styleRule) { return; }

	var de = document.documentElement;

	// fix top
	var viewHeight = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	if (viewHeight < elem.offsetHeight) {
		elem.style.top = 0;
		elem.style.marginTop = 0;
	} else {
		elem.style.top = styleRule.style.top;
		elem.style.marginTop = styleRule.style.marginTop;
	}

	// fix left
	var viewWidth = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	if (viewWidth < elem.offsetWidth) {
		elem.style.left = 0;
		elem.style.marginLeft = 0;
	} else {
		elem.style.left = styleRule.style.left;
		elem.style.marginLeft = styleRule.style.marginLeft;
	}
}

if(Event.observe) {
	Event.observe(window, "load", function(){ adjustCenteredContent("homeContent","root.css"); });
	Event.observe(window, "resize", function(){ adjustCenteredContent("homeContent","root.css"); });
	Event.observe(window, "load", function(){
		Event.observe(window, "mousemove", function(e){
			if(e == null) { e = window.event; }
			if(!window.objBabyRuth) { objBabyRuth = document.getElementById('objBabyRuth'); }
			if(objBabyRuth && typeof objBabyRuth.closeNav == 'function') {
				var pos = getPosition(objBabyRuth);
				if(e.clientY < pos[0] || e.clientX > pos[1] || e.clientY > pos[2] || e.clientX < pos[3]) {
					objBabyRuth.closeNav();
				}
			}
		});
	});
}

function getPosition(obj) {
	var curleft = curtop = curbottom = curright = 0;
	var objWidth = obj.offsetWidth;
	var objHeight = obj.offsetHeight;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	curbottom = curtop + objHeight;
	curright = curleft + objWidth;
	return [curtop,curright,curbottom,curleft];
}

function download() {

	if(arguments.length > 0) {
		if (arguments[0].match(/.jpg$/)) {
			var downloadPopup = window.open('Downloads/Wallpaper.aspx?filename=' + arguments[0],'downloadPopup','width=640,height=480,scrollbars=1');
			downloadPopup.focus();
		}

		if (arguments[0].match(/.zip$/)) {
			if(!$('downloadFrame')) { createDownloadFrame(); }
			$('downloadFrame').setAttribute('src', 'Downloads/Screensavers/' + arguments[0]);
		}

		if (arguments[0].match(/.pdf$/)) {
			var pdfPopup = window.open('Downloads/Scorecard/' + arguments[0],'pdfPopup');
			pdfPopup.focus();
		}
	}
}

function createDownloadFrame() {
	var downloadFrame = document.createElement('iframe');
	downloadFrame.setAttribute('id', 'downloadFrame');
	downloadFrame.setAttribute('width', '0');
	downloadFrame.setAttribute('height', '0');
	document.getElementsByTagName("body")[0].appendChild(downloadFrame);
}

function gotoExternal() {
	if(arguments.length > 0) {
		openExitPopup(arguments[0]);
	}
}

function openExitPopup() {
	if(arguments.length > 0) {
		var exitPopup = window.open('Exit.aspx?for=' + arguments[0],'exitPopup','width=863,height=470,scrollbars=1,resizable=1,menubar=1,toolbar=1,location=1,status=1');
		exitPopup.focus();
	}
}
