/*
var dateArray = new Array();

var d1 = new Date("January 22, 2006 12:00");
var d2 = new Date("December 25, 2005 12:00");
var d3 = new Date("March 3, 2006 12:00");

dateArray[dateArray.length] = d1;
dateArray[dateArray.length] = d2;
dateArray[dateArray.length] = d3;

dateArray.sort(dateCompareDesc);

for (var i = 0; i < dateArray.length; i++) {
	alert(dateArray[i]);
}
*/

var evt = new Object();
evt.name = "";
evt.date = "";
evt.desc = "";
evt.starttime = "";
evt.endtime = "";
evt.location = "";
evt.link = "";
evt.host = "";
evt.cost = "";
evt.instructions = "";
evt.contact = "";

evt.Event = function(name, date, desc, starttime, endtime, location, link, host, cost, instructions, contact) {
	this.name = name;
	this.date = new Date(date);
	this.desc = desc;
	this.starttime = starttime;
	this.endtime = endtime;
	this.location = location;
	this.link = link;
	this.host = host;
	this.cost = cost;
	this.instructions = instructions;
	this.contact = contact;
}

evt.Event.prototype = {
	getHTML:function() {
		
		var now = this.date;
		var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
		var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
		var today =  days[now.getDay()] + ", " + months[now.getMonth()] + " " + date + ", " + parseInt(now.getFullYear());
		var numMonth = now.getMonth();
		var numDay = now.getDate();
		var yearPlc = parseInt(now.getFullYear());
		var s;
		if (this.link == ""){
		s = "<div style='text-align:left;width:100%;padding:10px;'>" + this.name + "";
		}else{
		    if(this.link.indexOf('') > 0){
		        s = "";
		    }else{
		       
    		}
		}
		
	
		var locationSubs = this.location.split(',',3);//[0]Venue,[1]Street,[2]City;
		var newTimeValue = this.starttime.replace(/:/,"");
		var encodeValue = encodeURI("http://planyp.us/plans/new?url=http://www.jacobsohn.com&title=" + this.name + "&description=" + this.desc + "&location_name=" + (locationSubs[0]+" "+locationSubs[1]) + "&location=" + locationSubs[(locationSubs.length -1)] + "&time=" + newTimeValue + "&date=" + yearPlc + "-" + numMonth + "-" + numDay);
		s += "</div>";
		s = s.replace(/[\r\n]/g, "");
		return s;
	}
}

evt.EventList = function () {
	this.arr = new Array();
	this.currentDate = 0;
}

evt.EventList.prototype = {
	addEvent:function(name, date, desc, starttime, endtime, location, link, host,cost, instructions, contact) {
		this.arr[this.arr.length] = new evt.Event(name, date, desc, starttime, endtime, location, link, host, cost,instructions, contact);
	},
	sortAsc:function() {
		this.arr.sort(dateCompareAsc);
	},
	sortDesc:function() {
		this.arr.sort(dateCompareDesc);
	},
	getCurrentEvent:function() {
		if (this.currentDate <= this.arr.length) {
			return this.arr[this.currentDate];
		} else {
			
		}
	},
	popEvent:function() {
		if (this.currentDate <= this.arr.length) {
			this.currentDate++;
		}			
	},
	reset:function() {
		this.currentDate = 0;
		/*
		for(var i = 0; i < this.arr.length; i++) {
			alert(this.arr[i].date);
		}
		*/
	}
}


/* custom date comparison functions that work with our event object */

function dateCompareAsc(a,b) {
	if (a.date<b.date) return -1;
	else if (a.date>b.date) return 1;
	else return 0;
}

function dateCompareDesc(a,b) {
	if (a.date>b.date) return -1;
	else if (a.date<b.date) return 1;
	else return 0;
}

var gEventList;

// calendar
var cfgCalendarWidth;
var cfgCalendarBorder;
var cfgCalendarBorderColor;
var cfgCalendarBackground;
var cfgCalendarPadding;
var cfgCalendarSpacing;

// month
var cfgMonthFont;
var cfgMonthFontSize;
var cfgMonthFontColor;
var cfgMonthBackground;

//day
var cfgDayFont;
var cfgDayFontSize;
var cfgDayFontColor;
var cfgDayBackground;

// cell
var cfgCellFont;
var cfgCellFontSize;
var cfgCellNumFontSize;
var cfgCellFontColor;
var cfgCellBackground;
var cfgCellMinHeight;

// event cell
var cfgEventCellFont;
var cfgEventCellFontSize;
var cfgEventCellNumFontSize;
var cfgEventCellFontColor;
var cfgEventCellBackground;

// today
var cfgTodayBackground;
var cfgTodayFontColor;

// blank
var cfgBlankBackground;

function LoadCalendar () {

	var xmlDoc = this.req.responseXML;
	var events = xmlDoc.getElementsByTagName("event");
	
	var configNodes = xmlDoc.getElementsByTagName("config");
	var config = configNodes[0];
	
	// calendar
	cfgCalendarWidth = config.attributes.getNamedItem("calWidth").value;
	cfgCalendarBorder = config.attributes.getNamedItem("calBorder").value;
	cfgCalendarBorderColor = config.attributes.getNamedItem("calBorderColor").value;
	cfgCalendarBackground = config.attributes.getNamedItem("calBackground").value;
	cfgCalendarPadding = config.attributes.getNamedItem("calPadding").value;
	cfgCalendarSpacing = config.attributes.getNamedItem("calSpacing").value;
	
	// month
	cfgMonthFont = config.attributes.getNamedItem("monthFont").value;
	cfgMonthFontSize = config.attributes.getNamedItem("monthFontSize").value;
	cfgMonthFontColor = config.attributes.getNamedItem("monthFontColor").value;
	cfgMonthBackground = config.attributes.getNamedItem("monthBackground").value;
	
	//day
	cfgDayFont = config.attributes.getNamedItem("dayFont").value;
	cfgDayFontSize = config.attributes.getNamedItem("dayFontSize").value;
	cfgDayFontColor = config.attributes.getNamedItem("dayFontColor").value;
	cfgDayBackground = config.attributes.getNamedItem("dayBackground").value;
	
	// cell
	cfgCellFont = config.attributes.getNamedItem("cellFont").value;
	cfgCellNumFontSize = config.attributes.getNamedItem("cellNumFontSize").value;
	cfgCellFontSize = config.attributes.getNamedItem("cellFontSize").value;
	cfgCellFontColor = config.attributes.getNamedItem("cellFontColor").value;
	cfgCellBackground = config.attributes.getNamedItem("cellBackground").value;
	cfgCellMinHeight = config.attributes.getNamedItem("cellMinHeight").value;

	// event cell
	cfgEventCellFont = config.attributes.getNamedItem("eventCellFont").value;
	cfgEventCellNumFontSize = config.attributes.getNamedItem("eventCellNumFontSize").value;
	cfgEventCellFontSize = config.attributes.getNamedItem("eventCellFontSize").value;
	cfgEventCellFontColor = config.attributes.getNamedItem("eventCellFontColor").value;
	cfgEventCellBackground = config.attributes.getNamedItem("eventCellBackground").value;
	
	// today
	cfgTodayBackground = config.attributes.getNamedItem("todayBackground").value;
	cfgTodayFontColor = config.attributes.getNamedItem("todayFontColor").value;
	
	// blank
	cfgBlankBackground = config.attributes.getNamedItem("blankBackground").value;
	
	var eventList = new evt.EventList();
	
	for (var i = 0; i < events.length; i++) {
		eventList.addEvent(getData(events[i], "name"), 
			getData(events[i], "date"), 
			getData(events[i], "desc"),
			getData(events[i], "starttime"),
			getData(events[i], "endtime"),
			getData(events[i], "location"),
			getData(events[i], "link"),
			getData(events[i], "host"),
			getData(events[i], "cost"),
			getData(events[i], "instructions"),
			getData(events[i], "contact")
			);
	}
	
	eventList.sortAsc();
	gEventList = eventList;
	
	//DisplayCalendar(0, 2006);
	DisplayCalendar();
	
}

function DisplayCalendar1 (theMonth, theYear) {

	gEventList.reset();

	var calendar = new Date();
	var today = calendar.getDate();
		
	var day_of_week = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var month_of_year = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	var year = calendar.getFullYear();
	var month = calendar.getMonth();
	var today = calendar.getDate();
	var weekday = calendar.getDay();

	calendar.setDate(1);
	calendar.setMonth(calendar.getMonth());

	if (theMonth == null)
		theMonth = month;
	if (theYear == null)
		theYear = year;
		
	calendar.setMonth(theMonth);
	calendar.setYear(theYear);
	calendar.setHours(0);
	calendar.setMinutes(0);
	calendar.setSeconds(0);
	calendar.setMilliseconds(0);
	
	var calHTML = "";
	
	var nextMonth;
	var nextYear;
	
	var prevMonth;
	var prevYear;
	
	if (theMonth == 0) {
		prevMonth = 11;
		prevYear = theYear - 1;
	} else {
		prevMonth = theMonth - 1;
		prevYear = theYear;
	}
	
	if (theMonth == 11) {
		nextMonth = 0;
		nextYear = theYear + 1;
	} else {
		nextMonth = theMonth + 1;
		nextYear = theYear;
	}
	
	calHTML += "<table style=\"font-family: " + cfgCellFont + "; font-size: " + cfgCellFontSize + "; color: " + cfgCellFontColor + "; border: " + cfgCalendarBorder + "px solid " + cfgCalendarBorderColor + "; background-color: " + cfgCalendarBackground + ";\" border=0 width=" + cfgCalendarWidth + " cellpadding=\"" + cfgCalendarPadding + "\" cellspacing=0>";

	calHTML += "<tr>";
	calHTML += "<td style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\"><a href=\"javascript:DisplayCalendar(" + prevMonth + "," + prevYear + ")\">" + month_of_year[prevMonth] + "</a></td>";
	calHTML += "<td colspan=5 align=center style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\">" + month_of_year[theMonth] + " " + theYear + "</td>";
	calHTML += "<td align=\"right\" style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\"><a href=\"javascript:DisplayCalendar(" + nextMonth + "," + nextYear + ")\">" + month_of_year[nextMonth] + "</a></td>";
	calHTML += "</tr>";

	calHTML += "<tr>";
	for (var iDay = 0; iDay < 7; iDay++) {
		if (iDay == weekday && theMonth == month)
			calHTML += "<td style=\"font-family: " + cfgDayFont + "; font-size: " + cfgDayFontSize + "; color: " + cfgDayFontColor + "; background-color: " + cfgDayBackground + ";\"><b>" + day_of_week[iDay] + "</b></td>";
		else
			calHTML += "<td style=\"font-family: " + cfgDayFont + "; font-size: " + cfgDayFontSize + "; color: " + cfgDayFontColor + "; background-color: " + cfgDayBackground + ";\">" + day_of_week[iDay] + "</td>";
	}
	calHTML += "</tr>";
	
	calHTML += "<tr valign=top>";
	
	// fill in blank days until day 1 of the month
	for(var index = 0; index < calendar.getDay(); index++) {
		calHTML += "<td style=\"background-color: " + cfgBlankBackground + ";\"></td>";
	}

	// set current event to start of the month
	if (gEventList.getCurrentEvent() != null) {
		while(gEventList.getCurrentEvent().date < calendar -1) {
			gEventList.popEvent();
			
			if (gEventList.getCurrentEvent() == null)
				break;
		}
	}
	
	for(var index = 0; index < 31; index++) {
		if (calendar.getDate() > index) {
			week_day = calendar.getDay();
			
			if (week_day == 0)
				calHTML += "<tr valign=top>"
				
			if (week_day != 7) {
				var day = calendar.getDate();
				
				
				if (today == day && theMonth == month) {
					calHTML += "<td width='600' style=\"height: " + cfgCellMinHeight + "px; color: " + cfgTodayFontColor + "; background-color: " + cfgTodayBackground + ";font-size:" + cfgEventCellNumFontSize + "px;\">"
					calHTML += "<b style=\"font-size:" + cfgEventCellNumFontSize + "px;\">" + day + "</b><br />";
										
				} else {
					calHTML += "<td width='600' style=\"height: " + cfgCellMinHeight + "px; background-color: " + cfgCellBackground + ";font-size:" + cfgEventCellNumFontSize + "px;\">"
					calHTML += day + "<br />";
				}
				
				// might be more than one event, so iterate through events)
				if(gEventList.getCurrentEvent() != null) {
					while(gEventList.getCurrentEvent().date.getDate() == day && gEventList.getCurrentEvent().date.getMonth() == theMonth) {
						calHTML += "<br />" + gEventList.getCurrentEvent().name + "<br />";
						
						gEventList.popEvent();
						
						// if next event is null, break out of loop
						if(gEventList.getCurrentEvent() == null)
							break;
					}
				}
				
				calHTML += "</td>"
			}
			
			if (week_day == 7)
				calHTML += "</tr>";
		}
		
		calendar.setDate(calendar.getDate() + 1);
	}
	
	for (var index = week_day; index < 6; index++) {
		calHTML += "<td style=\"background-color: " + cfgBlankBackground + ";\"></td>";
	}
	
	calHTML += "</tr></table>";
	
	var calDiv = document.getElementById("cal");
	calDiv.innerHTML = calHTML;


}



function DisplayCalendar (theMonth, theYear) {

	gEventList.reset();

	var calendar = new Date();
	var today = calendar.getDate();
		
	var day_of_week = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var month_of_year = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	var year = calendar.getFullYear();
	var month = calendar.getMonth();
	var today = calendar.getDate();
	var weekday = calendar.getDay();

	calendar.setDate(1);
	calendar.setMonth(calendar.getMonth());

	if (theMonth == null)
		theMonth = month;
	if (theYear == null)
		theYear = year;
		
	calendar.setMonth(theMonth);
	calendar.setYear(theYear);
	calendar.setHours(0);
	calendar.setMinutes(0);
	calendar.setSeconds(0);
	calendar.setMilliseconds(0);
	
	var calHTML = "";
	
	var nextMonth;
	var nextYear;
	
	var prevMonth;
	var prevYear;
	
	if (theMonth == 0) {
		prevMonth = 11;
		prevYear = theYear - 1;
	} else {
		prevMonth = theMonth - 1;
		prevYear = theYear;
	}
	
	if (theMonth == 11) {
		nextMonth = 0;
		nextYear = theYear + 1;
	} else {
		nextMonth = theMonth + 1;
		nextYear = theYear;
	}
	
	calHTML += "<table style=\"font-family: " + cfgCellFont + "; font-size: " + cfgCellFontSize + "; color: " + cfgCellFontColor + "; border: " + cfgCalendarBorder + "px solid " + cfgCalendarBorderColor + "; background-color: " + cfgCalendarBackground + ";\" border=0 width=" + cfgCalendarWidth + " cellpadding=\"" + cfgCalendarPadding + "\" cellspacing=\"" + cfgCalendarSpacing + "\">";

	calHTML += "<tr>";
	calHTML += "<td colspan=7>";
	
	
	calHTML += "<table border =\"0\">";
	calHTML += "<td style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\"><a onClick=\"showEvent(' ');\" href=\"javascript:DisplayCalendar(" + prevMonth + "," + prevYear + ")\"><img width=14 height=14 src=\"/assets/root/images/event_images/left.gif\" border=\"0\"></a></td>";
	calHTML += "<td width=600 colspan=5 align=center style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\">" + month_of_year[theMonth] + " " + theYear + "</td>";
	calHTML += "<td align=\"right\" style=\"background-color: " + cfgMonthBackground + "; font-weight: bold; font-family: " + cfgMonthFont + "; font-size: " + cfgMonthFontSize + "; color: " + cfgMonthFontColor + ";\"><a onClick=\"showEvent(' ');\"  href=\"javascript:DisplayCalendar(" + nextMonth + "," + nextYear + ")\"><img width=14 height=14 src=\"/assets/root/images/event_images/right.gif\" border=\"0\"></a></td>";
	calHTML += "</table>";
	
	
	calHTML += "</td>";
	calHTML += "</tr>";

	calHTML += "<tr>";
	for (var iDay = 0; iDay < 7; iDay++) {
		if (iDay == weekday && theMonth == month)
			calHTML += "<td style=\"font-family: " + cfgDayFont + "; font-size: " + cfgDayFontSize + "; color: " + cfgDayFontColor + "; background-color: " + cfgDayBackground + ";\"><b>" + day_of_week[iDay] + "</b></td>";  
		else
			calHTML += "<td style=\"font-family: " + cfgDayFont + "; font-size: " + cfgDayFontSize + "; color: " + cfgDayFontColor + "; background-color: " + cfgDayBackground + ";\">" + day_of_week[iDay] + "</td>";
	}
	calHTML += "</tr>";
	
	calHTML += "<tr valign=top>";
	
	// fill in blank days until day 1 of the month
	for(var index = 0; index < calendar.getDay(); index++) {
		calHTML += "<td style=\"background-color: " + cfgBlankBackground + ";\"></td>";
	}

	// set current event to start of the month
	if (gEventList.getCurrentEvent() != null) {
		while(gEventList.getCurrentEvent().date < calendar -1) {
			gEventList.popEvent();
			
			if (gEventList.getCurrentEvent() == null)
				break;
		}
	}
	
	for(var index = 0; index < 31; index++) {
		if (calendar.getDate() > index) {
			week_day = calendar.getDay();




			
			if (week_day == 0)
				calHTML += "<tr valign=top>"
				
			if (week_day != 7) {
				var day = calendar.getDate();
				
				
				if (today == day && theMonth == month) {
					calHTML += "<td width='14%' style=\"height: " + cfgCellMinHeight + "px; color: " + cfgTodayFontColor + "; background-color: " + cfgTodayBackground + ";\">"
					calHTML += "<b><span style=\"font-size: " + cfgCellNumFontSize + "\">" + day + "</span></b><br />";
										
				} else {
					if (gEventList.getCurrentEvent() != null && (gEventList.getCurrentEvent().date.getDate() == day && gEventList.getCurrentEvent().date.getMonth() == theMonth)) {
						calHTML += "<td width='14%' style=\"height: " + cfgCellMinHeight + "px; color: " + cfgEventCellFontColor + "; font-size: " + cfgEventCellFontSize + "; background-color: " + cfgEventCellBackground + ";\">"
						calHTML += "<b><span style=\"font-size: " + cfgEventCellNumFontSize + "px;\">" + day + "</span></b><br />";					
					} else {
						calHTML += "<td width='14%' style=\"height: " + cfgCellMinHeight + "px; font-size: " + cfgEventCellFontSize + "; background-color: " + cfgCellBackground + ";\">"
						calHTML += "<b><span style=\"font-size: " + cfgCellNumFontSize + "px;\">" + day + "</span></b><br />";					
					}
				}
				
				// might be more than one event, so iterate through events)
				if(gEventList.getCurrentEvent() != null) {
					var numTimes = 0;
					while(gEventList.getCurrentEvent().date.getDate() == day && gEventList.getCurrentEvent().date.getMonth() == theMonth) {
						
						// horrible spacing hack
						if (numTimes > 0)
							calHTML += "<div style=\"font-size: 1px; height: 10px; width: 1px;\"></div>";
						else
							calHTML += "<div style=\"font-size: 1px; height: 4px; width: 1px;\"></div>";
								
						/*var desc = gEventList.getCurrentEvent().desc;
						desc = desc.replace(/'/g,'\\u0027');
						desc = desc.replace(/"/g,'\\u0022');*/
						
						var html = gEventList.getCurrentEvent().getHTML();
						html = html.replace(/'/g,'\\u0027');
						html = html.replace(/"/g,'\\u0022');
							
						calHTML += "<span style=\"cursor: pointer; text-decoration: underline;line-height:1.2;margin: 0px 3px;display:block;\" onclick=\"showEvent('" + html + "');\">" + gEventList.getCurrentEvent().name + "</span>";
						
						gEventList.popEvent();
						
						numTimes++;
						
						// if next event is null, break out of loop
						if(gEventList.getCurrentEvent() == null)
							break;
					}
				}
				
				calHTML += "</td>"
			}
			
			if (week_day == 7)
				calHTML += "</tr>";
		}
		
		calendar.setDate(calendar.getDate() + 1);
	}
	
	for (var index = week_day; index < 6; index++) {
		calHTML += "<td style=\"background-color: " + cfgBlankBackground + ";\"></td>";
	}
	
	calHTML += "</tr></table>";
	
	var calDiv = document.getElementById("cal");
	calDiv.innerHTML = calHTML;


}

function showEvent(html) {

	var event = document.getElementById("event");
	


}


