var rlEventAccordion = new Class({
	heights : [],
	open : null,
	expLinkTextRegExp : /\[mehr lesen\]/i,
	tween : {
		duration : 500,
		trans : Fx.Transitions.linear
	},

	
		
	initialize : function(){
	
		$$("div.act").each(function(item,i){
			

			this.heights[i] = [item.getHeight(), item.getHeight()];
			
			
			var tmp = item.clone();
			
			tmp.setStyles({
				position : 'absolute',
				width : item.getWidth(),
				visibility : 'hidden'
				
			}).inject($$('.textbox')[0]);//o hai ie!
			
			item.store('accID',i.toString());
			
			tmp.getElements('.hidden').setStyle('display','block');

			this.heights[i][1] = tmp.getHeight();
			
			item.set({
				tween : {
					onComplete : function(){
						if(this.subject.retrieve('accID') != acc.open)
						{
							this.subject.getElements('.hidden').setStyle('display','none');
							$('expLink-'+this.subject.retrieve('accID')).tween('opacity',1);
						}
						else
						{
							$('expLink-'+this.subject.retrieve('accID')).tween('opacity',0);
						}
					},
					
					duration : this.tween.duration,
					transition : this.tween.trans
					
				},				
				styles:{
					overflow : 'hidden',
					height : this.heights[i][0]
				}});
			
			tmp.destroy();


			item.getElements("p a").each(function(a){
				if(a.get('text').test(this.expLinkTextRegExp))
				{
					a.addEvent('click',function($e){
						$e.stop();
						
						acc.toggle(this.retrieve('accID'));
						
					}).store('accID',i).set('id','expLink-'+i);
				}
			},this);

			
		},this);
		
		

	},
	
	toggle : function($accID){
		if($accID == this.open)return;
		
		
		if(this.open !== null){
			$$("div.act")[this.open].tween('height',this.heights[this.open][0]);
		}
		
		this.open = $accID;
		
		$$("div.act")[this.open].tween('height',this.heights[this.open][1]);
		$$("div.act")[this.open].getElements('.hidden').setStyle('display','block');
	}
	
	
	
});

var aus, rlAussteller = new Class({
	
	open : null,
	
	initialize : function(){
		$$("td img").addEvents({
			click : function($e){
			
				aus.show(this.get('id').split("_").getLast());
			
			}
		});	
		
	},
	
	show : function($aID)
	{
		
		this.open = $aID;
		new Element("div",{
			
			id : 'curtain',
			styles : {
				opacity : 0,
				height: $(window).getScrollSize().y + 140,
				width: $(window).getScrollSize().x,
				top:-140,
				zIndex : 444
			},
			
			events : {
				click : function($e){
					aus.close();
				}
			},
			
			tween : {
				onComplete : function(){
					aus.showContent();
				}
			}
			
		}).inject($(document.body)).tween('opacity',0.6);
	},
	
	showContent : function(){
		
		var tmp = $('div_'+this.open).clone();
		tmp.set('id','cloned_'+this.open);//hey ie!
		
		tmp.set({
		
			styles : {
				
				position:'absolute',
				width:500,
				display:'block',
				opacity : 0,
				zIndex : 999,
				top : 200 + $(window).getScrollTop(),
				left : (($(window).getSize().x - 540) / 2) + 118
				
			},
			tween : {
				onComplete : function(){
					
					new Element("a",{
						text : 'schliessen',
						id : 'close-btn',
						href : 'javascript:aus.close()'
						
					}).inject($('cloned_'+aus.open)).morph({
							top : -20,
							height : 20
						});
					
				}
			}
		}).inject($(document.body)).tween('opacity',1);
		
		
		
	},
	
	close : function(){
		$('curtain').destroy();
		$('close-btn').destroy();
		$('cloned_'+aus.open).destroy();
	}
});




var nl, rlNewsLetterBox = new Class({
	

	
	initialize : function(){
		$$("#newsletter a").addEvent('click',function($e){
			$e.stop();
			nl.tryToSubmit();
		});

		$$("#newsletter input").addEvent('focus',function($e){

			this.setStyle('background','none');

		});
	
	},
	
	tryToSubmit : function(){
		
		$$(".nlError").each(function(item){
			item.destroy();
		});
		
		var Err = false;
		
		$$("#newsletter input").each(function(item){
			
			if(item.get('value').length < 3){
				Err = true;
				item.set('tween',{ duration : 1000, transition : 'elastic:in:out' }).tween('backgroundColor',['#ff5555','#ffffff']);
			}
			
		},this);
		
		if(!Err){
				new Request.JSON({
					url : 'nl.php',
					onComplete : function(){
						$('newsletter').set({
							styles : {
								overflow : 'hidden'
							},
							
							morph : {
								onComplete : function(){
									$('newsletter-danke').morph({
										opacity : [0,1],
										height : 183
									})
								}
							}
						}).morph({
							opacity : 0,
							height : 0
						});
					}
				}).post({
					
					name : $('ipt-name').get('value'),
					email : $('ipt-email').get('value')
				});
		}
	}
	
});



window.addEvent('load',function(){
	
	if($$("div.act").length > 0){
			acc = new rlEventAccordion();
	}
	
	if($('ausstellerbox')){
		aus = new rlAussteller();
	}


	nl = new rlNewsLetterBox();
	
});

