// -------------------------------------------------------- On Load --------------------------------------------------------


function On_Load_Functions()
	{
	set_search_box_label();
	set_search_box_button();
	set_pdf_class();
	//if(has_page_id(7)) reorder_navigation('#navigation_3 ul');
	if(has_page_id(8)) modify_search_browser();	// "Suche"
	if(has_page_id(43)) set_up_guidelines();	// "Leitlinien"
	if(has_page_id(151)) number_tags('#content_1 td');	// "Pressemitteilungen"
	if(has_page_id(349)) number_tags('#content_1 td');	// "Pressearchiv"
	if(has_page_id(152)) number_tags('#content_1 td');	// "Downloads"
	if(has_page_id(14)) number_tags('#content_1 td');	// "Netzwerk"
	if(has_page_id(15) || has_parent_id(12)) number_tags('#content_1 ul');	// "Standorte" & "Karriere"
	}
	
	
	
// -------------------------------------------------------- Navigation_1 --------------------------------------------------------


sfHover = function() {
	var sfEls = document.getElementById("navigation_1").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
	
	
// -------------------------------------------------------- Navigation_2 --------------------------------------------------------


sfHover2 = function() {
	var sfEls = document.getElementById("navigation_2").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover2);
	
	
	

// -------------------------------------------------------- Navigation_4 --------------------------------------------------------


sfHover4 = function() {
	var sfEls = document.getElementById("navigation_4").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover4);
	
	

	

// -------------------------------------------------------- Search Box --------------------------------------------------------

function set_search_box_label()
	{
	if(MM_findObj('search_box_input_field').value == "")
		{
		if(MM_findObj('search_box_language').value == 1)	//English
			MM_findObj('search_box_input_field').value = "Search";
		else
			MM_findObj('search_box_input_field').value = "Stichwortsuche";
		}
	}	
	
function remove_search_box_label(input_field)	
	{
	if(input_field.value == "Search" || input_field.value == "Stichwortsuche")
		input_field.value = "";
	}
	
function set_search_box_button()	
	{
	if(is_english_version())
		{
		//get button if in English version and set it
		document.getElementsBySelector('.english #tx_indexedsearch_submit_button')[0].innerHTML = "go";
		}
	//in any case show it
	show('#tx_indexedsearch_submit_button');
	}
	
	
	
	
	

	
// -------------------------------------------------------- set_pdf_class --------------------------------------------------------

function set_pdf_class()
	{
	//only for IE
	if (window.attachEvent && !has_parent_id(1082))		// "Suche" 
		{
		all_links = document.all.tags("A");
		for(i=0; i<all_links.length; i++) 
			if (all_links[i].href.indexOf('.pdf') != -1)
				all_links[i].className = "pdf_link";
		}
	}
	

	
// --------------------------------------------------- modify_search_browser ---------------------------------------------------


function modify_search_browser()
	{
	first_page_found = false;
	first_page_number = null;

	//format intro text
	number_of_results = document.getElementsBySelector('#content_1 div.tx-indexedsearch-browsebox strong');
	intro_text = document.getElementsBySelector('#content_1 div.tx-indexedsearch-whatis');
	if(number_of_results.length > 0 && intro_text.length > 0)
		if(is_english_version())
			intro_text[0].innerHTML += " resulted in " + number_of_results[1].innerHTML + " hits";
		else
			intro_text[0].innerHTML += " ergab " + number_of_results[1].innerHTML + " Treffer";
	show('#content_1 div.tx-indexedsearch-whatis');
	show('#content_1 div.tx-indexedsearch-browsebox');

	// format links
	links = document.getElementsBySelector('#content_1 ul.browsebox a');
	for(i=0; i<links.length;i++)
		{
		//add |
		if(links[i].innerHTML.indexOf("Weiter") != -1)
			links[i].innerHTML = "<span class=\"next\">|</span> Weiter >>";
		if(links[i].innerHTML.indexOf("Next") != -1)
			links[i].innerHTML = "<span class=\"next\">|</span> Next >>";
		if(links[i].innerHTML.indexOf("Zur") != -1)
			links[i].innerHTML = "<< Zur&uuml;ck <span class=\"previous\">|</span>";
		if(links[i].innerHTML.indexOf("Previous") != -1)
			links[i].innerHTML = "<< Previous <span class=\"previous\">|</span>";
		if((isNumeric(links[i].innerHTML) && first_page_found == false)
		|| (first_page_number != null && links[i].innerHTML == first_page_number))
			{
			first_page_found = true;
			links[i].className += "first_page";
			first_page_number = links[i].innerHTML; 
			}
		}

	// add Prefix "Page"
	list_items = document.getElementsBySelector('#content_1 ul.browsebox li');
	for(i=0; i<list_items.length;i++)
		{
		if(list_items[i].innerHTML.indexOf("first_page") != -1)
			{
			if(is_english_version())
				list_items[i].innerHTML = "<strong class=\"page_label\">Page:</strong> " + list_items[i].innerHTML;
			else
				list_items[i].innerHTML = "<strong class=\"page_label\">Seite:</strong> " + list_items[i].innerHTML;
			}
		}

	browsebox = document.getElementsBySelector('#content_1 ul.browsebox');
	if(browsebox.length > 0)
		{
		browsebox[0].style.visibility = "visible";
		browsebox[1].style.visibility = "visible";
		}
		
	}



// --------------------------------------------------- set_up_guidelines ---------------------------------------------------


function set_up_guidelines()
	{
	number_tags("#guidelines h3");
	number_tags("#guidelines div");
	h3_tags = document.getElementsBySelector("#guidelines h3");
	for(i=0; i<h3_tags.length; i++)
		//setup onclick function
		h3_tags[i].onclick = function() 
			{
			//get next div
			next_div = document.getElementsBySelector("#guidelines div." + this.className.replace(/ |sfhover/g, ""))[0];

			//now toggle
			if(next_div.style.display != "block")
				next_div.style.display = "block";
			else
				next_div.style.display = "none";
			}

	//setup IE6 for hovering the h3s 
	sfHover5 = function() {
		var sfEls = document.getElementById("guidelines").getElementsByTagName("h3");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover5);
	}








// -----------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------- General Functions --------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------
	

function mark_first_item(selector)
	{
	tags = document.getElementsBySelector(selector);
	if(tags.length > 0)
		tags[0].className+=" first_item";
	}
	
function mark_last_item(selector)
	{
	tags = document.getElementsBySelector(selector);
	if(tags.length > 0)
		tags[tags.length-1].className+=" last_item";
	}
	
function number_tags(tags)
	{
	tags = document.getElementsBySelector(tags);
	if(tags.length > 0)
		for(i=0; i<tags.length; i++)
			tags[i].className+=" item_" + (i+1);
	}
	
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function show(selector)
	{
	elements = document.getElementsBySelector(selector);
	if(elements.length > 0)
		for(i=0; i<elements.length;i++)
			elements[i].style.visibility = "visible";
	}


function hide(selector)
	{
	elements = document.getElementsBySelector(selector);
	if(elements.length > 0)
		for(i=0; i<elements.length;i++)
			elements[i].style.visibility = "hidden";
	}


function display(selector)
	{
	elements = document.getElementsBySelector(selector);
	if(elements.length > 0)
		for(i=0; i<elements.length;i++)
			elements[i].style.display = "block";
	}


function remove(selector)
	{
	elements = document.getElementsBySelector(selector);
	if(elements.length > 0)
		for(i=0; i<elements.length;i++)
			elements[i].style.display = "none";
	}


function isNumeric(num) {
	if (num >=0 || num < 0)
	return true;
	return false;
	}


function is_english_version()
	{
	wrapper_div = document.getElementsBySelector('#wrapper');
	if(wrapper_div[0].className.indexOf("english") != -1)
		return true;
	else 
		return false;
	}


function has_parent_id(id)
	{
	body_tag = document.getElementsBySelector('body');
	if(body_tag[0].className.indexOf("parent_id_"+id) != -1)
		return true;
	else 
		return false;
	}


function has_page_id(id)
	{
	body_tag = document.getElementsBySelector('body');
	if(body_tag[0].id.indexOf("page_id_"+id) != -1)
		return true;
	else 
		return false;
	}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// ----------------------------------- reorder_navigation -----------------------------------

/*

a b		0 1
c d		2 3
e f		4 5

a d		0 3
b e		1 4
c f		2 5

*/

	function reorder_navigation(element)	
		{ 
		parent_ul = document.getElementsBySelector(element)[0];
		new_list_column_1 = document.createDocumentFragment();
		new_list_column_2 = document.createDocumentFragment();
		list_items = new Array();
		
		//remove old list items and save them
		if(parent_ul.hasChildNodes())
			while (parent_ul.childNodes.length >= 1)
				if(parent_ul.firstChild.tagName == "LI")
					list_items.push(parent_ul.removeChild(parent_ul.firstChild));       
				else parent_ul.removeChild(parent_ul.firstChild);
		else return;

		//fill up lists with new order				
		for(i=0; i<Math.ceil(list_items.length/2); i++)
			new_list_column_1.appendChild(list_items[i].cloneNode(true));
		for(i=Math.ceil(list_items.length/2); i<list_items.length; i++)
			new_list_column_2.appendChild(list_items[i].cloneNode(true));

		//add new list items to parent_ul
		while(new_list_column_1.childNodes.length >= 1)
			{
			if(new_list_column_1.firstChild != null) parent_ul.appendChild(new_list_column_1.firstChild); else break;
			if(new_list_column_2.firstChild != null) parent_ul.appendChild(new_list_column_2.firstChild); else break;
			}

		//make hover for IE
		sfHover3 = function() {
			var sfEls = document.getElementById("navigation_3").getElementsByTagName("LI");
			for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() {
					this.className+=" sfhover";
				}
				sfEls[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				}
			}
		}
		if (window.attachEvent) window.attachEvent("onload", sfHover3);

		//show it
		show(element);
		}
		





/* document.getElementsBySelector(selector)
   - returns an array of element objects from the current document
     matching the CSS selector. Selectors can contain element names, 
     class names and ids and can be nested. For example:
     
       elements = document.getElementsBySelect('div#main p a.external')
     
     Will return an array of all 'a' elements with 'external' in their 
     class attribute that are contained inside 'p' elements that are 
     contained inside the 'div' element which has id="main"

   New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
   See http://www.w3.org/TR/css3-selectors/#attribute-selectors

   Version 0.4 - Simon Willison, March 25th 2003
   -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
   -- Opera 7 fails 
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

/* That revolting regular expression explained 
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute 
   Tag
*/
