document.observe("dom:loaded",function(){
   
   automagicMenu($$('#navigation')[0],$$('h1')[0],true);
      //$('navigation').appendChild(automagicMenu(1));
      
});

var theCounter = 0;

function toggleContentByHeading(element,show)  {
   //show content based by heading
   var theHeading = element.nodeName;
   var elementDepth = parseInt(element.nodeName.substr(1,element.nodeName.length-1));
   var startToggle = false;
   $('inhalte').descendants().each(function(node,index){
         
         
         
         //untrigger
         if(startToggle == true && node.hasClassName('heading'))   {
            var nodeDepth = parseInt(node.nodeName.substr(1,node.nodeName.length-1));
            if(nodeDepth <= elementDepth) {
               startToggle = false;  
            }
         }
         
         //trigger
         if(node.id == element.id)  {
            startToggle = true;  
         }
         
         //show or hide?
         if(startToggle == true) {
            node.show();  
         }  else  {
            node.hide();  
         }
         
   });
}

function automagicMenu(inElement,startOutline,allowEqual)   {

   /*$R('h1','h6').each(function(tag,index)   {
         //document.writeln(tag+':'+$$(''+tag+'')[0].innerHTML);
         $$(tag).each(function(node,bindex)   {
               document.writeln(node.innerHTML);
         });
   });*/
   
   var theHeadings = $A();
   $('inhalte').descendants().each(function(node,index)  {
         node.hide();
         if($R('H1','H6').include(node.nodeName))  {
             theHeadings.push(node);
             node.addClassName('heading');
         }
   });
   //declare outside function to make it sort of global
   var updateList;
   
   theHeadings.each(function(node,index)  {
         
         node.writeAttribute('id','heading-'+index);
         var onclick = "toggleContentByHeading($('heading-"+index+"'),true);"
         var current = parseInt(node.nodeName.substr(1,node.nodeName.length-1));
         var htmlFragment = '<a href="#'+node.innerHTML+'" onclick="'+onclick+'">'+'<span>Level '+current+':</span> '+node.innerHTML+'</a>'
         //var htmlFragment = '<a href="#'+node.innerHTML.toJSON().toQueryString()+'" onclick="'+onclick+'">'+'<span>'+current+'. </span>'+node.innerHTML+'</a>'
         
         
         
         
         if(index == 0) {
            
            
            inElement.appendChild(new Element('ul'));
            updateList = inElement.lastChild;
            updateList.appendChild(new Element('li').update(htmlFragment));
            
            
         }  else  {
            
            var before = parseInt(theHeadings[index-1].nodeName.substr(1,theHeadings[index-1].nodeName.length-1));
            
            //inElement.appendChild(new Element('a').update(current+' '+before));
            if(current - before == 0)  {
               updateList.appendChild(new Element('li').update(htmlFragment));   
            }
            if(current - before > 0)  {
               
               /*for(var i=0;i<current-before;i++)   {
                  updateList = updateList.lastChild;
                  updateList.appendChild(new Element('ul'));
                  updateList = updateList.lastChild;
               }*/
               updateList = updateList.lastChild;
               updateList.appendChild(new Element('ul'));
               updateList = updateList.lastChild;
               updateList.appendChild(new Element('li').update(htmlFragment));   
            }
            if(current - before < 0)  {
               //updateList = updateList.parentNode;
               eval('updateList = updateList'+".parentNode.parentNode".times(before-current)+';');
               updateList.appendChild(new Element('li').update(htmlFragment));   
            }
            
            
         }
         
         //document.writeln(theHeadings[index].nodeName);
   });   
   
   //show start
   toggleContentByHeading($('heading-1'),true);
   
}



function ord(inString) {
   var theString = inString;
   return theString.charCodeAt(0);
}

function chr(inNumber)  {
   //var theNumber = inNumber;
   return String.fromCharCode(inNumber)
}


function str_repeat ( input, multiplier ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: str_repeat('-=', 10);
    // *     returns 1: '-=-=-=-=-=-=-=-=-=-='   
    return new Array(multiplier+1).join(input); 
}

function encrypt(inString)   {
   var theString = "";
   for(var theIndex = 0; theIndex < inString.length; theIndex++)  {
      var theOrd = ord(inString.substr(theIndex,1))+'';
      //theString += theOrd.length < 5? str_repeat('-',5-theOrd.length) : theOrd;
      theString += theOrd.length < 3? str_repeat('0',3-theOrd.length) +  theOrd : theOrd;
      //theString += "\n";
   }
   //document.writeln(theString);
   return theString;
}

function decrypt(inString) {
   var theString = "";
   for(var theIndex = 0; theIndex < inString.length; theIndex+=3)  {
      //var theChr = chr(parseInt(inString.substr(theIndex,3)));
      //theString += theChr;
      //theString += parseInt(inString.substr(theIndex,3));
       theString += chr(inString.substr(theIndex,3));
      //theString += "\n";
   }
   return theString;
}
//window.onload = function() {
//   document.writeln(encrypt('sometext'));
//}


