var elementsCalendar = Class.create();
elementsCalendar.prototype = {
	
	initialize: function(options) 
	{
		this.params = new Object();
		
		this.param_default("inputField",     null);
		this.param_default("displayArea",    null);
		this.param_default("button",         null);
		this.param_default("eventName",      "click");
		this.param_default("ifFormat",       "%Y/%m/%d");
		this.param_default("daFormat",       "%Y/%m/%d");
		this.param_default("singleClick",    true);
		this.param_default("disableFunc",    null);
		this.param_default("dateStatusFunc", this.params["disableFunc"]);
		this.param_default("firstDay",       0); 
		this.param_default("align",          "Br");
		this.param_default("range",          [1900, 2999]);
		this.param_default("weekNumbers",    true);
		this.param_default("flat",           null);
		this.param_default("flatCallback",   null);
		this.param_default("onSelect",       null);
		this.param_default("onClose",        null);
		this.param_default("onUpdate",       null);
		this.param_default("date",           null);
		this.param_default("showsTime",      false);
		this.param_default("timeFormat",     "24");
		this.param_default("electric",       true);
		this.param_default("step",           2);
		this.param_default("position",       null);
		this.param_default("cache",          false);
		this.param_default("showOthers",     false);
		
		
		this.options = options;
		this.calendar = new Calendar(1, new Date(), this.dateChanged.bind(this),this.closeCalendar.bind(this));
		this.calendar.showsTime = this.params.showsTime;
		this.calendar.time24 = "24";
		this.calendar.params = this.params;
		this.calendar.weekNumbers = true;
		this.calendar.setRange(new Date().getFullYear(), 2999);
		this.calendar.setDateStatusHandler(null);
		this.calendar.yearStep = this.params['step'];
		
		$(this.options.button).onclick = this.showCalendar.bind(this);
		
		if(this.options.updateFieldOnStartup != false)
		{
			this.updateDateField(new Date());
		}
	},
	
	
	param_default : function (pname, def) 
	{ 
		if (typeof this.params[pname] == "undefined") 
		{ 
			this.params[pname] = def; 
		}
	},
	
	showCalendar : function ()
	{
		this.calendar.create();
		/*this.calendar.refresh();*/
		this.calendar.showAtElement($(this.options.button), "Br");
	},
	
	closeCalendar : function ()
	{
		calendar.hide();
	},
	
	dateChanged : function (calendar)
	{
		if (calendar.dateClicked) 
		{
		  this.updateDateField(calendar.date);
		  calendar.hide();
		}
	},
	
	updateDateField : function (date)
	{
		if(this.options.output == "select")
		{
			this.updateSelectBoxes(date);
		}
		else if(this.options.output == "text")
		{
			this.updateTextField(date);
		}
		else
		{
			this.updateInputFields(date);
		}
	},
	
	updateInputFields : function (date)
	{
		$(this.options.dayInput).value = date.getDate();
		$(this.options.monthInput).value = parseInt(date.getMonth() + 1);
		$(this.options.yearInput).value = date.getFullYear();
	},
	
	updateTextField : function (date)
	{
		$(this.options.textfield).innerHTML = date.getDate() + "." + parseInt(date.getMonth() + 1) + "." + date.getFullYear();
	},
	
	updateSelectBoxes : function (date)
	{
		
		var dayoptions = document.getElementById(this.options.daySelect).getElementsByTagName('option');
		for(var i=0; i<dayoptions.length;i++)
		{
			if(dayoptions[i].value == date.getDate())
			{
				dayoptions[i].selected = true;
			}
			else
			{
				dayoptions[i].selected = false;
			}
		}
		
		var monthoptions = document.getElementById(this.options.monthSelect).getElementsByTagName('option');
		for(var i=0; i<monthoptions.length;i++)
		{
			if(parseInt(monthoptions[i].value) == parseInt(date.getMonth() + 1))
			{
				monthoptions[i].selected = true;
			}
			else
			{
				monthoptions[i].selected = false;
			}
		}
		
		var yearoptions = document.getElementById(this.options.yearSelect).getElementsByTagName('option');
		for(var i=0; i<yearoptions.length;i++)
		{
			if(yearoptions[i].value == date.getFullYear())
			{
				yearoptions[i].selected = true;
			}
			else
			{
				yearoptions[i].selected = false;
			}
		}
		
	}
	
}