var root='http://www.kinder-und-hausarztpraxis.de/';

var boxes={};
var heights={'ok':false};

window.addEvent('load',function(){
	if($('team')){
		heights['first']=$$('.firstpic')[0].getStyle('height');
		heights['second']=$$('.secondpic')[0].getStyle('height');

		// $$('.secondpic')[0].setStyle('height',0);

		heights['ok']=true;
	}
});

window.addEvent('domready',function(){
	$('suche').setStyle('opacity',0);
	$('options').innerHTML='<ul><li id="fplus"><a href="#" title="Schrift vergrößern" onclick="zoomin();return false;"></a></li><li id="fminus"><a href="#" title="Schrift verkleinern" onclick="zoomout();return false;"></a></li><li id="print""><a href="#" title="Seite drucken" onclick="window.print();return false;"></a></li><li id="search"><a href="#" title="Seite drucken" onclick="$(\'suche\').fade(\'toggle\');return false;"></a></li></ul>';

	$each($$('#teaser .teaser li.minimize'),function(obj,i){
		obj.getChildren('div')[0].natH=obj.getChildren('div')[0].getStyle('height');
		obj.getChildren('div')[0].closed=true;

		obj.getChildren('div')[0].setStyle('height','0');
		obj.getChildren('div')[0].set('morph',{duration:900,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});

		obj.getChildren('h2')[0].setStyle('background','url('+root+'layout/enlarge.gif) no-repeat right bottom');
		obj.getChildren('h2')[0].setStyle('cursor','pointer');

		obj.getChildren('h2')[0].addEvent('click',function(){
			this.getParent().getChildren('div')[0].closed=!this.getParent().getChildren('div')[0].closed;

			if(this.getParent().getChildren('div')[0].closed){
				this.setStyle('background-image','url('+root+'layout/enlarge-active.gif)');
				this.getParent().getChildren('div')[0].morph({'height':0,'overflow':'hidden'});
			}else{
				this.setStyle('background-image','url('+root+'layout/sim-active.gif)');
				this.getParent().getChildren('div')[0].morph({'height':this.getParent().getChildren('div')[0].natH});
				(function(){this.getParent().getChildren('div')[0].setStyle('overflow','visible')}).delay(900,this);
			}
		});

		obj.getChildren('h2')[0].addEvent('mouseover',function(){
			if(this.getParent().getChildren('div')[0].closed){
				this.setStyle('background-image','url('+root+'layout/enlarge-active.gif)');
			}else{
				this.setStyle('background-image','url('+root+'layout/sim-active.gif)');
			}
		});

		obj.getChildren('h2')[0].addEvent('mouseout',function(){
			if(this.getParent().getChildren('div')[0].closed){
				this.setStyle('background-image','url('+root+'layout/enlarge.gif)');
			}else{
				this.setStyle('background-image','url('+root+'layout/sim.gif)');
			}
		});
	});

	if(document.cookie){
		document.cookie.match(/fontSize=(.+?);/);
		var size=RegExp.$1*1;

		if(size){
			$$('html')[0].style.fontSize=size+"em";
		}
	}

	if($('team')){
		$$('.firstpic')[0].set('morph',{duration:900,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		$$('.secondpic')[0].set('morph',{duration:900,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		$$('.secondpic')[0].setStyles({
			'display':'block',
			'opacity':0
		});

		$$('.firstpic')[0].addEvent('mouseover',function(){
			if(!heights['ok'])
				return;

			this.morph({
				'opacity':0,
				'height':0
			});
			$$('.secondpic')[0].morph({
				'opacity':1,
				'height':heights['second']
			});
		});

		$$('.secondpic')[0].addEvent('mouseleave',function(){
			if(!heights['ok'])
				return;

			$$('.firstpic')[0].morph({
				'opacity':1,
				'height':heights['first']
			});
			this.morph({
				'opacity':0,
				'height':0
			});
		});

		var displaydiv=new Element('div',{
			'id':'fotodescriptor',
			'style':{
				'opacity':1
			}
		});

		displaydiv.set('morph',{duration:900,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});		
		displaydiv.inject($$('.secondpic')[0]);

		$$('.secondpic map area').each(function(area){
			area.addEvents({
				'mouseenter':function(event){
					$$('li.'+this.className).addClass('active');

					$('fotodescriptor').set('html',$$('li.'+this.className)[0].get('html'));
					$('fotodescriptor').morph({
						'left':Math.min(Math.max(event.page.x-this.getParent('div').getPosition().x-110,0),298),
						'top':event.page.y-this.getParent('div').getPosition().y+50
					});
				},
				'mouseout':function(event){
					$$('li.'+this.className).removeClass('active');
				}
			});
		});
	}

	if($('news')){
		$('news').getElements('li').each(function(li){
			li.p=li.getElement('p').get('html');
			li.getElement('p').set('html',li.p.replace(/^(.{0,30}).+$/,"$1 ..."));

			var a=new Element('a',{
				'href':'#',
				'html':li.getElement('h2').get('html')
			});
			li.getElement('h2').set('html','');
			a.inject(li.getElement('h2'));

			li.setStyles({
				'cursor':'pointer'
			});

			li.addEvents({
				'click':function(){
					var swap=this.p;
					this.p=this.getElement('p').get('html');
					this.getElement('p').set('html',swap);
				}
			});
		});
	}
});

function zoomin(){
	zoom(.25)
}

function zoomout(){
	zoom(-.25)
}

function zoom(add){
	var regex=new RegExp(/(\d*)(\.?)(\d*)/);
	var html=$$('html')[0];

	html.style.fontSize.match(regex);

	if(RegExp.$2=='.'){
		var size=''+RegExp.$1+'.'+RegExp.$3;
	}else{
		var size=''+RegExp.$1+RegExp.$3;
	}

	size*=1;
	if(size==0)
		size=1;

	size+=add;
	size=Math.min(Math.max(size,.5),1.75);

	html.style.fontSize=size+"em";

	document.cookie='fontSize='+size+';'; 
}
