/**
 * Class to wrap the functionality of the Date class.
 * Provides ability to format properties of date objects into
 * more user friendly notations.
 *
 * @version 1.0
 */
function DateFormat() {
	this.date = new Date();
	this.universal = false;
	this.dayNamesShort = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	this.dayNamesLong = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	this.monthNamesShort = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	this.monthNamesLong = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

	/**
	 *
	 *
	 * @param format The format of the date string to return.
	 * @return A date string according to the specified format.
	 */
	this.getDate = function(format) {
		var dateString = '';
		var date = null;
		var day = null;
		var month = null;
		var year = null;
		var hour = null;
		var minute = null;
		var second = null;

		if(this.universal) {
			date = this.date.getUTCDate();
			day = this.date.getUTCDay();
			month = this.date.getUTCMonth();
			year = this.date.getUTCFullYear();
			hour = this.date.getUTCHours();
			minute = this.date.getUTCMinutes();
			second = this.date.getUTCSeconds();
		}
		else {
			date = this.date.getDate();
			day = this.date.getDay();
			month = this.date.getMonth();
			year = this.date.getFullYear();
			hour = this.date.getHours();
			minute = this.date.getMinutes();
			second = this.date.getSeconds();
		}

		for(var i = 0; i < format.length; i++) {
			switch(format.charAt(i)) {

				// Two digit day of the month, with leading zero: 01 to 31
				case 'd':
					if(date < 10) {
						dateString += '0';
					}

					dateString += date;
					break;

				// Three letter abbreviated day name: Sun to Sat 
				case 'D':
					dateString += this.dayNamesShort[day];
					break;

				// Day of the month, without leading zero: 1 to 31
				case 'j':
					dateString += date;
					break;

				// Full day name: Sunday to Saturday
				case 'l':
					dateString += this.dayNamesLong[day]	
					break;

				// Ordinal suffix for the day of the month, 2 characters: st, nd, rd or th
				case 'S':
					if((date == 1) || (date == 21) || (date == 31)) {
						dateString += 'st';
					}
					else if((date == 2) || (date == 22)) {
						dateString += 'nd';
					}
					else if((date == 3) || (date == 23)) {
						dateString += 'rd';
					}
					else {
						dateString += 'th';
					}

					break;

				// Day of the week: 0 (Sunday) to 6 (Saturday)
				case 'w':
					dateString += day;
					break;

				// Full month name: January to December
				case 'F':
					dateString += this.monthNamesLong[month];
					break;

				// Two digit month of the year, with leading zero: 01 to 12
				case 'm':
					if(month < 9) {
						dateString += '0';
					}

					dateString += (month + 1);
					break;

				// Three letter abbreviated month name: Jan to Dec
				case 'M':
					dateString += this.monthNamesShort[month];
					break;

				// Month of the year, without leading zero: 1 to 12
				case 'n':
					dateString += (month + 1);
					break;

				// Four digit year: 1901 to 2038
				case 'Y':
					dateString += year;
					break;

				// Two digit abbreviated year: 01 to 99 (1999) then 00 to 38 (2038)
				case 'y':
					dateString += year.toString().substring(2);
					break;

				// 12-hour format hour of the day, without leading zero: 1 to 12
				case 'g':
					if(hour > 11) {
						dateString += ((hour + 1) - 12);
					}
					else { 				   						 
						dateString += (hour + 1);
					}

					break;

				// 24-hour format hour of the day, without leading zero: 0 to 23
				case 'G':					 
					dateString += hour;
					break;
					
				// 12-hour format hour of the day, with leading zero: 01 to 12
				case 'h':
					if((hour < 9) || ((hour > 11) && (hour < 21))) {
						dateString += '0';
					}

					if(hour > 11) {
						dateString += ((hour + 1) - 12);
					}
					else { 				   						 
						dateString += (hour + 1);
					}

					break;

				// 24-hour format hour of the day, with leading zero: 00 to 23
				case 'H':
					if(hour < 9) {
						dateString += '0';
					}
					 
					dateString += hour;
					break;

				// Minute of the hour, with leading zero: 00 to 59
				case 'i':
					if(minute < 9) {
						dateString += '0';
					}

					dateString += minute;
					break;

				// Second of the minute, with leading zero: 00 to 59
				case 's':
					if(second < 9) {
						dateString += '0';
					}
					
					dateString += second;
					break;

				// Seconds since 1/1/1970 00:00:00 GMT: 1104497999
				case 'U':
					dateString += this.date.getTime();
					break;

				default:
					dateString += format.charAt(i);
					break;
			}
		}

		return dateString;
	}

	/**
	 * Sets whether formatted date strings should return values according to
	 * universal time, otherwise known as Greenwich Mean Time (GMT).
	 *
	 * @param universal True to return date strings according to universal time.
	 */
	this.setUniversalTime = function(universal) {
		this.universal = universal
	}
}