// Global variables

// Set to true to disable various links in 
// print friendly versions of files.
var printFriendly = false;

// Stuff printed out at the end, duplicating certain stuff at the top:
var trailingStuff='';

//  The URL of the last sermon printed as a link in the sermon archive,
//  used to generate the "more" link.
var lastSermonUrl=null;

//  Array of labels for when we have a list of 
//  Set up for printer friendly display
function SetPrintFriendly(pf)
{
   // Obsolete, we use checkPrintFriendly instead
   //printFriendly = pf==null?true:pf;
}

function checkPrintFriendly()
{
   // We are a printer friendly version if our name ends in _pr.html
   var loc=document.location;
   printFriendly=(loc.search=="?pr");
   return printFriendly;
}

//   Print text to the document, unless it is a printer friendly
//   version
function WindowPrint(s)
{
   if (!printFriendly)
      document.write(s);
}
//  Function to write out the constant top part of the body
function topStuff()
{
   // See if we are a printer friendly version
   checkPrintFriendly();


   if (printFriendly)
      return;


   // Write out the main table, and the navigation material in the first cell
   document.write(
      '    <center> ' + 
      '    <table id="mainTable" cellspacing="0" width="800"> ' +
      '     <tr>' +
      '       <td id="header" valign="top" >' + 
      '         <img align="left" style="margin: -3px 10px -3px 1px" src="images/uccchurch.gif" height="70">' + 
      '         <div id="title"><b>Monroe Congregational Church, United Church of Christ</b></div>' + 
      '         <div id="subtitle">Uniting Faithfulness with Discovery since 1905</div>' + 
      '       </td>' +
      '     </tr>' +
      '     <tr>' +
      '       <td id="nav">' + 
      '         <span> ' + 
      '           <a class="navLink" href="index.html">Home</a> | ' + 
      '           <a class="navLink" href="sermons.html">Sermon Archive</a> | ' + 
      '           <a class="navLink" href="education.html">Christian Education</a> | ' + 
      '           <a class="navLink" href="music.html">Music</a> | ' + 
      '           <a class="navLink" href="women.html">Women&#039;s Fellowship</a> | ' + 
      '           <a class="navLink" href="calendar.html">Calendar</a> | ' + 
      '           <a class="navLink" href="links.html">Links</a> | ' + 
      '         </span>' + 
      '       </td>' +
      '     </tr>' + 
      '     </center>'
   );

   if (typeof(subnav) != 'undefined')
   {
      switch (subnav)
      {
        case "sermons":
           document.write(
              '     <tr>' +
              '       <td id="subnav">' + 
              '         <span>Sermon archive for: ' + 
              '           <a class="subnavLink" href="sermons.html">2010</a> | ' + 
              '           <a class="subnavLink" href="sermons2009.html">2009</a> | ' + 
              '           <a class="subnavLink" href="sermons2008.html">2008</a> | ' + 
              '           <a class="subnavLink" href="sermons2007.html">2007</a> | ' + 
              '           <a class="subnavLink" href="sermons2006.html">2006</a> | ' + 
              '           <a class="subnavLink" href="sermons2005.html">2005</a> | ' + 
              '           <a class="subnavLink" href="sermons2004.html">2004</a> | ' + 
              '           <a class="subnavLink" href="sermons2003.html">2003</a> | ' + 
              '           <a class="subnavLink" href="sermons2002.html">2002</a> | ' + 
              '           <a class="subnavLink" href="sermons2001.html">2001</a> | ' + 
              '           <a class="subnavLink" href="sermons2000.html">2000</a> | ' + 
              '         </span>' + 
              '       </td>' +
              '     </tr>'
           );
           break;
        case "home":
           document.write(
              '     <tr>' +
              '       <td id="subnav">' + 
              '         <span>' + 
              '           <a class="subnavLink" href="abouttom.html">About Pastor Tom</a> | ' + 
              '           <a class="subnavLink" href="pastor.html">A Message from the Pastor</a> | ' + 
              '           <a class="subnavLink" href="history.html">Our History</a> | ' + 
              '           <a class="subnavLink" href="mission.html">Our Mission</a> | ' + 
              '           <a class="subnavLink" href="reflections.html">Reflections</a> | ' + 
              '           <a class="subnavLink" href="pictures.html">Pictures</a> | ' + 
              '         </span>' + 
              '       </td>' +
              '     </tr>'
           );
           break;
        case "groups-x":
           document.write(
              '     <tr>' +
              '       <td id="subnav">' + 
              '         <span>' + 
              '           <a class="subnavLink" href="women.html">Women&#39;s Fellowship</a> | ' + 
              '           <a class="subnavLink" href="pastor.html">Men&#39;s Group</a> | ' + 
              '           <a class="subnamorelinkvLink" href="history.html">Bible Study</a> | ' + 
              '           <a class="subnavLink" href="reflections.html">Book Group</a> | ' + 
              '         </span>' + 
              '       </td>' +
              '     </tr>'
           );
           break;
      }
   }
   document.write(
      '     <tr>' +
      '       <td id="mainContent" valign="top"> '  
   );
}

// Function to write out the common tail end
function bottomStuff()
{
    // if we are a printer friendly version, skip this
   if (printFriendly)
       return;
   document.write(trailingStuff +
      '<center>' +
      '  <script>linkPrintFriendly();</script> ' + 
      '    </td></tr>' + 
      '    <tr><td id="footer">' + 
	  ' &nbsp;' + 
      ' </td></tr>' + 
      '</table>' + 
      '</center>'
   );
}

// Function to write out the navigation column
function navTable()
{
   document.write(
      '<FONT SIZE=+1><B><I>' +
      '    <p> ' +
      '    <a href="index.html" > Home </a> <br> ' + 
      '    <a href="http://calendar.yahoo.com/delenarenee" target="_new"> Calendar </a> <P> ' + 
      '    <a href="pastor.html"> Message from the Pastor </a> <br>' +
      '    <a href="worship.html"> Worship </a> <br>' +
      '    <a href="sermons.html"> Sermon Archive </a> <br>' +
      '    <a href="Reflections.html"> Reflections </a> <br>' +
      '    <a href="newsletter.html"> Newsletter </a> <br>' +
      '    <a href="education.html"> Christian Education </a> <br>' +
      '    <a href="photoalbum.html"> Photo Album </a> <br>' +
      '    <a href="music.html"> Music </a> <br>' +
      '    <a href="women.html"> Women\'s Fellowship</a>' +
      //'    <a href="community.html"> Community Outreach </a> <br>' +
      //'    <a href="accept.html"> An Accepting Church </a> <br>' +
      '    <P> ' +
      '    <a href="Constitution.html"> Constitution&Bylaws </a> <br>' +
      '    <a href="mission.html"> Our Mission </a> <br>' +
      '    <a href="History.html"> Our History </a> <br>' +
      '    <P>' +
      '    <A href="http://www.ucc.org"> United Church of Christ </A><br>' +
      '    <A href="http://www.pncucc.org"> Pacific Northwest Conference </A><br>' +
      '    Send us Mail:<br>' +
      '    &nbsp;&nbsp;&nbsp;&nbsp;<A href="mailto:office@monroeucc.org"> office</A><br>' +
      '    &nbsp;&nbsp;&nbsp;&nbsp;<A href="mailto:pastor@monroeucc.org"> pastor</A><br>' +
      '    &nbsp;&nbsp;&nbsp;&nbsp;<A href="mailto:musicdirector@monroeucc.org"> music director</A><br>' +
      '    &nbsp;&nbsp;&nbsp;&nbsp;<A href="mailto:webmaster@monroeucc.org"> webmaster</A><br>' +
      '</FONT></I></B>'
   );
}

// print the title line
function printTitle(title,subt)
{
   document.write('<H3><B>'+ title + '</H3></B>');
   if (typeof(subt) != 'undefined')
      subTitle(subt);
   document.title = "Monroe Congregational Church: " + title;
}
//  A secondary subtitle
function subTitle(title)
{
   document.write('<B><CENTER>'+ title + '</B>');
}

//  Print a link to the last sermon, duplicating the link
//  created when we printed the title of the sermon
function printMore()
{
    if (lastSermonUrl != null)
       document.write(
           '<A HREF="'
         + lastSermonUrl
         + '.html">&nbsp;&nbsp;Read more... </A>'
       );
}

//  Print the title of a sermon in the sermon archive.
//  If we previously printed a sermon title, and the caller does
//  not specifically ask not to do this, print out the "more" link
//  at the end of the previous sermon snippet.
function linkSermon(url, title, prependMore)
{
   lastSermonUrl=url;
   document.write('<hr width="100%" size="1" color="#99cc99">');
   document.write(
       '<h3 align="left">' + title + '</h3>'
   );
}
function linkSermonSubt(url, title, subt, prependMore)
{
   linkSermon(url,title,prependMore);
   if (subt != null)
      subTitle(subt);
}
//  Display previous and next links
//     sequence: common front end to prev and next urls
//     prev, next: the trailing part of the url's, with the .html suffix omitted
//     showNext: if false, don't show next.  We generally set this to false, then
//               change it when the next week's sermon is uploaded.
function linkNextPref(sequence,prev,next,showNext)
{
  // If we are a printer friendly version, skip this.
  if (printFriendly)
    return;

  var out = 
     '<table border="0" cellpadding="10" cellspacing="0" width="100%"> <tr> ';
  
  if (prev != null && prev != '')
      out += '<td><A HREF="' + sequence+ prev + '.html">Previous</A></td>';
  out += '<td width=100%></td>';
  if (showNext && next != null && next != '')
      out += '<td><A HREF="' + sequence+ next + '.html">Next</A></td>';
  out += '</table>';
  document.write(out);
  // Get it to print out again at the end
  trailingStuff += out;
}

//  Link to a printer friendly version.
//    name: the of the current document, without the .html
function linkPrintFriendly()
{
   //if (printFriendly)
   //   return;
   var loc=document.location;
   var path=loc.pathname;
   var start=path.lastIndexOf("/")+1;
   var end,name,out;

   if (printFriendly)
   {
      // If we are a print friendly version, generate a link back to
      // the People Friendly Version, but don't print it.
      end=path.indexOf("_pr.htm")-1;
      if (end <= 0)
          end = path.length;
      name = path.substr(start,end);
      out = '<SPAN CLASS=noprint><A HREF="'
             + name
             + '">Back to People Friendly Version<BR></A></SPAN>';
   }else
   {
      var end=path.length;
      var name = path.substr(start,end);

      var out =
       '<div class="printFriendly"><A HREF="' 
         + name 
         + '?pr">Printer-friendly version<BR></A></div>';
   }
   document.write(out);
   // Get it to print out again at the end
   trailingStuff += out;
}

// Turn a string into a part of a URL by changing spaces to '+', ect.
// As used, the string is part of a scripture citation, so we only worry
// about spaces and colens

function makeUrl(inString)
{
   var out = inString;
       out = out.replace( / +/g , '+');
       out = out.replace( /:/g , "%3A");
       out = out.replace( /;/g , "%3B");
       out = out.replace( /,/g , "%2C");
   return out;
}

function makeBtextUrl(citation)
{
    out =  "http://bible.oremus.org/?passage="
           + makeUrl(citation)
           + "&vnum=yes&version=nrsv";
    return out;
}

function makeBtextLink(citation, synopis, tail)
{
    if (synopis==null)
        synopis="";

    if (tail==null)
        tail = ' '
    else
        tail = ', '
        

    document.write(
        '<A TARGET="_blank" HREF="'
      + makeBtextUrl(citation)
      + '">'
      + citation
      + '</A>' + synopis + tail
    );
}
function makeBtextAnchor(citation)
{
    document.write(
        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A TARGET="_blank" HREF="'
      + makeBtextUrl(citation)
      + '">'
    );
}

function popBtextWin(citation)
{
    var win=window.open("", "_blank", "");
    var doc = win.document;
    doc.location=makeBtextUrl(citation);
}

// End a section of the newsletter, prints out a Back To Top
// link and bar
function EndSection(theEnd)
{
   WindowPrint('<p><A href="#top">Back to Top</A><br>');
   if (theEnd==null || !theEnd)
       document.write('<HR WIDTH="100%" SIZE="10" COLOR="black">');
}


// Debug method to dump an object
function Dump(o, name)
{
   if (typeof(name) == 'undefined')
      name = typeof(o);
   var s = "Dump of " + name + ":\n  ";
   s += name + " is a " + typeof(o) + '\n';

   var cnt=2;
   if ( o == null)
      s += "Null object\n";
   else if (typeof(o) == 'object')
      for (i in o )
      {
         if (cnt == 20)
         {
              s += "more to come\n";
              alert(s);
              s= "Continued dump of " + name + ":\n  ";
              cnt=0;
         }
         cnt++;
         s += i + ": " + o[i] + "\n  ";
      }
   else
      s += o;
   alert(s);
}
