// Simulates PHP's date function
Date.prototype.format = function (format) {
	var curChar, i, returnStr = '', replace = Date.replaceChars;
	for (i = 0; i < format.length; i++) {
		curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};
Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],

	// Day
	d: function () { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function () { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function () { return this.getDate(); },
	l: function () { return Date.replaceChars.longDays[this.getDay()]; },
	N: function () { return this.getDay() + 1; },
	S: function () { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function () { return this.getDay(); },
	z: function () { return "Not Yet Supported"; },
	// Week
	W: function () { return "Not Yet Supported"; },
	// Month
	F: function () { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function () { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function () { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function () { return this.getMonth() + 1; },
	t: function () { return "Not Yet Supported"; },
	// Year
	L: function () { return "Not Yet Supported"; },
	o: function () { return "Not Supported"; },
	Y: function () { return this.getFullYear(); },
	y: function () { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function () { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function () { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function () { return "Not Yet Supported"; },
	g: function () { return this.getHours() % 12 || 12; },
	G: function () { return this.getHours(); },
	h: function () { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function () { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function () { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function () { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function () { return "Not Yet Supported"; },
	I: function () { return "Not Supported"; },
	O: function () { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; },
	T: function () { return "Not Yet Supported"; },
	Z: function () { return this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function () { return "Not Yet Supported"; },
	r: function () { return this.toString(); },
	U: function () { return this.getTime() / 1000; }
};


/* SVN FILE: $Id: visitnc.search.js 3576 2011-02-19 21:12:36Z michaelklauss $ */
/* -- date picker -- */
var Picker = Class.create();
Picker.prototype = {
	button_back    : 'cal_back',
	button_next    : 'cal_next',
	calendar       : 'cal',
	calendar_title : 'cal_title',
	calendar_div   : 'cal_element',
	selecteddate   : null,
	months         : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	monthlen       : [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
	days           : ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
	today          : new Date(),
	current_year   : new Date().getFullYear(),
	current_month  : new Date().getMonth(),
	callback       : null,
	cal_root       : null,
	initialize: function (start, callback, ele) {
		var that = this;
		if (callback) { this.callback = callback; }
		this.today.setHours(0);
		this.today.setMinutes(0);
		this.today.setSeconds(0);
		this.today.setMilliseconds(0);
		var s = new Date(start);
		if (!s.getDate()) {
			s = new Date();
		}
		s.setHours(0);
		s.setMinutes(0);
		s.setSeconds(0);
		s.setMilliseconds(0);
		this.current_month = s.getMonth()+1;
		this.current_year  = s.getFullYear();
		this.selecteddate = s;
		// create & position root calendar element
		this.removeCal(); // kill existing
		var style = {'zIndex': '100000', 'display': 'none', 'position': 'absolute', 'top': 0, 'left': 0};
		if (ele) {
			var pos    = Element.cumulativeOffset(ele);
			style.top  = pos.top + 'px';
			style.left = (pos.left - 10) + 'px';
		}
		var div = new Element('div', {'id': 'cal_root'});
		this.cal_root = div;
		div.setStyle(style);
		this.render(this.current_month, this.current_year);
		this.cal_root.observe('mouseleave', function () {
			that.removeCal();
		});
	},
	removeCal: function () {
		if ($('cal_root')) {
			this.cal_root.stopObserving();
			$('cal_root').remove();
			this.cal_root = null;
		}
	},
	selectDate : function (d) {
		this.selecteddate = d;
		this.render(this.current_month, this.current_year);
		if (this.callback !== null) {
			this.callback(this.selecteddate);
		}
		this.removeCal();
	},
	move : function (n) {
		var m = this.current_month + n;
		var y = this.current_year;
		if (m === 13) {m = 1; y++;}
		if (m === 0) {m = 12; y--;}
		this.current_year  = y;
		this.current_month = m;
		this.render(m, y);
	},
	render : function (month, year) {
		var i, that = this;// clear current calendar
		this.cal_root.update();
		// css display elements
		var div1 = new Element('div', {'id': 'cal_element'});
		// nav prev month
		var eleBack = new Element('a', {'id': 'cal_back'});
		eleBack.update('&lt;');
		eleBack.observe('click', function () {that.move(-1);});
		div1.insert(eleBack);
		// month title
		var cal_title = new Element('div', {'id': 'cal_title'});
		cal_title.update(this.months[month - 1] + ' ' + year);
		div1.insert(cal_title);
		// nav next month
		var eleNext = new Element('a', {'id': 'cal_next'});
		eleNext.update('&gt;');
		eleNext.observe('click', function () {that.move(1);});
		div1.insert(eleNext);
		// calendar table
		var cal = new Element('table', {'id': 'cal','border': 0, 'cellspacing': 0});
		div1.insert(cal);
		// user prompt
		var p = new Element('p');
		p.update('<strong>Click<\/strong> date to select.');
		div1.insert(p);
		// footer
		this.cal_root.insert(div1);
		var nodes = cal.descendants();
		nodes.each(function (node) {node.remove();});
		var monthStart = new Date(year, month - 1, 1);    // first day of the month
		monthStart.firstDay = monthStart.getDay() + 1;  // day of week that our month starts
		// find Feb leap year
		this.monthlen[1] = (((monthStart.getFullYear() % 100 !== 0) && (monthStart.getFullYear() % 4 === 0)) || (monthStart.getFullYear() % 400 === 0))? 29 : 28 ;
		// css class for start & end of week
		var cls = ['nol', '', '', '', '', '', 'nor'];
		// build table & header
		var tbody = new Element('tbody');
		var tr  = new Element('tr');
		for (i = 0; i < 7; i++) {
			var th = new Element('th', {'class': cls[i]});
			th.update(this.days[i]);
			tr.insert(th);
		}
		tbody.insert(tr);
		// how many rows of days in this cal?
		var rows = Math.ceil(((monthStart.firstDay - 1) + this.monthlen[month - 1]) / 7);
		var ii = 1;
		for (j = 0; j < rows; j++) {
			tr = new Element('tr');
			for (i = 0; i <= 6; i++) {
				var day = ((ii - monthStart.firstDay >= 0) && (ii - monthStart.firstDay < this.monthlen[month - 1]) )? ii - monthStart.firstDay + 1 : null ;
				var td = new Element('td');
				td.writeAttribute({'title': 'Click to select date.'});
				td.thisdate = (day === null)? null : new Date(year, month - 1, day);
				td.addClassName(cls[(ii - 1) % 7]); // set class for first && last day of week
				if (j == (rows - 1)) {td.addClassName('nob');} // last row gets last row class
				if (td.thisdate !== null) {
					if (td.thisdate.valueOf() == this.selecteddate.valueOf()) {
						td.addClassName('sel');
						td.setStyle({'fontStyle': 'italic'});
					}
					if (this.today.valueOf() == td.thisdate.valueOf()) {
						td.addClassName('today');
						td.setStyle({'fontWeight': 'bold'});
					}
					td.update(day);
					td.observe('click', function (evt) {
						var ele = evt.element();
						that.selectDate(ele.thisdate);
					});
				} else {
					td.update(day);
				}
				tr.insert(td);
				ii++;
			}
			tbody.insert(tr);
		}
		cal.insert(tbody);
		if (!document.body.insert) {Element.extend(document.body);}
		document.body.insert(this.cal_root);
		this.cal_root.show();
	}
};
