//DOM Core
document.getElementsByClassName = function(className) {
	var matches = new Array();
	var regex = new RegExp("\\b"+className+"\\b");
	var elements = this.getElementsByTagName("*");
	for (var i = 0; i < elements.length; i++) {
		if (regex.test(elements[i].className)) {
			matches.push(elements[i]);
		} //end regex.test(elements[i].className) IF
	} //end var i = 0; i < elements.length; i++ FOR
	return(matches);
};
function emptyElement(target) {
	while (target.hasChildNodes()) {
		target.removeChild(target.firstChild);
	} //end target.hasChildNodes() WHILE
} //end emptyElement
function makeStrong(text) {
	var strong = document.createElement("strong");
	strong.appendChild(document.createTextNode(text));
	return (strong);
} //end makeStrong
function makeBr() {
	var br = document.createElement("br");
	return (br);
} //end makeBr
function autoShade() {
	var tables = document.getElementsByTagName("table");
	for (i = 0; i < tables.length; i++) {
		if (tables[i].getAttribute("autoshade") != "no") {
			var rows = tables[i].getElementsByTagName("tr");
			for (j = 0; j < rows.length; j++) {
				if (j % 2 == 0) {
					var current = rows[j].className;
					rows[j].className = "shaded "+current;
				} //end j % 2 == 0 IF
			} //end j = 0; j < rows.length; j++ FOR
		} //end tables[i].getAttribute("autoshade") != "no" IF
	} //end i = 0; i < tables.length; i++ FOR
} //end autoShade

//Text Size Core
var currentTextSize = 2;
var sizes = {
	"main" : new Array(12,14,16,18,20),
	"footer" : new Array(10,12,14,16,18),
	"small" : new Array(10,12,14,16,18)
};
function changeTextSize(amount) {
	currentTextSize += amount;
	if (currentTextSize < 0) {
		currentTextSize = 0;
	} //end currentTextSize < 0 IF
	if (currentTextSize > 4) {
		currentTextSize == 4;
	} //end currentTextSize > 4 IF
	
	document.getElementsByTagName("body")[0].style.fontSize = sizes.main[currentTextSize]+"px";
	document.getElementById("footer").style.fontSize = sizes.footer[currentTextSize]+"px";
	var smalls = document.getElementsByClassName("small");
	for (i = 0; i < smalls.length; i++) {
		smalls[i].style.fontSize = sizes.small[currentTextSize]+"px";
	} //end i = 0; i < smalls.length; i++ FOR
} //end changeTextSize

//Google Maps Core
function initializeMap() {
	if (document.getElementById("map")) {
		var map = new GMap2(document.getElementById("map"));
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GLargeMapControl3D());
		var point = new GLatLng(39.8754658,-76.8646782);
		map.setCenter(point,17);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml("<strong>Windy Hill Senior Center</strong><br />50 North East Street, Suite 2<br />Spring Grove, PA 17362<br /><br /><a target=\"_blank\" href=\"http://maps.google.com/maps?daddr=50+North+East+Street+17362+(Windy+Hill+Senior+Center)\">Directions to Here</a><br /><a target=\"_blank\" href=\"http://maps.google.com/maps?saddr=50+North+East+Street+17362+(Windy+Hill+Senior+Center)\">Directions from Here</a>");
		setTimeout("hideMapText();",500);
	} //end document.getElementById("map") IF
} //end initializeMap
function hideMapText() {
	var spans = document.getElementById("map").getElementsByTagName("span");
	for (i = 0; i < spans.length; i++) {
		if (spans[i].innerHTML.substr(0,7) == "Imagery") {
			spans[i].parentNode.style.display = "none";
			break;
		} //end spans[i].firstChild.data.substr(0,7) == "Imagery" IF
	} //end i = 0; i < spans.length; i++ FOR
} //end hideMapText

//AJAX Core
function HttpClient(method,async,xmlhttp,result) {
	this.requestType = method;
	this.isAsync = async;
	this.xmlhttp = xmlhttp;
	this.requestResult = result;
} //end HttpClient
HttpClient.prototype = {
	callback:false,
	onSend:false,
	onLoad:false,
	onError:function(error) {
		alert(error.message);
	}/*onError:function(error)*/,
	init:function() {
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
			var success = false;
			for (var i = 0; i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			} //end var i = 0; i < XMLHTTP_IDS.length && !success; i++ FOR
			if (!success) {
				throw new Error('Unable to create XMLHttpRequest.');
			} //end !success IF
		} //end try/catch
	} /*end init:function()*/,
	makeRequest:function(url,payload) {
		if (!this.xmlhttp) {
			this.init();
		} //end !this.xmlhttp IF
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		var self = this;
		if (this.callback != false) {
			this.xmlhttp.onreadystatechange = function() {self._readyStateChangeCallback();}
		} //end this.callback != false IF
		if (this.requestType == "POST") {
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		} //end this.requestType == "POST" IF
		this.xmlhttp.send(payload);
		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		} //end !this.isAsync IF
	} /*end makeRequest:function(url,payload)*/,
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				if (this.onSend != false) {
					this.onSend();
				} //end this.onSend != false IF
			break;
			case 4:
				if (this.onLoad != false) {
					this.onLoad();
				} //end this.onLoad != false IF
				if (this.xmlhttp.status == 200) {
					if (this.requestResult == 'XML') {
						this.callback(this.xmlhttp.responseXML);
					} else {
						this.callback(this.xmlhttp.responseText);
					} //end this.requestResult == 'XML' IF
				} else {
					this.onError(new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
				} //end this.xmlhttp.status == 200 IF
			break;
		} //end this.xmlhttp.readyState SWITCH
	} //end _readyStateChangeCallback:function()
} //end HttpClient.prototype

//Fade Core
function fade(target,direction) {
	targetElement = target;
	dir = direction;
	o = (dir == "out" ? 1 : 0);
	if (document.all) {
		o = (o == 1 ? 100 : 0);
		targetElement.style.filter="alpha(opacity="+o+")";
		timer = setInterval("fadeIE()",30);
	} else if (document.getElementById && !document.all) {
		timer = setInterval("fadeGecko()",30);
	} //end document.all || (document.getElementById && !document.all) IFs
} //end fade
function fadeGecko() {
	target = targetElement;
	direction = dir;
	if (o <= 1.05 && direction == "in") {
		target.style.MozOpacity = o;
		target.style.KhtmlOpacity = o;
		target.style.opacity = o;
		o += 0.05;
	} else if (o >= -0.05 && direction == "out") {
		target.style.MozOpacity = o;
		target.style.KhtmlOpacity = o;
		target.style.opacity = o;
		o -= 0.05;
	} else {
		clearInterval(timer);
		if (direction == "out") {
			emptyElement(target);
			target.style.width = "0px";
		} //end direction == "out" IF
	} //end (o < 1.05 && direction == "in") && (o > -0.05 && direction == "out") IFs
} //end fadeGecko
function fadeIE() {
	target = targetElement;
	direction = dir;
	if (o <= 120 && direction == "in") {
		target.filters.alpha.opacity = o;
		o += 20;
	} else if (o >= -20 && direction == "out") {
		target.filters.alpha.opacity = o;
		o -= 20;
	} else {
		clearInterval(timer);
		if (direction == "out") {
			emptyElement(target);
			target.style.width = "0px";
		} //end direction IF
	} //end (o < 120 && direction == "in") || (o > -20 && direction == "out") IFs
} //end fadeIE

//Tracking Core
var x = 0;
var y = 0;
var o = 0;
var sW = 0;
var sH = 0;
function getMouseCoords(e) {
	if (document.all) {
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	} else {
		x = e.pageX;
		y = e.pageY;
	} //end document.all IF
	return true;
} //end getMouseCoords
document.onmousemove = getMouseCoords;
function getScreenRes() {
	if (self.innerWidth) {
		sW = self.innerWidth;		
		sH = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		sW = document.documentElement.clientWidth;
		sH = document.documentElement.clientHeight;
	} else if (document.body) {
		sW = document.body.clientWidth;
		sH = document.body.clientHeight;
	} //end self.innerWidth || (document.documentElement && document.documentElement.clientWidth) || document.body IFs
	window.onresize = getScreenRes;
} //end getScreenRes
getScreenRes();

//Calendar Core
function showDetails(id) {
	//Prevent details box from displaying off-screen
	if ((x+500) > sW) {
		x = sW-550;
	} //end (x+500) > sW IF
		
	//Fetch data
	fetchData(id);
	
	//Position details box
	var target = document.getElementById("details");
	target.style.left = x+"px";
	target.style.top = y+"px";
	target.style.width = "500px";
} //end showDetails
function fetchData(id) {
	var http = new HttpClient("POST",true,false,"XML");
	http.callback = function(xmlDoc) {
		var target = document.getElementById("details");
		emptyElement(target);
		
		if (xmlDoc.getElementsByTagName("error").length > 0) {
			var data = xmlDoc.getElementsByTagName("error")[0];
			
			var header = document.createElement("div");
			header.setAttribute("id","details_header");
			header.appendChild(document.createTextNode(data.getAttribute("title")));
			target.appendChild(header);
		
			var closeBox = document.createElement("div");
			closeBox.setAttribute("id","details_close");
			if (document.all) {
				closeBox.setAttribute("onclick",hideDetails);
			} else {
				closeBox.setAttribute("onclick","hideDetails();");
			} //end document.all IF
			closeBox.setAttribute("title","Hide Error Message");
			closeBox.appendChild(document.createTextNode("X"));
			target.appendChild(closeBox);
		
			var content = document.createElement("div");
			content.setAttribute("id","details_content");
			content.setAttribute("class","error");
			content.appendChild(document.createTextNode(data.getAttribute("message")));
		} else {
			var target = document.getElementById("details");
			emptyElement(target);
			
			var data = xmlDoc.getElementsByTagName("event")[0];
			
			var header = document.createElement("div");
			header.setAttribute("id","details_header");
			header.appendChild(document.createTextNode(data.getAttribute("name")));
			target.appendChild(header);
			
			var closeBox = document.createElement("div");
			closeBox.setAttribute("id","details_close");
			if (document.all) {
				closeBox.setAttribute("onclick",hideDetails);
			} else {
				closeBox.setAttribute("onclick","hideDetails();");
			} //end document.all IF
			closeBox.setAttribute("title","Hide Details");
			closeBox.appendChild(document.createTextNode("X"));
			target.appendChild(closeBox);
			
			var content = document.createElement("div");
			content.setAttribute("id","details_content");
					
			content.appendChild(makeStrong("When: "));
			content.appendChild(document.createTextNode(data.getAttribute("when")));
			content.appendChild(makeBr());
			
			content.appendChild(makeStrong("Details:"));
			content.appendChild(makeBr());
			var fragments = data.getAttribute("details").split("::BR::");
			for (i = 0; i < fragments.length; i++) {
				content.appendChild(document.createTextNode(fragments[i]));
				content.appendChild(makeBr());
			} //end i = 0; i < fragments.length; i++ FOR
		} //end xmlDoc.getElementsByTagName("error").length > 0 IF
		target.appendChild(content);
		
		//Fade in
		fade(target,"in");
	} //end http.callback = function(response)
	http.makeRequest("ajax/details.php","id="+id);
} //end fetchData
function hideDetails() {
	var target = document.getElementById("details");
	fade(target,"out");
} //end hideDetails