/**
 * $Id: calendar.js,v 1.2 2008/05/16 13:49:15 ord Exp $
 *
 * CalendarMonth class.
 */
function getTableNode(e)
{
	return(e.parentNode.parentNode.parentNode.parentNode);
}

/**
 * Constructor
 *
 * @param id - ???
 * @param year - full year
 * @param month
 */
function CalendarMonth(tableNode, id, year, month, tableType, period)
{
	this.id = id;
	this.year = year;
	this.month = month;
	this.period = period;
	this.tableType = tableType;

	this.tableNode = tableNode;
	// The div, or what not that should contain the
	this.parentId = "theparentid";


// alert(tableNode);
	// alert("Setting id to " + this.id);
	this.render = CalendarMonth_render;
	this.advanceMonth = CalendarMonth_increaseMonth;
	this.decreaseMonth = CalendarMonth_decreaseMonth;
	this.increaseMonth = CalendarMonth_increaseMonth;
	this.decreaseYear = CalendarMonth_decreaseYear;
	this.increaseYear = CalendarMonth_increaseYear;
}

/**
 * New function, for creating the calendar month completely with javascript.
 */
function CalendarMonth_createTable()
{

	table = document.createElement('table');
	for( week = 1; week < 6; week++ )
	{
		row = document.createElement('tr');
		for( day = 1; day < 7; day++ )
		{
			cell = document.createElement('td');
			row.appendChild(cell);
		}
		table.appendChild(row);
	}
}

/**
 *
 * 
 */
function CalendarMonth_render()
{
	var names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
	var cell;
	var date = new Date();
	var time;

	// Initialise the date.
	date.setDate(1);
	date.setFullYear(this.year);
	date.setMonth(this.month - 1);
	date.setHours(3); // to avoid daylight savings problems.
	date.setMinutes(0);
	date.setSeconds(0);
	date.setMilliseconds(0);
	time = date.getTime();

	var prevDate = getPrevMonth(date);
	var nextDate = getNextMonth(date);

	// Replace the text in the headers
	// var node = document.getElementById(this.id + "_prevmonth");
	// setCellText(node, names[prevDate.getMonth()] );

	node = document.getElementById(this.id + "_curr");
	setCellText(node, names[(date.getMonth())] + " " + date.getFullYear() );

	//node = document.getElementById(this.id + "_next");
	//setCellText(node, names[(nextDate.getMonth())] );


	// Get the rows.
	// Note, this includes the rows of table headers,
	// so they must be skipped.

	// Bugfix: Depending on if there are text nodes the offset can vary.
	// Look for the child node with TBODY instead of guessing it's offset.
	length = this.tableNode.childNodes.length;
	for(i = 0; i < length && this.tableNode.childNodes[i].nodeName != "TBODY"; i++){}
	rows = this.tableNode.childNodes[i].childNodes;

	index = 1;	
	for(i = 0; i < rows.length; i++)
	{
		cells = rows[i].childNodes;
		for(j = 0; j < cells.length; j++)
		{
			cell = cells[j];
			if(cell.nodeName == "TD")
			{
				// Clear out anything after the month
				if(date.getMonth() != this.month - 1)
				{
					clearCell(cell);
				} else if(index < date.getDay()) {
					// Clear out anything before the month starts
					clearCell(cell);				
				} else {
					setCellContent(cell, date, this.tableType, this.period);
					time = (time + ((86400) * 1000));
					date.setTime(time);
				}
				index++;
			}
		}
	}
}


function CalendarMonth_decreaseMonth()
{
	this.month--;
	if(this.month == 0)
	{
		this.month = 12;
		this.year--;
	}
	this.render();
}

function CalendarMonth_decreaseYear()
{
	this.year--;
	this.render();
}

function CalendarMonth_increaseMonth()
{
	this.month++;
	if(this.month == 13)
	{
		this.month = 1
		this.year++
	}
	this.render();
}

function CalendarMonth_increaseYear()
{
	this.year++;
	this.render();
}


function getCellId(calid, i)
{
	return(calid + "_day_cell" + i);
}


function setCellText(cell, text)
{
	// document.write("Cell is: " + cell + "<br />");
	if(cell.childNodes.length > 0)
	{
		cell.childNodes[0].nodeValue = text;
	} else {
		cell.appendChild(document.createTextNode(text));
	}
}

function setCellContent(cell, date, table_type, period)
{
	var hrefNode = cell.childNodes[0];
	var text = date.getDate();

	var month = date.getMonth() + 1;
	var day   = date.getDate();
	if(month <= 9)
		month = '0' + month;
	if(day <= 9)
		day = '0' + day;

	var target=table_type + "_" + period
	var href = period + "-" + (date.getFullYear()) + month + day + ".html" + "#" + target;

	// document.write("Cell is: " + cell + "<br />");
	if(hrefNode.childNodes.length > 0)
	{
		hrefNode.childNodes[0].nodeValue = text;
	} else {
		hrefNode.appendChild(document.createTextNode(text));
	}
	hrefNode.href=href;
	// Set the date property.
	hrefNode.date = new Date(date);
}

function clearCell(cell)
{
	var hrefNode = cell.childNodes[0];
	var text = "";
	var href = "#";

	// document.write("Cell is: " + cell + "<br />");
	if(hrefNode.childNodes.length > 0)
	{
		hrefNode.childNodes[0].nodeValue = text;
	} else {
		hrefNode.appendChild(document.createTextNode(text));
	}
	hrefNode.href=href;
}

function getPrevMonth(date)
{
	var ret = getNavDate(date, "prev");
	return(ret);
}

function getNextMonth(date)
{
	var ret = getNavDate(date, "next");
	return(ret);
}

function getNavDate(date, dir)
{
	var ret = new Date;

	// ret.setMillisecond(0);
	ret.setSeconds(date.getSeconds());
	ret.setMinutes(date.getMinutes());
	ret.setHours(date.getHours());
	ret.setDate(date.getDate());
	ret.setMonth(date.getMonth());
	ret.setFullYear(date.getFullYear());

	m = ret.getMonth();
	time = ret.getTime();
	while( ret.getMonth() == m)
	{
		if( dir == "prev" )
			time = time - (24*60*60*1000);
		else
			time = time + (24*60*60*1000);
		ret.setTime(time);
	}
	if( dir == "prev" )
	{
		// document.write("Going back in time<br />");
		time = time - (24*60*60*1000);
		ret.setTime(time);
	}
	// document.write(dir + " " + ret.toString() + "<br />");
	return(ret);
}

function nextMonth()
{
	var date = getNextMonth(global_curr_date);
	
//	setMonth(date.getYear() + 1900, date.getMonth() + 1);
	setMonth(2007, 6);
}



