// Extend the built in Date object be more clever

/*
instance functions are:

date.daysInMonth	= return the number of days in the current month
date.dayName		= return name of current day
date.monthName		= return name of current month
date.shortMonthName	= return short name of current month
date.isLeapYear		= return true / false for current year
date.yearStartDay	= return number of first day of current year 0-6. 0=Sunday
date.monthStartDay	= return number of first day of current month 0-6. 0=Sunday
date.smallString	= return "1st jan 2005"

static functions:
Date.monthName(int 0-11)	= returns name of month
Date.shortMonthName(int 0-11)	= returns name of month

Date.dayName(int 0-6)		= returns name of day
Date.toDays = date % 86400000

*/



new Date();	// to force creation of a prototype if it's not already been done.

Date.monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
Date.shortMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
Date.dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

Date.monthStartDayData=     [0,3,3,6,1,4,6,2,5,0,3,5];
Date.monthStartDayDataLeap= [6,2,3,6,1,4,6,2,5,0,3,5];

Date.daysInMonthsData = [31,28,31,30,31,30,31,31,30,31,30,31];

Date.prototype.daysInMonth = function() {		// a class method.  call with Calendar.daysInMonth();	
	
	var month = this.getMonth();		// 0 - 11
	var year = this.getFullYear();	// for digit year
	
	if (month != 1) {	// It's not February
		return Date.daysInMonthsData[month]
		
	} else {
		// figure out if we are a leap year ot not - we want to know about February
		if (this.isLeapYear()) {
			return 29;	
		} else {
			return 28;	
		}		
	}
}

Date.prototype.toDays = function() {
	return parseInt(this.valueOf() / 86400000);	
}


// static functions
Date.dayName = function(day) {
	return 	Date.dayNames[day];
}
Date.monthName = function(month) {
	return 	Date.monthNames[month];
}
Date.shortMonthName = function(month) {
	return 	Date.shortMonthNames[month];
}


Date.prototype.monthName = function() {
	// returns the name of the current month
	return Date.monthName(this.getMonth());	
}

Date.prototype.shortMonthName = function() {
	// returns the name of the current month
	return Date.shortMonthName(this.getMonth());	
}

Date.prototype.dayName = function() {
	// returns the name of the current month
	return Date.dayName(this.getDay());	
}



Date.prototype.smallString = function() {
	// return e.g. 1st Jan 2005	
	
	return this.getDate() + this.getDate().valueOf().dateEnding() + " " + this.shortMonthName() + " " + this.getFullYear();
}

Date.prototype.isLeapYear = function() {		// a class method call with Calendar.isLeapYear();
	var year = this.getFullYear();
	var leap = false;
	
	if (
			(year % 4 ==0) &&			// leap years must be divisable by 4
			(
				(year % 100 !=0)		// if they are also divisiable by 100, 
				||						// then they must also be divisable by 
				(						// 400 to still be leap year
					(year % 100 ==0)
					&&
					(year % 400 ==0)
				)
			)
		) {
		
		leap = true	
	}
		
	return leap;
}





Date.prototype.yearStartDay = function() {
	
	// returns 0-6. 0 = Sunday
	
	// divide the year by twelve, add the remainder to the quotient,
	// and add the number of times four goes into the remainder.
	// e.g. For 1978: 78/12 = 6 remainder 6. 4 goes into 6 once and 6 + 6 + 1 = 13, which is the same day as 6.
	
	
	var year = this.getFullYear();
	
	var smallYear = year % 100;
	
	var remainder = smallYear % 12;
	var quotient = parseInt(smallYear / 12);
	
	
	var dayOfWeek = (remainder + quotient + parseInt(remainder / 4)) % 7;
	
	return dayOfWeek; // 0-6
	
}


Date.prototype.monthStartDay = function () {
	var year = this.getFullYear();

	
	var smallYear = year % 100;
	
	var remainder = smallYear % 12;
	var quotient = parseInt(smallYear / 12);
	
	
	var monthExtra = this.isLeapYear()
					?	Date.monthStartDayDataLeap[this.getMonth()]
					:	Date.monthStartDayData[this.getMonth()]
					;
	//alert("year:" + year + " leap:" + this.isLeapYear() + " month:" + this.getMonth() + " smYear:" + smallYear + " remiander:" + remainder + " quotient:" + quotient + " extra:" + monthExtra);
	var dayOfWeek = (remainder + quotient + parseInt(remainder / 4) + monthExtra ) % 7;
	
	return dayOfWeek; // 0-6
}


