﻿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() {
  zoom=(click1[nr]==0)?locArray[2]:zoomDec;
  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="<b>"+locArray[3]+"</b>";
  map.openInfoWindowHtml(point, myHtml);
  }
    });
  return marker;
}

function mapsload(locations,locationsAll) 
{
  if (GBrowserIsCompatible())
  {
    mapsIsLoaded=true;
    var map = new GMap2($$(".picContent")[0]);
    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 setWholeHight(elid,size)
{
  var sz=$(elid).getStyle("paddingTop").toInt()+$(elid).getStyle("paddingBottom").toInt()+$(elid).getStyle("marginTop").toInt()+$(elid).getStyle("marginBottom").toInt();
	$(elid).setStyle("height",size-sz+"px");
}

function setDivsSameHight(elids)
{
  var height,i,pb,pt,mt,mb,size;
  height=0;
	for(i=0;i<elids.length;i++)
  {
    size=$(elids[i]).getSize();
		height=(size.y>height)?size.y:height;
  }
	for(i=0;i<elids.length;i++)
  {
    pt=$(elids[i]).getStyle("padding-top").toInt();
		pb=$(elids[i]).getStyle("padding-bottom").toInt();
		mt=$(elids[i]).getStyle("margin-top").toInt();
		mb=$(elids[i]).getStyle("margin-bottom").toInt();
		
		$(elids[i]).setStyle("height",height-pt-pb-mt-mb);
  }
}

function getOuterDimensions(elid)
{
	var pt,pb,mt,mb,pl,pr,ml,mr,w,h;
	pt=$(elid).getStyle("padding-top").toInt();
	pb=$(elid).getStyle("padding-bottom").toInt();
	
	mt=$(elid).getStyle("margin-top").toInt();
	mb=$(elid).getStyle("margin-bottom").toInt();
	
	pl=$(elid).getStyle("padding-left").toInt();
	pr=$(elid).getStyle("padding-right").toInt();
	
	ml=$(elid).getStyle("margin-left").toInt();
	mr=$(elid).getStyle("margin-right").toInt();
	
	w=$(elid).getStyle("width").toInt()+pl+pr+ml+mr;
	h=$(elid).getStyle("height").toInt()+pt+pb+mt+mb;
	
	return {width: w,height: h};
}


function initNav(img,dataType)
{
	nimg=new Image();
  nimg.src=img.src.substring(0,img.src.length-(dataType.length+2))+"h."+dataType;
  nimg.style.display="none";
  $(document.body).appendChild(nimg);
	normal(img,dataType);
}

function onloadNav(elId,dataType)
{
  var imgs=$(elId).getElements("img");
	for(i=0;i<imgs.length;i++) 
  {
    imgs[i].onmouseout=function(e) {normal( this,dataType );};
    imgs[i].onmouseover=function(e) {hover( this,dataType );};  
    initNav(imgs[i],dataType);
  }
}

function normal(img,dataType)
{
	if(img.parentNode.className=="aktiv") n="h";
  else n="n";
  img.src=img.src.substring(0,img.src.length-(dataType.length+2))+n+"."+dataType;
}

function hover(img,dataType)
{
  img.src=img.src.substring(0,img.src.length-(dataType.length+2))+"h."+dataType;
}

//gets ChildNode Index to direkt parent
//returns index
//needs testElementV1000
//1n: DOM-Element
//2n: (#Id,.Class,Nodename,DOM-Element)
function getChildPosV1000(child,allowedElements)
{
  var pn;
	if(child==false) return false;
  pn=child.parentNode.childNodes;
	var a=-1;
	for(var i=0;i<pn.length;i++) 
	{
  	if(testElementV1000(pn[i],allowedElements)===false) continue;
		a++;
  	if(pn[i]==child) return a;
	}
  return false;
}

overlay=function()
{
	this.addOverlay=function(el)
	{
		var div=new Element("div",{id:"overlay",styles: {zIndex: 1000,top:"0px",left:"0px",width:$(document.body).getScrollSize().x+"px",height:$(document.body).getScrollSize().y+"px",position: "absolute"}});
		$(document.body).insertBefore(div,$(document.body).firstChild);
		
		    div=new Element("div",{id:"overlayInput",styles: {zIndex: 1001,position: "absolute"}});
		
		var backLink=new Element("a",{
			href: "#",
			title: "Schließen",
			id: "closeOverlayDivA",
			text: "x",
			events:
			{
				click: function(e){e.stop(); this.closeOverlay();}.bind(this)
			}
		});
		
		div.adopt(backLink);
		div.adopt(el);
		
		$(document.body).insertBefore(div,$(document.body).firstChild);
		resizeOverlayEvent=this.newPosOverlay.bind(this);
		window.addEvent("resize",resizeOverlayEvent);
		this.newPosOverlay();
	}
	
	this.overlayRePos=function()
	{
		var elSz=$("overlayInput").getSize();
		var boSz=$(document.body).getSize();
		var boSc=$(document.body).getScroll();
		var t=Math.round((boSz.y-elSz.y)/2);
		t=(t<0)?0:t;
		$("overlayInput").style.top=t+boSc.y+"px";
		var l=Math.round((boSz.x-elSz.x)/2);
		l=(l<0)?0:l;
		$("overlayInput").style.left=l+boSc.x+"px";
	}
	
	this.newPosOverlay=function()
	{
		var boSz=$(document.body).getSize();
		var boSc=$(document.body).getScroll();
		var elSz=$("overlayInput").getSize();
		var t=Math.round((boSz.y-elSz.y)/2);
		t=(t<0)?0:t;
		$("overlayInput").style.top=t+boSc.y+"px";
		var l=Math.round((boSz.x-elSz.x)/2);
		l=(l<0)?0:l;
		$("overlayInput").style.left=l+boSc.x+"px";
	}

	this.closeOverlay=function()
	{
		window.removeEvent("resize",resizeOverlayEvent);
		$(document.body).removeChild($("overlay"));
		$(document.body).removeChild($("overlayInput"))
		return true;
	}
}



function getScreenSize()
{
  boSz=$(document.body).getSize();
	return boSz;
}

function implode(array,delimiter,start,end)
{
  if(start==null) start=0;
	if(end==null) end=array.length;
	var text="";
  for(var i=start;i<end;i++) text+=array[i]+delimiter;
	return text.substr(0,text.length-2);
}

//createTextNode
function cTn(tn) 							{ 	return document.createTextNode(tn); 		}
//createElement
function cEm(el) 							{		return new Element(el);			}
//cloneNode
function cN(el)  							{		return document.cloneNode(el);					}
//appendChild
function aC(pa,el)  					{		return pa.appendChild(el);							}
//append Elemenents to one child
function aCs(pa,els)   		 		{ 	for(i=0;i<els.length;i++) aC(pa,els[i]);}

//create element x-times 
function cEs(el,count) 
{		
  els=new Array();
  for(i=0;i<count;i++)  els[i]=cEm(el);
  return els;		
}

function returnDayName(day)
{
  if(day==1) return "Montag";
  if(day==2) return "Dienstag";
  if(day==3) return "Mittwoch";
  if(day==4) return "Donnerstag";
  if(day==5) return "Freitag";
  if(day==6) return "Samstag";
  if(day==0 || day==7) return "Sonntag";
}

function returnMonthName(mnr)
{
  if(mnr==0) return "Januar";
  if(mnr==1) return "Februar";
  if(mnr==2) return unescape("M%E4rz");
  if(mnr==3) return "April";
  if(mnr==4) return "Mai";
  if(mnr==5) return "Juni";
  if(mnr==6) return "Juli";
  if(mnr==7) return "August";
  if(mnr==8) return "September";
  if(mnr==9) return "Oktober";
  if(mnr==10) return "November";
  if(mnr==11) return "Dezember";
}

function returnMonthLength(mnr,year)
{
  if(mnr==0) return 31;
  if(mnr==1) 
  {
    r=28;
    if (year % 4 == 0) r++;
    if (year % 100 == 0) r--;
    if (year % 400 == 0) r++;
    return r;
  }
  if(mnr==2) return 31;
  if(mnr==3) return 30;
  if(mnr==4) return 31;
  if(mnr==5) return 30;
  if(mnr==6) return 31;
  if(mnr==7) return 31;
  if(mnr==8) return 30;
  if(mnr==9) return 31;
  if(mnr==10) return 30;
  if(mnr==11) return 31;
}

//makes 0=monday and 6=sunday not english formated
function parseEngDayToGerman(actdate)
{
  actDay=actdate.getDay()-1;
	actDay=(actDay<0)?6:actDay;
	return actDay;
}

//returns php time() (in js time() is with milisecounds
function dateToUnixTime(actdate) 
{
  actdate=actdate.getTime()+"";
	return actdate.substr(0,actdate.length-3);
}

//reset hours/minutes/secounds/milisec. from date
function dateToFullDayInUnixTime(actdate)
{
  actdate.setHours(0);
	actdate.setMilliseconds(0);
  actdate.setMinutes(0);
	actdate.setSeconds(0);
	return dateToUnixTime(actdate);
}


function removeChilds(parent)
{
  var pn,childs,l;
	pn=parent.childNodes;
	l=pn.length;
	childs=new Array();
	for(var i=0;i<l;i++) 
	{
  	childs[childs.length]=parent.removeChild(parent.firstChild);
	}
  return childs;
}

//gets ChildNode Index to direkt parent
//returns index
//needs testElementV1000
//1n: DOM-Element
//2n: (#Id,.Class,Nodename,DOM-Element)
function getChildPosV1000(child,allowedElements)
{
  var pn;
	if(child==false) return false;
  pn=child.parentNode.childNodes;
	var a=-1;
	for(var i=0;i<pn.length;i++) 
	{
  	if(testElementV1000(pn[i],allowedElements)===false) continue;
		a++;
  	if(pn[i]==child) return a;
	}
  return false;
}

//gets a "non-direct" perent, identified by:
//1n: DOM-Element
//2n: (#Id,.Class,Nodename,DOM-Element)
//3o: how Deep to search
function searchRelativeParentV1000(el,pIdentifier,depth)
{
  if(pIdentifier==null) return false;
	if(depth==null) depth=20; 
	var ptest=false;
	while(ptest==false && depth>0)
	{
  	oldEl=el;
		el=el.parentNode;
		ptest=testElementV1000(el,pIdentifier)
		depth--;
	}
	if(ptest===true) return el;
	else return false;
}

//test if element belongs to a given el
//el: DOM-Element
//2n: (#Id,.Class,Nodename,DOM-Element)
function testElementV1000(el,classID)
{
  var ptest=false;
	//if classId == ClassName
	if(classID.indexOf(".")===0 
  	&& el.className==classID.substr(1)) ptest=true;
	
	//if classId == IdName
	if(classID.indexOf("#")===0 
  	&& el.id==classID.substr(1)) ptest=true;
	
	//else (!IdName && !ClassName)
	if(classID.indexOf(".")===false 
  	&& classID.indexOf("#")===false)
		{
  		//if classId == nodeName
			if(el.nodeName==classID) ptest=true;
			//if classId == Element
			if(el==classID) ptest=true
		}
	return ptest;	
}

function moveThisInRowV1000(el,offsetTop,to)
{
  //get the DOM-Parent of el
	var p=searchRelativeParentV1000(el,offsetTop);
	if(p===false) return false;
	
	//get the x in p.parent.childNodes[x]
	var pos=getChildPosV1000(p,offsetTop);
	p=p.parentNode;
	
	//is no offsetParent found?
	if(pos===false) return false;
	
	//if is first or last Element
	if($$(offsetTop).length>=(pos+to+1) && pos+to>=0)
	{
  	if($$(offsetTop).length==(pos+to+1)) 
  		p.appendChild(p.removeChild($$(offsetTop)[pos]),$$(offsetTop)[pos+to]);
		else 
			p.insertBefore(p.removeChild($$(offsetTop)[pos]),$$(offsetTop)[pos+to]); 
	}
}

function repGermanUmlauts(str)
{
  str=str.replace(unescape("%E4"),"ae");
	str=str.replace(unescape("%F6"),"oe");
	str=str.replace(unescape("%FC"),"ue");
	str=str.replace(unescape("%C4"),"Ae");
	str=str.replace(unescape("%D6"),"Oe");
	str=str.replace(unescape("%DC"),"Ue");
	str=str.replace(unescape("%DF"),"ss");
  return str;
}

function trim(str)
{
  return str.replace(/\s+$/,"").replace(/^\s+/,"");
}

/*function initNav(img)
{
  nimg=new Image();
  nimg.src=img.src.substring(0,img.src.length-5)+"h.gif";
  nimg.style.display="none";
  $(document.body).appendChild(nimg);
  normal(img);
}*/

slideShow=function(el,SLIDE_PAUSE,SLIDE_DURATION)
{
	this.el=$(el);
	this.SLIDE_PAUSE=SLIDE_PAUSE;
	this.SLIDE_DURATION=SLIDE_DURATION;
	this.opacity=100;
	this.imgEffects=new Array();
	//this.stop=false;

	this.initSlide=function()
	{
		var imgs=this.el.getElementsByTagName("img");
		if(imgs.length==0) return false;
		this.el.setStyles({position:"relative",opacity:0});
		//this.el.addEvent("click",function(){this.stop=true}.bind(this));
		for(var i=0;i<imgs.length;i++)	$(imgs[i]).setStyles({position: "absolute",top: 0});
		this.startSlide();
	}
	
	this.slideComplete=function()
	{
		var imgs=this.el.getElementsByTagName("img");
		imgs[0].parentNode.insertBefore(imgs[0].parentNode.removeChild(imgs[imgs.length-1]),imgs[0]);
		this.el.getElementsByTagName("img")[0].setStyles({opacity: 1});
		/*if(this.stop===false)*/ setTimeout(this.doIt.bind(this),this.SLIDE_PAUSE);
	}
	
	this.doIt=function()
	{
		var imgs=this.el.getElementsByTagName("img");
		var e=new Fx.Morph($(imgs[imgs.length-1]), {duration: this.SLIDE_DURATION,onComplete: this.slideComplete.bind(this)})
		e.set({opacity: 1});
		e.start({opacity: 0});
	}
	
	this.startSlide=function()
	{
		this.el.setStyle("visibility","visible");
		var e=new Fx.Morph(this.el, {duration: 200,onComplete: setTimeout(this.doIt.bind(this),this.SLIDE_PAUSE)})
		e.start({opacity: 1});
	}
}

function deleteElement(el)
{
	el.parentNode.removeChild(el);
}

//http://javascript.about.com/library/blsort2.htm
function randOrd()
{
  return (Math.round(Math.random())-0.5); 
} 

//http://javascript.about.com/library/blsort2.htm
function sortArrayRand(anyArray)
{
  anyArray.sort(randOrd);
	return anyArray;
}

function image_resize(img,maxHeight,maxWidth,rePos,makeBigger)
{
  img=$(img);
	var width=img.width;
	var height=img.height;
	
	var z=1;
	
	if((width>maxWidth || (width<=maxWidth && makeBigger===true)) && width>=height) 
	{
  	z=maxWidth/width;
  	if(makeBigger===true && maxHeight<Math.round(height*z)) z=maxHeight/height;
	}
	if((height>maxHeight || (height<=maxHeight && makeBigger===true)) && height>width) 
	{
  	z=maxHeight/height;
		if(makeBigger===true && maxWidth<Math.round(width*z)) z=maxWidth/width;
	}
	
	img.setStyle("width",Math.round(width*z));
	img.setStyle("height",Math.round(height*z));

	if(rePos===true)
	{
  	var newWidth=img.getStyle("width").toInt();
  	var newHeight=img.getStyle("height").toInt();
		var pL=Math.round((maxWidth-newWidth)/2);
		var pT=Math.round((maxHeight-newHeight)/2);
		if(newWidth<maxWidth) img.setStyle("paddingLeft",pL==1?0:pL);
		if(newHeight<maxHeight) img.setStyle("paddingTop",pT==1?0:pT);
	}
}

function abs(x)
{
  if(x<0) x=-x;
  return x;
}
