/*
# javascript datepicker voor data
# ###############
# Onderdeel van sQuarecoW CMS
# © 2006-2008 sQuarecoW new media
# Versie: 2.03
# Module: cms
# ###############
*/
	function datepicker() {
		$$('.datepicker').each(function(el) {
			
			//definieer variabelen
			shortDayNotitions = ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'];
			shortMonthNotitions = ['jan', 'feb', 'mrt', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'];
			longMonthNotitions = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'];
			monthLengths = [31,28,31,30,31,30,31,31,30,31,30,31];
			dateFormat = 'dd-mm-jjjj';
			
			//startdatum
			today =  new Date();
			if (checkLeapYear(today.getFullYear()) == true)
				monthLengths[1] = 29;
					
			//laat maar zien
			el.getElement('img.calendar_edit').removeEvents();
			el.getElement('img.calendar_edit').addEvent('click', function(event) {
				event = new Event(event);
				
				if (!$defined($('datepicker'))) {
					showCalendar(event.page.x -30, event.page.y - 30);
					buildCalendar(today, el.getElement('input'));
				}
			});
			
			// leeg het veld
			el.getElement('img.bin').removeEvents();
			el.getElement('img.bin').addEvent('click', function(event) {
				el.getElement('input').set('value', '');
			});
		});
	}
		//maak het kalenderschermpje
	function showCalendar(left, top) {
		new Element('div', {
			'id': 'datepicker',
			'styles': {
				'position': 'absolute',
				'left': left,
				'top': top
			}
		}).inject(document.body).set('html', '<table id="calendar"><thead id="thead"><tr><th id="th_prev"></th><th colspan="5" id="th_now"></th><th id="th_next"></th></tr><tr id="days"></tr></thead><tfoot></tfoot></table>');
		
		$('datepicker').addEvent('mouseleave', function() {
			$('datepicker').destroy();
		});
		
		for (i = 0; i < 7; i++) {
			
			new Element('th', {
			}).inject($('days')).set('html', shortDayNotitions[i]);
		}
	}
	
	//maak de kalender
	function buildCalendar(date, input) {
		
		//als er al een kalender is, verwijderd die dan
		if ($defined($('tbody')))
			$('tbody').destroy();
		
		new Element('tbody', {
			'id': 'tbody'
		}).inject($('thead'), 'after');
		
		//eerste regel (vorige maand, huidige maand, volgende maand)
		$('th_prev').set('html', '<a href="#" id="prev" title="Vorige maand" class="link"></a>');
		$('prev').addEvent('click', function(e) {
			e.stop();
			buildCalendar(getPrevMonth(date), input);
		});
		$('th_now').set('html', longMonthNotitions[date.getMonth()] + ' ' + date.getFullYear());
		$('th_next').set('html', '<a href="#" id="next" title="Volgende maand" class="link"></a>');
		$('next').addEvent('click', function(e) {
			e.stop();
			buildCalendar(getNextMonth(date), input);
		});
		
		firstDayOfMonth = new Date(date);
		firstDayOfMonth.setDate(1);
		
		day = 0;
		
		//weken
		for (row = 0;day<=monthLengths[date.getMonth()];row++) {
			
			new Element('tr', {
				'id': 'tr' + row
			}).inject($('tbody'), 'bottom');
			
					
			//dagen
			for (col = 0;day<=monthLengths[date.getMonth()]&&col<7;col++) {
			
				new Element('td', {
					'id': 'td' + row + col
				}).inject($('tr' + row));
				
				//starten
				if (day == 0 && col == firstDayOfMonth.getDay()) {
					day = 1;
				}
				
				//dagen van de vorige maand
				if (day == 0) {					
					$('td' + row+ col).set('text', monthLengths[date.getMonth()-1 < 0 ? 11 : date.getMonth()-1] - (firstDayOfMonth.getDay() - 1 - col));
					$('td' + row + col).set('class', 'other-month');
					$('td' + row + col).addEvent('click', function(e) {
						e.stop();
						buildCalendar(getPrevMonth(date), input);
					});
				}
				
				if (day > 0) {
					//alert(day);
					$('td' + row + col).set('text', day);
					$('td' + row + col).addEvent('click', function() {
						input.set('value', this.retrieve('day'));
						$('datepicker').destroy();
					});
					formattedDate = new Date(date);
					formattedDate.setDate(day);
					
					$('td' + row + col).store('day', formatDate(formattedDate));
					
					day++;
					
					if (day == today.getDate()+1 && date.getMonth() == today.getMonth() && date.getFullYear() == today.getFullYear() ) {
						$('td' + row + col).set('class', 'today');
						$('td' + row + col).set('title', 'Vandaag');
					}
				}
					
				//else
				//	$('td' + row+col).set('html', '-');				
			}
			
			//zijn er nog dagen over in deze week?
			if (day>monthLengths[date.getMonth()] && col < 7) {
				var nexMonthDay= 1;
				for (col = col;col<7;col++) {
					
					new Element('td', {
						'id': 'td' + row + col
					}).inject($('tr' + row));
					
					$('td' + row + col).set('text', nexMonthDay);
					$('td' + row + col).set('class', 'other-month');
					$('td' + row + col).addEvent('click', function(e) {
						e.stop();
						buildCalendar(getNextMonth(date), input);
					});
					nexMonthDay++;
					
				}
			}
		}
	}
	
	//schrikkeljaar?
	function checkLeapYear(year) {
		
		//jaar deelbaar door vier
		if (year % 4 == 0) {
			
			//maar niet door honderd
			if (year % 100 != 0)
				return true;
			
			//behalve bij 400
			else {
				if (year % 400 == 0)				
					return true;
				
				else
					return false;
			}
		}
		
		else {
			return false;
		}	
	}
	
	//maak een datum uit de opgegeven string, aan de hand van de opgegeven dateFormat
	function makeDate(date) {
				
		if (dateFormat.search('-') >= 0)
			separator = '-';
		
		else if (dateFormat.search('/') >= 0)
			separator = '/';
			
		else if (dateFormat.search(' ') >= 0)
			separator = ' ';
		
		dateParts = date.split(separator);
		dateFormatParts = dateFormat.split(separator);
		
		return_date = new Date();
		
		//zoek waar de dag/maand/jaar is
		for (i=0;i<3;i++) {
			if (dateFormatParts[i].search(/d/i) >= 0)
				return_date.setDate(dateParts[i]);
		
			if (dateFormatParts[i].search(/m/i) >= 0)
				return_date.setMonth(dateParts[i] -1);
		
			if (dateFormatParts[i].search(/y/i) >= 0 || dateFormatParts[i].search(/j/i) >= 0)
				return_date.setYear(dateParts[i]);
		}
		
		return return_date;
	}
	
	//datum teruggeven op aangegeven manier
	function formatDate(date) {
		
		if (dateFormat.search('-') >= 0)
			separator = '-';
		
		else if (dateFormat.search('/') >= 0)
			separator = '/';
			
		else if (dateFormat.search(' ') >= 0)
			separator = ' ';
		
		dateFormatParts = dateFormat.split(separator);
		
		return_date = '';
		
		//zoek waar de dag/maand/jaar is
		for (i=0;i<3;i++) {
			if (dateFormatParts[i].search(/d/i) >= 0)
				return_date += date.getDate() + separator;
		
			if (dateFormatParts[i].search(/m/i) >= 0)
				return_date += (date.getMonth() + 1) + separator;
		
			if (dateFormatParts[i].search(/y/i) >= 0 || dateFormatParts[i].search(/j/i) >= 0)
				return_date += date.getFullYear();
		}
		
		return return_date;	
	}
	
	//vorige maand
	function getPrevMonth(date) {
		
		return_date = new Date();
		return_date.setDate(1);
		
		if (date.getMonth() - 1 < 0) {			
			return_date.setMonth(11);
			return_date.setYear(date.getFullYear() - 1);
		}
		
		else {
			return_date.setMonth(date.getMonth() - 1);
			return_date.setYear(date.getFullYear());
		}
		
		return return_date;
	}
	
	//volgende maand
	function getNextMonth(date) {
		
		return_date = new Date();
		return_date.setDate(1);
		
		if (date.getMonth() + 1 > 11) {			
			return_date.setMonth(0);
			return_date.setYear(date.getFullYear() + 1);
		}
		
		else {			
			return_date.setMonth(date.getMonth() + 1);
			return_date.setYear(date.getFullYear());
		}
		
		return return_date;
	}
	
window.addEvent('domready', function(){
	
	datepicker();
	
	//het dupliceren van een formulierveld ('geleend' van forms.js)
	//let op een  'add_form_field' link
	if ($defined($('add_datepicker'))) {
		$('add_datepicker').addEvent('click', function(event) {
			event.stop();
			
			$('clone_field').clone().injectBefore($('field_placeholder'));
			
			datepicker();
		});
	}
});
