var Birthday = new Class({
		
	day: null, month: null, year: null, period: null,
	months: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
	overlay: null,
	container: null,
	more: null,
	
	initialize: function(more) {
		this.more = more;
		this.day = $('childbirth-d').get('value').toInt();
		this.month = $('childbirth-m').get('value').toInt();
		this.year = $('childbirth-y').get('value').toInt();
		this.period = $('childbirth-p').get('value').toInt();
		if (!this.validate()) {
			return;
		} 
		this.make_overlay();
	},
	
	validate: function() {
		if (isNaN(this.day) || isNaN(this.month) || isNaN(this.year)) {
			alert('Data ostatniej miesiączki jest niepoprawna.');
			return false;			
		}
		if (this.year < 2008) {
			alert('Data ostatniej miesiączki jest niepoprawna (nieprawidłowy rok).');
			return false;			
		}
		if (this.month < 1 || this.month > 12) {
			alert('Data ostatniej miesiączki jest niepoprawna (nieprawidłowy miesiąc).');
			return false;			
		}
		if (this.day < 1 || this.day > (this.months[this.month - 1] + (this.year % 4 == 0 && this.month == 2 ? 1 : 0))) {
			alert('Data ostatniej miesiączki jest niepoprawna (nieprawidłowy dzień).');
			return false;			
		}	
		if (isNaN(this.period) || this.period < 1 || this.period > 40) {
			alert('Długość cyklu jest niepoprawna');
			return false;
		}
		return true;
	},
	
	make_overlay: function() {
		var $this = this;
		this.overlay = new Element('div', {
			'class': 'birthday-overlayer'
		}).setStyles({
			'opacity': 0,
			'height': $(document.body).getScrollSize().y + 'px'
		}).inject($(document.body));
		new Fx.Morph(this.overlay, {'duration': 500}).start({'opacity': [0, 0.5]}).chain(function() {
			$this.make_container()
		});
	},
	
	make_container: function() {
		var $this = this;
		this.container = new Element('div', {
			'class': 'birthday-layer'
		}).setStyles({
			'opacity': 0,
			'left': ((window.getWidth() - 478) / 2).toInt(),
			'top': ((window.getHeight() - 200) / 2).toInt() + window.getScrollTop(),
			'visibility': 'visible'
		}).inject($(document.body));
		var container = new Element('div', {
			'class': 'birthday-container'
		}).inject(this.container);
		new Element('img', {
			'alt': 'zamknij',
			'src': '/htdoc/images/btn/close.gif',
			'border': 0,
			'class': 'birthday-close'
		}).inject(
			new Element('a', {
				'href': 'javascript:void(null)',
				'title': 'zamknij'
			}).addEvent('click', function() {
				$this.overlay.destroy();
				$this.container.destroy();
			}).inject(container)
		);
		var result = new Element('div', {
			'class': 'birthday-result'
		}).set('text', this.result()).inject(container);
		new Element('div', {
			'class': 'birthday-howto'
		}).inject(container);
		new Element('img', {
			'alt': 'więcej',
			'src': '/htdoc/images/btn/more.gif',
			'border': 0,
			'class': 'birthday-more'
		}).inject(
			new Element('a', {
				'href': $this.more,
				'title': 'więcej'
			}).inject(container)
		);
		new Fx.Morph(this.container, {'duration': 500}).start({'opacity': [0, 1]});
	},
	
	result: function() {
		var offset = 7 + this.period - 28,
			day = this.day + offset,
			month = this.month,
			year = this.year;
		if (day > this.months[this.month - 1]) {
			day = day - this.months[this.month - 1];
			month = (this.month % 12) + 1;
			year += this.month != month ? 1 : 0;
		} else if (day < 1) {
			month = this.month > 1 ? this.month - 1 : 12;
			year -= this.month != month ? 1 : 0;
			day = this.months[month - 1] + day;  
		}
		if (month > 3) {
			month -= 3;
			if (this.year == year) {
				year += 1;
			}
		} else {
			month += 9; 
		}

		return (day < 10 ? '0' + day : day) + '.' + (month < 10 ? '0' + month : month) + '.' + year;  
	}
});

