var ExpandingTeaser = Fx.Styles.extend({
	options: {
		open:false
	},	

	
	initialize: function(el,toggler,body,options) {			
		this.container = $(el);
		var coords = $(body).getCoordinates();
		this.states = [
			{height:0,opacity:0},
			{height:coords['height'],opacity:1}
		];		

		$(toggler).addEvent('click',this.toggle.bindWithEvent(this));						
		
		this.parent($(body),options);
				
		this.set(this.states[0]);				
		this.container.removeClass('expTeaserOpen');
	},
		
	toggle: function(e,open) {
		this.open = $chk(open) ? open : !this.open;
		this.start(this.states[1 * this.open]);
		if (this.open)
			this.container.addClass('expTeaserOpen');
		else 
			this.container.removeClass('expTeaserOpen');		
	}	
});


window.addEvent(
	'domready',
	function() {	
		$$('.expTeaser').each(function(el) {
			new ExpandingTeaser(el,$E('.expTeaserHandle',el),$E('.expTeaserBody',el));
		});
					
		var submitAdded = false;	
		var dd = null;
		
		$$('input.autolabel, input.autolabel2').each(function(el) {
			(el.$attributes = el.$attributes ? el.$attributes : {}).initialLabel = el.value;
			el.addEvents({
				focus : function() {if (el.value == el.$attributes.initialLabel) el.value = '';},
				blur  : function() {if (el.value.trim() == '' )el.value = el.$attributes.initialLabel;}
			});
			
			dd = $(el.getParent());
			if (dd.getTag() == 'dd') {
				$(dd).getPrevious('dt').setStyle('display','none');
			}
			
			if (el.hasClass('autolabel') && !submitAdded) {
				$(el.form).addEvent(
					'submit',
					function(ev) {
						ev = new Event(ev);						
						var canSubmit = ev.target.getElements('input[type=text]').every(
							function(el) {return (el.getValue() && el.getValue() != el.$attributes.initialLabel);							}
						);
						if (!canSubmit) {
							alert('Bitte füllen Sie alle Felder aus!');
							ev.stop();
							ev.stopPropagation();
							ev.preventDefault();
							return false;
						}
					}
				);
				submitAdded = true;
			}
		});
	}
);

