var calendar = function(){
	this.container = null;
	this.scriptUrl = null;
	this.showDate = null;
	this.options = {
		markedDate: null,
		onClick: 'void'
	}
}

calendar.prototype = {
	Register: function(containerId, scriptUrl, options)
	{
		for (var n in options) {
			this.options[n] = options[n];
		}
		this.container 	= $elm(containerId);
		this.scriptUrl 	= scriptUrl;
		var date = new Date();
		this.showDate = {
			month: 	(date.getMonth()+1),
			year: 	date.getFullYear()
		}
		this.Update();
		return this;
	},
	Update: function()
	{
		var self = this;
		jQuery.get(this.scriptUrl, {
			month: 		this.showDate.month,
			year: 		this.showDate.year,
			markedDate: this.options.markedDate,
			onClick:	this.options.onClick,
			fetchData:	this.options.fetchData
		}, function(data){
			self.container.innerHTML = data;
			window.setTimeout(function(){
				sIFR.replace(rockwell, {
					selector:	'.calendar #entryModifyCalendar h2',
					wmode:		'transparent',
					fitExactly:	true,
					thickness:	100,
					sharpness:	200,
					offsetTop:	-2,
					css: [
						'.sIFR-root { font-size: 13px; color: #505050; }',
						'a { color: #505050; text-decoration: none; }',
						'a:hover { color: #505050; text-decoration: none; }'
					]
				});
				sIFR.replace(rockwell, {
					selector:	'h2',
					wmode:		'transparent',
					fitExactly:	true,
					thickness:	100,
					sharpness:	200,
					css: [
						'.sIFR-root { font-size: 13px; color: #ffffff; }',
						'a { color: #ffffff; text-decoration: none; }',
						'a:hover { color: #ffffff; text-decoration: none; }'
					]
				});
			}, 1);
		});
	},
	Prev: function()
	{
		this.Navigate(-1);
	},
	Next: function()
	{
		this.Navigate(1);
	},
	Navigate: function(c)
	{
		var newMonth = this.showDate.month+c;
		var newYear  = this.showDate.year;
		if (newMonth < 1)
		{
			newMonth = 12;
			newYear--;
		}
		else if (newMonth > 12)
		{
			newMonth = 1;
			newYear++;
		}
		this.showDate = {
			month: newMonth,
			year:  newYear
		}
		this.Update();
		calendar.fn.activeCell = null;
	}
}

calendar.fn = {
	activeCell: null,
	SwapActive: function(cal_id, newActive)
	{
		if (this.activeCell == null) {
			this.activeCell = document.getElementById('cal_'+cal_id+'_active');
		}
		if (this.activeCell) {
			this.activeCell.className = this.activeCell.className.replace(' active','');
		}
		newActive.parentNode.className += ' active';
		this.activeCell = newActive.parentNode;
	}
}