//////////////////////////////////////////////////////////////////////
//
// @source: https://www.brucker.ch/styles/brucker.js.in 
// 
// Copyright (C) 2013 Achim D. Brucker <brucker@spamfence.net> 
//
// This JavaScript code is free software: you can redistribute it 
// and/or modify it under the terms of the GNU General Public License 
// (GNU GPL) as published by the Free Software Foundation, either 
// version 3 of the License, or (at your option) any later version.  
// The code is distributed WITHOUT ANY WARRANTY; without even the 
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
// PURPOSE.  See the GNU GPL for more details.
//
// As additional permission under GNU GPL version 3 section 7, you
// may distribute non-source (e.g., minimized or compacted) forms of
// that code without the copy of the GNU GPL normally required by
// section 4, provided you include this license notice and a URL
// through which recipients can access the Corresponding Source. 
// To satify this requirement, you can also provide the license information 
// and the link to the Corresponding Source using the  JavaScript Web 
// Labels (http://www.gnu.org/licenses/javascript-labels.html).
//
//////////////////////////////////////////////////////////////////////

function isType(type, obj) {
    var clas = Object.prototype.toString.call(obj).slice(8, -1);
    return obj !== undefined && obj !== null && clas === type;
}

function closeSection(id)
{
    if(! isType('String', id)){
        id = this.id;
    }
    var bodyObj = document.getElementById(id+"Body");
    var obj     = document.getElementById(id);
        
    bodyObj.style.display='none';
    obj.style.color="grey";
    obj.innerHTML=obj.innerHTML.replace("Collapse","Expand");
    obj.innerHTML=obj.innerHTML.replace("Zuklappen","Aufklappen");
    obj.innerHTML=obj.innerHTML.replace("[-]","[+]");
    obj.innerHTML=obj.innerHTML.replace("open","close");
}

function openSection(id)
{
    if(! isType('String', id)){
        id = this.id;
    }
    var bodyObj = document.getElementById(id+"Body");
    var obj     = document.getElementById(id);
	
    bodyObj.style.display='block';
    obj.style.color=col;
    obj.innerHTML=obj.innerHTML.replace("Expand","Collapse");
    obj.innerHTML=obj.innerHTML.replace("Aufklappen","Zuklappen");
    obj.innerHTML=obj.innerHTML.replace("[+]","[-]");
    obj.innerHTML=obj.innerHTML.replace("close","open");
}

function switchSection(id)
{
    if(! isType('String', id)){
        id = this.id;
    }

    var bodyObj = document.getElementById(id+"Body");
    if(bodyObj.style.display=='block' || bodyObj.style.display===''){
	closeSection(id);
    }else{
        openSection(id);
    }		

}

function initSwitchSection (){
   var tags = document.getElementsByTagName('H4');
   for(var i=0;i<tags.length;i++){
	if(tags[i].className=='sectionCollapse'){
	    tags[i].onclick = switchSection;
            col = tags[i].style.color; 
            switchSection(tags[i].id);
	}	
	
    }	
}


function initCitations(){
   var tags = document.getElementsByTagName('A');
   for(var i=0;i<tags.length;i++){
	if(tags[i].className=='cite'){
            var cell = document.getElementById(tags[i].href.replace(/^.*#/,""));
            var citeID;
            if(document.all){
                 citeID=cell.innerText.replace(" ","").replace("[","").replace("]","");
            } else{
                 citeID=cell.textContent.replace(" ","").replace("[","").replace("]","");
            }
            tags[i].innerHTML=citeID;
	}	
	
    }	
    
}
 

function init()
{
    initSwitchSection();
    initCitations();
//    initSwitchSection(document.getElementsByTagName('H1'));
//    initSwitchSection(document.getElementsByTagName('H2'));
//    initSwitchSection(document.getElementsByTagName('H3'));
//    initSwitchSection(document.getElementsByTagName('H4'));
}

var col;
window.onload = init;

