﻿function hoverAndAct(img,dataType)
{
	img.parentNode.className="active";
	img.src=img.src.substring(0,img.src.length-(dataType.length+2))+"h."+dataType;
}

function normalAndDeact(img,dataType)
{
	img.parentNode.className="";
	normal(img,dataType);
}

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,hh;
	height=0;
	
	for(i=0;i<elids.length;i++)
	{
		if(elids[i]==null) return;
		hh=$(elids[i]).getSize().y-$(elids[i]).getStyle("margin-top").toInt()-$(elids[i]).getStyle("margin-bottom").toInt();
		height=(hh>height)?hh: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);
	}
}

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=="active") 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;
}


function addOverlay(el,overlayName)
{
	if(overlayName==null) overlayName="";
	var zIndex=100+($$(".overlay").length*2);
	var div=new Element("div",
	{
		id: "overlay"+overlayName,
		"class":"overlay "+overlayName,
		styles: {
		position:"absolute",
		zIndex:zIndex,
		height:$(document.body).getScrollSize().y+"px",
		width:$(document.body).getScrollSize().x+"px",
		left:0,
		top:0
		}
	});
	
	$(document.body).insertBefore(div,$(document.body).firstChild);
	
	div=new Element("div");
	div.id="overlayInput"+overlayName;
	div.setStyles({
		position:"absolute",
		zIndex:zIndex+1,
		//width: "auto"
		overflow: "auto"
	});
	
	var backLink=cEm("a");
	backLink.href="#";
	backLink.title="Schließen";
	backLink.id="closeOverlayDivA";
	backLink.className="closeOverlayDivA";
	backLink.appendChild(document.createTextNode("x"));
	backLink.onclick=function(){closeOverlay(overlayName); return false;};
	
	div.appendChild(backLink);
	div.appendChild(el);
	
	$(document.body).insertBefore(div,$(document.body).firstChild);
	
	window.addEvent("resize",function()
	{
		newPosOverlay(overlayName);
	});
	newPosOverlay(overlayName);
}

function overlayRePos(overlayName)
{
	if(overlayName==null) overlayName="";
	if($("overlayInput"+overlayName)==null) return;
	var elSz=$("overlayInput"+overlayName).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"+overlayName).style.top=t+boSc.y+"px";
	var l=Math.round((boSz.x-elSz.x)/2);
	l=(l<0)?0:l;
	$("overlayInput"+overlayName).style.left=l+boSc.x+"px";
	if($("overlay"+overlayName)==null) return;
	$("overlay"+overlayName).setStyles({
		height:$(document.body).getScrollSize().y+"px",
		width:$(document.body).getScrollSize().x+"px"
	});
}

function newPosOverlay(overlayName)
{
	if(overlayName==null) overlayName="";
	if($("overlayInput"+overlayName)==null) return;
	var boSz=$(document.body).getSize();
	var boSc=$(document.body).getScroll();
	var elSz=$("overlayInput"+overlayName).getSize();
	var t=Math.round((boSz.y-elSz.y)/2);
	t=(t<0)?0:t;
	$("overlayInput"+overlayName).style.top=t+boSc.y+"px";
	var l=Math.round((boSz.x-elSz.x)/2);
	l=(l<0)?0:l;
	$("overlayInput"+overlayName).style.left=l+boSc.x+"px";
	if($("overlay"+overlayName)==null) return;
	$("overlay"+overlayName).setStyles({
		height:$(document.body).getScrollSize().y+"px",
		width:$(document.body).getScrollSize().x+"px"
	});
}

function closeOverlay(overlayName)
{
	if(overlayName==null) overlayName="";
	if($("overlayInput"+overlayName)==null) return;
	$(document.body).removeChild($("overlay"+overlayName));
	$(document.body).removeChild($("overlayInput"+overlayName))
	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);
}*/

function initSlideShow(elsId,SLIDE_PAUSE,SLIDE_SPEED,SLIDE_STEPS)
{
	if(elsId==null || $$(elsId).length==0) return false;
	if(SLIDE_PAUSE==null) SLIDE_PAUSE=5000;
	if(SLIDE_SPEED==null) SLIDE_SPEED=40;
	if(SLIDE_STEPS==null) SLIDE_STEPS=4;
	
	setTimeout("slideIt('"+elsId+"',100,"+SLIDE_PAUSE+","+SLIDE_SPEED+","+SLIDE_STEPS+")",2000);
}

function slideIt(elId,opacity,SLIDE_PAUSE,SLIDE_SPEED,SLIDE_STEPS)
{
  var imgs=$$(elId);
	if(opacity<101 && opacity>=0)
	{
  	imgs[imgs.length-1].setStyle("opacity",opacity/100);
		opacity=opacity-SLIDE_STEPS;
		setTimeout("slideIt('"+elId+"',"+opacity+","+SLIDE_PAUSE+","+SLIDE_SPEED+","+SLIDE_STEPS+")",SLIDE_SPEED);
	}
	else
	{
  	var img=imgs[0].parentNode.removeChild(imgs[imgs.length-1]);
		imgs[imgs.length-1].setStyle("opacity",1);
		imgs[0].parentNode.insertBefore(img,imgs[0]);
		setTimeout("slideIt('"+elId+"',100,"+SLIDE_PAUSE+","+SLIDE_SPEED+","+SLIDE_STEPS+")",SLIDE_PAUSE);
	}
}

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;
}

uploadFormOverlay = 
{
	start: function(upID,uploadPhpURL,readyfuncName)
	{
		var div=new Element("div",{styles:{backgroundColor: "#fff",padding: 20,border: "3px solid #aaa"}});
		div.adopt(new Element("form",{action:uploadPhpURL,method:"post",enctype:"multipart/form-data",encoding:"multipart/form-data",target:"upload_target"+upID,events: {submit: function()
		{
			document.getElementById('loadInfo'+upID).style.display = '';
			document.getElementById('loadForm'+upID).style.display = 'none';
		}},
		html: 
		'<h1>Bild hochladen</h1>'+
		'<span id="loadInfo'+upID+'" style="display: none">Lade hoch...</span>'+
		'<div id="loadForm'+upID+'">'+
			'<p>Neues Bild hochladen (max. 500kB und nur .jpg)<br/><input name="'+upID+'" type="file" /></p>'+
			'<input type="hidden" name="fileId" value="'+upID+'" />'+
			'<input type="hidden" name="readyfuncName" value="'+readyfuncName+'" />'+
			'<table width="280px"><tr><td><input type="reset" class="flatbutton" onclick="closeOverlay(\''+upID+'\'); return false;" name="reset" value="schließen" /></td>'+
			'<td style="text-align:right"><input type="submit" class="flatbutton" name="submitBtn" value="hochladen" /></td></tr></table>'+	
		'</div>'+
		'<iframe name="upload_target'+upID+'" src="'+uploadPhpURL+'" style="width:0;height:0;border:0px solid #fff;"></iframe>'		
		}));
		addOverlay(div,upID);
	},
	done: function(uploadedImageName,errormsg,result,upID,readyfuncName)
	{
			document.getElementById('loadInfo'+upID).style.display = 'none';
			document.getElementById('loadForm'+upID).style.display = '';
			if(result == false)
			{
				if(errormsg==="") errormsg="Upload fehlgeschlagen";
				alert(errormsg);
			}
			else window[readyfuncName](uploadedImageName);
	}
}
imageBrowser=
{
	open: function(href,retFunc,browserName,imgPath)
	{
		var errorFunc=function(){alert("Fehler, Aktion konnte nicht ausgeführt werden")};
		var myRequest = new Request.JSON(
		{
			url: href,
			onSuccess: function(responseJSON,responseText){closeOverlay(browserName+"wait"); if(responseJSON===false) errorFunc();else imageBrowser.show(responseJSON,retFunc,browserName,imgPath,href);},
			onError: errorFunc
		});
		myRequest.send();
		addOverlay(new Element("div",{styles: {border: "3px solid #ccc",backgroundColor: "#fff",padding:"30px",fontSize:"18pt"},text: "bitte warten..."}),browserName+"wait");
	},
	
	show: function(responseJSON,retFunc,browserName,imgPath,href)
	{
		var fnc=retFunc;
		if(responseJSON.length==0) 
		{
			alert("Keine Bilder vorhanden. Nutzen Sie die Funktion \"Bild hochladen\"");
			return;
		}
		var div=new Element("div",{styles: {border: "3px solid #ccc",'width': 600,overflow: 'auto',backgroundColor: "#fff",padding:"30px"}});
		
		addOverlay(div,browserName+"show");
		responseJSON.each(function(ite,ind)
		{
			var img=new Image();
			img.src=imgPath+ite;
			img=$(img);
			img.setStyles({
				width:100,
				height:100,
				padding: "0 10px 5px 0"
			});
			var s=new Element("div",{id: "previewImage"+ind,styles:{position: 'relative',float: 'left',paddingBottom: 10,width: 110,height: 150}});
			s.adopt(new Element("span",{styles:{position: 'absolute',cursor: "pointer",backgroundColor: "#f00",color: "#fff",padding: "2px 4px",top: 0,right: 10,display: 'block'},text: "x",events:{click: function(){imageBrowser.del(href,browserName,ite,"previewImage"+ind)}}}));
			s.adopt(img);
			s.adopt(new Element("span",{text:ite}));
			s.inject(div);
			img.addEvent("load",function(){newPosOverlay(browserName+"show")});
			img.addEvent("click",function(){fnc(ite);closeOverlay(browserName+"show")});
		});
		new Element("span",{styles:{clear: "both",float:"right",display: "block"},'class': "flatbutton",text:"abbrechen",events:{click: function(){closeOverlay(browserName+"show");}}}).inject(div);
	},
	
	del: function(href,browserName,ite,divid)
	{
		if(confirm("Bild \""+ite+"\" wirklich löschen?")!==true) return;
		var errorFunc=function(){alert("Fehler, Aktion konnte nicht ausgeführt werden")};
		var myRequest = new Request.JSON(
		{
			url: href,
			onSuccess: function(responseJSON,responseText){closeOverlay(browserName+"delete"); if(responseJSON===true) $(divid).dispose(); else errorFunc();},
			onError: errorFunc
		});
		myRequest.post("delete="+ite);
		addOverlay(new Element("div",{styles: {border: "3px solid #ccc",backgroundColor: "#fff",padding:"30px",fontSize:"18pt"},text: "bitte warten..."}),browserName+"delete");
	}
}
