function Calendar(input, initObj)
{
	this.input = $(input);
	this.field = $(initObj.field) || null;
	this.hiddenField = $(initObj.hiddenField) || null;
	this.secondHiddenField = $(initObj.secondHiddenField) || null;
	this.obj = initObj.obj || null;
	this.format = initObj.format || 'YYYY-MM-DD';
	this.range = initObj.range || 'all';
	this.layer = null;
	this.table = null;
	this.tbody = null;
	this.headers = initObj.headers || ['H','K','Sz','Cs','P','Sz','V'];
	this.months = initObj.months || ['január','február','március','április','május','június','július','augusztus','szeptember','október','november','december'];
	this.years = initObj.years || [];
	this.yearSelect = null;
	this.monthSelect = null;
	this.hourSelect = null;
	this.hourText = initObj.hourText || ' óra ';
	this.minuteSelect = null;
	this.minuteText = initObj.minuteText || ' perc ';
	this.withTime = initObj.withTime || false;
	this.closer = null;
	this.closerTitle = initObj.closerTitle || 'Bezárás'
	this.adder = null;
	this.adderTitle = initObj.adderTitle || 'Dátum hozzáadása'
	this.actDate = null;
	this.lastClicked = null;
	this.actDay = null;
	this.displayed = false;
	this.cells = new Array();
	this.init();
}

Calendar.prototype.init = function()
{
	// create calendar layer
	this.layer = document.createElement('div');
	this.layer.className = 'calendarLayer';
	document.body.appendChild(this.layer);

	// create selects
	this.yearSelect = document.createElement('select');
	this.yearSelect.className = 'calendar_year_select';
	this.monthSelect = document.createElement('select');
	this.monthSelect.className = 'calendar_month_select';
	var selectLayer = document.createElement('div');
	selectLayer.className = 'calendar_select_layer';
	selectLayer.appendChild(this.yearSelect);
	selectLayer.appendChild(this.monthSelect);
	cleardiv = document.createElement('div');
	cleardiv.className = 'clear';
	selectLayer.appendChild(cleardiv);
	this.layer.appendChild(selectLayer)

	// create calendar table
	this.table = document.createElement('table');
	this.table.className = 'calendar_table';
	this.table_tbody = document.createElement('tbody');	

	this.table.appendChild(this.table_tbody);
	this.layer.appendChild(this.table);

	// create header
	for(var i=0; i<1; i++)
	{
		tr = document.createElement('tr');
		for(var j=0; j<7; j++)
		{
			td = document.createElement('td');
			td.className = 'calendar_header';	
			td.innerHTML = this.headers[j];
			tr.appendChild(td);
		}
		this.table_tbody.appendChild(tr);
	}
	// create table cells
	var tr, td;
	for(var i=0; i<6; i++)
	{
		tr = document.createElement('tr');
		for(var j=0; j<7; j++)
		{
			td = document.createElement('td');
			td.className = 'calendar_day';			
			tr.appendChild(td);
			this.cells.push(td);
		}
		this.table_tbody.appendChild(tr);
	}

	// create calendar footer
	var footer = document.createElement('div');
	footer.className = 'calendar_footer';
	this.layer.appendChild(footer);

	// Hour and Minute selects
	if(this.withTime)
	{
		var selectLayer = document.createElement('div');
		selectLayer.className = 'calendar_select_layer';
		footer.appendChild(selectLayer);
		this.hourSelect = document.createElement('select');	
		this.hourSelect.className = 'calendar_hour_select';
		this.minuteSelect = document.createElement('select');
		this.minuteSelect.className = 'calendar_minute_select';	

		selectLayer.appendChild(this.hourSelect);
		var span = document.createElement('span');
		span.appendChild(document.createTextNode(this.hourText));
		selectLayer.appendChild(span);

		selectLayer.appendChild(this.minuteSelect);
		var span = document.createElement('span');
		span.appendChild(document.createTextNode(this.minuteText));
		selectLayer.appendChild(span);
	}

	// calendar closer
	this.closer = document.createElement('div');
	this.closer.className = 'calendar_closer';
	this.closer.title = this.closerTitle;
	var clearer = document.createElement('div');
	clearer.className = 'clear';
	footer.appendChild(this.closer);
	if(this.withTime)
	{
		this.adder = document.createElement('div');
		this.adder.className = 'calendar_adder';
		this.adder.title = this.adderTitle;
		footer.appendChild(this.adder);
	}
	footer.appendChild(clearer);

	// setting events and show calender
	this.settings();
	this.show();
}

Calendar.prototype.settings = function()
{
	var that = this;
	var today = new Date();

	// setting year select		
	var act_year = today.getFullYear();		
	if(!this.years.length)
	{	
		this.years = [act_year, act_year-1];
	}
	
	for(var i=0; i<this.years.length; i++)
	{
		this.yearSelect.options[i] = new Option(this.years[i], this.years[i]);
		if(this.years[i] == act_year)
		{
			this.yearSelect.options[i].selected = true;
		}
	}	
	this.yearSelect.onchange = function()
	{
		that.show();
	}

	// setting month select
	var act_month = today.getMonth();
	for(var i=0; i<this.months.length; i++)
	{
		this.monthSelect.options[i] = new Option(this.months[i], i);
		if(i == act_month)
		{
			this.monthSelect.options[i].selected = true;
		}
	}
	this.monthSelect.onchange = function()
	{
		that.show();
	}

	if(this.withTime)
	{
		// setting hour select				
		var hour;
		for(var i=0; i<24; i++)
		{
			hour = (i<10)? '0' + i : i;
			this.hourSelect.options[i] = new Option(hour, hour);		
		}		

		// setting minute select				
		var minute;
		for(var i=0; i<60; i++)
		{
			minute = (i<10)? '0' + i : i;
			this.minuteSelect.options[i] = new Option(minute, minute);		
		}	
		
		// calendar adder
		this.adder.onclick = function()
		{
			that.dateClick();
		}
	}

	// calendar close
	this.closer.onclick = function()
	{
		that.close();
	}
}

Calendar.prototype.show = function()
{	
	var that = this;	
	var act_year, act_month, act_day, today = new Date();
	if(this.monthSelect.value == today.getMonth() && this.yearSelect.value == today.getFullYear())
	{
	    act_year = today.getFullYear();
		act_month = today.getMonth();
	    act_day = today.getDate();
		act_date = true;
	}
	else
	{
	    act_year = this.yearSelect.value;
		act_month = this.monthSelect.value;	
		act_day = 0;
	}
    
	this.actDay = this.cells[0];
	this.lastClicked = this.cells[0];	

    var begin_date = new Date(act_year, act_month, 1);             
    var date_to_write = begin_date.getTime();
    var first_day = begin_date;          
    var day_counter = 1;                
         
    for (var i=0; i<this.cells.length; i++)
    {    
       var day = new Date(date_to_write);
       var begin_day = (first_day.getDay() == 0)? 6 : first_day.getDay()-1;

       if(begin_day > i || day.getMonth() != act_month)
       {                 
          this.cells[i].innerHTML = '<br/>';
		  this.cells[i].className = 'calendar_day'; 
       }
       else
       {                                
          this.cells[i].className = 'calendar_day_active';  
          this.cells[i].id = day.getTime();                              
          this.cells[i].innerHTML = day.getDate();
		  if(act_day == day.getDate())
		  {
		     this.cells[i].className += ' calendar_actual_day'; 	
			 this.actDay = this.cells[i];
			 this.lastClicked = this.cells[i];				 
		  }
		  this.cells[i].onclick = function()
		  {			  
			  if(!that.withTime)
			  {
				  that.actTime = this.id;
				  that.dateClick();
			  }
			  else
			  {
				  that.lastClicked.className = that.lastClicked.className.replace(' calendar_actual_day','');
				  that.lastClicked = this;
				  if(!this.className.match('actual_day'))
				  {
					  that.lastClicked.className += ' calendar_actual_day';
				  }				  
				  that.actTime = this.id;
			  }									  
		  }
		  this.cells[i].onmouseover = function()
		  {			                                       
			  var tds = this.parentNode.getElementsByTagName('td');
			  var tdslength =tds.length;
			  for(var i=0; i<tdslength; i++)
			  {
				  tds[i].className += ' calendar_over';
			  }
			  this.className += ' calendar_over_actual';
		  }
		  this.cells[i].onmouseout = function()
		  {
			  var tds = this.parentNode.getElementsByTagName('td');
			  var tdslength =tds.length;
			  for(var i=0; i<tdslength; i++)
			  {
				  tds[i].className = tds[i].className.replace(' calendar_over','');				  
			  }
			  this.className = this.className.replace(' calendar_over_actual','');
		  }
          day.setDate(++day_counter); 
          date_to_write = day.getTime();                                                  
       }                              
	}			
}
   
Calendar.prototype.dateClick = function(time)
{
	try
	{
		if(this.actTime != null)
		{
			if(this.input.nodeName == 'INPUT')
			{
				this.input.value = this.convertDate(this.actTime);
			}
			else
			{
				if(this.hiddenField)
				{
					this.hiddenField.value = this.convertDate(this.actTime);					
				}
				if(this.secondHiddenField)
				{
					this.secondHiddenField.value = this.convertDate(this.actTime);
				}
				if(this.field)
				{
					this.field.innerHTML = this.convertDate(this.actTime);
				}
				else
				{
					this.input.innerHTML = this.convertDate(this.actTime);
				}
				this.obj.closeClick();
			}
			this.actTime = null;
		}
		this.close();
	}
	catch(e)
	{
		alert('There is no input field, belonging to this datepicker!');
	}
}

Calendar.prototype.convertDate = function(time)
{
   var date = new Date(); 
   date.setTime(time);                        
   var return_value = '';  
   var year = date.getFullYear();
   var month = (((date.getMonth())+1) < 10)? '0' + ((date.getMonth())+1) : ((date.getMonth())+1);
   var day = (date.getDate() < 10)? '0' + date.getDate() : date.getDate();
   switch (this.format)
   {
       case 'YYYY-MM-DD' : return_value = year + '-' + month + '-' + day + this.getTime();           
                           break;
       case 'YYYY-DD-MM' : return_value = year + '-' + month + '-' + day + this.getTime();
                           break;
       case 'MM-DD-YYYY' : return_value = month + '-' + day + '-' + year + this.getTime(); 
                           break;    
	             default : return_value = year + '-' + month + '-' + day + this.getTime(); 
				           alert('No matches for thr given date format, default format (YYYY-MM-DD) is applied!');
   }                
   return return_value;
}

Calendar.prototype.display = function()
{	
	this.displayed = true;
	var offset = Element.cumulativeOffset(this.input);
	this.layer.style.top = (offset[1] + this.input.clientHeight + 2) + 'px';
	this.layer.style.left = (offset[0]) + 'px';
	this.layer.style.display = 'block';	
}

Calendar.prototype.getTime = function()
{	
	if(this.withTime)
	{
		return ' ' + this.hourSelect.value + ':' + this.minuteSelect.value;
	}
	else
	{
		return '';
	}
}

Calendar.prototype.timeReset = function()
{
	try
	{		
		this.hourSelect.options[0].selected = true;
		this.minuteSelect.options[0].selected = true;
	}
	catch(e){}
}

Calendar.prototype.close = function()
{
	this.layer.style.display = 'none';
	this.timeReset();
	this.lastClicked.className = this.lastClicked.className.replace(' calendar_actual_day','');
	this.lastClicked = this.actDay;	
	this.lastClicked.className += ' calendar_actual_day';
	this.displayed = false;	
}

Calendar.prototype.onchangeSimulator = function()
{
}