 // ----------------------------------------------------------------------------
// - extranet                                                                 -
// - ethernix extranet javascript module                                      -
// ----------------------------------------------------------------------------
// - This software is the property of the copyright holder and is distributed -
// - under the term of the 'ethernix end-user license agreement' (EULA). This -
// - software cannot be copied, nor distributed without the express written   -
// - authorisation  of the copyright holder.                                  -
// - In no event, shall the copyright holder  or  its  licensor be liable for -
// - any direct, indirect, incidental or special damages  arising  in any way -
// - out of the use of this software.                                         -
// ----------------------------------------------------------------------------
// - author    (c) 2006-2010 amaury darsch                                    -
// - copyright (c) 2006-2010 ethernix eurl                                    -
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// - general purpose functions                                                -
// ----------------------------------------------------------------------------

// add an element node to the body
// @param node the node to add

function addNodeToBody (node) {
  var body = document.getElementsByTagName ("body")[0];
  body.appendChild (node);
}

// add an empty paragraph

function addEmptyParagraph () {
  // add an empty paragraph
  addNodeToBody (document.createElement ("p"));
}

// create a new image node by source, geometry and alternate
// @param src the image source
// @param wth the image width
// @param hgt the image height
// @param alt the image alternate

function createImageNode (src, wth, hgt, alt) {
  // create the image node
  var img = document.createElement ("img");
  // add the image attributes
  img.setAttribute ("src",    src);
  img.setAttribute ("width",  wth);
  img.setAttribute ("height", hgt);
  img.setAttribute ("alt",    alt);
  // here is the image
  return img;
}

// create a new anchor node by reference
// @param ref the node reference

function createAnchorNode (ref) {
  // create the anchor node
  var arn = document.createElement ("a");
  // add the node attribute
  arn.setAttribute ("href", ref);
  // here is the node
  return arn;
}

// create a new anchor node by reference and text
// @param ref the node reference
// @param txt the node text

function createAnchorWithText (ref, txt) {
  // create the reference node
  var arn = document.createElement ("a");
  // add the node attribute
  arn.setAttribute ("href", ref);
  // create the text node
  var lnk = document.createTextNode (txt);
  arn.appendChild (lnk);
  // here is the node
  return arn;
}

// create a rule by class
// @param name the class name to use

function createRulerByClass (name) {
  var hr = document.createElement ("hr");
  // add the node attribute
  hr.setAttribute ("class", name);
  // here is the node
  return hr;
}

// ----------------------------------------------------------------------------
// - title generation function                                                -
// ----------------------------------------------------------------------------

// create a column group node

function createTitleCgr () {
  // create the column group
  var cgr = document.createElement ("colgroup");
  // create the first column element
  var cl1 = document.createElement ("col");
  cl1.setAttribute ("width", "30%");
  // create the second column element
  var cl2 = document.createElement ("col");
  cl2.setAttribute ("width", "70%");
  // add the columns to the group
  cgr.appendChild (cl1);
  cgr.appendChild (cl2);
  // here is the column group
  return cgr;
}

// create a title table node

function createTitleTable () {
  // create the table uelement
  var tbl = document.createElement ("table");
  // add the class attribute
  tbl.setAttribute ("class", "full");
  // create the column group and add it
  var cgr = createTitleCgr ();
  tbl.appendChild (cgr);
  // here we are
  return tbl;
}

// create the title left element

function createTitleLeft () {
  // create the title data element
  var tdl = document.createElement ("td");
  // set date attribute
  tdl.setAttribute ("class",  "title");
  tdl.setAttribute ("align",  "left");
  tdl.setAttribute ("valign", "bottom");
  // create the image
  var img = createImageNode ("/img/ethernix.png", "120", "120",
			     "The Ethernix Company");
  tdl.appendChild (img);
  // here is the node
  return tdl;
}

// create the table right element
// @param title the table title text

function createTitleRight (title) {
  // create the title data element
  var tdr = document.createElement ("td");
  // set data attribute
  tdr.setAttribute ("class",  "title");
  tdr.setAttribute ("align",  "right");
  tdr.setAttribute ("valign", "bottom");
  // set title text
  var txt = document.createTextNode (title);
  tdr.appendChild (txt);
  // here is the node
  return tdr;
}

// create the title row
// @param title the table title text

function createTitleRow (title) {
  // create the table row
  var tr  = document.createElement ("tr");
  // create the left title node
  var tdl = createTitleLeft ();
  tr.appendChild (tdl);
  // create the right title node
  var tdr = createTitleRight (title);
  tr.appendChild (tdr);
  // here is the row
  return tr;
}

// create standard page title node
// @param title the page title

function createPageTitleNode (title) {
  // create the title table
  var tbl = createTitleTable ();
  // create the table row
  var tr  = createTitleRow (title);
  tbl.appendChild (tr);
  // here is the node
  return tbl;
}

// create the standard page title
// @param title the page title

function createPageTitle (title) {
  // create the title table
  var tbl = createPageTitleNode (title);
  // append the table to the body
  addNodeToBody (tbl);
}

// ----------------------------------------------------------------------------
// - footer generation function                                               -
// ----------------------------------------------------------------------------

// create a footer table

function createFooterTable () {
  // create the table uelement
  var tbl = document.createElement ("table");
  // add the class attribute
  tbl.setAttribute ("class", "full");
  // here we are
  return tbl;
}

// create the footer left element

function createFooterLeft () {
  // create the title data element
  var tdl = document.createElement ("td");
  // set date attribute
  tdl.setAttribute ("id",    "lfoot");
  tdl.setAttribute ("align", "left");
  // create the image
  var mai = createImageNode ("/img/mai.png", "16", "16",
			     "Ethernix webmaster");
  // add anchor node
  var man = createAnchorNode ("mailto:webmaster@ethernix.com");
  man.appendChild (mai);
  tdl.appendChild (man);
  // here is the node
  return tdl;
}

// create the footer right element

function createFooterRight () {
  // create the title data element
  var tdr = document.createElement ("td");
  // set data attribute
  tdr.setAttribute ("class", "footer");
  tdr.setAttribute ("align", "right");
  // add footer text
  var txt = document.createTextNode ("Copyright 2006-2010 ");
  tdr.appendChild (txt);
  // add footer anchor
  var arn = createAnchorWithText ("http://www.ethernix.com", "Ethernix eurl");
  tdr.appendChild (arn);
  // here is the node
  return tdr;
}

// create the footer row

function createFooterRow () {
  // create the table row
  var tr  = document.createElement ("tr");
  // create the left footer node
  var tdl = createFooterLeft ();
  tr.appendChild (tdl);
  // create the right footer node
  var tdr = createFooterRight ();
  tr.appendChild (tdr);
  // here is the row
  return tr;
}

// create the standard page footer

function createPageFooter () {
  // create the footer ruler
  var hr = createRulerByClass ("foot");
  addNodeToBody (hr);
  // create the title table
  var tbl = createFooterTable ();
  // create the table row
  var tr  = createFooterRow ();
  tbl.appendChild (tr);
  // append the table to the body
  addNodeToBody (tbl);
}

// add a new image link to the footer
// @param img the image path
// @param alt the image alternate
// @param url the url anchor

function addPageLeftFooter (img, alt, url) {
  // get the left footer node
  var lfn = document.getElementById ("lfoot");
  // create a space
  var spc = document.createTextNode (" ");
  lfn.appendChild (spc);
  // create the image node
  var lfi = createImageNode (img, "16", "16", alt);
  // create the anchors
  var lfa = createAnchorNode (url);
  lfa.appendChild (lfi);
  lfn.appendChild (lfa);
}

// ----------------------------------------------------------------------------
// - header menu generation function                                          -
// ----------------------------------------------------------------------------

// create a header node

function createHeaderNode () {
  // create the div element
  var div = document.createElement ("div");
  // add the class attribute
  div.setAttribute ("class", "head");
  // here we are
  return div;
}

// create the standard page header menu
// @param robj the reference object

function createPageHeader (robj) {
  // create the top ruler
  var hrt = createRulerByClass ("head");
  addNodeToBody (hrt);
  // create the head node
  var div = createHeaderNode ();
  // add the home node
  var arn = createAnchorWithText ("/index.xht", "home");
  div.appendChild (arn);
  // loop in the reference array
  if (robj) {
    for(var ref in robj) {
      // add the separator
      var spc = document.createTextNode (" | ");
      div.appendChild (spc);
      // add the reference
      var txt = robj[ref];
      var arn = createAnchorWithText (ref, txt);
      div.appendChild (arn);
    }
  }
  // append the header to the body
  addNodeToBody (div);
  // create the bottom ruler
  var hrb = createRulerByClass ("head");
  addNodeToBody (hrb);
}

// create a special page header menu without home
// @param robj the reference object

function createSpecialHeader (robj) {
  // create the top ruler
  var hrt = createRulerByClass ("head");
  addNodeToBody (hrt);
  // create the head node
  var div = createHeaderNode ();
  // loop in the reference array
  if (robj) {
    for (var ref in robj) {
      // add the separator
      if (div.hasChildNodes () == true) {
	var spc = document.createTextNode (" ");
	div.appendChild (spc);
      }
      // add the reference
      var txt = robj[ref];
      var arn = createAnchorWithText (ref, txt);
      div.appendChild (arn);
    }
  }
  // append the header to the body
  addNodeToBody (div);
  // create the bottom ruler
  var hrb = createRulerByClass ("head");
  addNodeToBody (hrb);
}

// create the base home header menu
// @param robj the reference object

function createHomeHeader (robj) {
  // create the top ruler
  var hrt = createRulerByClass ("head");
  addNodeToBody (hrt);
  // create the head node
  var div = createHeaderNode ();
  // add the home node
  var arn = createAnchorWithText ("/index.xht", "home");
  div.appendChild (arn);
  // append the header to the body
  addNodeToBody (div);
  // create the bottom ruler
  var hrb = createRulerByClass ("head");
  addNodeToBody (hrb);
}

// - menu generation function                                                 -
// ----------------------------------------------------------------------------

// create a menu div node

function createMenuBarNode () {
  // create the div element
  var div = document.createElement ("div");
  addNodeToBody (div);
  // add the class attribute
  div.setAttribute ("class", "mbar");
  // create the table element
  var tbl = document.createElement ("table");
  // add the class attribute
  tbl.setAttribute ("class", "mbar");
  // add the table to the div node
  div.appendChild (tbl);
  // create a table row
  var bar = document.createElement ("tr");
  tbl.appendChild (bar);
  // here we are
  return bar;
}

// create a menu list node
// @param mlst the menu list

function createMenuList (mlst) {
  // create the list object
  var menu = document.createElement ("ul");
  menu.setAttribute ("class", "mbar");
  // loop in the menu list
  if (mlst) {
    for (var ref in mlst) {
      // create the anchor node
      var txt = mlst[ref];
      var arn = createAnchorWithText (ref, txt);
      // create the menu item
      var item = document.createElement ("li");
      // add the anchor
      item.appendChild (arn);
      // add the item
      menu.appendChild (item);
    }
  }
  // here is the menu
  return menu;
}

// create a menu object by name and menu list
// @param name the menu name
// @param mlst the menu list

function createMenuObject (name, mlst) {
  // create the menu object
  var menu = document.createElement ("td");
  menu.setAttribute ("class", "mbar");
  // create the menu title
  var titl = document.createTextNode (name);
  menu.appendChild (titl);
  // create the menu list
  menu.appendChild (createMenuList (mlst));
  // here it is
  return menu;
}

// create the menu bar by reference object
// @param robj the reference object

function createMenuBar (robj) {
  // create the top ruler
  var hrt = createRulerByClass ("menu");
  addNodeToBody (hrt);
  // create the menu bar node
  var bar = createMenuBarNode ();
  // loop in the reference array
  if (robj) {
    for (var ref in robj) {
      var mnu = robj[ref];
      var mob = createMenuObject (ref, mnu);
      bar.appendChild (mob);
    }
  }
  // create the bottom ruler
  var hrb = createRulerByClass ("menu");
  addNodeToBody (hrb);
}

// ----------------------------------------------------------------------------
// - formatting function                                                      -
// ----------------------------------------------------------------------------

// create a section node
// @param title the section title

function createSectionNode (title) {
  // add an empty paragraph
  addEmptyParagraph ();
  // create the div element
  var div = document.createElement ("div");
  // add the class attribute
  div.setAttribute ("class", "menu");
  // set title text
  var txt = document.createTextNode (title);
  div.appendChild (txt);
  // add the node to the body
  addNodeToBody (div);
  // add an empty paragraph
  addEmptyParagraph ();
}

// ----------------------------------------------------------------------------
// - helper functions                                                         -
// ----------------------------------------------------------------------------

// the public header

var EthernixPublicHeader = {
  "/xht/company.xht"  : "company",
  "/xht/news.xht"     : "news"
};

// create the public header

function createPublicHeader () {
  createPageHeader (EthernixPublicHeader);
}
