var LayerAbstract = Class.create({
	initialize: function(element) {
		this.element = $(element);
		this.loaded = false;
	},
	
	load: function() {
		this.loaded = true;
	},
	
	toggle: function() {
		if(!this.loaded) {
			this.load();
		}
		this.element.toggle();
	},
	
	show: function() {
		if(!this.loaded) {
			this.load();
		}
		this.element.show();
	},
	
	hide: function() {
		this.element.hide();
	}
});

var LayerEmpfehlen = Class.create(LayerAbstract, {
	load: function() {
		var th = this;
		new Ajax.Request('/ajax/empfehlen.html', {
			onSuccess: function(transport) {
				th.element.update(transport.responseText);
				th.element.down('.close').down('a').observe('click', th.hide.bindAsEventListener(th));
				th.element.down('form').observe('submit', th.submit.bindAsEventListener(th));
				th.element.down('input[name=empf_url]').value = location.pathname;
				th.element.down('.fehler').hide();
				th.element.down('.erfolg').hide();
				new Draggable(th.element, { scroll: window, handle: th.element.down('.handle') });
				th.loaded = true;
			}
		});
	},
	
	submit: function(event) {
		event.stop();
		var th = this;
		this.element.down('.fehler').hide();
		this.element.down('.erfolg').hide();
		this.element.down('form').request({
			onSuccess: function(transport) {
				var data = transport.responseJSON;
				if(data.success) {
					th.element.down('.erfolg').update(data.msg).show();
				} else {
					th.element.down('.fehler').update(data.msg).show();
				}
			}
		});
	}
});

var LayerIcq = Class.create(LayerAbstract, {
	load: function() {
		var th = this;
		new Ajax.Request('/ajax/icq.html', {
			onSuccess: function(transport) {
				th.element.update(transport.responseText);
				th.element.down('.close').down('a').observe('click', th.hide.bindAsEventListener(th));
				new Draggable(th.element, { scroll: window, handle: th.element.down('.handle') });
				th.loaded = true;
			}
		});
	}
});

var LayerKontakt = Class.create(LayerAbstract, {
	load: function() {
		var th = this;
		new Ajax.Request('/ajax/kontakt.html', {
			onSuccess: function(transport) {
				th.element.update(transport.responseText);
				th.element.down('.close').down('a').observe('click', th.hide.bindAsEventListener(th));
				th.element.down('form').observe('submit', th.submit.bindAsEventListener(th));
				th.element.down('span.fehler').hide();
				th.element.down('span.erfolg').hide();
				new Draggable(th.element, { scroll: window, handle: th.element.down('.handle') });
				th.loaded = true;
			}
		});
	},
	
	submit: function(event) {
		event.stop();
		var th = this;
		th.element.select('label').each(function(item) {
			item.removeClassName('fehler');
		});
		this.element.down('span.fehler').hide();
		this.element.down('span.erfolg').hide();
		this.element.down('form').request({
			onSuccess: function(transport) {
				var data = transport.responseJSON;
				$A(data.errfields).each(function(item) {
					th.element.down('label[for=lyr_'+item+']').addClassName('fehler');
				});
				if(data.success) {
					th.element.down('span.erfolg').update(data.msg).show();
				} else {
					th.element.down('span.fehler').update(data.msg).show();
				}
			}
		});
	}
});

var LayerMinikalender = Class.create({
	initialize: function(element) {
		var date = new Date();
		this.cur_month = date.getMonth() + 1;
		this.cur_year = date.getFullYear();
		this.cur_land = '';
		this.element = $(element);
		this.input = null;
		this.loaded = false;
	},
	
	load: function() {
		var th = this;
		new Ajax.Request('/ajax/minikalender.html', {
			onSuccess: function(transport) {
				th.element.update(transport.responseText).down('.close').down('a').observe('click', th.hide.bindAsEventListener(th));
				new Draggable(th.element, { scroll: window, handle: th.element.down('.handle') });
				th.update();
				th.loaded = true;
			}
		});
	},
	
	show: function(input) {
		if(!this.loaded) {
			this.load();
		}
		var input = $(input);
		if(Object.isUndefined(input)) {
			this.input = null;
		} else {
			this.input = input;
			this.element.clonePosition(this.input, {
				setWidth: false,
				setHeight: false,
				offsetLeft: -10,
				offsetTop: this.input.getHeight()
			});
		}
		this.element.show();
	},
	
	hide: function(event, value) {
		if(!Object.isUndefined(value) && this.input != null) {
			this.input.value = value;
		}
		this.element.hide();
	},
	
	toggle: function() {
		if(this.element.visible()) {
			this.hide();
		} else {
			this.show();
		}
	},
	
	showTooltip: function(event, text) {
		var el = Event.element(event);
		var pos = el.positionedOffset();
		var el_tt = this.element.down('.tooltip');
		el_tt.update(text).show();
		el_tt.clonePosition(el, {
			setWidth: false,
			setHeight: false,
			offsetLeft: Math.min(this.element.getWidth()-el_tt.getWidth()-pos[0], 0),
			offsetTop: 20
		});
	},
	
	hideTooltip: function(event) {
		this.element.down('.tooltip').hide();
	},
	
	nextMonth: function() {
		this.cur_month += 1;
		if(this.cur_month == 13) {
			this.cur_month = 1;
			this.cur_year += 1;
		}
		this.update();
	},
	
	prevMonth: function() {
		this.cur_month -= 1;
		if(this.cur_month == 0) {
			this.cur_month = 12;
			this.cur_year -= 1;
		}
		this.update();
	},
	
	setBundesland: function(land) {
		if(this.cur_land != land) {
			this.cur_land = land;
			this.update();
		}
	},
	
	update: function() {
		var th = this;
		new Ajax.Request('/ajax/minikalender.php', {
			method: 'post',
			parameters: {
				month: this.cur_month,
				year: this.cur_year,
				land: this.cur_land
			},
			onSuccess: function(transport) {
				$('minikal_monat').update(transport.responseText);
				$('minikal_monat').select('.hasTooltip').each(function(item) {
					item.observe('mouseover', th.showTooltip.bindAsEventListener(th, item.title)).title = null;
					item.observe('mouseout', th.hideTooltip.bindAsEventListener(th));
				});
				$('minikal_monat').down('table.tage').select('td').each(function(item) {
					item.observe('click', th.hide.bindAsEventListener(th, item.down('input').value));
				});
			}
		});
	}
});

var LayerVersicherung = Class.create(LayerAbstract, {
	load: function() {
		var th = this;
		new Ajax.Request('/ajax/versicherung.php', {
			onSuccess: function(transport) {
				th.element.update(transport.responseText);
				th.element.down('.close').down('a').observe('click', th.hide.bindAsEventListener(th));
				new Draggable(th.element, { scroll: window, handle: th.element.down('.handle') });
				th.loaded = true;
			}
		});
	},
	
	show: function() {
		if(!this.loaded) {
			this.load();
		}
		this.element.show();
		var offset = document.viewport.getScrollOffsets();
		this.element.setStyle({ top: offset.top + 10 + 'px' });
	}
});