//	=============================================================== //
// 	multi-line linkicons for IE
// 	see: http://www.icant.co.uk/sandbox/inlinelinks/
//	t.breitner 2007 | www.mpicc.de
//	=============================================================== //
function inlineLinks()
	{
		if(!document.getElementById || !document.createTextNode){return;}
		if (navigator.appName != "Microsoft Internet Explorer") {return;}
		



		var linkParent='content'; 

		var d=document.getElementById(linkParent); 
		if(!d){return;}
		var links=d.getElementsByTagName('a');
		for(var i=0;i<links.length;i++) 
		{
			var newimg=document.createElement('span');
			newimg.className = 'ielink';
			links[i].insertBefore(newimg,links[i].firstChild);
		}
	}





//	=============================================================== //
// 	cookies
//	t.breitner 2007 | www.mpicc.de
//	=============================================================== //

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


//	=============================================================== //
// 	mpicc-fontsizer 
//	t.breitner 2007 | www.mpicc.de
//	=============================================================== //

var currfontsize = 71; 	//set to default css-body-fontsize-value
var defaultSize = 71;	//set to default css-body-fontsize-value
var maxSize = 200;
var minSize = 60;

function applyFontSize(size) {
	body = document.getElementsByTagName('body')[0];
	body.style.fontSize = size +'%';
}

function fontSizer(inc) {
	if (!document.getElementById) return;

	currfontsize += inc;
	if (currfontsize < minSize ) {
		currfontsize = minSize;
	}
	if (currfontsize > maxSize ) {
		currfontsize = maxSize;
	}

	applyFontSize(currfontsize);
	createCookie('mpicc-fontsize',currfontsize,'7'); 
	
}

function fontSizerReset() {
	if (!document.getElementById) return;
	
	currfontsize = defaultSize;

	applyFontSize(currfontsize);
	if (readCookie('mpicc-fontsize')) {
		eraseCookie('mpicc-fontsize'); 
	}
}

function sizerOnLoad() {
	if (!document.getElementById) return;

	var cookieFontSize = readCookie('mpicc-fontsize');
	
	if (cookieFontSize && cookieFontSize > 0) { 
		currfontsize = parseInt(cookieFontSize);
  		applyFontSize(currfontsize);
 	}
}

//	=============================================================== //
// 	insert js-dependend functions
//	t.breitner 2007 | www.mpicc.de
//	============================================================== //

function insertPagefunctions() {
	if(!document.getElementById) {return;}


	// javascript:alert(document.compatMode);
	/*
	var quirksModeContainer = document.createElement('li');
	var quirksModeContent = document.createTextNode(document.compatMode);
	quirksModeContainer.setAttribute((document.all ? "className" : "class"), "hide");
	quirksModeContainer.appendChild(quirksModeContent); 
	var quirksTag = quirksModeContainer;
	*/

/*
	<li class="print"><a href="javascript:setActiveStyleSheet('print');">#server.mpi_langtexts[url.lid][4]#</a></li>
	*/

	var PagefunctionPrintContainer 	= document.createElement('li');
	var PagefunctionPrintLink	= document.createElement('a');
	var PagefunctionPrintText	= document.createTextNode(printStr);
  var span = document.createElement('span');

	/* PagefunctionPrintContainer.setAttribute((document.all ? "className" : "class"), "print"); */
  PagefunctionPrintContainer.className = "print";
	PagefunctionPrintLink.setAttribute('href',  'javascript:setActiveStyleSheet("print");');
	PagefunctionPrintLink.appendChild(span); 
	PagefunctionPrintLink.appendChild(PagefunctionPrintText); 
	PagefunctionPrintContainer.appendChild(PagefunctionPrintLink); 
	var printButton = PagefunctionPrintContainer;
	

/* 
	<li id="toggleStyle" class="gotofluidlayout"><a href="javascript:toggleStyle();">Angepasste Seitenbreite</a></li> 
	*/

	var PagefunctionSwitcherContainer 	= document.createElement('li');
	var PagefunctionSwitcherLink		= document.createElement('a');
	var PagefunctionSwitcherText		= document.createTextNode(fluidLayoutStr);
  var span = document.createElement('span');
	
	PagefunctionSwitcherContainer.id		= 'toggleStyle';
	/* PagefunctionSwitcherContainer.setAttribute((document.all ? "className" : "class"), "gotofluidlayout"); */
	PagefunctionSwitcherContainer.className = "gotofluidlayout";
	PagefunctionSwitcherLink.setAttribute('href',  'javascript:toggleStyle();');
	PagefunctionPrintLink.appendChild(span); 
	PagefunctionSwitcherLink.appendChild(PagefunctionSwitcherText); 
	PagefunctionSwitcherContainer.appendChild(PagefunctionSwitcherLink); 
	var layoutSwitcher = PagefunctionSwitcherContainer;


/*
	<li class="fontsize">
		<a id="fsa" href="javascript:fontSizer(-5)" title="Schriftgr&ouml;&szlig;e verringern">A-</a> |
		<a id="fsaa" href="javascript:fontSizerReset()" title="Schriftgr&ouml;&szlig;e zur&uuml;cksetzen">A</a> |
		<a id="fsaaa" href="javascript:fontSizer(5)" title="Schriftgr&ouml;&szlig;e erh&ouml;hen">A+</a>
	</li>
	*/
	
	var PagefunctionFontsizeContainer 	= document.createElement('li');
	var PagefunctionFontsizeDivider		= document.createTextNode(" | ");
	PagefunctionFontsizeDividerClone 	= PagefunctionFontsizeDivider.cloneNode(true);

	var PagefunctionFontsizeDecreaseLink	= document.createElement('a');
	var PagefunctionFontsizeResetLink	= document.createElement('a');
	var PagefunctionFontsizeIncreaseLink	= document.createElement('a');

	var PagefunctionFontsizeDecreaseText	= document.createTextNode('A-');
	var PagefunctionFontsizeResetText	= document.createTextNode('A');
	var PagefunctionFontsizeIncreaseText	= document.createTextNode('A+');

	PagefunctionFontsizeContainer.setAttribute((document.all ? "className" : "class"), "fontsize"); 
	
	PagefunctionFontsizeDecreaseLink.setAttribute("id", "fsa");
	PagefunctionFontsizeDecreaseLink.setAttribute("title", decreaseFontsizeStr);
	 
	PagefunctionFontsizeResetLink.setAttribute("id", "fsaa"); 
	PagefunctionFontsizeResetLink.setAttribute("title", resetFontsizeStr);
	
	PagefunctionFontsizeIncreaseLink.setAttribute("id", "fsaaa"); 
	PagefunctionFontsizeIncreaseLink.setAttribute("title", increaseFontsizeStr);
/*
	document.getElementById('fsa').setAttribute("title", decreaseFontsizeStr);
	document.getElementById('fsaa').setAttribute("title", resetFontsizeStr);
	document.getElementById('fsaaa').setAttribute("title", increaseFontsizeStr);
*/
	
	PagefunctionFontsizeDecreaseLink.setAttribute('href', 'javascript:fontSizer(-5)');
	PagefunctionFontsizeDecreaseLink.appendChild(PagefunctionFontsizeDecreaseText); 

	PagefunctionFontsizeResetLink.setAttribute('href', 'javascript:fontSizerReset()');
	PagefunctionFontsizeResetLink.appendChild(PagefunctionFontsizeResetText); 

	PagefunctionFontsizeIncreaseLink.setAttribute('href', 'javascript:fontSizer(+5)');
	PagefunctionFontsizeIncreaseLink.appendChild(PagefunctionFontsizeIncreaseText); 

	PagefunctionFontsizeContainer.appendChild(PagefunctionFontsizeDecreaseLink);
	PagefunctionFontsizeContainer.appendChild(PagefunctionFontsizeResetLink);
	PagefunctionFontsizeContainer.appendChild(PagefunctionFontsizeIncreaseLink);
	
	var fontsizeSwitcher = PagefunctionFontsizeContainer;


var pagefunctionsList=document.getElementById('pagefunctions').getElementsByTagName('ul')[0]; 

pagefunctionsList.appendChild(printButton); 
pagefunctionsList.appendChild(layoutSwitcher);
pagefunctionsList.appendChild(fontsizeSwitcher);
// pagefunctionsList.appendChild(quirksTag); 

fontsizeSwitcher.insertBefore(PagefunctionFontsizeDivider, document.getElementById('fsa').nextSibling);
fontsizeSwitcher.insertBefore(PagefunctionFontsizeDividerClone, document.getElementById('fsa').nextSibling.nextSibling.nextSibling);
}



//	=============================================================== //
// 	mpicc-styleswitcher
//	t.breitner 2007 | www.mpicc.de
// 	inspired from http://www.alistapart.com/stories/alternate/
//	============================================================== //

function applyStyleSheet(title) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  setToggleText(title);
}




function setActiveStyleSheet(title) {

    var styleBeforePrint = readCookie('mpicc-style');

    applyStyleSheet(title);
    if ((title == 'mobile') || (!title)) {
        	createCookie('mpicc-style','standardlayout','7');
    }
    else if (title == 'print') {
		// set overlay with back button
		var cookie = readCookie('mpicc-style');
		var styleBeforePrint = cookie ? cookie : 'standardlayout';
		var printButtonContainer   	= document.createElement('div');
		var printButtonLink 		= document.createElement('a');
		var printButtonText 		= document.createTextNode(backToScreenViewStr);

		printButtonContainer.id = 'backfromprint';
		printButtonLink.setAttribute('href',  'javascript:backToScreenView(\''+styleBeforePrint+'\')');
		printButtonLink.appendChild(printButtonText); 
		printButtonContainer.appendChild(printButtonLink); 
		document.getElementById('header').appendChild(printButtonContainer);
    }
    else {
        	createCookie('mpicc-style',title,'7'); 
    }
}




function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}


// return to user default view from print preview
function backToScreenView(whichStyle) {
	if (!document.getElementById) return;

	var printButtonContainer = document.getElementById('backfromprint'); 
	var printButtonParent = document.getElementById('header'); 

	printButtonParent.removeChild(printButtonContainer); 	
	setActiveStyleSheet(whichStyle); 	
}


// to be called by onload
function initializeStyleSwitcher() {
	var cookie = readCookie('mpicc-style');
	var title = cookie ? cookie : 'standardlayout';
	applyStyleSheet(title);
}







//	=============================================================== //
// 	mpicc-togglestyle
// 	t.breitner | www.mpicc.de 
//	=============================================================== //

// called by applyStyleSheet
function setToggleText(style) {
	if ((style == 'fluidlayout') || (style == 'mobile') || (style == 'print') || (!style)) {
		/* document.getElementById('toggleStyle').setAttribute((document.all ? "className" : "class"), "gotostandardlayout"); */
    document.getElementById('toggleStyle').className = "gotostandardlayout";
		document.getElementById('toggleStyle').firstChild.firstChild.nodeValue=standardLayoutStr; 
	}

	if (style == 'standardlayout') {
		/* document.getElementById('toggleStyle').setAttribute((document.all ? "className" : "class"), "gotofluidlayout"); */
    document.getElementById('toggleStyle').className = "gotofluidlayout";
		document.getElementById('toggleStyle').firstChild.firstChild.nodeValue=fluidLayoutStr; 
	}

}

// called by button
function toggleStyle() {
	if (!document.getElementById) return;
	var title = getActiveStyleSheet();
	
	if (title == 'standardlayout') {
		setActiveStyleSheet('fluidlayout');
	}

	if ((title == 'fluidlayout') || (title == 'mobile') || (title == 'print') || (!title)) {
		setActiveStyleSheet('standardlayout');
	}
}








//	=============================================================== //
// 	mpicc-footenotelinks
// 	t.breitner | www.mpicc.de
//	=============================================================== //

/*------------------------------------------------------------------------------
Function:       footnoteLinks()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  8 May 2005
Version:        1.3
Homepage:       http://www.easy-designs.net/code/footnoteLinks/
License:        Creative Commons Attribution-ShareAlike 2.0 License
                http://creativecommons.org/licenses/by-sa/2.0/
Note:           If you change or improve on this script, please let us know by 
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/

function footnoteLinks(containerID,targetID) {
  if (!document.getElementById || 
      !document.getElementsByTagName ||
      !document.createElement) return false;
  if (!document.getElementById(containerID) ||
      !document.getElementById(targetID)) return false;

  var container = document.getElementById(containerID);
  var target    = document.getElementById(targetID);

  // Target Header
  var h2        = document.createElement('h2');
  addClass.apply(h2,['printonly']);
  var h2_txt    = document.createTextNode('Links:');
  h2.appendChild(h2_txt);
  target.appendChild(h2);

  // Target ol
  var ol   = document.createElement('ol');
  addClass.apply(ol,['printonly']);


  // Search for links and add URL to ol
  var coll = container.getElementsByTagName('*');
  // List of seen links
  var myArr = [];

  var thisLink;
  var num = 1;
  for (var i=0; i<coll.length; i++) {
    var thisClass = coll[i].className;
    if ( 
	    	(coll[i].getAttribute('href') || coll[i].getAttribute('cite')) 	&&
		(thisClass == '' || thisClass.indexOf('ignore') == -1)         	&&
		(coll[i].getAttribute('href') !== '#top') 
	) { 

      // take URL from href or cite attribute
      thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;

      // find out footnote number
      var note_txt;

      // Find out if link already exists in myArr
      var j = inArray.apply(myArr,[thisLink]);
      if ( j || j===0 ) {
        note_txt = document.createTextNode(j+1);
      } else {
        // create list element for footer and append to ol
        var li     = document.createElement('li');
        var li_txt = document.createTextNode(thisLink);
        li.appendChild(li_txt);
        ol.appendChild(li);

	// remember link, in case number re-appears
        myArr.push(thisLink);
        note_txt = document.createTextNode(num);

	// count up link numbering
        num++;
      }

      // create footnote reference (²)
      var note = document.createElement('sup');
      addClass.apply(note,['printonly']);
      note.appendChild(note_txt);

      // append footnote reference inside blockquote block or after link element
      if (coll[i].tagName.toLowerCase() == 'blockquote') {
        var lastChild = lastChildContainingText.apply(coll[i]);
        lastChild.appendChild(note);
      } else {
        coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
      }
    }
  }
  target.appendChild(ol);
  addClass.apply(document.getElementsByTagName('html')[0],['noted']);
  return true;
}


/*------------------------------------------------------------------------------
    Excerpts from the jsUtilities Library
    Version:        2.1
    Homepage:       http://www.easy-designs.net/code/jsUtilities/
    License:        Creative Commons Attribution-ShareAlike 2.0 License
                    http://creativecommons.org/licenses/by-sa/2.0/
    Note:           If you change or improve on this script, please let us know.
------------------------------------------------------------------------------*/
// Removed functions only needed for backwards compatibility to IE5 and below

    function inArray(needle) {
      for (var i=0; i < this.length; i++) {
        if (this[i] === needle) {
          return i;
        }
      }
      return false;
    }
    function addClass(theClass) {
      if (this.className != '') {
        this.className += ' ' + theClass;
      } else {
        this.className = theClass;
      }
    }
    function lastChildContainingText() {
      var testChild = this.lastChild;
      var contentCntnr = ['p','li','dd'];
      while (testChild.nodeType != 1) {
        testChild = testChild.previousSibling;
      } 
      var tag = testChild.tagName.toLowerCase();
      var tagInArr = inArray.apply(contentCntnr, [tag]);
      if (!tagInArr && tagInArr!==0) {
        testChild = lastChildContainingText.apply(testChild);
      }
      return testChild;
    }



//	=============================================================== //
// 	mpicc-pageload
// 	prevent flicker at window.onload in case fontsize/stylesheets were switched
//	see: http://dean.edwards.name/weblog/2006/06/again/
// 	Dean Edwards/Matthias Miller/John Resig
//	t.breitner 2007 | www.mpicc.de
//	=============================================================== //


function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
    footnoteLinks('content','contentcontainer');
    insertPagefunctions(); 
    sizerOnLoad();
    initializeStyleSwitcher();
    inlineLinks();
}

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}




// check if mpicc-common.js is called in sslgw-context: 
function checkSSLGW() {
	if(typeof getIVEHostname == 'function') {
		alert("Seitenfunktionen stehen bei der Ausführung hinter dem SSL-Gateway nicht zur Verfügung."); 
	}
	else {
/* for other browsers */
window.onload = init;
	}
}

/* fuer Bildergalerie im Intranet */
function show_pic(picID){
  var myObj = document.getElementById(picID);
  if(myObj.style.display == "none"){
    myObj.style.display = "block";
  }else{
    myObj.style.display = "none";
  }
}

/* open Popupfenster Subseite Objekte, vom alten Layout übernommen */
function compact_openwindow(url,name,options) {
	newwin=window.open(url,name,options);
	newwin.focus();
}

