ClubJudge.Calendar = Class.create();
ClubJudge.Calendar.prototype = {
	initialize: function(parent, id, type, sub, sep, format, month, p) {
		this.sep = sep || '-';
		this.month = month || 'Jan';
		this.format = format || 'ymd';
		this.id = $(id);
		this.parent = $(parent);
		this.type =  type || 'datepicker';
		this.sub = sub || 'agenda';
		
		this.ban = p || false;
		
		if(!$('cjDateTable_0')) {
			this.num = 0;
		} else {
			this.num = 1;
		}
		
		this.dayShort = ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'];
		this.dayLong = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'];
		
		this.monthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'];
		this.monthLong = ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'];
		
		if(this.type == 'datepicker') {
			Event.observe(this.id, 'click', function() { this.createCalendar() }.bind(this), false);
		} else {
			this.writeTable();
		}
	},
	
	createCalendar: function() {
		this.posX = this.id.offsetLeft;
		this.posY = this.id.offsetTop + this.id.offsetHeight;
		
		if(this.ban) {
			if(this.id.id == 'sdate') {
				if(!$('cjDatePicker_0')) {
					this.num = 0;
					
					this.cjDatePicker = document.createElement('div');
					this.cjDatePicker.id = 'cjDatePicker_0';
					this.cjDatePicker.className = 'cjDatePicker';
					this.cjDatePicker.style.display = 'none';
					
					this.parent.appendChild(this.cjDatePicker);
				} else {
					this.num = 0;
					this.cjDatePicker = $('cjDatePicker_' + this.num);
				}
			} else {
				if(!$('cjDatePicker_1')) {
					this.num = 1;
					
					this.cjDatePicker = document.createElement('div');
					this.cjDatePicker.id = 'cjDatePicker_1';
					this.cjDatePicker.className = 'cjDatePicker';
					this.cjDatePicker.style.display = 'none';
					
					this.parent.appendChild(this.cjDatePicker);
				} else {
					this.num = 1;
					this.cjDatePicker = $('cjDatePicker_' + this.num);
				}
			}
		} else {
			if(!$('cjDatePicker_' + this.num)) {
				this.cjDatePicker = document.createElement('div');
				this.cjDatePicker.id = 'cjDatePicker_' + this.num;
				this.cjDatePicker.className = 'cjDatePicker';
				this.cjDatePicker.style.display = 'none';
				
				this.parent.appendChild(this.cjDatePicker);
			} else {
				this.cjDatePicker = $('cjDatePicker_' + this.num);
			}
		}
		
		this.cjDatePicker.style.cssText = 'position:absolute; top:' + this.posY + 'px; left:' + this.posX + 'px'
		//this.cjDatePicker.style.display = ((this.cjDatePicker.style.display == 'none') ? 'block' : 'none');
		
		if(!$('cjDateTable_' + this.num)) {
			this.writeTable();
		} else {
			$('cjDatePicker_' + this.num).style.display = '';
		}
	},
	
	writeTable: function(num) {
		this.dateTable = document.createElement('table');
		this.dateTable.id = 'cjDateTable_' + this.num;
		this.dateTable.className = 'cjDateTable';
		
		this.dateTableHeadC = document.createElement('thead');
		
		this.dateTableHeadTR = document.createElement('tr');
		this.dateTableHeadTR.id = 'cjDateTableTitle_' + this.num;

		this.dateTableHeadTTD = document.createElement('td');
		this.dateTableHeadTTD.setAttribute(((document.all) ? 'colSpan' : 'colspan'), 5); //IE gives me another headache..
		this.dateTableHeadTTD.id = 'titleDateYear_' + this.num;
		
		if(this.type == 'datepicker') {
			this.cDate = new Date();
			this.dateTableSelectMonth = document.createElement('select');
			this.dateTableSelectMonth.id = 'cjSelectMonth_' + this.num;
			this.dateTableSelectMonth.style.width = '100px';
			this.monthSelect(this.cDate.getMonth());
			
			this.dateTableHeadTTD.appendChild(this.dateTableSelectMonth);
			
			this.dateTableSelectYear = document.createElement('select');
			this.dateTableSelectYear.id = 'cjSelectYear_' + this.num;
			this.dateTableSelectYear.style.width = '65px';
			this.yearSelect(2003);
			
			this.dateTableHeadTTD.appendChild(this.dateTableSelectYear);
		} else if(this.type == 'calendar') {
			this.cDate = new Date();
			this.dateTableSelectMonth = document.createElement('select');
			this.dateTableSelectMonth.id = 'cjSelectMonth_' + this.num;
			this.dateTableSelectMonth.style.width = '100px';
			this.monthSelect(this.cDate.getMonth());
			
			this.dateTableSelectYear = document.createElement('select');
			this.dateTableSelectYear.id = 'cjSelectYear_' + this.num;
			this.dateTableSelectYear.style.width = '65px';
			this.yearSelect(this.cDate.getFullYear());
			
			this.dateTableHeadTTD.appendChild(this.dateTableSelectMonth);
			this.dateTableHeadTTD.appendChild(this.dateTableSelectYear);
		}
		
		this.dateTableHeadBTNTDP = document.createElement('td');
		
		this.dateTableHeadBTNP = document.createElement('button');
		this.dateTableHeadBTNP.innerHTML = '&laquo;&laquo;';
		this.dateTableHeadBTNP.id = 'cjTableBtnPrev_' + this.num;
		
		this.dateTableHeadBTNTDP.appendChild(this.dateTableHeadBTNP);
		
		this.dateTableHeadBTNTDN = document.createElement('td');
		
		this.dateTableHeadBTNN = document.createElement('button');
		this.dateTableHeadBTNN.id = 'cjTableBtnNext_' + this.num;
		this.dateTableHeadBTNN.innerHTML = '&raquo;&raquo;';
		
		this.dateTableHeadBTNTDN.appendChild(this.dateTableHeadBTNN);
		
		this.dateTableHeadTR.appendChild(this.dateTableHeadBTNTDP);
		this.dateTableHeadTR.appendChild(this.dateTableHeadTTD);
		this.dateTableHeadTR.appendChild(this.dateTableHeadBTNTDN);
		
		this.dateTableHeadDTR = document.createElement('tr');
		this.dateTableHeadDTR.id = 'cjDateTableDay';
		
		for(var i = 0; i < this.dayShort.length; i++) {
			this.dateTableHeadDTD = document.createElement('td');
			this.dateTableHeadDTD.id = 'cjDateDay_' + this.num + '_' + i;
			this.dateTableHeadDTD.className = 'cjDateDay';
			this.dateTableHeadDTD.innerHTML = this.dayShort[i];
			
			this.dateTableHeadDTR.appendChild(this.dateTableHeadDTD);
		}
		
		this.dateTableHeadC.appendChild(this.dateTableHeadTR);
		this.dateTableHeadC.appendChild(this.dateTableHeadDTR);
		
		this.dateTable.appendChild(this.dateTableHeadC);
				
		//this.dateTableBodyC = '<tbody id="cjDateBottom_' + this.num + '">' + this.updateDate() + '</tbody>';
		this.dateTableBodyC = document.createElement('tbody');
		this.dateTableBodyC.id = 'cjDateBottom_' + this.num;
		//this.dateTableBodyC.innerHTML = 'aba';
		
		//this.dateTable.innerHTML += this.dateTableBodyC;
		this.dateTable.appendChild(this.dateTableBodyC);
		
		if(this.type == 'datepicker') {
			this.cjDatePicker.appendChild(this.dateTable);
			this.cjDatePicker.style.display = 'block';
		} else {
			this.parent.appendChild(this.dateTable);
		}
		
		this.setObserve()
	},
	
	setObserve: function() {
		Event.observe($('cjTableBtnNext_' + this.num), 'click', function() {
			this.updateStr = $('cjTableBtnNext_' + this.num).getAttribute('year');
			this.updateStr = this.updateStr.split('.');
			
			if(this.type == 'datepicker') {
				this.oldMonth = this.tDay.getMonth() - 1;
			
				if(this.oldMonth == -1) {
					this.oldMonth = 11;
				}
				$('month_' + this.num + '_' + this.oldMonth).removeAttribute('selected');
			}
			
			this.newMonth = this.tDay.getMonth();

			if(this.updateStr[0] > maxYear) {
				$('cjTableBtnNext_' + this.num).disabled = true;
			} else {
				if($('cjTableBtnPrev_' + this.num).disabled = true) {
					$('cjTableBtnPrev_' + this.num).disabled = false;
				}
				this.updateDate(this.updateStr[0], this.updateStr[1]);
			}
			
			if(this.newMonth == -1) {
				this.newMonth = 11;
			}
			
			if(this.type == 'datepicker') {
				$('cjSelectMonth_' + this.num).selectedIndex = this.newMonth;
				
				$('month_' + this.num + '_' + this.newMonth).setAttribute('selected', 'selected');
				$('month_' + this.num + '_' + ((this.newMonth == 0) ? 11 : this.newMonth - 1)).removeAttribute('selected');
				
				if(this.newMonth == 0) {
					$('cjSelectYear_' + this.num).selectedIndex = $('cjSelectYear_' + this.num).selectedIndex + 1;
				}
			} else {
				$('cjSelectMonth_' + this.num).selectedIndex = this.newMonth;
				
				$('month_' + this.num + '_' + this.newMonth).setAttribute('selected', 'selected');	
				$('month_' + this.num + '_' + ((this.newMonth == 0) ? 11 : this.newMonth - 1)).removeAttribute('selected');
				
				if(this.newMonth == 0) {
					$('cjSelectYear_' + this.num).selectedIndex = $('cjSelectYear_' + this.num).selectedIndex + 1;
				}
			}
		}.bind(this), false);

		Event.observe($('cjTableBtnPrev_' + this.num), 'click', function() {
			this.updateStr = $('cjTableBtnPrev_' + this.num).getAttribute('year');
			this.updateStr = this.updateStr.split('.');
			
			if(this.type == 'datepicker') {
				this.oldPMonth = this.tDay.getMonth() - 1;
			
				if(this.oldPMonth == -1) {
					this.oldPMonth = 0;
				}
				$('month_' + this.num + '_' + this.oldPMonth).removeAttribute('selected');
			}
			
			if(this.updateStr[0] < minYear) {
				$('cjTableBtnPrev_' + this.num).disabled = true;
			} else {
				if($('cjTableBtnNext_' + this.num).disabled = true) {
					$('cjTableBtnNext_' + this.num).disabled = false;
				}
				this.updateDate(this.updateStr[0], this.updateStr[1]);
			}

			this.newPMonth = this.tDay.getMonth() - 1;

			if(this.newPMonth == -1) {
				this.newPMonth = 11;
			}
			
			if(this.type == 'datepicker') {
				$('cjSelectMonth_' + this.num).selectedIndex = this.newPMonth;
				
				$('month_' + this.num + '_' + this.newPMonth).setAttribute('selected', 'selected');
				$('month_' + this.num + '_' + ((this.newPMonth == 11) ? 0 : this.newPMonth + 1)).removeAttribute('selected');
				
				if(this.newPMonth == 11) {
					$('cjSelectYear_' + this.num).selectedIndex = $('cjSelectYear_' + this.num).selectedIndex - 1;
				}
			} else {	
				$('cjSelectMonth_' + this.num).selectedIndex = this.newPMonth;
				
				$('month_' + this.num + '_' + this.newPMonth).setAttribute('selected', 'selected');
				$('month_' + this.num + '_' + ((this.newPMonth == 11) ? 0 : this.newPMonth + 1)).removeAttribute('selected');
				
				if(this.newPMonth == 11) {
					$('cjSelectYear_' + this.num).selectedIndex = $('cjSelectYear_' + this.num).selectedIndex - 1;
				}
			}
		}.bind(this), false);
	
		Event.observe($('cjSelectMonth_' + this.num), 'change', function() {
				
			if(this.type == 'datepicker') {
				this.updateDate($('cjSelectYear_' + this.num).value, $('cjSelectMonth_' + this.num).value);
			} else if(this.type == 'calendar') {
				this.updateDate($('cjSelectYear_' + this.num).value, $('cjSelectMonth_' + this.num).value);
			}
		
		}.bind(this));
		
		Event.observe($('cjSelectYear_' + this.num), 'change', function() {
		
			if(this.type == 'datepicker') {
				this.updateDate($('cjSelectYear_' + this.num).value, $('cjSelectMonth_' + this.num).value);
			} else if(this.type == 'calendar') {
				this.updateDate($('cjSelectYear_' + this.num).value, $('cjSelectMonth_' + this.num).value);
			}
		
		}.bind(this));
		
		if(this.type == 'datepicker') {
			this.insertDays();
		} else if(this.type == 'calendar') {
			this.insertDays();
		}
	},
	
	insertDays: function() {
		//this.updateDate($('cjSelectYear_' + this.num).value, $('cjSelectMonth_' + this.num).getAttribute('month'));
		this.cDate = new Date();
		this.updateDate(this.cDate.getFullYear(), this.cDate.getMonth());
	},
	
	monthSelect: function(month) {
		if(this.type == 'datepicker') {
			for (var i = 0; i < this.monthLong.length; i++) {
				this.selectMonth = document.createElement('option');
				this.selectMonth.value = i;
				this.selectMonth.id = 'month_' + this.num + '_' + i;
				this.selectMonth.innerHTML = this.monthLong[i];
				if(this.selectMonth.value == month) {
					this.selectMonth.setAttribute('selected', 'selected');
				}
				
				this.dateTableSelectMonth.appendChild(this.selectMonth);
			}
		} else {
			for (var i = 0; i < this.monthLong.length; i++) {
				this.selectMonth = document.createElement('option');
				this.selectMonth.value = i;
				this.selectMonth.id = 'month_' + this.num + '_' + i;
				this.selectMonth.innerHTML = this.monthLong[i];
				if(this.selectMonth.value == month) {
					this.selectMonth.setAttribute('selected', 'selected');
				}
				
				this.dateTableSelectMonth.appendChild(this.selectMonth);
			}		
		}
	},
	
	yearSelect: function(year) {
		if(this.type == 'datepicker') {
			if(this.sub == 'agenda') {
				mYear = new Date();
				minYear = mYear.getFullYear();
				maxYear = minYear + 2;
			} else {
				minYear = 1950;
				maxYear = 2007;		
			}
		} else {
			minYear = 2004;
			mYear = new Date();
			maxYear = mYear.getFullYear() + 1;
		}
		
		if(!$('year_' + this.num + '_' + year)) {			
			for(var i = minYear; i <= maxYear; i++) {
				this.selectYear = document.createElement('option');
				this.selectYear.value = i;
				this.selectYear.id = 'year_' + this.num + '_' + i;
				this.selectYear.innerHTML = i;
				
				if(this.selectYear.value == year) {
					this.selectYear.setAttribute('selected', 'selected');
				}
		
				this.dateTableSelectYear.appendChild(this.selectYear);
			}
		}
	},
	
	createDateUpdate: function(dateVal, adjust, type) {
		var newMonth = (dateVal.getMonth() + adjust) % 12;
		var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);

		if (newMonth < 0) {
			newMonth += 12;
			newYear += -1;
		}
		
		return newYear + '.' + newMonth;
	},
	
	updateDate: function(year, month) {
		/*if($('cjDateBottom_' + this.num) != null) {
			$('cjDateBottom_' + this.num).innerHTML = '';
		}*/
		
		if((month >= 0) && (year > 0)) {
			this.tDay = new Date(year, month, 1);
		} else {
			this.tDay = new Date();
			this.tDay.setDate(1);
		}
		
		// Assign next year & month to next & prev button
		if($('cjTableBtnNext_' + this.num)) {
			$('cjTableBtnNext_' + this.num).setAttribute('year', this.createDateUpdate(this.tDay, 1));
		}
		
		if($('cjTableBtnPrev_' + this.num)) {
			$('cjTableBtnPrev_' + this.num).setAttribute('year', this.createDateUpdate(this.tDay, -1));
		}
		//***********************************************
		
		//this.day = this.tDay.getDay();
		this.day = new Date().toUTCString();
		
		this.day = this.day.split(',');		this.day = this.day[1].split(' ');
		this.day = this.day[1];
		
		this.date = new Date();
		this.month = this.tDay.getMonth();
		this.year = this.tDay.getFullYear();
		
		this.monthDays = '<tr>';
			
		for(var y = 0; y < this.tDay.getDay(); y++) {
			this.monthDays += '<td>&nbsp;</td>';
		}
			
		do {
			this.dayNum = this.tDay.getDate();
			this.dayMonth = (this.tDay.getMonth() + 1);
			
			if(this.day == this.dayNum && (this.date.getMonth() + 1) == this.dayMonth && this.year == this.date.getFullYear()) {
				this.monthDays += '<td date="' + this.setDayDate(this.tDay) + '" class="cjSelDate hover" id="' + this.num + '_' + this.dayNum + '">' + this.dayNum + '</td>';
			} else {
				this.monthDays += '<td date="' + this.setDayDate(this.tDay) + '" class="hover" id="' + this.num + '_' + this.dayNum + '_' + this.month + '">' + this.dayNum + '</td>';
			}
			
			if(this.tDay.getDay() == 6) {
				this.monthDays += '</tr><tr>';
			}
			
			this.tDay.setDate(this.tDay.getDate() + 1);
		} while(this.tDay.getDate() > 1) {

			if(this.tDay.getDay() > 0) {
				for(var x = 6; x > this.tDay.getDay(); x--) {
					this.monthDays += '<td>&nbsp;</td>';
				}
			}
		}
		
		this.monthDays += '</tr>';
		
		if(year != undefined) {
			this.oYear = $('cjSelectYear_' + this.num).value;
			this.yearSelect(this.oYear);
			
			if(document.all) {
				// IE need getElementById(name), don't ask me why..
				$('cjDateBottom_' + this.num).update(this.monthDays);
			} else {
				this.dateTableBodyC.update(this.monthDays);
			}
		} else {
			return this.monthDays;
		}
		
		this.observeDate();
	},
	
	observeDate: function() {
		this.dates = document.getElementsByClassName('hover');
		
		for(var i = 0; i < this.dates.length; i++) {
			if(this.type == 'datepicker') {
				Event.observe(this.dates[i], 'click', function(evnt) { this.updateDateField(Event.element(evnt).getAttribute('date')) }.bind(this), false);
			} else {
				Event.observe(this.dates[i], 'click', function(evnt) { 
					this.dateURL = Event.element(evnt).getAttribute('date');
					this.dateURL = this.dateURL.split('-');

					window.location = '/agenda/' + this.dateURL[0] + '-' + this.dateURL[1] + '-' + this.dateURL[2] + '/' 
				}.bind(this), false);
			}
		}
	},
	
	updateDateField: function(date) {
		if(this.ban) {
			this.id.value = date + ' 00:00:00';
		} else {
			this.id.value = date;
		}
		this.id.focus();
		
		$('cjDatePicker_' + this.num).style.display = 'none';
	},
	
	setDayDate: function(date) {
		this.dateVal = date;
		
		this.dayStr = "00" + this.dateVal.getDate();
		
		this.month = this.dateVal.getMonth() + 1;
		this.monthStr = "00" + this.month;
		
		this.dayStr = this.dayStr.substring(this.dayStr.length - 2);
		this.monthStr = this.monthStr.substring(this.monthStr.length - 2);
		
		switch(this.format) {
			case "ymd":
				return this.dateVal.getFullYear() + this.sep + this.monthStr + this.sep + this.dayStr;
			default:
				return this.dateVal.getFullYear() + this.sep + this.monthStr + this.sep + this.dayStr;
		}
	},
	
	getCurrDate: function(type) {
		this.type = type;
		this.nDate = new Date();
		
		if(this.type == 'currYear') {
			return this.nDate.getFullYear();
		} else if(this.type == 'currMonth') {
			return this.nDate.getMonth();
		} else if(this.type == 'month') {
			return this.month;
		} else if(this.type == 'year') {
			return this.year;
		} else {
			throw('Exception: Specify month or year!');
		}
	}
}