var details=false;
var tpg1;
var tpgPopUp;
var tpgPopUpBig;
var styleMap;
var map;
var map_params = {};
var aktiv;
map_params.markers = [];
map_params.maps =[];

function openPopup(URL){
	URL = URL+"&newTname=popup";
	var popup = window.open(URL , 'popup', 'width=650,height=470,left=100,top=50,scrollbars=Yes');
}

function openLoginPopup(URL){
	URL = URL+"&newTname=popup";
	var popup = window.open(URL , 'popup', 'width=400,height=300,left=100,top=50,scrollbars=Yes');
}

function openBigPopup(URL){
	URL = URL+"&newTname=popup";
	var popup = window.open(URL , 'popup', 'width=650,height=600,left=100,top=50,scrollbars=Yes');
}

function openIMAGEPopup(URL,x,y){
        var params="width="+x+","+"height="+y+",left=100,top=50,scrollbars=yes";
        var popup = window.open(URL, 'IMAGE', params);
        popup.focus();
}


function openLinkPopup(URL,width,height,posx,posy){
        if (width=='') width=400;
        if (height=='') height=300;
        if (posx=='') posx=100;
        if (posy=='') posy=50;
        params='width='+width+',height='+height+',left='+posx+',top='+posy+',scrollbars=Yes';
        var popup = window.open(URL , 'link', params);
}

function login(){
  openLoginPopup('/index.php?menu=login');
}

function openParent(URL){
  window.opener.location.href=URL;
  window.opener.focus();
}

// Ab hier experimetal
/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }

  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();

  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }

  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }

  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }

  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }

  document.body.innerHTML = bodyText;
  return true;
}


/*
 * This displays a dialog box that allows a user to enter their own
 * search terms to highlight on the page, and then passes the search
 * text or phrase to the highlightSearchTerms function. All parameters
 * are optional.
 */
function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
  // This function prompts the user for any words that should
  // be highlighted on this web page
  if (!defaultText) {
    defaultText = "";
  }

  // we can optionally use our own highlight tag values
  if ((!textColor) || (!bgColor)) {
    highlightStartTag = "";
    highlightEndTag = "";
  } else {
    highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
    highlightEndTag = "</font>";
  }

  if (treatAsPhrase) {
    promptText = "Please enter the phrase you'd like to search for:";
  } else {
    promptText = "Please enter the words you'd like to search for, separated by spaces:";
  }

  //searchText = prompt(promptText, defaultText);
  searchText = defaultText;
  if (!searchText)  {
    //alert("No search terms were entered. Exiting function.");
    return false;
  }

  return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
}

function getContent(url,responseID,waitText){
	if (url==""){
		url=lastAjaxCall;
	}
	if (url==null){
		window.location.reload();
	}
      	var http= Ajax.getTransport();
      	http.open ("GET",url,true);
      	if (tpg1) tpg1.onUnload();
      	myElement=document.getElementById(responseID);
      	myElement.innerHTML="<center><h1>"+waitText+"</h1></center>";
      	http.onreadystatechange = function(){
      		if (http.readyState==4){
      			myElement.innerHTML=http.responseText;
      			myElement.innerHTML.evalScripts();
      		}
		lastAjaxCall=url;
      	}
      	http.send(null);
	test =document.getElementById(responseID).style.height;
	alert(responseID+" : "+test+"px");
   }

   function postContent(url,formElem,responseID,waitText){
	if (url==""){
		url=lastAjaxCall;
	}
	if (url==null){
		window.location.reload();
	}
      	var http= Ajax.getTransport();
      	http.open ("POST",url,true);
	myElement=document.getElementById(responseID);
      	myElement.innerHTML="<center><h1>"+waitText+"</h1></center>";
      	http.onreadystatechange = function(){
      		if (http.readyState==4){
      			myElement.innerHTML=http.responseText;
      			myElement.innerHTML.evalScripts();
      		}
		lastAjaxCall=url;
      	}
	//var myForm=document.getElementById(formID);
	//alert(myForm);
	parameters=Form.serialize(formElem);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	http.send(parameters);
   }

function showElement(id){
	$(id).show();
}

function hideElement(id){
	$(id).hide();
}

function toggleElement(id){
	$(id).toggle();
}

function lcheckshow(id1,id2){
	if(document.getElementById(id1).style.display == 'none') {
		$(id1).appear({ from: 0.0, to: 1.0, duration: 0.5 });
		if (id2 != null)	{
			$(id2).appear({ from: 0.0, to: 1.0, duration: 0.5 });
		}
	}
}

function lcheckhide(id1,id2){
	$(id1).fade({ from: 1.0, to: 0.0, duration: 0.5 });
	if (id2 != null)	{
		$(id2).fade({ from: 1.0, to: 0.0, duration: 0.5 });
	}
}

function trim(s)
{
	s.replace(/^\s+|\s+$/g, '') ;
	return s;
}


function createAutocompleter(formfield,contentdiv,params){
	myElement=document.getElementById(contentdiv);
	myList=myElement.innerHTML;
	myList=trim(myList)
	mylistArray=myList.split(",")
	myElement.innerHTML="";
	new Autocompleter.Local(formfield,contentdiv,mylistArray,{fullSearch:true});

}







function sleep(milliSeconds){
    var startTime = new Date().getTime(); // get the current time
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

function clearElement(myElement){
	$(myElement).innerHTML="";
}
/* Neu AJAX-Loader */
function getContent(url,responseID,waitText){
	var myElement=document.getElementById(responseID);
	myElement.removeClassName('active');
	if (url==""){
		url=lastAjaxCall;
	}
	if (url==null){
		window.location.reload();
	}
	var http= Ajax.getTransport();
      	http.open ("GET",url,true);
      	if (tpg1) tpg1.onUnload();

	http.onreadystatechange = function(){
      		if (http.readyState==4){
			myElement.innerHTML=http.responseText;
      			myElement.innerHTML.evalScripts();
			var max_height=0;
			$$('.content-box-3').each(function (myElement,index){
				if (myElement.down('div').getHeight()> max_height) {
					max_height=myElement.down('div').getHeight();
				}
			});
			$$('.content-box-3').each(function (myElement,index){
				myElement.setStyle({
					height: max_height + 'px'
				});
			});
			myElement.addClassName('active');
		}
		lastAjaxCall=url;
      	}
      	http.send(null);
   }

   function copyContent(from, to){
	$(to).innerHTML=$(from).innerHTML;
	$(from).innerHTML.evalScripts();
	var max_height=0;
	$$('.content-box-3').each(function (myElement,index){
		if (myElement.down('div').getHeight()> max_height) {
			max_height=myElement.down('div').getHeight();
		}
	});
	$$('.content-box-3').each(function (myElement,index){
		myElement.setStyle({
			height: max_height + 'px'
		});
	});
	$(to).addClassName('active');
   }


    function makeGetContent (url,responseID,waitText){
	return function () {
		getContent(url,responseID,waitText);
	};
   }

   function buildGetContent(url,responseID,waitText){
	return function(){
		getContent(url,responseID,waitText);
	};

   }

   function postContent(url,formElem,responseID,waitText){
	if (url==""){
		url=lastAjaxCall;
	}
	if (url==null){
		window.location.reload();
	}
      	var http= Ajax.getTransport();
      	http.open ("POST",url,true);
	myElement=document.getElementById(responseID);
      	myElement.innerHTML="<center><h1>"+waitText+"</h1></center>";
      	http.onreadystatechange = function(){
      		if (http.readyState==4){
      			myElement.innerHTML=http.responseText;
      			myElement.innerHTML.evalScripts();
      		}
		lastAjaxCall=url;
      	}
	//var myForm=document.getElementById(formID);
	//alert(myForm);
	parameters=Form.serialize(formElem);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	http.send(parameters);
   }





/**
* Event.simulate(@element, eventName[, options]) -> Element
*
* - @element: element to fire event on
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
*
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
*
**/
(function(){

  var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
  }
  var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
  }

  Event.simulate = function(element, eventName) {
    var options = Object.extend(defaultOptions, arguments[2] || { });
    var oEvent, eventType = null;

    element = $(element);

    for (var name in eventMatchers) {
      if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
      throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent) {
      oEvent = document.createEvent(eventType);
      if (eventType == 'HTMLEvents') {
        oEvent.initEvent(eventName, options.bubbles, options.cancelable);
      }
      else {
        oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
          options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
          options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
      }
      element.dispatchEvent(oEvent);
    }
    else {
      options.clientX = options.pointerX;
      options.clientY = options.pointerY;
      oEvent = Object.extend(document.createEventObject(), options);
      element.fireEvent('on' + eventName, oEvent);
    }
    return element;
  }

  Element.addMethods({ simulate: Event.simulate });
})()



function moveDIV( var1, var2, nameouter, nameinnner ) {
	if ( var1 == 0 ) {
		document.getElementById("themen-scroller-inner").style.left = "0px";
		document.getElementById("themen-scroller-inner").style.top = "0px";
	} else {
		if ( var2 ) {
			/* alter Wert ohne Einheit */
			var2 = document.getElementById("themen-scroller-inner").style.left;
			var2 = Number(var2.substring(0, var2.length-2)) + var1;

			var3 = document.getElementById("themen-scroller-inner").style.width;
			var3 = Number(var3.substring(0, var3.length-2));

			var4 = document.getElementById("themen-scroller").style.width;
			var4 = Number(var4.substring(0, var4.length-2)) - var3;

			// wenn nicht am linken "rahmen" angekommen
			if ( var2 <= 0 ) {
				// wenn nicht am rechten "rahmen" angekommen
				if ( var2 >= var4 ) {
					document.getElementById("themen-scroller-inner").style.left = var2 + "px";
				} else {
					document.getElementById("themen-scroller-inner").style.left = var4 + "px";
				}
			} else {
				document.getElementById("themen-scroller-inner").style.left = "0px";
			}
		} else {
			leave=false;
			/* alter Wert ohne Einheit */
			var2 = document.getElementById("themen-scroller-inner").style.top;
			var2 = Number(var2.substring(0, var2.length-2)) + var1;

			var3 = document.getElementById("themen-scroller-inner").offsetHeight;

			var4 = document.getElementById("themen-scroller").style.height;
			var4 = Number(var4.substring(0, var4.length-2)) - var3;

			var5 = document.getElementById("themen-scroller-inner").offsetHeight;

			var6= document.getElementById("themen-scroller").offsetHeight;
			if (var6>var5) {
				leave=true;
			}
			// wenn nicht am oberen "rahmen" angekommen
			if (!leave){
				if ( var2 <= 0 ) {
					// wenn nicht am unteren "rahmen" angekommen
					if ( var2 >= var4 ) {
						document.getElementById("themen-scroller-inner").style.top = var2 + "px";
					} else {
						document.getElementById("themen-scroller-inner").style.top = var4 + "px";
					}
				} else {
					document.getElementById("themen-scroller-inner").style.top = "0px";
				}
			}
		}
	}
}

function setupProjektMouseOver () {
  function hideTooltip(el) {
    var box = $(el).up('.content-box-inner').down('.content-box-3');
    box.down('.replace-tooltip-outer').removeClassName('active').down('.replace-tooltip').innerHTML = '';
  }

  function mouseover(event) {
    var box = $(this).up('.content-box-inner').down('.content-box-3');
    box.down('.replace-tooltip').innerHTML = $(this).down('.project-teaser').innerHTML;
    box.down('.replace-tooltip-outer').addClassName('active');
  }

  function mouseout(event) {
    hideTooltip(this);
  }

  function click(event) {
    var box = $(this).up('.content-box-inner').down('.content-box-3');
    var replace = box.down('.replace');
    replace.innerHTML = $(this).down('.project-content').innerHTML;
    replace.addClassName('active');
    $(this).siblings().invoke('removeClassName', 'active');
    $(this).addClassName('active');
    hideTooltip(this);
    var newHeight = $$('.content-box-3').max(function(el) {
      return el.down('.replace').getHeight();
    });
    $$('.content-box-3').invoke('setStyle', {'height': newHeight + 'px' });
  }

  $$('div.carousel').each(function (element) {
    element.observe('mouseout', mouseout);
    element.observe('mouseover', mouseover);
    element.observe('click', click);
  });
}

function toggleChild(id){
	myElement=document.getElementById('togglediv_'+id);
	Effect.toggle('togglediv_'+id, 'blind', {duration:0.25});
	return false;
}

Element.observe(window, "load", setupProjektMouseOver);

(function () { /*** MODUL: Carousel **/
	function carouselLoader(){
		$$('.content-box-2').each(function (myElement,index){
			var carousel=myElement.getElementsByClassName('carousel');
			if (carousel[0]){
				carousel[0].addClassName('active');
				Event.simulate(carousel[0],'click');
			}
		});
	}
	function setCarouselWidthOnLoad(){
		$$('.carousel-inner').each(function (myElement,index){
			var total=0;
			myElement.childElements().each(function(myInnerDiv,index2){
				total+=myInnerDiv.getWidth();
			});
			myElement.setStyle({
				width: total + 'px'
			});
		});
    }
	Element.observe(window, "load", function() {
		setCarouselWidthOnLoad();
		setTimeout(carouselLoader, 500);
	});
})(); /*** ENDE MODUL Carousel **/


(function () { /*** MODUL: Dynamische Tabs **/
    // Konfiguration
	var content_width = 1024;
    var max_content_box_width = 420;
	var content_box_margin = 4;
    var content_box_active_width = 620;

	// Werden durch window.load initialisiert
	var content_boxes;
	var content_boxes_3;

	Event.observe(window,"load", function () {
		content_boxes = $$('.content-box');
		content_boxes_3 = $$('.content-box-3');
		register_accordion_onclick();
	});

	function register_accordion_onclick(){
		if ($$('.content-box').length==0){
			return false;
		}
		if (window.location.hash.length > 1) {
			if (window.location.hash.indexOf('_') == -1) {
				activate_anchor_tab(window.location.hash);
			} else {
				activate_anchor_tab(window.location.hash.substring(0, window.location.hash.indexOf('_')), true);
			}
		} else if ($$('.content-box.active').length==0){
			content_boxes.first().addClassName('active');
		}
		content_boxes.each(function(myContentElement,index){
			Event.observe(myContentElement.select('a').first(),'click',function(){
				$$('.content-box.active').first().removeClassName('active');
				myContentElement.addClassName('active');
				breite();
			});
		});
		breite();
	}

	//Event.observe(window,"resize",breite);
	function breite(){
        return;
		var total_width = $('content').getWidth() - 20;
	    var box_count = content_boxes.length - 1;

		var box_breite = Math.floor((total_width - content_box_active_width - content_box_margin * (box_count + 1)) / box_count);
	    if (box_breite>max_content_box_width) {
		    box_breite=max_content_box_width;
	    }
        content_boxes.each(function (myElement,index){
		    myElement.setStyle({
			    width: (myElement.hasClassName('active') ? content_box_active_width : box_breite) + 'px'
		    });
	    });

	    var max_height = content_boxes_3.max(function (myElement,index){
		    return myElement.getHeight() - 60;
	    });
	    content_boxes_3.each(function (myElement,index){
		    myElement.setStyle({
			    height: max_height + 'px'
		    });
	    });

	    if (box_count * (box_breite + 4) + content_box_active_width > 980) {
            document.getElementById("foot").style.width = box_count * (box_breite + 4) + content_box_active_width + 'px';
        };
    }

	function activate_anchor_tab(anchor_name, dont_scroll){
		if (anchor_name.charAt(0)!="#"){
			anchor_name="#"+anchor_name;
		}
		var anchors=$$(anchor_name);
		if(anchors.size()>0){
			var actives =$$('.content-box.active');
			if (actives.size()>0){
				$$('.content-box.active').first().removeClassName('active');
			}
			anchors.first().up('.content-box').addClassName('active');
		}
		if (!dont_scroll && anchor_name.indexOf('_') == -1) {
			window.scrollTo(0,0);
		}
		breite();
	}

	// Unterbinden der default-action bei anchors
	Element.observe(window,"load",function(event){
		function get_base(url){
			var baseurl;
			var hashpos=url.indexOf("#");
			if (hashpos===-1){
				baseurl=url;
			} else{
				baseurl=url.substring(0,hashpos);
			}
			return baseurl;
		}
		function get_hash(url){
			var hashpos=url.indexOf("#");
			if (hashpos==-1){
				return "";
			} else {
				return url.substring(hashpos);
			}
		}
		var baseurl = get_base(window.location.href);
		$$('a').each(function(el){
			if (get_base(el.href)==baseurl){
				// Seiten-Interner Anchor oder gleiche Seite
				el.observe('click',function(e){
					// Zugehörigen Anchor aktivieren
					var hash = get_hash(this.href);
					if (hash.length > 0) {
						if (hash.indexOf('_') == -1){
							activate_anchor_tab(hash);
							window.location.hash = hash;
							window.scrollTo(0,0);
						} else {
							activate_anchor_tab(hash.substring(0, hash.indexOf('_')));
							window.location.hash = hash;
						}
					}
					e.stop();
				});
			}
		});
	});
})(); /*** ENDE MODUL Dynamische Tabs **/

(function () { /*** MODUL HTML-Menu **/
	Event.observe(window, "load", initDhtmlGoodiesMenu);
})();

