/*
 * vendome.js
 *
 * $Id: google-map.js,v 1.7 2005/12/29 01:33:46 randy Exp $
 */

/**
 * IE doesn't define the DOM-standard constants on Node. Define the
 * ones we might use so we can actually use them.
 */
if ( ! Node )
{
   var Node =
   {
      ELEMENT_NODE:           1,
      ATTRIBUTE_NODE:         2,
      TEXT_NODE:              3,
      DOCUMENT_NODE:          9,
      DOCUMENT_FRAGMENT_NODE: 11
   };
}

/****************************************************************************
 * STYLE CONTROLS
 ***************************************************************************/

function initStylePrefs()
{
   // add the style controls
   var div = document.createElement( "div" );
   div.className = "styleControls";
   document.body.appendChild( div );
   div.appendChild( styleButton( "tall",
                    "Expand masthead to full height",
                    "Expand masthead button" ) );
   div.appendChild( styleButton( "short",
                    "Collapse masthead to minimize scrolling",
                    "Collapse masthead button" ) );

   // see if have style preference in cookie
   var styleHeight = getCookie( "styleHeight" );
   if ( styleHeight )
      setStyleHeight( styleHeight );

   function styleButton( styleHeight, title, alt )
   {
      var img = document.createElement( "img" );
      img.src = "/images/" + styleHeight + ".jpg";
      img.alt = alt;
      var elem = document.createElement( "a" );
      elem.appendChild( img );
      elem.href = "#";
      elem.onclick = function()
      {
         setStyleHeight( styleHeight );
         return( false );
      }
      elem.title = title;
      elem.className = styleHeight + "Style";
      elem.style.color = "white";
      return( elem );
   }
}

function setStyleHeight( height )
{
   if ( height == "short" )
      document.body.className = "short";
   else
      document.body.className = undefined;

   setCookie( "styleHeight", height );
}

function getCookie( name )
{
   var cookieString = document.cookie;
   if ( cookieString == "" )
      return;

   var cookieList = cookieString.split( /\s*;\s*/ );
   for ( var i = 0 ; i < cookieList.length ; ++i )
   {
      var eq = cookieList[ i ].indexOf( '=' );
      if ( eq == -1 )
         continue;
      var cName = cookieList[ i ].substr( 0, eq );
      var cValue = cookieList[ i ].substr( eq + 1 );
      if ( name == cName )
         return( cValue );
   }
   return; // not found
}

function setCookie( name, value )
{
   var oneYear = new Date();
   oneYear.setFullYear( oneYear.getFullYear() + 1 );
   var cookieValue = name + "=" + value
                     + "; expires=" + oneYear.toGMTString()
                     + "; path=/";
   document.cookie = cookieValue;
}

/****************************************************************************
 * E-MAIL
 ***************************************************************************/

function enableCommunication()
{
   function getText( elem )
   {
      var text = '';
      var children = elem.childNodes;
      for ( var i = 0 ; i < children.length ; ++i )
      {
         var child = children[ i ];
         if ( child.nodeType == Node.TEXT_NODE 
              || child.nodeType == Node.CDATA_SECTION_NODE )
            text += child.data;
         else
            text += getText( child );
      }
   
      // strip leading and trailing whitespace
      text = text.replace( /^\s+/, '' );
      text = text.replace( /\s+$/, '' );
      
      return( text );
   }
   
   var spans = document.getElementsByTagName( 'span' );
   for ( var i = 0 ; i < spans.length ; ++i )
   {
      if ( spans[ i ].className == 'ea' )
      {
         var orig = spans[ i ];
         var text = getText( orig );
         var addr = text;
         addr = addr.replace( /\s*\(at\)\s*/, '@' );
         addr = addr.replace( /\s*\(dot\)\s*/, '.' );
         while ( orig.hasChildNodes() != 0 )
            orig.removeChild( orig.childNodes[ 0 ] );
         
         var link = document.createElement( 'a' );
         link.href = 'mailto:' + addr;
         var tnode = document.createTextNode( addr );
         link.appendChild( tnode );
         orig.appendChild( link );
      }
   }
}