/**
JAVASCRIPT DOCUMENT
Version 2.0 - February 11, 2009

Sacramento Bike Hikers Ride Schedule script files

copyright 2009 by Big Bear Studios

Required class settings:
(The following class must be defined in the html page. The
 only attributed required is background-color.)

  dateCellNormal - normal cell's background color
   dateCellHover - cell's color with mouse over
dateCellSelected - the currently selected cell
       cellBlank - non-date cell's background color

*/

var weekDays = 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 oldCell = null;

var startUpCalendar = {
	onPostUpdate:function(notifier, data)
	{
		var now2 = new Date();
		var newSelection = document.getElementById("SelMonth");
		
		// find the current month on the options list
		for(j=0; j<newSelection.options.length; j++){
			if (now2.getMonth() == newSelection.options[j].id){
				break;
			}
		}
		if (j != 0){
			// set new selection, triggering dsMonths observer
			newSelection.options[j].selected = true;
			dsMonths.setCurrentRowNumber(j);
		}
		else{
			//no change so force calendar build
			buildCalendar();
		}
		
	}
}
		
Spry.Data.Region.addObserver("monthList", startUpCalendar);	//dsMonths dataset

dsRides.addObserver(function(nType, notifier, data){
	if (nType == "onPostLoad"){
		//alert("the row has changed ...");
		buildCalendar();
	}
});

function cellSelect(cell,y,m,d){
	// argument - y, year, integer, e.g. 2009
	// argument - m, month, integer, 0-11
	// argument - d, date, string, "1" - "31"
	
	function listFilter(ds,row,rowNumber){
		var rideDate = row["dateCode"];
		if (dateStr == rideDate)
			return row;
		else
			return null;
	}

	// build the date string - "Friday, June 3"
	var today = new Date(y,m,Number(d));
	var dateStr = weekDays[today.getDay()] + months[today.getMonth()] + " " + today.getDate();
	if (oldCell != null)
		oldCell.className = "dateCellNormal";// clear the last selected cell's background
	cell.className = "dateCellSelected";
	oldCell = cell;
	dsRides.filter(listFilter);
	
	// set the first rides as the current row so the detail region updates
	var rows = dsRides.getData();
	if (rows.length != 0)
		dsRides.setCurrentRow(rows[0]["ds_RowID"]);
	//return dateStr;
	
}

function buildCalendar(){
	var spacer = "\240"; // Non-breaken Spacer, e.g. &nbsp;
	var curMonthInfo = dsMonths.getCurrentRow();
	var curMonth = Number(curMonthInfo["rMonth"]);
	var curYear = Number(curMonthInfo["Year"]);
	var daysInMonth = Number(curMonthInfo["Days"]);
	var MonthStr = curMonthInfo["MonthName"];
	var thisMonth = new Date(curYear,curMonth);
	var firstDay = thisMonth.getDay();
	var calendarTable = document.getElementById("calendarTable");
	var cells = calendarTable.getElementsByTagName("td");
	
	function setCurrentDate(){
		// function sets the selected date on the just build
		// calendar. If it is the current month then it set
		// the date to the current date, otherwise it sets
		// it to the first day
		var today = new Date();
		var i = 0;
		var targetDate = 0;
		
		oldCell = null;
		
		if (today.getMonth() == curMonth)
			targetDate = today.getDate()
		else
			targetDate = 1
		for(i=0; i<cells.length; i++){
			if (cells[i].innerHTML == String(targetDate)){
				cellSelect(cells[i],curYear,curMonth,String(targetDate));
				break;
			}
		}
	}
	
	function selectedDate(y,m,d){
		// y - argument, integer , e.g. 2009
		// m - argument, integer, 0-11
		// d - arguemnt, string, the selected date 1-31
		// thisDay, date, the current working date
		// dow, integer, the day of the week 0-6
		var today = new Date(y,m,Number(d));
		var dateStr = weekDays[today.getDay()] + months[today.getMonth()] + " " + today.getDate();
		return dateStr;
	}
	
	//alert("the first day of the month is: " + firstDay);
	//alert("There are " + cells.length + " in the table");
	//alert("Node Name: " + cells[0].nodeName);
	//var cellText = document.createTextNode("XXX");
	//cells[0].appendChild(cellText);
	// Clear out the old calendar
	for(i=7; i<cells.length; i++){
		//alert("for ... " + i);
		var newText = document.createTextNode(spacer);
		var oldText = cells[i].firstChild;
		//alert("for... '" + oldText.nodeValue + "'");
		cells[i].replaceChild(newText,oldText);
		cells[i].onclick = null;
		cells[i].onmouseover = null;
		cells[i].onmouseout = null;
		//cells[i].className = ""
		cells[i].className = "cellBlank";
	}
	//alert("Days in the Month: " + daysInMonth);
	//alert("Calendar Start: " + calendarStart + "   Calendar End: " + calendarEnd);
	var calendarStart = 7 + firstDay;
	var calendarEnd = calendarStart + (daysInMonth);
	var date = 1;//calendar dates, starting a 1
	for(i=calendarStart; i<calendarEnd; i++){
		var cellText = document.createTextNode(date);
		var oldText = cells[i].firstChild;
		cells[i].replaceChild(cellText,oldText);
		//cells[i].style.backgroundColor = bgDate;
		cells[i].className = "dateCellNormal"
		cells[i].onclick = function(){
			//alert("cell Date: " + this.innerHTML);
			cellSelect(this,curYear,curMonth,this.innerHTML);
			//alert("The Date String: '" + selectedDate(curYear,curMonth,this.innerHTML) + "'");
		}
		cells[i].onmouseover = function(){
			if (this.className != "dateCellSelected")
				this.className = "dateCellHover";
		}
		cells[i].onmouseout = function(){
			//alert("background: " + this.style.backgroundColor + "  bgSelected: " + bgSelected);
			if (this.className != "dateCellSelected")
				this.className = "dateCellNormal";
		}
		date = date + 1;
	}
	setCurrentDate();
}
