//Gregorian is used for calculating the liturgical seasons to set the color scheme for the site
var Gregorian = {
	listings:{
		"specials":{
			"laetareSunday":{
				"on":"fourthSundayOfLent",
				"name":"Laetare Sunday",
				"color":"rose"
			},
			"allSoulsDay":{
				"on":"allSoulsDay",
				"name":"All Souls Day",
				"color":"black"
			},
			"gaudeteSunday":{
				"on":"thirdSundayOfAdvent",
				"name":"Gaudete Sunday",
				"color":"rose"
			}
		},
		"seasons":{
			"xmas":{
				"starts":"newYearsDay",
				"ends":"epiphanySunday",
				"name":"Christmastide",
				"color":"gold"
			},
			"timeAfterEpiphany":{
				"after":"epiphanySunday",
				"before":"ashWednesday",
				"name":"Time after the Epiphany",
				"color":"green"
			},
			"lent":{
				"starts":"ashWednesday",
				"before":"holyThursday",
				"name":"Lent",
				"color":"violet"
			},
			"triduum":{
				"starts":"holyThursday",
				"before":"easter",
				"name":"Pachal Triduum",
				"color":"red"
			},
			"easter":{
				"starts":"easter",
				"ends":"pentecostSunday",
				"name":"Easter Time",
				"color":"gold"
			},
			"timeAfterPentecost":{
				"after":"pentecostSunday",
				"before":"firstSundayOfAdvent",
				"name":"Time after Pentecost",
				"color":"green"
			},
			"advent":{
				"starts":"firstSundayOfAdvent",
				"before":"christmasDay",
				"name":"Advent",
				"color":"purple"
			},
			"christmas":{
				"starts":"christmasDay",
				"ends":"newYearsEve",
				"name":"Christmastide",
				"color":"gold"
			}
		},
		"holidays":{
			"newYearsDay":{
				"label":"New Years Day",
				"date":"1/1"
			},
			"epiphanySunday":{
				"label":"Epiphany Sunday",
				"flag":"",
				"date":"1/8"
			},
			"ashWednesday":{
				"label":"Ash Wednesday",
				"days":"-46"
			},
			"fourthSundayOfLent":{
				"label":"Fourth Sunday Of Lent",
				"days":"-21"
			},
			"palmSunday":{
				"label":"Palm Sunday",
				"days":"-7"
			},
			"holyThursday":{
				"label":"Holy Thursday",
				"days":"-3"
			},
			"goodFriday":{
				"label":"Good Friday",
				"days":"-2"
			},
			"holySaturday" :{
				"label":"Holy Saturday",
				"days":"-1"
			},
			"easter":{
				"label":"Easter",
				"days":"0"
			},
			"ascensionThursday":{
				"label":"Ascension Thursday",
				"days":"39"
			},
			"pentecostSunday":{
				"label":"Pentecost Sunday",
				"days":"49"
			},
			"allSoulsDay":{
				"label":"All Souls Day",
				"date":"11/2"
			},
			"firstSundayOfAdvent":{
				"label":"First Sunday Of Advent",
				"flag":"",
				"date":"12/3"
			},
			"secondSundayOfAdvent":{
				"label":"Second Sunday Of Advent",
				"flag":"",
				"date":"12/10"
			},
			"thirdSundayOfAdvent":{
				"label":"Third Sunday Of Advent",
				"flag":"",
				"date":"12/17"
			},
			"fourthSundayOfAdvent":{
				"label":"Fourth Sunday Of Advent",
				"flag":"",
				"date":"12/24"
			},
			"christmasEve":{
				"label":"Christmas Eve",
				"date":"12/24"
			},
			"christmasDay":{
				"label":"Christmas Day",
				"date":"12/25"
			},
			"newYearsEve":{
				"label":"New Years Eve",
				"date":"12/31"
			}
		},
		"themes":{
			"green":{
				"pale":"d3e7d3",
				"light":"67ad67",
				"shade":"282",
				"url":"green.png"
			},
			"violet":{
				"pale":"f3daff",
				"light":"d47ff",
				"shade":"c145ff",
				"url":"violet.png"
			},
			"red":{
				"pale":"fcd3d3",
				"light":"f36767",
				"shade":"e22",
				"url":"red.png"
			},
			"gold":{
				"pale":"f8eed3",
				"light":"e8c467",
				"shade":"da2",
				"url":"gold.png"
			},
			"rose":{
				"pale":"fff1fc",
				"light":"ffd0f3",
				"shade":"fbe",
				"url":"rose.png"
			},
			"black":{
				"pale":"cccccc",
				"light":"4f4f4f",
				"shade":"000",
				"url":"black.png",
				"flag":""
			},
			"purple":{
				"pale":"dcd5e4",
				"light":"866ea3",
				"shade":"4f2d7a",
				"url":"purple.png"
			}
		}
	},
	easter:function(Y){
		//first sunday after first full moon after vernal equinox
		//Gauss Algorithm
		var a = (Y%19);
		var b = Math.floor(Y/100);
		var c = (Y%100);
		var d = Math.floor(b/4);
		var e = (b%4);
		var i = Math.floor(c/4);
		var k = (c%4);
		var f = Math.floor((b+8)/25);
		var g = Math.floor((b-f+1)/3);
		var h = (19*a + b - d - g + 15)%30;
		var L = (32 + 2*e + 2*i - h - k) % 7;
		var m = Math.floor((a + 11*h + 22*L)/451);
		var n = (h+L-7*m+114);
		var dt = new Date(Y,Math.floor(n/31)-1,(n%31)+1);
		return dt;
	},
	holiday:function(list,label,year){
		var day = list[label];
		var dt;
		if("date" in day){
			var arr = day.date.split("/");
			dt = new Date(year,parseInt(arr[0])-1,parseInt(arr[1]));
			if("flag" in day){
				dt.setDate(dt.getDate()-dt.getDay());
			}
		}else{
			dt = this.easter(year);
			dt.setDate(dt.getDate()+parseInt(day.days));
		}
		return dt;
	},
	dateString:function(dt){
		return (dt.getMonth()+1)+"/"+dt.getDate()+"/"+dt.getFullYear();
	},
	numVal:function(dt){
		return dt.getFullYear()*10000 + (dt.getMonth()+1)*100 + dt.getDate();
	},
	getTheme:function(json,dt){
		var year = dt.getFullYear();
		var check = this.numVal(dt);
		var holidays = {};
		for(var x in json.holidays){
			holidays[x] = this.numVal(this.holiday(json.holidays,x,year));
		}
		for(var x in json.specials){
			if(holidays[json.specials[x].on]==check){
				return {"name":json.specials[x].name,"color":json.specials[x].color};
			}
		}
		var ready = false;
		for(var x in json.seasons){
			if("starts" in json.seasons[x]){
				ready = ((holidays[json.seasons[x].starts])<=check);
			}else{
				ready = ((holidays[json.seasons[x].after])<check);
			}
			if("ends" in json.seasons[x]){
				ready = ready && ((holidays[json.seasons[x].ends])>=check);
			}else{
				ready = ready && ((holidays[json.seasons[x].before])>check);
			}
			if(ready){
				return {"name":json.seasons[x].name,"color":json.seasons[x].color};
			}
		}
		return {"name":"","color":""};
	},
	getCSS:function(theme){
		return {
			"body":{"background":"#"+theme.shade+" url(Images/"+theme.url+") repeat-y center top"},
			".body, .side, #title, #header":{
				"background-color": "#"+theme.shade
			},
			"#sched, .side, .side a, #title, #footer, #footer a":{
				"color":("flag" in theme?"black":"white")
			},
			".static":{
				"background-color":"#"+theme.light
			}
		}
	}
}
var Utility = {
	timer:0,
	theme:"",
	displayItem:function(i){
		$(".topitem:eq("+i+")").find(".subnav").slideDown(100);
	},
	eachSubMenu:function(n){
		$(this).hover(function(){
				clearTimeout(Utility.timer);
				Utility.timer = setTimeout("Utility.displayItem("+n+")",150);
			},function(){
				clearTimeout(Utility.timer);
				$(this).find(".subnav").fadeOut(200);
			});
		$(this).find(".subnav").hover(function(){
				clearTimeout(Utility.timer);
				Utility.timer = setTimeout("Utility.displayItem("+n+")",150);
			},function(){
				clearTimeout(Utility.timer);
				$(this).find(".subnav").fadeOut(200);
			});
	},
	jQuery:function(){
		//get search variables and page
		var get = Utility.request();
		var page = "Welcome";
		if("page" in get){
			page = get.page.split("-").join(" ");
		}
		
		//liturgical theme
		var dt = new Date();
		var theme = Gregorian.getTheme(Gregorian.listings,dt);
		var flag = ("flag" in Gregorian.listings.themes[theme.color]);
		Utility.theme = Gregorian.listings.themes[theme.color];
		var themeCSS = Gregorian.getCSS(Gregorian.listings.themes[theme.color]);
		for(var x in themeCSS){
			$(x).css(themeCSS[x]);
		}
		
		//display schedule
		var temp = Utility.processMarkdown(json["Schedule"]);
		//temp = temp.split(" ").join("\u00A0");
		$("#sched").html(temp);
		
		//build menu from Markdown
		$("#menu").html(Utility.processMarkdown(json["Menu"]));
		$("#menu ul").attr("class","topnav");
		$("#menu li").attr("class","topitem");
		$("#menu ul ul").attr("class","subnav");
		$("#menu ul ul").css("display","none");
		$("#menu li li").attr("class","subitem");
		$("#menu li a").addClass("link");
		$("#menu li li a").attr("class","drop");
		$(".topitem a").css({"color":("flag" in theme?"black":"white"),"background-color":"transparent"});
		$(".subnav a").css({"color":("flag" in theme?"black":"white"),"background-color":"transparent"});
		
		//menu link adjustment code here//
		$("#menu a:not([href='#'],[href='?'],[href='@'],[href^='mailto:'],[href^='?page=Calendar'])").attr("target","_blank");
		$("#menu a[href='?']").each(function(i,e){
				$(this).attr("href","?page="+$(this).attr("title").split(" ").join("-"));
			});
		$("#menu a[href='@']").each(function(i,e){
				$(this).attr("href","?page=email&to="+$(this).attr("title"));
			});
		$("#menu a[href^='mailto:']").each(function(i,e){
				$(this).attr("href",$(this).attr("href").replace("mailto:","?page=email&to="));
			});
		$("#menu a[href$='.pdf']").each(function(i,e){
				$(this).attr("href","docs/"+$(this).attr("href"));
			});
		$("#menu a[href$='.doc']").each(function(i,e){
				$(this).attr("href","docs/"+$(this).attr("href"));
			});
		$("#menu a[href='#']").each(function(i,e){
				var sel = $(this).attr("title");
				$(this).attr("title","Click to Expand/Collapse");
				$(this).attr("href","javascript:");
				sel = sel.split(" ").join(":eq(")+")";
				$(sel).hide();
				$(this).click(function(){
						$(sel).slideToggle();
					});
			});
		
		//menu link go
		$(".topitem").each(Utility.eachSubMenu);
		
		//menu link appearance
		$(".link").hover(function(){
				$(this).css({"color":(flag?"white":"black"),"background-color":(flag?"black":"white")});
			},function(){
				$(this).css({"color":("flag" in theme?"black":"white"),"background-color":"transparent"});
			});
		$(".drop").hover(function(){
				$(this).css({"color":(flag?"white":"black"),"background-color":(flag?"black":"white")});
			},function(){
				$(this).css({"color":("flag" in theme?"black":"white"),"background-color":"transparent"});
			});
		
		$("#footer a").hover(function(){
				$(this).css({"color":(flag?"white":"black"),"background-color":(flag?"black":"white")});
			},function(){
				$(this).css({"color":("flag" in theme?"black":"white"),"background-color":"transparent"});
			});
		$("#title,#footer").show();
		
		var content = json[page];
		//shape content
		if(page in json){
			Utility.shapeContent(page,get,content);
		}else{
			$.ajax({
					url: "content/email.php",
					data: get,
					success: function(result){
						$("#content").html(result);
					}
				});
		}
		
		//header
		var cat = $(".subitem a[title='"+page+"']").parent().parent().parent().find("a:eq(0)").attr("title");
		var headers = json.Headers.split("\n");
		function searchHeaders(txt){
			for(var y = 0; y<headers.length; y++){
				if(headers[y]==txt){
					return y;
				}
			}
			return -1;
		}
		var x = searchHeaders(page);
		if(x==-1&&cat!=undefined){
			x = searchHeaders(cat);
		}
		if(x==-1){
			x = 0;
		}
		x++;
		$("#header").css({"background-image":"url(headers/Number"+x+".png)"});
		/*
		if($("#menu").height()>$("#content").height()){
			$("#content").height($("#menu").height());
		}
		*/
		Utility.setWidth();
		$(window).resize(Utility.setWidth);
	},
	shapeContent:function(page,get,content){
		if(page=="Calendar"){
			$("#content").html("<div id=\""+page.split(" ").join("-")+"\">"+Utility.getContent(content,get)+"</div>");
			$("#content a[href*='page']").each(function(){
					$(this).parent().hover(function(){
							$(this).css({"background-color":theme.pale});
						},function(){
							$(this).parent().css({"background-color":"white"});
						});
				});
		}else{
			$("#content").html("<div id=\""+page.split(" ").join("-")+"\">"+Utility.processMarkdown(content)+"</div>");
			$("#content td, #content th").css({"padding":".5em .25em","white-space":"nowrap"});
			$("#content tr").each(function(n){
					if(n%2==1){
						$(this).css({"background-color":"#"+Utility.theme.pale});
					}
				});
		}
		//link adjustment code here//
		$("#content a:not([href='#'],[href='?'],[href='@'],[href^='mailto:'],[href^='?page=Calendar'])").attr("target","_blank");
		$("#content a[href='?']").each(function(i,e){
				$(this).attr("href","?page="+$(this).attr("title").split(" ").join("-"));
			});
		$("#content a[href='@']").each(function(i,e){
				$(this).attr("href","?page=email&to="+$(this).attr("title"));
			});
		$("#content a[href^='mailto:']").each(function(i,e){
				$(this).attr("href",$(this).attr("href").replace("mailto:","?page=email&to="));
			});
		$("#content a[href$='.pdf']").each(function(i,e){
				$(this).attr("href","docs/"+$(this).attr("href"));
			});
		$("#content a[href$='.doc']").each(function(i,e){
				$(this).attr("href","docs/"+$(this).attr("href"));
			});
		$("#content a[href='#']").each(function(i,e){
				var sel = $(this).attr("title");
				$(this).attr("title","Click to Expand/Collapse");
				$(this).attr("href","javascript:void(0)");
				sel = sel.split(" ").join(":eq(")+")";
				$(sel).hide();
				$(this).click(function(){
						$(sel).slideToggle();
					});
			});
		//link adj code ends
		
		//Gallery setup
		if(page=="Gallery"){
			Utility.setGallery("gallery/");
		}else{
			$("#content img").each(function(i,e){
					$(this).attr("src","Images/"+$(this).attr("src"));
				});
			$("#content em img").css({"float":"left"});
			$("#content strong img").css({"float":"right"});
		}
	},
	setWidth:function(){
		var m = $("#menu").width();
		var s = $("#sched").width();
		var w = Math.max(m,s);
		$(".side div").width(w);
		var l = $("#menu").offset().left;
		$(".subnav").each(function(){
				var v = l+m;
				var t = $(this).parent().offset().top;
				$(this).css({"left":v+"px","top":t+"px"});
			});
		var aw = $("#all").width();
		var al = $("#all").offset().left;
		var ar = aw+al;
		var bw = $(document).width();
		if(!window.ActiveXObject){
			var str = "background-image: "+Utility.getVendor()+"linear-gradient(left, #"+Utility.theme.shade+", #fff "+al+"px, #fff "+ar+"px, #"+Utility.theme.shade+" "+bw+"px)";
			//alert(str);
			$("body").attr("style",str);
		}
	},
	getVendor:function(){
		var ua = navigator.userAgent;
		var navs = ["Firefox","Chrome","Opera","MSIE"];
		var vend = ["-moz-","-webkit-","-o-",""];
		for(var x = 0; x<navs.length; x++){
			if(ua.indexOf(navs[x])>-1){
				return vend[x];
			}
		}
		return "";
	},
	setGallery:function(folder){
		var thumbs = $('#Gallery ul');
		thumbs.addClass("thumbs");
		thumbs.attr("id","thumbs_1");
		$(".thumbs img").each(function(i,e){
				$(this).attr("src",folder+$(this).attr("src"));
				$(this).attr("width",$(this).attr("title"));
				$(this).attr("title",$(this).attr("alt"));
			});
		$('#Gallery').flickrGallery({per_page: 50});
		//$("#menu").height(900);
	},
	processMarkdown:function(txt){
		//function called for converting Markdown to HTML
		var markdown = new MarkdownDeep.Markdown();
		markdown.ExtraMode = true;
		markdown.SafeMode = false;
		return markdown.Transform(txt);
	},
	request:function(){
		//Converts search string values into a JSON object
		if(window.location.search==""){
			return {};
		}
		var arr = window.location.search.slice(1).split("&");
		var pair;
		var out = {};
		for(var x = 0; x<arr.length; x++){
			pair = arr[x].split("=");
			out[pair[0]] = (pair.length>1?decodeURIComponent(pair[1]):"");
		}
		return out;
	},
	getContent:function(json,request){
		//builds and returns the calendar ui
		var calendar = this.generateCalendar(json);
		var list = ["year","month","day","event"];
		var x = 0;
		while(list[x] in request){
			x++;
		}
		switch(x){
		case 1:
			return EventsUI.getYear(calendar,request.year);
		case 2:
			return EventsUI.getMonth(calendar,request.year,request.month);
		case 3:
			return EventsUI.getDay(calendar,request.year,request.month,request.day);
		case 4:
			return EventsUI.getEvent(calendar,request.year,request.month,request.day,request.event);
		case 0:
			var dt = new Date();
			var y = Number(dt.getFullYear()).toString();
			var m = Number(dt.getMonth()+1).toString();
			return EventsUI.getMonth(calendar,y,m);
		default:
			return "Error, cannot generate Calendar; " +x;
		}
		return calendar;
	},
	generateCalendar:function(table){
		//builds the JSON calendar object from the Markdown Calendar Table
		var rows = table.split("\n");
		var tags = {"event":3,"start":4,"end":5,"desc":6};
		for(var x = 2; x<rows.length; x++){
			rows[x] = rows[x].split(" \| ");
		}
		var out = {};
		var temp;
		for(var x = 2; x<rows.length; x++){
			if(!(rows[x][0] in out)){
				out[rows[x][0]] = {};
			}
			if(!(rows[x][1] in out[rows[x][0]])){
				out[rows[x][0]][rows[x][1]] = {};
			}
			if(!(rows[x][2] in out[rows[x][0]][rows[x][1]])){
				out[rows[x][0]][rows[x][1]][rows[x][2]] = [];
			}
			temp = {};
			for(var y in tags){
				if(rows[x][tags[y]]!=""){
					temp[y] = rows[x][tags[y]];
				}
			}
			out[rows[x][0]][rows[x][1]][rows[x][2]].push(temp);
		}
		return out;
	}
}
//Calendar contains functions for calendar calculations
var Calendar = {
	months:['January','February','March','April','May','June','July','August','September','October','November','December'],
	nextMonth:function(yr,mo){
		var dt = new Date(yr,mo,1);
		dt.setMonth(dt.getMonth()+1);
		return dt;
	},
	prevMonth:function(yr,mo){
		var dt = new Date(yr,mo,1);
		dt.setMonth(dt.getMonth()-1);
		return dt;
	},
	getYear:function(yr){
		var fs = this.firstDaysOfEveryMonth(yr);
		var ms = this.allMaxDays(yr);
		var c;
		var d;
		var out = [];
		var week;
		var month;
		for(var x = 0; x<12; x++){
			month = [];
			week = [];
			c = 0;
			d = 1;
			while(c++<fs[x]){
				week.push(" ");
			}
			while(d<=ms[x]){
				if(week.length>=7){
					month.push(week);
					week = [];
				}
				week.push(Number(d++).toString());
			}
			if(week.length>0){
				while(week.length<7){
					week.push(" ");
				}
				month.push(week);
				week = [];
			}
			out.push(month);
		}
		return out;
	},
	getMonth:function(yr,mo){
		var out = [];
		var week = [];
		var f = this.firstDayOfMonth(mo,yr);
		var m = this.maxDays(mo,yr);
		var c = 0;
		var d = 1;
		while(c++<f){
			week.push(" ");
		}
		while(d<=m){
			if(week.length>=7){
				out.push(week);
				week = [];
			}
			week.push(Number(d++).toString());
		}
		if(week.length>0){
			while(week.length<7){
				week.push(" ");
			}
			out.push(week);
		}
		return out;
	},
	firstDayOfYear:function(yr){
		return (yr + Math.floor((yr-1)/4) - Math.floor((yr-1)/100) + Math.floor((yr-1)/400))%7;
	},
	firstDayOfMonth:function(mo,yr){
		var f = this.firstDayOfYear(yr);
		var m = 1;
		var d = f;
		while(m<mo){
			d = (d+this.maxDays(m++,yr))%7;
		}
		return d;
	},
	firstDaysOfEveryMonth:function(yr){
		var f = this.firstDayOfYear(yr);
		var m = 1;
		var d = f;
		var ds = [d];
		while(m<12){
			d = (d+this.maxDays(m++,yr))%7;
			ds.push(d);
		}
		return ds;
	},
	allMaxDays:function(yr){
		var out = [];
		for(var x = 1; x<=12; x++){
			out.push(this.maxDays(x,yr));
		}
		return out;
	},
	maxDays:function(mo,yr){
		switch(mo){
		case 2:
			return 28 + (yr%4==0?1:0);
		case 9:
		case 4:
		case 6:
		case 11:
			return 30;
		default:
			return 31;
		}
	}
}
//Calendar UI functions: build ui using calendar JSON object
var EventsUI = {
	getEvent:function(events,year,month,day,num){
		//builds UI for a single event
		var index = parseInt(num);
		var out = "";
		if(year in events){
			if(month in events[year]){
				if(day in events[year][month]){
					if(events[year][month][day].length>index&&index>=0){
						var event = events[year][month][day][index];
						out = "<div id=\"out\" class=\"calendar\">";
						out += "<h2>"+event.event+"</h2><h3><a href=\"?page=Calendar&year="+year+"&month="+month+"\">";
						out += Calendar.months[parseInt(month)-1]+"</a> <a href=\"?page=Calendar&year="+year+"&month="+month+"&day="+day+"\">";
						out += day+"</a>, <a href=\"?page=Calendar&year="+year+"\">"+year+"</a></h3>";
						if("start" in event){
							out += "<h3>Starts: "+event.start+"</h3>";
							if("end" in event){
								out += "<h3>Ends: "+event.end+"</h3>";
							}
						}else{
							out += "<h3>All Day Event</h3>";
						}
						if("desc" in event){
							out += "<p>"+event.desc+"</p>";
						}
						out += "</div>";
					}
				}
			}
		}
		return out;
	},
	getDay:function(events,year,month,day){
		//builds ui for a single day
		var out = "<div id=\"out\" class=\"calendar\">";
		out += "<h2><a href=\"?page=Calendar&year="+year+"&month="+month+"\">";
		out += Calendar.months[parseInt(month)-1]+"</a> "+day+", <a href=\"?page=Calendar&year="+year+"\">"+year+"</a></h2><ul>";
		if(year in events){
			if(month in events[year]){
				if(day in events[year][month]){
					var today = events[year][month][day];
					for(var x = 0; x<today.length; x++){
						out += "<li><dl><dt><a href=\"?page=Calendar&year="+year+"&month="+month+"&day="+day+"&event="+x+"\">"+today[x].event+"</a></dt>";
						if("start" in today[x]){
							out += "<dd>Starts: "+today[x].start+"</dd>";
							if("end" in today[x]){
								out += "<dd>Ends: "+today[x].end+"</dd>";
							}
						}else{
							out += "<dd>All Day Event</dd>";
						}
						out += "</dl></li>";
					}
				}
			}
		}
		out += " </ul>";
		out += "</div>";
		return out;
	},
	getMonth:function(events,year,month){
		//builds ui for a given month
		var prev = Calendar.prevMonth(parseInt(year),parseInt(month)-1);
		var next = Calendar.nextMonth(parseInt(year),parseInt(month)-1);
		var days = Calendar.getMonth(parseInt(year),parseInt(month));
		var out = "<div id=\"out\" class=\"calendar\">";
		out += "<table>";
		out += "<tr>";
		out += "<th><a href=\"?page=Calendar&year="+prev.getFullYear()+"&month="+(prev.getMonth()+1)+"\">&lt;&lt;&lt;</a></th>";
		out += "<th colspan=\"5\">"+Calendar.months[parseInt(month)-1];
		out += " <a href=\"?page=Calendar&year="+year+"\">"+year+"</a></th>";
		out += "<th><a href=\"?page=Calendar&year="+next.getFullYear()+"&month="+(next.getMonth()+1)+"\">&gt;&gt;&gt;</a></th>";
		out += "</tr>";
		out += "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
		for(var x = 0; x<days.length; x++){
			out += "<tr>";
			for(var y = 0; y<days[x].length; y++){
				out += "<td>";
				if(days[x][y]!=" "){
					out += "<h4 class=\"event\"><a href=\"?page=Calendar&year="+year+"&month="+month+"&day="+days[x][y]+"\">"+days[x][y]+"</a></h4>";
					if(year in events){
						if(month in events[year]){
							if(days[x][y] in events[year][month]){
								var today = events[year][month][days[x][y]];
								out += "<ul class=\"event\">";
								for(var z = 0; z<today.length; z++){
									out += "<li><h6><a href=\"?page=Calendar&year="+year+"&month="+month+"&day="+days[x][y]+"&event="+z+"\"><b>"+today[z].event+"</b>";
									if("start" in today[z]){
										out += " <i>"+today[z].start;
										if("end" in today[z]){
											out += " - "+today[z].end;
										}
										out += "</i>";
									}
									out += "</a></h6></li>";
								}
								out += "</ul>";
							}
						}
					}
				}
				out += " </td>";
			}
			out += "</tr>";
		}
		out += "</table>";
		out += "</div>";
		return out;
	},
	getYear:function(events,year){
		//builds ui for a given year
		var prev = parseInt(year)-1;
		var next = parseInt(year)+1;
		var days = Calendar.getYear(parseInt(year));
		var out = "<div id=\"out\" class=\"calendar\"><h2><a href=\"?page=Calendar&year="+prev+"\">&lt;&lt;&lt;</a>"+year+"<a href=\"?page=Calendar&year="+next+"\">&gt;&gt;&gt;</a></h2><ul>";
		for(var mo = 0; mo<days.length; mo++){
			var month = Number(mo+1).toString();
			out += "<li class=\"month\"><table>";
			out += "<tr><th colspan=\"7\"><a href=\"?page=Calendar&year="+year+"&month="+(mo+1)+"\">"+Calendar.months[mo]+"</a></th></tr>";
			out += "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
			for(var x = 0; x<days[mo].length; x++){
				out += "<tr>";
				for(var y = 0; y<days[mo][x].length; y++){
					out += "<td>";
					if(days[mo][x][y]!=" "){
						out += "<table class=\"count\"><tr><td><a href=\"?page=Calendar&year="+year+"&month="+(mo+1)+"&day="+days[mo][x][y]+"\">"+days[mo][x][y]+"</a></td><td class=\"inner\">";
						if(year in events){
							if(month in events[year]){
								if(days[mo][x][y] in events[year][month]){
									out += "[<i>"+events[year][month][days[mo][x][y]].length+"</i>]";
								}
							}
						}
						out += "</td></tr></table>";
					}
					out += " </td>";
				}
				out += "</tr>";
			}
			out += "</table></li>";
		}
		out += "</ul></div>";
		return out;
	}
}
var Editor = {
	type:"Markdown Primer",
	getContent:function(){
		if(Editor.type=="Markdown Primer"){
			return markdownPrimer;
		}else{
			return json[Editor.type];
		}
	},
	setSelect:function(){
		var labels = [];
		for(var x in json){
			labels.push(x);
		}
		labels.sort();
		var select = [];
		for(var x = 0; x<labels.length; x++){
			select.push("<option>"+labels[x]+"</option>");
		}
		return "<select id=\"type\">\n<option>Markdown Primer</option>\n"+select.join("\n")+"\n</select>";
	},
	selectChange:function(c){
		return function(){
			if($("#save").val()=="SAVE (NOT SAVED)"&&Editor.type!="Markdown Primer"){
				if(confirm("This page has not been saved.\n\nPress 'OK' to save or 'Cancel' to NOT save:")){
					Editor.savePage(c);
				}
			}
			$("#save").val("Save");
			Editor.type = $("#type").val();
			if(!(Editor.type in json)){
				Editor.type = "Markdown Primer";
			}
			$("#mark").val(Editor.getContent());
			Editor.previewPage();
		}
	},
	savePage:function(t,c,m){
		var check = t in json;
		var data = {id:t,mark:m,fileName:"piusJSON.js",code:c};
		if(data.id=="Markdown Primer"){
			alert("Changes to Markdown Primer cannot be saved.");
		}else{
			Editor.previewPage();
			$.get("content/saveJSON.php",data,function(result){
					if(parseInt(result)==0){
						alert(data.id+" has been saved");
						json[data.id] = data.mark;
						$("#save").val("Save");
						if(!check){
							Editor.type = "Markdown Primer";
							$("#select").html(Editor.setSelect());
							$("#type").change(Editor.selectChange(c));
							$("#mark").val(markdownPrimer);
						}
					}else{
						alert("Error:"+data.id+" cannot be saved."+result);
					}
				});
		}
	},
	previewPage:function(){
		Utility.shapeContent(Editor.type,{},$("#mark").val());
		$("#content a:not([href*='javascript:'])").attr("target","_blank");
		$("#content a[href*='page']").each(function(){
				var h = $(this).attr("href");
				$(this).attr("href","index.html"+h);
			});
	},
	setUp:function(){
		if(window.ActiveXObject){
			alert("Please do not use Internet Explorer for this editor.\nUse Firefox or Google Chrome instead.");
		}else{
			var c = prompt("password");
			$("#upload").attr("src","content/upload.php?code="+c);
			$.get("content/checkCode.php",{"code":c},function(result){
					if(result.indexOf("true")>=0){
						$("#select").html(Editor.setSelect());
						Editor.type = "Markdown Primer";
						$("#mark").val(Editor.getContent());
						Editor.previewPage();
						$("#type").change(Editor.selectChange(c));
						$("#preview").click(function(){
								Editor.previewPage();
								$("#save").val("SAVE (NOT SAVED)");
							});
						$("#save").click(function(){
								Editor.savePage(Editor.type,c,$("#mark").val());
							});
						$("#pass").click(function(){
								if(confirm("This will reset the password and send\nthe new password to the following email:\n\nchurch@piusx.org\n\nClick OK to reset the password\n-or-\nClick Cancel to NOT reset the password.")){
									$.get("content/passReset.php",{"code":c},function(result){
											if(parseInt(result)==0){
												alert("Your password has been reset.  Please check church@piusx.org for the new password");
												location.reload();
											}else{
												alert("Your password cannot be reset at this time.  Please try again later.");
											}
										});
								}else{
									alert("Your password has not been reset.");
								}
							});
						$("#add").click(function(){
								var p = "Please enter the name of the page to be created.\n\nNames cannot already exist.\n\nNames cannot contain ampersands or hyphens.";
								var title = prompt(p);
								while(title.indexOf("-")>-1||title.indexOf("&")>-1||(title in json)){
									title = prompt(p);
								}
								if(title!==""){
									Editor.savePage(title,c,"");
								}
							});
						$("#delete").click(function(){
								if(json[Editor.type].length>0){
									alert("This page cannot be deleted at this time.\nIt may only be deleted if the page is empty");
								}else{
									var data = {id:Editor.type,fileName:"piusJSON.js",code:c};
									$.get("content/deleteJSON.php",data,function(result){
											if(parseInt(result)==0){
												delete json[Editor.type];
												$("#select").html(Editor.setSelect());
												$("#type").change(Editor.selectChange(c));
												$("#mark").val(markdownPrimer);
												$("#content").html(Utility.processMarkdown(markdownPrimer));
												alert(Editor.type+" has been deleted");
											}else{
												alert(Editor.type+" could not be deleted at this time.");
											}														
										});
								}
							});
					}else{
						alert("Cannot access editor");
					}
				});
		}
	}
}


