// ***************************** CityDesk menu generator *****************************
// *
// *  (c) by Dieter Spahni, 2008, http://www.spahni.com
// *
// *  The following scripts generate a dynamic menu with the site map
// *  as built by the CityDesk CMS. The first articles in each folder
// *  are automatically used as index.
// *
// *  Used variables from CityDesk:
// *  extra1    the menu label (if empty, the header or filename is used)
// *  extra2    the tooltip and status line (if empty, the header or filename is used)
// *
// ***********************************************************************************

// detect current language
var CD_LPath = escape(self.location.pathname);
CD_LPath = CD_LPath.substr(0,CD_LPath.indexOf("/",1)+1);
// for /index.html, select language DE
if (CD_LPath == "") { CD_LPath = "/de/"; }

// =============================== City Desk Articles =====================================

// array with all nodes
var tree = new Array();

// Node elements of the menu tree
function Node(eltern, ebene, anzahl, nodePath, menuClass, absLink, fileName, headline, extra1, extra2, menulabel, tooltip) {
  this.eltern = eltern;
  this.ebene = ebene;
  this.anzahl = anzahl;
  this.nodePath = nodePath;
  this.menuClass = menuClass;
  /* CityDesk elements */
  this.absLink = absLink;
  this.fileName= fileName;
  this.headline = headline;
  this.extra1 = extra1;
  this.extra2 = extra2;
  this.menulabel = menulabel;
  this.tooltip = tooltip;
}


// Push new article onto stack, check for text substitution
function NewArticle (absLink, fileName, headline, extra1, extra2) {
  var menulabel = extra1;
  if (menulabel.length == 0) {menulabel = (headline.length > 0)? headline : fileName;}
  var tooltip = extra2;
  if (tooltip.length == 0) { tooltip = (headline.length > 0)? headline : fileName;}
  var nodePath = (absLink.lastIndexOf("/") > 0)? CD_LPath+absLink.substring(0,absLink.lastIndexOf("/")+1) : CD_LPath;
  var eltern = 0;
  // calculate ebene
  var ebene = 0;
  var anzahl = 0;
  var pos = -1;
  while ((pos = nodePath.indexOf("/",pos+1)) >= 0) { ebene++; }

  if (tree.length == 0) {
     // first node
     // document.writeln("Root:   "+absLink+"<br>");
     } else {
     // any node but not first node: search for parent node
     // same level AND same path ?
     if ((ebene == tree[tree.length-1].ebene) && (nodePath == tree[tree.length-1].nodePath) ) {
        // continue within same [sub]menue
        // if this is the 2nd item, correct parent node
        eltern = (tree[tree[tree.length -1].eltern].ebene == tree[tree.length -1].ebene)? tree[tree.length -1].eltern : tree.length -1;
        // document.writeln("Same:   "+absLink+"<br>");
        } else {
        // stepping one level down?
        if (ebene == tree[tree.length-1].ebene+1) {
           // step one level down
           eltern = (tree[tree[tree.length -1].eltern].ebene == tree[tree.length -1].ebene)? tree[tree.length -1].eltern : tree.length -1;
           // document.writeln("Down:   "+absLink+"<br>");
           } else {
           // stepping up: search parent
           var searchPath = nodePath;  
           var searchEbene = ebene;
           // strip last folder in searchPath if previous node is on a different path
           if (tree[tree.length -1].nodePath.substring(0,searchPath.length) != searchPath) {
              searchPath = nodePath.substring(0,nodePath.lastIndexOf("/"));        // strip ending slash
              searchPath = searchPath.substring(0,searchPath.lastIndexOf("/")+1);  // strip last folder
              searchEbene--;                                                       // and set search level accordingly
           }
           eltern = 0;
           while (!((searchEbene == tree[eltern].ebene) && (searchPath == tree[eltern].nodePath))) {eltern++};
           // document.writeln("Srch:   "+absLink+" searchPath="+searchPath+"<br>");
        }
     }   
  }
  tree.push(new Node(eltern, ebene, anzahl, nodePath, "", absLink, fileName, headline, extra1, extra2, menulabel, tooltip));
  tree[eltern].anzahl++;
}


// Push article updates onto stack, check for text substitution
function NewUpdate (absLink, fileName, headline, extra1, extra2, mDate, mTime) {
  var menulabel = extra1;
  if (menulabel.length == 0) {menulabel = (headline.length > 0)? headline : fileName;}
  var tooltip = extra2;
  if (tooltip.length == 0) { tooltip = (headline.length > 0)? headline : fileName;}
  tooltip += " ("+mDate+" "+mTime+")";
  var nodePath = (absLink.lastIndexOf("/") > 0)? CD_LPath+absLink.substring(0,absLink.lastIndexOf("/")+1) : CD_LPath;
  var eltern = 0;
  // calculate ebene
  var ebene = 0;
  var anzahl = 0;
  var pos = -1;
  while ((pos = nodePath.indexOf("/",pos+1)) >= 0) { ebene++; }

  // same level AND same path ?
  if ((ebene == tree[tree.length-1].ebene) && (nodePath == tree[tree.length-1].nodePath) ) {
     // continue within same [sub]menue
     // if this is the 2nd item, correct parent node
     eltern = (tree[tree[tree.length -1].eltern].ebene == tree[tree.length -1].ebene)? tree[tree.length -1].eltern : tree.length -1;
     // document.writeln("Same:   "+absLink+"<br>");
     } else {
     // stepping one level down?
     if (ebene == tree[tree.length-1].ebene+1) {
        // step one level down
        eltern = (tree[tree[tree.length -1].eltern].ebene == tree[tree.length -1].ebene)? tree[tree.length -1].eltern : tree.length -1;
        // document.writeln("Down:   "+absLink+"<br>");
        } else {
        // stepping up: search parent
        var searchPath = nodePath;  
        var searchEbene = ebene;
        // strip last folder in searchPath if previous node is on a different path
        if (tree[tree.length -1].nodePath.substring(0,searchPath.length) != searchPath) {
           searchPath = nodePath.substring(0,nodePath.lastIndexOf("/"));        // strip ending slash
           searchPath = searchPath.substring(0,searchPath.lastIndexOf("/")+1);  // strip last folder
           searchEbene--;                                                       // and set search level accordingly
        }
        eltern = 0;
        while (!((searchEbene == tree[eltern].ebene) && (searchPath == tree[eltern].nodePath))) {eltern++};
        // document.writeln("Srch:   "+absLink+" searchPath="+searchPath+"<br>");
     }
  }   
  tree.push(new Node(eltern, ebene, anzahl, nodePath, "", absLink, fileName, headline, extra1, extra2, menulabel, tooltip));
  tree[eltern].anzahl++;
}


function WriteCode(codeLine) {
// document.forms["CityMenuExporter"].elements["CityMenuExport"].value += codeLine;
document.writeln(codeLine);
}

function PrintTree() {
document.writeln("<table border=1><tr><td>#</td><td>eltern</td><td>ebene</td><td>anzahl</td><td>nodePath</td><td>menuClass</td><td>absLink</td><td>fileName</td><td>headline</td><td>extra1</td><td>extra2</td><td>menulabel</td><td>tooltip</td></tr>");
for (i=0; i < tree.length ; i++) { with (tree[i]) {
  document.write("<tr>");
  document.write("<td>"+i+"</td>");
  document.write("<td>"+eltern+"</td>");
  document.write("<td>"+ebene+"</td>");
  document.write("<td>"+anzahl+"</td>");
  document.write("<td>"+nodePath+"</td>");
  document.write("<td>"+menuClass+"</td>");
  document.write("<td>"+absLink+"</td>");
  document.write("<td>"+fileName+"</td>");
  document.write("<td>"+headline+"</td>");
  document.write("<td>"+extra1+"</td>");
  document.write("<td>"+extra2+"</td>");
  document.write("<td>"+menulabel+"</td>");
  document.write("<td>"+tooltip+"</td>");
  document.write("</tr>");
  }
}
document.writeln("</table>");
}

// =============================== City Desk Makro    =====================================
NewArticle("root","Root","Root","Root","Root");
	
// fill up array with all nodes

NewArticle("index.html","index","Welcome !","Homepage","Homepage for Dieter Spahni");

NewArticle("topics/index.html","index","Business Topics","Business Topics","Articles about current business topics");

NewArticle("topics/IST.html","IST","IST - Information Society and Technology","IST","Information Society and Technology");

NewArticle("topics/eGovernment.html","eGovernment","eGovernment in the Information Society","E-Government","E-Government in the Information Society");

NewArticle("teaching/index.html","index","Teaching at Universities","Teaching","Teaching at Universities");

NewArticle("teaching/unibe.html","unibe","Teaching at the University of Bern","Univ of Bern","University of Bern - Center of Competence for Public Management");

NewArticle("teaching/bfh.html","bfh","Teaching at the University of Applied Sciences Bern","UAS Bern","University of Applied Sciences Bern");

NewArticle("teaching/zhaw.html","zhaw","Teaching at the University of Applied Sciences Zurich, Winterthur","UAS Zurich","University of Applied Sciences Zurich, Winterthur");

NewArticle("teaching/diplomarbeiten.html","diplomarbeiten","List of Diploma and Master Thesis","Master Thesis","List of reviewed Diploma and Master Thesis of the past years");

NewArticle("publications/index.html","index","Selected Publications","Publications","Selected articles and books");

NewArticle("publications/articles/index.html","index","Articles in Journals and Newspapers","Articles","Articles in Journals and Newspapers");

NewArticle("publications/articles/RI2006-3.html","RI2006-3","Innovative Geschäftsprozesse dank flexibler Software","Ruf Informiert 2006-3","Innovative Geschäftsprozesse dank flexibler Software");

NewArticle("publications/articles/SDN2005.html","SDN2005","Designorientierte Forschungsfragen in E-Business und E-Government aus der Sicht der Ecademy","SDN publication","Forschungslandschaften im Umfeld des Designs");

NewArticle("publications/articles/ElGov.html","ElGov","Electronic Government Strategies and Implementations","E-Gov Strategy","Electronic Government Strategies and Implementations");

NewArticle("publications/articles/IRIS2003.html","IRIS2003","Web Services in eGovernment","WS in eGov","Web Services in eGovernment");

NewArticle("publications/articles/EGOV2003.html","EGOV2003","URN:Technology","URN:Technology","URN:Technology - A Building Block of the Swiss eGovernment Platform");

NewArticle("publications/articles/HICSS2004.html","HICSS2004","Managing Access to Distributed Resources","HICSS'04","Managing Access to Distributed Resources - Paper presented at HICSS-37 (2004)");

NewArticle("publications/articles/eGP2004-1.html","eGP2004-1","eGovernment im internationalen Umfeld","EGOV'03-DEXA'04","Report: EGOV'03 and DEXA'04&nbsp;");

NewArticle("publications/books/index.html","index","Books and Contributions, Working Papers and Teaching Material.","Books","Books and Contributions");

NewArticle("publications/books/eGovernment1.html","eGovernment1","eGovernment 1 - Eine Standortbestimmung","eGov 1","eGovernment 1 - Eine Standortbestimmung");

NewArticle("publications/books/eGovernment2.html","eGovernment2","eGovernment 2 - Perspektiven und Prognosen","eGov 2","eGovernment 2 - Perspektiven und Prognosen");

NewArticle("publications/books/Dissertation.html","Dissertation","Dissertation: Verfahren zur Bestimmung geeigneter Teilsysteme und deren Sequenzierung","Dissertation","Verfahren zur Bestimmung geeigneter Teilsysteme und deren Sequenzierung");

NewArticle("publications/books/SystemsEngineering.html","Systems Engineering","Systems Engineering - Problem Solving and Project Management","SE","Systems Engineering");

NewArticle("publications/Sabbatical.html","Sabbatical","Publishing activities during my sabbatical","Sabbatical","Articles published during my sabbatical");

NewArticle("fun/index.html","index","Fun Topics","Fun Topics","My personal fun topics");

NewArticle("fun/EGNOS.html","EGNOS","GPS EGNOS Report","GPS with WAAS","Short report about using WAAS/EGNOS with Garmin GPS V");

NewArticle("fun/cal.html","cal","Perpetual Calendar","Calendar","Perpetual calendar for Switzerland with user-defined layout.");

NewArticle("fun/RSS.html","RSS","RSS : Really Simple Syndication / RDF Site Summary","RSS","RDF Site Summary");

NewArticle("fun/fly/index.html","index","Flying as private pilot","Private Pilot","Flying as a private pilot");

NewArticle("fun/fly/PPL.html","PPL","PPL Training in Switzerland","PPL Training","Training for PPL in Switzerland");

NewArticle("fun/fly/planning.html","planning","Flight Planning","Flight Planning","Links for planning your flights (for use in Switzerland only, not yet translated)");

NewArticle("fun/HamRadio/index.html","index","Ham Radio - HB9SQX","Ham Radio","Bringing technology and communication together.");

NewArticle("fun/HamRadio/HB9SQX.html","HB9SQX","HB9SQX","HB9SQX","Home station HB9SQX");

NewArticle("fun/HamRadio/HB9SQX-8.html","HB9SQX-8","HB9SQX-8","HB9SQX-8","Portable station HB9SQX-8");

NewArticle("fun/HamRadio/HB9SQX-9.html","HB9SQX-9","HB9SQX-9","HB9SQX-9","Mobile station (car): HB9SQX-9");

NewArticle("fun/HamRadio/HB9SQX-15.html","HB9SQX-15","HB9SQX-15","HB9SQX-15","Mobile station (van) HB9SQX-15");

NewArticle("fun/gps/index.html","index","GPS server 2.8","GPS Server","This GPS server displays current GPS data and provides them to other applications");

NewArticle("fun/gps/screenshots.html","screenshots","GPS Server Screenshots","Screenshots","Screenshots of the GPS Server");

NewArticle("fun/gps/tech.html","tech","GPS Server Technology","Technology","Some hints about GPS Server Technology (NMEA, XML, XSLT)");

NewArticle("fun/gps/download.html","download","GPS Server Download - Version 2.8f","Download","Download GPS Server V. 2.8f");

NewArticle("news/index.html","index","News from Business and Hobbies","News","News from business and hobbies.");

NewArticle("news/Praxis.html","Praxis","Praxis Barbara Spahni","Office B. Spahni","Barbara hangs out her shingle for psychological consulting and Triple P");

NewArticle("news/EGOV2008.html","EGOV2008","EGOV 2008","EGOV 2008","Seventh international EGOV conference 2008");

NewArticle("news/GeoCaching.html","GeoCaching","GeoCaching","GeoCaching","Having outdoor fun with GPS for the family&nbsp;");

NewArticle("news/GPSserverupdate.html","GPS server update","GPS server update","GPS Server","Version 2.9 beta test");

NewArticle("news/Courtage_Spahni.html","Courtage_Spahni","Spahni Courtage Sàrl","Spahni Courtage Sarl","");


// =============================== End of City Desk Makro   ===============================

// reduziere ebene f�r alle Eintr�ge, die Untereintr�ge haben sollten, aber gar keine haben
for (i=1; i < tree.length ; i++) { if ( (tree[i].ebene-1 == tree[i-1].ebene) && (tree[i].anzahl == 0) ) tree[i].ebene--; }


// reduziere ebene f�r alle Eintr�ge mit Untereintr�gen (index.html)
for (i=1; i < tree.length ; i++) { if (tree[i].anzahl>0) tree[i].ebene--; }

// PrintTree();

// Code Generieren
// document.forms["CityMenuExporter"].elements["CityMenuExport"].value = "";

WriteCode('<span class="preload1"></span>');
WriteCode('<span class="preload2"></span>');
WriteCode('<div id="background">');
WriteCode('<ul class="menu2">');

var pEbene = tree[0].ebene;
var pEltern = tree[0].eltern;

// root node
tree[0].menuClass = "root";

for (i=1; i < tree.length ; i++) { with (tree[i]) {

    if (pEbene == ebene) {
       // continue within same [sub]menue
       if (anzahl == 0) {
          // item does not expand
          if (ebene == tree[0].ebene) {
             // top item
             menuClass = "top";
             WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b></b></a></li>');
             } else {
             if (tree[eltern].anzahl == 1) {
                // last item not expanding
                menuClass = "subbot";
                WriteCode('<li class="subbot"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'</a></li>');
                } else {
                // not last item not expanding
                menuClass = "-";
                WriteCode('<li><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'</a></li>'); 
                }
             }
          } else {
       	  // item is expanding
          if (ebene == tree[0].ebene) {
             // top item
             menuClass = "top+";
             WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b class="down"></b><!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->');
             } else {
             if (tree[eltern].anzahl == 1) {
                // last item expanding
                menuClass ="flybot"
                WriteCode('<li class="flybot"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'<!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->'); 
                // WriteCode('</ul><!--[if lte IE 6]></td></tr></table></a><![endif]-->');
                } else {
                // not last item expanding
                menuClass ="fly"
                WriteCode('<li class="fly"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'<!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->'); 
                }
             }
          }
       }

      else { // cEbene != ebene
       if (pEbene+1 == ebene) {
          // open new submenu
          if (tree[0].ebene+1 == ebene) WriteCode('<ul class="sub">'); else WriteCode('<ul>');
          if (anzahl == 0) {
             // firt item of submenu does not expand
             menuClass = "subtop";
             WriteCode('<li class="subtop"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'</a></li>');
             } else {
             // first item of submenu is expanding
             menuClass = "flytop";
             WriteCode('<li class="flytop"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'<!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->'); 
             }
          // step one level down
          pEbene++;
          pEltern = eltern; }

          else { // pEbene > ebene: step up!
           while (pEbene > ebene) { WriteCode('</ul><!--[if lte IE 6]></td></tr></table></a><![endif]-->'); pEbene--; }
           if (tree[eltern].anzahl == 1) {
              // last item of [sub]menu
              if (anzahl == 0) {
                 // last item is not expanding
                 if (ebene == tree[0].ebene) {
                    // last top item not expanding
                    menuClass = "top";
                    WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b></b></a></li>');
                    } else {
                    // last non top item not expanding
                    menuClass = "subbot";
                    WriteCode('<li class="subbot"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'</a></li>');
                    }
                 } else {
                 // last item is expanding
                 if (ebene == tree[0].ebene) {
                    // last top item expanding
                    menuClass = "top+";
                    WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b class="down"></b><!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->');
                    } else {
                    // last non top item expanding
                    menuClass = "flybot";
                    WriteCode('<li class="flybot"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'<!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->'); 
                    }
                 }
              } else {
              // not last item of [sub]menu
              if (anzahl == 0) {
                 // not last item is not expanding
                 if (ebene == tree[0].ebene) {
                    // not last top item not expanding
                    menuClass = "top";
                    WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b></b></a></li>');
                    } else {
                    // not last non top item not expanding
                    menuClass = "-";
                    WriteCode('<li class="subbot"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'</a></li>');
                    }
                 } else {
                 // not last item is expanding
                 if (ebene == tree[0].ebene) {
                    // not last top item expanding
                    menuClass = "top+";
                    WriteCode('<li class="top"><a href="'+CD_LPath+absLink+'" id="'+extra1+'" class="top_link" title="'+tooltip+'"><span>'+menulabel+'</span><b class="down"></b><!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->');
                    } else {
                    // not last non top item expanding
                    menuClass = "fly";
                    WriteCode('<li class="fly"><a href="'+CD_LPath+absLink+'" title="'+tooltip+'">'+menulabel+'<!--[if gte IE 7]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><iframe></iframe><![endif]-->'); 
                    }
                 }
              }


     }}
     tree[eltern].anzahl--;
     
}} // with ...   for  
WriteCode('</ul>');
WriteCode('</div>');
WriteCode('<br class="clear" />');

// PrintTree();

