window.addEvent("domready",function(e){
	$$(".mainNav ul")[0].getChildren("li").each(function(elem,a,b){elem.className=elem.className+" mainNavLi"+(a+1);});	
	corners();
	wetter.delay(100);
	if($$(".bigPicHidden img").length>2)
	{
		var vl=new Blender();
		vl.duration=3000;
		vl.pause=5000;
		$$(".bigPic")[0].adopt(vl.init($$(".bigPicHidden img")));
		vl.start.delay(4000,vl);
	}	
	new imageResizer().init();
	new imageAutoResizer().init();
	AutoToggler();
	
	try{mapsload(mapsLoader[0],mapsLoader[1])}catch(e){};
	if($("vereine")!=null && $$(".managerNav").length==0) new Vereine().init();
	$$('.mainNav')[0].getChildren("ul").each(function( elem,a,b ){slideNav(elem.getChildren("li"));});
	
	if($$(".waswo").length==1) new waswo();
	
	if($$(".newsEntry").length>8) 
	{
		var pgr=new paginator($$(".newsEntry"),8);
		var top=pgr.start();
		top.inject($$(".content")[0],"top");
		cloneEventsRecursive(top,top.clone(true)).inject($$(".content")[0],"bottom");
		pgr.select(null,0);
	}
});

function cloneEventsRecursive(from,to)
	{
		try{to.cloneEvents(from);}catch(e){return to;};
		for(var x=0;x<from.childNodes.length;x++) if(from.childNodes[x].nodeType!=3) cloneEventsRecursive(from.childNodes[x],to.childNodes[x]);
		return to;
	}

paginator = function(els,elementsPerPage)
{
	this.els=els;
	this.elementsPerPage=elementsPerPage;
	this.pages=new Array();
	
	this.start=function()
	{
		if(this.els.length==0) return;
		for(var x=0;x<this.els.length;x++)
		{
			var p=Math.floor(x/(this.elementsPerPage));
			if($type(this.pages[p])!=="array") this.pages[p]=new Array();
			this.pages[p][this.pages[p].length]=this.els[x];
		}
		var div=new Element("div",{text:"Seite: ",'class': "paginatorLine",styles: {clear: "both","text-align":"right"}});
		for(var x=0;x<this.pages.length;x++)
		{
			var a=new Element("a",{href: "#",styles: {borderRight: "1px solid #C5A6CC", padding: "0 4px"},'class': "paginateNavLinkA "+x,text: x+1,events:{click: this.select.bindWithEvent(this,x)}});
			if(x==this.pages.length-1) a.setStyle("borderRight","none");
			div.adopt(a);
		}
		return div;		
	}
	
	this.select=function(e,cP)
	{
		if(e!=null) e.stop();
		for(var x=0;x<this.pages.length;x++)
		{
			for(var y=0;y<this.pages[x].length;y++)
			{
				if(x!=cP) this.pages[x][y].setStyle("display","none");
				else this.pages[x][y].setStyle("display","block");
			}
			$$(".paginateNavLinkA."+x).each(function(ite){if(x!=cP) ite.removeClass("active"); else ite.addClass("active");});
			//else $$(".paginateNavLinkA."+x).each(function(ite){ite.removeClass("active");});
		}
	}
}

function waswo()
{
	this.seps=new Array(
		new Array("A","B","C","D","E"),
		new Array("F","G","H","I","K"),
		new Array("L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
	);
	
	this.show=function(x)
	{
		$$(".waswo")[0].getChildren("li").each(function(ite,ind){
			
			ite.setStyle("display",
			this.seps[x].contains(ite.className.substr(12))?"":"none");			
		}.bind(this));
	}
	
	this.init=function()
	{
		var div=new Element("div",{styles:{paddingBottom: "2px",marginBottom: "20px",borderBottom: "1px solid #E29747"}});
		for(var i=0;i<this.seps.length;i++)
		{
			var a=this.seps[i];
			var xx=i;
			var l=new Element("a",{href: "#",text: a[0]+"-"+a[a.length-1],styles:{padding: "2px 30px 2px 2px"},events:{
				"click": this.show.bind(this,i)
			}});
			l.addEvent("click",function(e){e.stop();});
			div.adopt(l);
		}
		div.inject($$(".content")[0],"top");
		this.show(0);
	}
	
	this.init();
}

window.addEvent("load",function(e)
{
	
	if($$("#mGallery").length>0) mGalery();
	if($$(".leftSide")[0]!==undefined && $$(".content")[0]!==undefined) setDivsSameMinHight($$(".leftSide")[0],$$(".content")[0]);
});

function setDivsSameMinHight(el1,el2)
{
	if(el1==null || el2==null) return;
	//var co1=el1.getCoordinates();
	//var co2=el2.getCoordinates();
	//var sz1=co1.bottom-co1.top;
	//var sz2=co2.bottom-co2.top;
	var padMar=el2.getStyle("padding-bottom").toInt()+el2.getStyle("padding-top").toInt()+el2.getStyle("margin-top").toInt();
	el2.setStyle("min-height",el1.getSize().y-padMar);
}

function Vereine()
{
	this.cats=new Array();
	this.init=function()
	{
		if($$(".ddb div.category").length==0) return;
		var div=new Element("div",{'class': "vereinsNavigation"}).inject($$(".content")[0],"top");
		var t=new Array();
		$$(".ddb div.category").each(function(ite,ind){
			var cat={id:ite.className.substr(10),text: $(ite.getElements("span")[0]).get("text")};
			if(t.contains(cat.id)===false) {this.cats[this.cats.length]=cat;t[t.length]=cat.id;}
		}.bind(this));
		
		this.cats.each(function(ite,ind){
			new Element("a",{
			href: "#",
				text: ite.text,events: 
				{click: 
					function(e)
					{
							if(e!=null) e.stop();
							this.show(ite.id);
							div.getElements("a").each(function(ite,inde)
							{
								if(ind==inde) ite.className=ite.className+" active"; 
								else ite.className=ite.className.replace(" active",""); 
							});
					}.bind(this)
				}
			}).inject(div);
		}.bind(this));
		var as=div.getElements("a");
		as[as.length-1].className=as[as.length-1].className+"last";
		as[0].fireEvent("click");
	}
	
	this.show=function(id)
	{
		$$(".ddb div.category").each(function(ite,ind){ite.setStyle("display","none")});
		if(id!=null) $$(".ddb div.category.c"+id).each(function(ite,ind){ite.setStyle("display","block")});
	}
}

function AutoToggler()
{
	$$(".autoToggleElementActor").each(function(ite,inde){
	var x=$(ite.parentNode).getChildren(".autoToggleElement");
	if(x==null) return;
	if(x.length==0) return;
	var myFx = new Fx.Slide(x[0]);
	ite.addEvent("click",function(e){
		e.stop();
		myFx.toggle();
	});
	myFx.hide(); 
	});
}
		
function mapsload(locations,locationsAll) 
{
  if (GBrowserIsCompatible())
  {
	var div=new Element("div",{styles: {width: 733,height: 342}});
	
	$$(".smallPic")[0].adopt(div);
	//div.inject($$(".smallPic")[0],"top");
	var map = new GMap2(div);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var point = new GLatLng(locationsAll[0],locationsAll[1]);
    map.setCenter(point, locationsAll[2],G_NORMAL_MAP);
    click1=new Array(locations.length);
    for(i=0;i<locations.length;i++) map.addOverlay(createMarker(i,locations[i],locationsAll[2],map));
  }
}

function createMarker(nr,locArray,zoomDec,map)
{
	var point = new GLatLng(locArray[0],locArray[1]);
	var marker = new GMarker(point,{title: locArray[3]});
	click1[nr]=0;
	GEvent.addListener(marker, "click",function(a) {
		zoom=(click1[nr]==0)?locArray[2]:zoomDec;
		console.log(zoom);
		map.setCenter(point,zoom,G_NORMAL_MAP);
		click1[nr]++;
		click1[nr]=(click1[nr]==2)?0:click1[nr];
		/*if(/*locArray[4]==1 && * /click1[nr]!=0)
		{
			var myHtml=locArray[3];
			map.openInfoWindowHtml(point, myHtml);
		}*/
	});	
	return marker;
}

function mGalery()
{
	var padding=15;
	var mf = new MooFlow($('mGallery'), {
		startIndex: 0,
		useSlider: true,
		useAutoPlay: false,
		useCaption: true,
		useResize: false,
		useMouseWheel: true,
		useKeyInput: true,
		
		onClickView: function(mixedObject){
			var div=new Element("div",{
				styles: {"max-width": 800,width: mixedObject.width,backgroundColor: "#fff",cursor: "pointer",padding: padding,"text-align":"right",border: "3px solid #aaa"},
				events: {click: function(){closeOverlay();}}
			});
			var img=new Image();
			img.src=mixedObject.src;
			$(img).inject(div);
			$(img).setStyles({"padding-bottom":padding});
			new Element("br").inject(div);
			new Element("strong",{text: mixedObject.title}).inject(div);
			new Element("p",{html: decodeURIComponent(mixedObject.alt).replace("\r","<br/>")}).inject(div);
			new Element("span",{text: "schließen",styles: {padding:"2px 4px",backgroundColor: "#aaa",color: "#fff",cursor: "pointer"}/*,events:{click: function(e){e.stop();closeOverlay();}}*/}).inject(div);
			$(img).addEvent("load",function(){addOverlay(div);}.delay(100));
		}});		
}

function corners()
{
	conerizer($$(".bigPic")[0],true,true,true,true);
	conerizer($$(".footer")[0],true,true,true,true);
	conerizer($$(".mContent.news")[0],true,false,false,false);
	conerizer($$(".baby")[0],false,true,true,false);
	conerizer($$(".startContentTeaser")[0],false,false,true,true);
	conerizer($$(".buttons")[0],false,true,true,false);
	conerizer($$(".leftTop")[0],true,false,false,true);
	conerizer($$(".smallPic")[0],false,true,true,false);
	conerizer($$(".navigation")[0],true,false,false,true);
	$$(".chunk").each(function(ite,ind){conerizer(ite,true,false,false,true);});
	conerizer($$(".content")[0],false,true,true,false);
	conerizer($$(".chunkimage")[0],true,false,false,true);	
	conerizer($$(".wetter")[0],false,true,true,false);
}

imageAutoResizer=function()
{
	this.init=function()
	{
		this.iR=new imageResizer();
		var imgs=$$(".main img");
		for(var i=0;i<imgs.length;i++)
		{
			if(imgs[i].parentNode.className=="resizeOnClick" || imgs[i].alt=="captcha") continue;
			var img=new Element("img");
			img.addEvent("load",this.parseImg.bind(this,new Array(img,imgs[i])));
			img.src=imgs[i].src;
		}
	}
	
	this.parseImg=function(imgNew,imgOld)
	{
		if(imgOld.width==imgNew.width) return true;
		else this.slimIt(imgOld);
	}
	
	this.slimIt=function(el)
	{
		var a=new Element("a",{href:el.src});
		a.wraps(el);
		this.iR.addImage(a);
	}
}

imageResizer=function()
{
	this.duration=1000;
	
	this.init=function()
	{
		var links=$$(".resizeOnClick");
		if(links.length==0) return;
		for(var i=0;i<links.length;i++) this.addImage(links[i]);
	}
	
	this.addImage=function(link)
	{
		var img=new Image();
		img=$(img);
		img.setStyles({display:"none"});
		$(document.body).adopt(img);
		img.onload=function(){this.setAttribute("width",this.width);this.setAttribute("height",this.height);};
		img.src=link.href;
		var sI=link.getElements("img");
		if(sI.length!=1 || sI[0].alt=="captcha") return false;
		link.addEvent("click",function(e){e.stop();this.show(img,sI[0])}.bind(this));
		link.target="_blank";
		return true;
	}
	
	this.show=function(img,sI)
	{
		var cI=img.clone();
		var fullSize={x: img.getAttribute("width"),y: img.getAttribute("height")};
		if(fullSize.x>800) 
		{
			fullSize.y=800/fullSize.x*fullSize.y;
			fullSize.x=800;			
		}
		
		if(sI.getSize().y*1.5>=fullSize.y) return;		
		cI.setStyles(
		{
			width: sI.getSize().x,
			height: sI.getSize().y,
			top: sI.getCoordinates().top,
			left: sI.getCoordinates().left,
			position: "absolute",
			display: "block",
			zIndex: 3,
			border: "0px solid #000"
		});
		
		var e=new Fx.Morph(cI, {duration: this.duration});
		var borderAndPaddingOffset=3;
		e.start(
			{
				width: fullSize.x,
				height: fullSize.y,
				borderWidth: 3,
				top: sI.getCoordinates().top-Math.round((fullSize.y-sI.getSize().y)/2)-borderAndPaddingOffset,
				left: sI.getCoordinates().left-Math.round((fullSize.x-sI.getSize().x)/2)-borderAndPaddingOffset
			}
		);
		cI.addEvent("click",function(x){
			e.cancel();
			e.onComplete=function(){cI.dispose();};
			e.start(
			{
				width: sI.getSize().x,
				height: sI.getSize().y,
				top: sI.getCoordinates().top,
				left: sI.getCoordinates().left,
				backgroundColor: "#fff",
				borderWidth: 0
			});
		});
		$(document.body).adopt(cI);
	}
}

function resizeImage(link)
{
	var img=link.getElements("img")[0];
	new Element("div",
	{
		styles:	
		{
			width: img.getSize().x,
			height: img.getSize().y,
			top: img.getCoordinates().top,
			left: img.getCoordinates().left,
			position: "absolute"
		}
	});
	bigPic
	div.adopt(bigPic);
	$(document.body).adopt(div);
}

function wetter()
{
	if($$(".wetter").length===0) return;
	var iF=new Element("iframe",{src:"wetter.html", border:"0", frameborder:"0", width:"248px", height:"240px", scrolling:"none"});
	$$(".wetter")[0].adopt(iF);
}

conerizer=function(el,tl,tr,ur,ul)
{
	if(el===undefined) return;
	el.setStyles({position:"relative"});
	if(tl===true) new Element("div",{"class": "corner10",styles:{top: 0,left: 0,width: 15,height: 15}}).inject(el,"top");
	if(ul===true) new Element("div",{"class": "corner00",styles:{bottom: 0,left: 0,width: 15,height: 15}}).inject(el,"top");
	if(tr===true) new Element("div",{"class": "corner11",styles:{top: 0,right: 0,width: 15,height: 15}}).inject(el,"top");
	if(ur===true) new Element("div",{"class": "corner01",styles:{bottom: 0,right: 0,width: 15,height: 15}}).inject(el,"top");
}

function slideNav(lis,x)
{
	lis.each( function( elem,a,b ){	
		var list = elem.getChildren('ul');
		
		if(!list || list.length!=1) return;
		list[0].setStyles({"display":"block",position: "absolute",width: 195});
		
		var myFx = new Fx.Slide(list[0],{mode:(x===true)?'horizontal':'vertical'}).hide();
		
		elem.addEvents({
			'mouseenter' : function(){
				myFx.cancel();
				myFx.slideIn();
			},'mouseleave' : function(){
				myFx.cancel();
				myFx.slideOut();
			}
		});
	});
}
Blender = function()
{
	this.duration=1000;
	this.pause=5000;
	this.els=new Array();
	this.parent=new Element("div",{
	styles: {position: "relative"}
	});
	this.current=0;
	this.e=false;
	this.stopAfterEffekt=false;
	this.stop=0;
	this.startingFN=false;
	
	this.completeCurrentFN=false;
	
	this.setCompleteCurrentFN=function(fn)
	{
		this.completeCurrentFN=fn;
	}
	
	this.setStartingFN=function(fn)
	{
		this.startingFN=fn;
	}
	
	this.init=function(els)
	{
		this.max=els.length-1;
		for(var i=0;i<els.length;i++)
		{
			this.els[i]=els[i].dispose();
		}
		this.insertNew(0);
		this.parent.childNodes[0].setStyle("opacity",1);
		
		this.parent.addEvent("mouseenter",function(e){this.setStopAfterEffekt(true);}.bind(this));
		this.parent.addEvent("mouseleave",function(e){this.setStopAfterEffekt(false);}.bind(this));
		
		return this.parent;
	}
	
	this.setStopAfterEffekt=function(b)
	{
		this.stopAfterEffekt=b;
	}
	
	this.startFirst=function()
	{
		this.start.delay(this.pause,this);
	}
	
	this.show=function(i)
	{
		this.stop++;
		this.current=i;
		this.insertNew(this.current);
		this.startEffekt(i);
	}
	
	this.startEffekt=function(c)
	{
		if(this.startingFN!==false) this.startingFN(c);
		this.e.start({opacity: 1});
	}
	
	this.start=function()
	{
		if(this.stop>0) 
		{
			this.stop--;
			return;
		}
		if(this.stopAfterEffekt===true) 
		{
			this.start.delay(this.pause,this);
			return;
		}
		this.current=this.getNext();
		this.insertNew(this.current);
		this.startEffekt(this.current);
	}
	
	this.getNext=function()
	{
		if(this.current+1>this.max) return 0;
		else return this.current+1;
	}
	
	this.insertNew=function(c)
	{
		var el=this.els[c].clone(true);
		el.setStyles({position: "absolute",display: "block",top: "0px",opacity: 0});
		//for IE8 bug?
		el.className=this.els[c].className;
		
		this.parent.adopt(el);
		this.e=new Fx.Morph(el, {duration: this.duration,onComplete: this.completeParticial.bind(this,c)});
	}
	
	this.completeParticial=function(c)
	{
		this.clear();
		this.start.delay(this.pause,this);
		if(this.completeCurrentFN!==false) this.completeCurrentFN(c);
	}
	
	this.clear=function()
	{
		var cN=this.parent.childNodes;
		if(cN.length>2)
		{
			for(var i=0;i<cN.length-2;i++)
			{
				cN[i].dispose();
			}
		}
	}
}


