/* JavaScript Document
   Author: Steven Grindle
   Date created:   2008 Dec 3
   Date last modified: 2009 May 30
   Purpose: Make a continuous (months merge with no gaps) calendar based on weeks which beginning week based on the client system timedate and the ending week is either a fixed number of weeks after the beginning week or a fixed ending week.
*/

function write_calendar(){
	//Javascript writes the html tags for a table which is always 7 columns (days of the week) wide and each row is a consecutive week with no gaps for different months.
	document.write("<table border='1' id='write_weeks'>");
	document.write("<caption>")
	document.write("<strong style='font-size:.8em;'>");
	document.write(monthName[sunday_previous_week.getMonth()]);
	document.write("&nbsp;");
	document.write(sunday_previous_week.getDate());
	document.write(", ");
	document.write(sunday_previous_week.getFullYear());
	document.write("&nbsp;&nbsp;to&nbsp;&nbsp;");
	document.write(monthName[date_stop.getMonth()]);
	document.write("&nbsp;");
	document.write(date_stop.getDate());	
	document.write(", ");
	document.write(date_stop.getFullYear());
	document.write("</strong>");
	document.write("</caption>");
	//optional caption
	document.write("<tr class='dow_header'>");
	days_of_week_header();
	//Write the days of the week in the first row
	document.write("</tr>");
	while (variable_timedate<date_stop){
		//the day to be entered in the calendar must be after the user defined beginning week
		if(variable_timedate<sunday_previous_week){
			document.write("<tr class='display_none'>");
			//don't write weeks (rows) before the Sunday of the previous week to current day
		}else{
			document.write("<tr>");
			//Start a new row (week)
		}
		write_week();
		//Write the 7 days (table cells of the week)
		document.write("</tr>");
	}
	document.write("</table>");
	//document.write(date_start+"<br />"+sunday_previous_week+"<br />"+s_p_w+"<br />"+date_stop);
	//diagnostic
}

function days_of_week_header(){
	for (i=0; i < 7; i++) {
		document.write("<td>"+day_name[i]+"</td>");
	}
}

function write_week(){
	//Write cells in the current row (week) of the calendar.
	for (i=0; i < 7; i++){
		integer_timedate = variable_timedate.getTime()
		if(integer_timedate == system_today){
			document.write("<td class='backg_seed' onmouseover='show_content("+integer_timedate+")' onmouseout='blankit("+integer_timedate+")'>");
			//if the variable_timedate is today, make a distinct background for the cell
		}
		else if(variable_timedate.getMonth()/2-Math.floor(variable_timedate.getMonth()/2)== 0){
			document.write("<td class='backg_odd' onmouseover='show_content("+integer_timedate+")' onmouseout='blankit("+integer_timedate+")'>");
			//if the variable_timedate is an odd numbered month, use a particular cell sytle that distinguishes odd numbered months from even numbered months.
			//It is absolutely necessary to make odd/even numbered months distinguishable BOTH on the web page AND the printed black and white hard copy since there are NO GAPS between months. 
		}
		else{
			document.write("<td class='backg_even' onmouseover='show_content("+integer_timedate+")' onmouseout='blankit("+integer_timedate+")'>");
			//if the variable_timedate is an even numbered month, a different particular sytle than odd 
		}
		document.write("<div id='"+integer_timedate+"'>");
		if((variable_timedate.getDate() == 1) || (integer_timedate == system_today)){
			//if it is the first day of the month or today's date, write year-month-date in small caps
			document.write("<strong style='font-size:.8em;'>");
			document.write(variable_timedate.getFullYear());
			document.write("&nbsp;");
			document.write(monthName[variable_timedate.getMonth()]);
			document.write("&nbsp;");
			document.write(variable_timedate.getDate());
			document.write("</strong>");
		} else if((variable_timedate.getDay() == 0) || (variable_timedate.getDay() == 6)){
			//if it is Sunday or Saturday write the month-date
			document.write(monthName[variable_timedate.getMonth()]);
			document.write("&nbsp;");
			document.write(variable_timedate.getDate());
		}
		else {
			document.write(variable_timedate.getDate());
			//default is write just the date.
		}
		//document.write("<br />hello");  //may need an if statement here to write the event into the calendar instead of using innerHTML ??
		document.write("</div>");
		document.write("</td>");
		variable_timedate.setTime(variable_timedate.getTime() + 86400000);
		//Change the variable_timedate to the next day
		//1 day x 24 hours/day x 60 minutes/hour x 60 seconds/minute x 1000 milliseconds/second = 86,400,000 milliseconds
		variable_timedate.setHours(12,0,0,0); //set time of variable_timedate to exactly noon in case of a timeshift due to daylight savings adjustments.
		}
}

function show_content(integer_timedate){ //show the content of the calendar cell in <div id="information">
			document.getElementById(integer_timedate).style.backgroundColor = "#666666";
			document.getElementById(integer_timedate).style.color = "#ffffff";			
			the_date = new Date();
			the_date.setTime(integer_timedate)
			date_text = the_date.getFullYear()+"&nbsp;"+monthName[the_date.getMonth()]+"&nbsp;"+the_date.getDate()
			//document.getElementById("information").innerHTML = date_text
			if(document.getElementById(integer_timedate+1)!=null){
				document.getElementById(integer_timedate+1).style.backgroundColor = "#666666";
				document.getElementById(integer_timedate+1).style.color = "#ffffff";
				//document.getElementById("information").innerHTML = document.getElementById(integer_timedate+1).innerHTML;
			}
}


function blankit(integer_timedate){ //reset the calendar cell
	//document.getElementById("information").innerHTML = "--";
	document.getElementById(integer_timedate).style.backgroundColor = "";
	document.getElementById(integer_timedate).style.color = "";			
	if(document.getElementById(integer_timedate+1)!=null){
		document.getElementById(integer_timedate+1).style.backgroundColor = "";
		document.getElementById(integer_timedate+1).style.color = "";
	}
}

function write_events(){
	//date_object = new Date();
	//syntax: create a date object equal to system timedate
	//var date_start = new Date(2009,4-1,19,12,0,0,0);
	//syntax: create a date object equal to Apr 19, 2009 at noon 
	document.write("<ul>");
	for (i=0;i<event_list.length;i++){
		//loop thru all events in Array event_list[] 
		//text_slice = event_list[i].slice(0,11);
		//slice the first 12 characters from each member of the array, 
		//The first 12 character MUST contain yyyy mmm dd or yyyy mmm d
		text_slice_year = event_list[i].slice(0,4); //4 digt year
		text_slice_month = event_list[i].slice(5,8); //3 letter month
		month_number=month3names.indexOf(text_slice_month,0)/3;
		//convert 3-letter month to month number 0 to 11
		text_slice_date = event_list[i].slice(9,11); //date number
		date_ev = new Date(text_slice_year,month_number,text_slice_date,12,0,0,0);
		//create the date object date_ev equal to noon of the date of the event
		//date_month=new Date(text_slice_year,month_number,text_slice_date,12,0,0,0).getMonth();
		//diagnostic to check that month_number is correct
		date_id = date_ev.getTime()+1;
		//date_id is an integer for uniquely tagging the list item 1 more millisecond than the calendar id
		if (date_ev < sunday_previous_week){
			//hide events before the sunday_previous_week, list events afterwards
			//consider skipping rather than hiding events in the list
			//only need to hide events if scrolling to hidden events is allowed.
			//alert_date(sunday_previous_week,2);
			//diagnostic to check correct calculation of sunday_previous_week
			document.write("<li id='"+date_id+"' onmouseover='highlight("+date_id+")' onmouseout='unhighlight("+date_id+")' class='display_none'>");
			document.write(event_list[i])
			//document.getElementById(date_id).innerHTML = [i]; 
			//will need this if user is allowed to scroll to hidden events
		}
		else{
			//write events into the list AND the calendar
			document.write("<li id='"+date_id+"' onmouseover='highlight("+date_id+")' onmouseout='unhighlight("+date_id+")'>");
			document.write(event_list[i])
			document.getElementById(date_id-1).innerHTML += "<br />"+event_list[i].slice(11);
			//write the event into the calendar
		}
		document.write("</li>");
	}
	document.write("</ul>");
}

function highlight(tdid){
	//alert(tdid);
	//alert_date(tdid,2);
	//diagnostic to check correct calculation of tdid
	document.getElementById(tdid).style.backgroundColor = "#66FFCC";
	document.getElementById(tdid-1).style.backgroundColor = "#66FFCC";
	//document.getElementById("information").innerHTML = document.getElementById(tdid).innerHTML; 
	//display the "contents of the mouseovered <li id => in the "information" <div>
}

function unhighlight(tdid){
	document.getElementById(tdid).style.backgroundColor = "";
	document.getElementById(tdid-1).style.backgroundColor = "";	
}


