//daruma.js
var Daruma = {
  LastChange: new Date(2008, 7, 5),
  Author: 'André Arnold',
  FileName: 'daruma.js',
  FileHome: 'http://www.japaninleipzig.de',
  
  Cookie: new Array(),
  
  start: function() {
    Daruma.Body = $$('body')[0];
    Daruma.Sections = $$('fieldset.part');
    
    //Set all sections to invisible
    Daruma.Sections.each(function(section, i) {
      Daruma.Sections[i].setStyle('display', 'none');
    });
    
    Daruma.Links = $$('a');
    Daruma.Links.each(function(link, i){
      var Anchor = link.href.substring(link.href.indexOf('#')+1, link.href.length); //Extract the anchor's referenced ID from the link's href
      Daruma.Sections.each(function(section, j){
        if (Anchor == section.id) Daruma.Links[i].addEvent('click',function(){ //if the referenced ID is equal to any of the current side's sections add a onclick-function
          Daruma.displaySection(j)
        });
      });
    });
    
    Daruma.displaySection(0); //Show the first section
    
    Daruma.updateLocation();
    //checks whether there is an anchor. if so, shows that section
    if(Daruma.Location) {
      Daruma.Sections.each(function(section,i){
        if(section.id == Daruma.Location) Daruma.displaySection(i)
      });
    }
  },
  
  displaySection: function(i) {
    Daruma.Sections.each(function(section, j) {
      var block = section.getStyle('display') == 'block';
      if (i == j) {
        if (block) return;
        Daruma.Sections[j].setStyle('display', 'block')
      } else {
        if (!block) return;
        Daruma.Sections[j].setStyle('display', 'none');
      }
    });
  },
  
  // updateLocation saves the current URI's anchor (should equal the current shown section) w/o the # to Daruma.Location  
  updateLocation: function(){
		Daruma.Location = window.location.hash.replace('#', '');
	},
	
	updateSection: function() {
    var loc = Daruma.Location;
    Daruma.updateLocation();
    if (loc != Daruma.Location) {
      Daruma.Sections.each(function(section,i){
        if(section.id == Daruma.Location) Daruma.displaySection(i);
      });
    }
  
  }
};


window.addEvent('domready', Daruma.start);
