var search={};

window.addEvent("domready",function(){
	// hover effect in the main menu
	$$('#mm a').each(function(a){
		var span=new Element('span');
		span.inject(a,'before');

		if(!a.hasClass('active')){
			span.setStyle('opacity',0);
			span.set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});

			a.addEvents({
				'mouseenter':function(){span.morph({'opacity':1});},
				'mouseleave':function(){span.morph({'opacity':0});}
			});
		}
	});
	// ---

	// prepare the spell overlay in the upper right corner
	var div=new Element('div',{'id':'spelloverlay'});
	div.inject($('spell'),'after');

	div.set('morph',{duration:2000,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
	div.morph({'left':970});
	// ---

	// options menu, making the search box invisble by default ...
	opts=$('options');
	if(opts){
		ul=new Element('ul');

		li=new Element('li',{
			'html':'<img src="http://www.marienbraeu.com/layout/enlarge.gif" alt="Schrift vergrößern" /> Schrift vergrößern'
		});
		li.set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		li.addEvents({
			'click':function(){zoomin();},
			'mouseover':function(){this.morph({'color':'#FFFFFF'});},
			'mouseout':function(){this.morph({'color':'#FDDFC2'});}
		});
		li.inject(ul);

		li=new Element('li',{
			'html':'<img src="http://www.marienbraeu.com/layout/minimize.gif" alt="Schrift verkleinern" /> Schrift verkleinern'
		});
		li.set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		li.addEvents({
			'click':function(){zoomout();},
			'mouseover':function(){this.morph({'color':'#FFFFFF'});},
			'mouseout':function(){this.morph({'color':'#FDDFC2'});}
		});
		li.inject(ul);

		li=new Element('li',{
			'html':'<img src="http://www.marienbraeu.com/layout/print.gif" alt="Seite drucken" /> Seite drucken'
		});
		li.set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		li.addEvents({
			'click':function(){window.print();},
			'mouseover':function(){this.morph({'color':'#FFFFFF'});},
			'mouseout':function(){this.morph({'color':'#FDDFC2'});}
		});
		li.inject(ul);

		li=new Element('li',{
			'html':'<img src="http://www.marienbraeu.com/layout/search.gif" alt="Seite durchsuchen" /> Seite durchsuchen'
		});
		li.set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
		li.addEvents({
			'click':function(){sucheToggle();},
			'mouseover':function(){this.morph({'color':'#FFFFFF'});},
			'mouseout':function(){this.morph({'color':'#FDDFC2'});}
		});
		li.inject(ul);

		ul.inject(opts);
	}
	// ---

	// retrieve the font size cookie
	if(document.cookie){
		document.cookie.match(/fontSize=(.+?);/);
		var size=RegExp.$1*1;

		if(size){
			$$('html')[0].style.fontSize=size+"em";
		}
	}
	// ---

	// prepare the search box
	search['height']=$('suche').getStyle('height');
	search['padding-top']=$('suche').getStyle('padding-top');
	search['padding-bottom']=$('suche').getStyle('padding-bottom');

	if(!$('sfield').value){
		$('suche').setStyle('height',0);
		$('suche').setStyle('padding-top',0);
		$('suche').setStyle('padding-bottom',0);

		$('suche').vis=false;
	}else{
		$('suche').setStyle('height',search['height']);
		$('suche').setStyle('padding-top',search['padding-top']);
		$('suche').setStyle('padding-bottom',search['padding-bottom']);

		$('suche').vis=true;
	}

	$('suche').set('morph',{duration:300,transition:Fx.Transitions.Sine.easeOut,link:'cancel'});
	// ---

	// launch the multibox
	if($$('a.mbox'))
		mb=new multiBox('mbox',{
			'overlay':new overlay()
		});
	// ---

	// recommendation clickable
	if($('shop-recommendation')){
		var div=$('shop-recommendation');
		div.href=div.getElement('.description a').get('href');
		div.href=div.getElement('.description').dispose();
		div.addEvent('click',function(){
			location.href=this.href;
		});
		div.setStyles({
			'cursor':'pointer'
		});
	}
	// ---

	// groups clickable
	if($$('div.groups .group')){
		$$('div.groups .group').each(function(div){
			div.href=div.getElement('a').get('href');
			div.addEvent('click',function(){
				location.href=this.href;
			});
			div.setStyles({
				'cursor':'pointer'
			});
		});		
	}
	// ---

	// articles clickable
	if($$('div.articles .article')){
		$$('div.articles .article').each(function(div){
			div.href=div.getElement('.description a').get('href');
			div.getElement('.description').dispose();
			div.addEvent('click',function(){
				location.href=this.href;
			});
			div.setStyles({
				'cursor':'pointer'
			});
		});		
	}
	// ---

	// cart clickable
	if($('shop-cart')){
		$('shop-cart').addEvent('click',function(){
			location.href=this.getElement('a').get('href');
		});
		$('shop-cart').setStyles({
			'cursor':'pointer'
		});
	}
	// ---

	// add an amount field to the cart button
	if($$('div.single-article .cart')){
		$$('div.single-article .cart').each(function(li){
			var input=new Element('input',{
				'type':'text',
				'class':'counter',
				'value':1,
				'events':{
					'change':function(){
						var c=parseInt(this.get('value'));
						c=c!='NaN'&&c>0?c:1;

						this.set('value',c);

						/\/Warenkorb\/parameter\/(\d+)\/\d+\/(.+)$/.exec(this.getParent().getElement('a').get('href'));
						var art=RegExp.$1;
						var link=RegExp.$2;

						this.getParent().getElement('a').set('href',this.getParent().getElement('a').get('href').replace(/\/Warenkorb\/parameter\/.+$/g,"/Warenkorb/parameter/"+art+"/"+c+"/"+link));
					}
				}
			});
			input.inject(li,'top');
		});
	}
});

function sucheToggle(){
	if($('suche').vis){
		$('suche').vis=false;
		$('suche').morph({'height':0,'padding-top':0,'padding-bottom':0});
	}else{
		$('suche').vis=true;
		$('suche').morph({'height':search['height'],'padding-top':search['padding-top'],'padding-bottom':search['padding-bottom']});
	}
}

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+';'; 
}