/* move recursively down a node writing a html textual representation
   of it and all subnodes that are of type element, attribute or text */
function serializeNode(node, indent)
{
  /* output is going to be are final product */
  var output = '';
  while (output.length < indent) output = output + ' ';

  /* tween is anything between the begining and ending tag */
  var tween = '';
  while (tween.length < indent + 2) tween = ' ' + tween;
  
  /* we only serialize down two types of nodes
     element nodes which are 1 and text nodes which are 3 */
  if (node.nodeType == 1)
  {
    /* an element starts with a less-then and then name of 
       the element */
    output = '<' + node.nodeName;

    /* atrributes to an element are stored in a namedNodeMap 
       called attributes */
    atrs = node.attributes;
    for(var i = 0; i < atrs.length; i++)
    {
      /* if the attribute is not blank or null add to are inside of tag */
      if(atrs[i].nodeValue != null && atrs[i].nodeValue != '')
        output = output + ' ' + atrs[i].nodeName + '="' + atrs[i].nodeValue + '"';
    }
    
    /* ok we can put the greater-then ending on our opening tag */
    output = output + '>\n';

    /* now lets get the html text between the openin and closing tag */
    if (node.childNodes.length)
    {      
      for(var i = 0; i < node.childNodes.length; i++)
        tween = tween + serializeNode(node.childNodes[i], indent + 2);
    }
    if (tween) output = output + tween;
 
    for (var i = 0; i < indent; i++) output = output + ' ';

    /* finally lets write out the close tag */
    output = output + '</' + node.nodeName + '>\n';
  }
  /* if this is a text node  or 3 are job is easy */
  else if (node.nodeType == 3) output = output + node.nodeValue + '\n';

  /* all other nodes are discarded */
  return output;
}

/* create a page that is printable from the header and content tag */
function rickPrint()
{  
  
  /* need to open a window to put our page in */
  var printWindow = window.open('','printerfriendlywindow','',true);  
  /* get a handle on the document in that window */
  var printDoc = printWindow.document;
  /* need a place to temporary store the content */
  var content = '';

  /*remove content from the previously opened window*/
  var printHtmlEls = printDoc.getElementsByTagName('html');
  var printHtmlEl = printHtmlEls[0];
  for(i=0; i < printHtmlEls.length; i++) 
  {
    printHtmlEl = printHtmlEls[i];
    while(printHtmlEl.childNodes.length)
    {
      printHtmlEl.removeChild(printHtmlEl.childNodes[0]);
    }
  }
  /* we start by writing out the html and header tags */
  printDoc.write('<html>\n  <header>\n');
  
  /* lets get a handle on the head for this document */
  var headEls = document.getElementsByTagName('head');
  var headEl = headEls[0]; 
    
  /* lets serialize all it's children so we can write them into our new page */
  for(var i = 0; i < headEl.childNodes.length; i++)
  {
    content = content + serializeNode(headEl.childNodes[i], 4);
  }
  
  /* write out the content plus the end header begin body tag */
  printDoc.write(content + '  </header>\n  <body>\n');
  content = ''; 
 
  /* lets get a handle on the body for this document */
  if (arguments.length)
  {
    var contentEl = document.getElementById(arguments[0]); 
  } else {
    var contentEl = document.getElementById('content');
  }

  /* lets serialize all it's children so we can write them into our new page */
  for(var i = 0; i < contentEl.childNodes.length; i++)
  {
    content = content + serializeNode(contentEl.childNodes[i], 4);
  }
  /* write out the content plus the end body end html tag */
  printDoc.write(content + '  </body>\n</html>');

  /* clean up and go home, our work is done here */
  printDoc.close();
}
