﻿var calendarNdx=0;
var calendars=new Array();

function Calendar(input_object, container, onselect) {
	this.onselect=onselect;
	this.container=container;
	this.ndx=calendarNdx++;
	calendars.push(this);
	this.input_object=input_object;
	this.selected=null;
	this._year=0;
	this._month=0;
	this._day=0;
	this.months=Array('január','február','március','április','május','június','július','augusztus','szeptember','október','november','december');
		
	if(input_object.value)date=input_object.value.split('.');
	else{
		date=Array();
		date[0]=new Date().getFullYear();
		date[1]=new Date().getMonth()+1;
		date[2]=new Date().getDate();
	}
	this._year=this.input_object.year=date[0];
	this._month=this.input_object.month=date[1];
	this._day=this.input_object.day=date[2];
	
	this.set_date=function (obj) {
		this.input_object.year=obj.year;
		this.input_object.month=obj.month;
		this.input_object.day=obj.day;
		this.input_object.value=
			obj.year+'.'+((obj.month.toString().length==1)?(0):(''))+
			obj.month+'.'+((obj.day.toString().length==1)?(0):(''))+
			obj.day;//+'.'
		if(obj!=this.selected){
			if(this.selected)this.selected.className=this.selected.nonSelectedClassName;
			obj.nonSelectedClassName=obj.originalClassName;
			obj.originalClassName=obj.originalClassName+' selected';
			this.selected=obj;
		}
		if (this.onselect) this.onselect();
	}
	
	this.update=function update() {
		date=new Date(this._year,this._month-1,1);
		weekday=date.getDay();
		this._year= date.getFullYear();
		this._month=date.getMonth()+1;
		if(!weekday)weekday=7;
		weekday--;
		this.container.getElementById('year_month_'+this.ndx).innerHTML=this._year+'. '+this.months[this._month-1];
		var count=0;
		this.selected=false;
		for(var i=0;i<6;i++)for(var j=0;j<7;j++){
			date=new Date(this._year,this._month-1,1+count-weekday);
			daytag=this.container.getElementById('day_'+count+'_'+this.ndx);
			daytag.year=date.getFullYear ();
			daytag.month=date.getMonth()+1;
			daytag.day=date.getDate();
			daytag.innerHTML=daytag.day;
			daytag.className=
				((j>4)?(' weekend'):(''))+
				((this._month!=daytag.month)?(' unused'):(''))+
				((
					this.input_object.year==daytag.year &&
					this.input_object.month==daytag.month &&
					this.input_object.day==daytag.day
				)?(' selected'):(''));
			if(this.input_object.year==daytag.year &&
				this.input_object.month==daytag.month &&
				this.input_object.day==daytag.day){
				this.selected=daytag;
				daytag.nonSelectedClassName=
					((j>4)?(' weekend'):(''))+
					((this._month!=daytag.month)?(' unused'):(''));
			}
			count++;
		}
	}
	
	this.innerHTML=function() {
		var ret="";
		ret+='<table class="calendar">';
		ret+='<tr>';
		ret+='	<td unselectable="on" class="head arrow" onclick="calendars['+this.ndx+']._month--;calendars['+this.ndx+'].update()">&lt;</td>';
		ret+='	<td colspan="5" class="head" id="year_month_'+this.ndx+'">&nbsp;</td>';
		ret+='	<td unselectable="on" class="head arrow" onclick="calendars['+this.ndx+']._month++;calendars['+this.ndx+'].update()">&gt;</td>';
		ret+='</tr>';
		ret+='<tr>';
		ret+='	<th>H</th>';
		ret+='	<th>K</th>';
		ret+='	<th>Sz</th>';
		ret+='	<th>Cs</th>';
		ret+='	<th>P</th>';
		ret+='	<th class="weekend">Sz</th>';
		ret+='	<th class="weekend">V</th>';
		ret+='</tr>';
		var index=0;
		for(var i=0;i<6;i++){
			ret+='<tr>';
			for(var j=0;j<7;j++){
				ret+='<td onclick="calendars['+this.ndx+'].set_date(this)" onmouseover="this.originalClassName=this.className;this.className=\'over\'" onmouseout="this.className=this.originalClassName" id="day_'+index+'_'+this.ndx+'"></td>';
				index++
			}
			ret+='</tr>';
		}
		ret+='</table>';
		return ret;
	}
	
	this.container.setInnerHTML(this.innerHTML());
	this.update();
}

//containers
function createCalendarPopup(input, relativeTo, offsetX, offsetY) {
	if (window.createPopup) return new calendarIEPopup(input, relativeTo, offsetX, offsetY);
	else return new calendarDivPopup(input, relativeTo, offsetX, offsetY);
}

function calendarDivPopup(input, relativeTo, offsetX, offsetY) {
	this.div=document.body.appendChild(document.createElement("div"));
	this.div.style.visibility="hidden";
	this.div.style.display="none";
	this.div.style.position="absolute";
	this.div.zIndex=999;
	this.relativeTo=relativeTo || input;
	this.offsetX=offsetX || 16;
	this.offsetY=offsetY || 0;
	
	this.getElementById=function(id) {
		return document.getElementById(id);
	}
	
	this.setInnerHTML=function(html) {
		this.div.innerHTML=html;
	}
	
	this.hide=function () {
		this.div.style.visibility="hidden";
		//release mousedown
		window.document.onmousedown=this.doc_onmousedown;
		window.onmousedown=this.win_onmousedown;
		if (typeof(Event)!='undefined' && window.releaseEvents)	window.releaseEvents(Event.MOUSEDOWN);
	}
	var me=this;
	var hide_me=function (event){
		if(!event)event=window.event;
		e=event.target || event.srcElement;
		if (e==relativeTo) return;
		while(e && e.className!='calendar') e=e.parentNode;
		if (!e) {
			me.hide()
			cancelBubble(event);
			cancelEvent(event);
		}
	};
	this.show=function () {
		var pos=getElementPosition(this.relativeTo);
		this.div.style.top=pos.y+this.offsetY;
		this.div.style.left=pos.x+this.offsetX;
		this.div.style.display="block";
		this.div.style.visibility="visible";
		//capture mousedown
		this.doc_onmousedown=window.document.onmousedown;
		window.document.onmousedown=hide_me;
		this.win_onmousedown=window.onmousedown;
		window.onmousedown=hide_me;
		if (typeof(Event)!='undefined' && window.captureEvents) window.captureEvents(Event.MOUSEDOWN);
	}
	this.toggle=function () {
		if (this.div.style.visibility=="visible") this.hide();
		else this.show();
	}
	this.calendar=new Calendar(input, this, function(){me.hide()});
}

function calendarIEPopup(input, relativeTo, offsetX, offsetY) {
	this.popup = window.createPopup();
	this.stylesheet=this.popup.document.createStyleSheet(cssURL+'crosscalendar.css');
	this.popupbody = this.popup.document.body;
	this.popup.document.calendars=calendars;

	this.relativeTo=relativeTo || input;
	this.offsetX=offsetX || 16;
	this.offsetY=offsetY || 0;

	this.getElementById=function(id) {
		return this.popup.document.getElementById(id);
	}
	
	this.setInnerHTML=function(html) {
		this.popupbody.innerHTML=html;
	}
	
	this.hide=function () {
		this.popup.hide();
		//release mousedown
		window.document.onmousedown=this.doc_onmousedown;
		window.onmousedown=this.win_onmousedown;
		if (typeof(Event)!='undefined' && window.releaseEvents)	window.releaseEvents(Event.MOUSEDOWN);
	}
	var me=this;
	var hide_me=function (event){
		if(!event)event=window.event;
		e=event.target || event.srcElement;
		if (e==relativeTo) return;
		while(e && e.className!='calendar') e=e.parentNode;
		if (!e) {
			me.hide()
			cancelBubble(event);
			cancelEvent(event);
		}
	};
	this.show=function () {
		//this.popup.show(0, 0, 0, 0);
		var realHeight = 145;//this.popupbody.scrollHeight;
		var realWidth = 200;//this.popupbody.scrollWidth;
		//this.popup.hide();
		this.popup.show(0, 0, realWidth, realHeight, relativeTo);
		//capture mousedown
		this.doc_onmousedown=window.document.onmousedown;
		window.document.onmousedown=hide_me;
		this.win_onmousedown=window.onmousedown;
		window.onmousedown=hide_me;
		if (typeof(Event)!='undefined' && window.captureEvents) window.captureEvents(Event.MOUSEDOWN);
	}
	this.toggle=function () {
		if (this.popup.isOpen) this.hide();
		else this.show();
	}
	this.calendar=new Calendar(input, this, function(){me.hide()});
}
