/**************************************************************
 file: controls.js (for UID/Books)
 date: 08Aug08
 update: 26Jan08: added print_chapter function
 developer: Bob Lewis
 description: contains variables and functions for books:
 Note: when new books are added by other authors or with
       chapters by authors other than the editor, the 
       print_chapter function must also be updated
***************************************************************/
//alert("controls.js");
//var top_mar = '-10';

var mainWidth;
var minWidth;
var mode;
var maxPages;
var tMargin;
var tTop;
var thisTitle;

//the following parses the query string 
//and sets values for mainWidth and mode;
query_str = unescape(window.location.search.substring(1));
if(query_str.length > 0)
  {
    var qsParams = new Array();
    var params = query_str.split('&');
    if(params.length > 1)
      {
        for (var i=0; i<params.length; i++) 
          {
            var pos = params[i].indexOf('=');
            if(pos > 0) 
              {
                qsParams[i] = params[i].substr(pos+1);
              }
          }
        mode = qsParams[0];
        if(mode == undefined)
          {
            mode = "outside";
          }
        if(qsParams[1])
          {
            mainWidth = qsParams[1];
          }
        else
          {
            mainWidth = "100%"; //"95%";
          }
        if(qsParams[2])
          {
            caller = qsParams[2];
          }
       }
     else
       {
         mode = params[0];
         mainWidth = "100%"; //95%
       }
  }
else
  {
    mode = "outside";
    mainWidth = "100%";//"95%";
  }

document.write("<style>a.visited{color:gray}</style>");


var cTitle = "<div align='left' id='currentTitle' style='margin-left:20;font-family:arial;font-size:9pt;color:white'></div>"
var cChapter = "<div align='right' id='currentChapter' style='margin-right:35;font-family:arial;font-size:9pt;color:white'></div>";

nav_table = "";
nav_table += "  <tr id='topRow'>";
nav_table += "    <td style='border-bottom:3 solid "+border_color+";height:20;color:white' valign='top' width='100%' bgcolor='"+background_color+"' colspan='3'>"+cTitle+cChapter+"</td>";
nav_table += "  </tr>";
nav_table += "  <tr id='bottomRow' style='height:30;'>";
nav_table += "    <td width='13%' valign='middle' style='text-indent:0.25in;border-left:3 solid "+border_color+";border-bottom:1 solid black;background-color:white'>";
nav_table += "      <img name='decr' border='0' alt='Decrease column width' src='../images/decrease.gif' style='cursor:hand' onclick='change_width(-1)' vspace='2'>&nbsp;";
nav_table += "      <img name='incr' border='0' alt='Increase column width' src='../images/increase.gif' style='cursor:hand' onclick='change_width(1)' vspace='2'></td>";
nav_table += "    <td id='center' width='73%' valign='middle' style='border-bottom:1 solid black;background-color:white'>";
//#206060
nav_table += "      <p align='center'>";
nav_table += "      <img name='prevP' border='0' alt='Preceding page' src='../images/prev.gif' style='cursor:hand' onclick='get_prev_page()'>";
nav_table += "      &nbsp;&nbsp;&nbsp;";
nav_table += "      <img name='conteudo' border='0' alt='Table of Contents' src='../images/contents.gif' width='67' height='14' style='margin-bottom:-2;cursor:hand' onclick='go_there(\"page00.htm\")'>";
nav_table += "      &nbsp;&nbsp;&nbsp;";
nav_table += "      <img name='nextP' border='0' alt='Next page' src='../images/next.gif' style='cursor:hand' onclick='get_next_page()'></p></td>";

nav_table += "    <td width='14%' valign='center' style='border-bottom:1 solid black;background-color:white'>";
//nav_table += "      <img name='incr' border='0' alt='Increase column width' src='../images/increase.gif' style='cursor:hand' onclick='change_width(1)' vspace='1'></td>";
nav_table += "      <img name='prnt' border='0' alt='Print this chapter' src='../images/print.gif' style='cursor:hand' onclick='print_chapter()' vspace='1'></td>";
nav_table += "  </tr>";
nav_table += "  <tr>";
nav_table += "    <td style='border-left:3 solid "+border_color+";height:10px;' valign='top' width='100%' align='center' bgcolor='white' colspan='3'>&nbsp;</td>";
nav_table += "  </tr>";
nav_table += "</table>";
nav_table += "</center>";

txt = "style='margin-bottom:-21'";
txt2 = "style='margin-bottom:-13'";
myLabel = "<p "+txt+">S</p><p "+txt+">E</p><p "+txt+">A</p><p "+txt+">R</p><p "+txt+">C</p><p "+txt2+">H</p><p "+txt+">R</p><p "+txt+">E</p><p "+txt+">S</p><p "+txt+">U</p><p "+txt+">L</p><p "+txt+">T</p><p "+txt+">S";
if(typeof(caller)!="undefined") 
nav_table += "<span id='search_alert' onclick='location.href=\""+caller+"\"' style='text-align:center;cursor:hand;display:none;color:green;text-decoration:none;font-family:arial;font-size:8pt;font-weight:bold;border-right:3 solid "+border_color+";border-bottom:3 solid "+border_color+";background-color:#E1F4C6;position:absolute;top:-3;left:-0;border-collapse: collapse;height:160px;width:15px'>"+myLabel+"</span>";


var nav_top = "";
nav_top += "<center>";

if(mode == "inside")
  {
    nav_top += "<table id='navtop' border='0' cellpadding='0' cellspacing='0' style='position:absolute;top:-15px;left:10;border-collapse: collapse;' bordercolor='#FFFFFF' width='110%'>";
  }
else
  {
    nav_top += "<table id='navtop' border='0' cellpadding='0' cellspacing='0' style='position:absolute;top:0px;left:-3;border-collapse: collapse;' bordercolor='#FFFFFF' width=110%'>";
  }
  
nav_top += nav_table;

var nav_bottom = "";
nav_bottom += "<center>";
nav_bottom += "<table id='navbottom' border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'>";
nav_bottom += nav_table;

window.onresize= initialize;
function initialize()
{
  body_height = document.body.clientHeight;
  document.all.top_nav.innerHTML = nav_top;

  /*
  if(mode == "outside" || typeof(mode)== "undefined")
    {
      document.all.navtop.style.width = document.body.offsetWidth;
    }
*/
  var top = document.all.top_nav.offsetTop;
  document.all.mainDiv.style.width = "95%"; //mainWidth; //"95%";
  document.all.mainDiv.style.marginLeft = "10";
  if(mode == "inside")
    {
      document.all.navtop.style.marginLeft = "-23";
    }

  if(thisTitle == "Voices of Concern")
    {
      document.all.currentTitle.innerHTML = "<i>"+thisTitle+"</i>";
      document.all.currentChapter.innerHTML = thisChapter+"&nbsp;&nbsp;"+thisAuthor;;
    }
  else 
    {
      document.all.currentTitle.innerHTML = "<i>"+thisTitle+"</i>&nbsp;&nbsp;by&nbsp;"+thisAuthor;
      document.all.currentChapter.innerHTML = thisChapter;

    }

  document.all.currentTitle.style.position = "relative";
  document.all.currentTitle.style.top = "7";
  document.all.currentTitle.style.fontWeight = "bold";

  document.all.currentChapter.style.position = "relative";
  document.all.currentChapter.style.bottom = "8";
  document.all.currentChapter.style.fontWeight = "bold";

  if(document.all.authorInfo)
    {
      document.all.authorInfo.style.marginTop = "40";
    }

  //adjustments to formatting for each book
  hFontSize = 20; //default headline font size

  if(thisTitle == "Thoughts on Unity")
    {
      maxPages = 22;
      tMargin = 25;      
    }
  if(thisTitle == "Renewal Through Recovery")
    {
      maxPages = 11;
      tMargin = 25;      
    }
  if(thisTitle == "Voices of Concern")
    {
      maxPages = 20;
      if(document.all.authorInfo)
        {
          document.all.authorInfo.style.marginTop = "50";
        }
      tMargin = 35;
    }
  if(thisTitle == "The Twisted Scriptures")
    {
      maxPages = 14;
      tMargin = 25;      
    }
  if(thisTitle == "The Death of the Custodian")
    {
      maxPages = 14;
      tMargin = 25;
    }
  if(thisTitle == "Heaven Help Us")
    {
      maxPages = 11;
    }
  if(thisTitle == "Pilgrimage of Joy")
    {
      maxPages = 8;
      tMargin = 65;      
    }
  if(thisTitle == "The Royal Priesthood")
    {
      maxPages = 20;
      tMargin = 65;      
    }
  if(thisTitle == "Adventure of Faith")
    {
      maxPages = 15;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "Colony of Heaven")
    {
      maxPages = 5;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "In the Beginning")
    {
      maxPages = 22;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "The Kingdom of the Messiah")
    {
      maxPages = 17;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "Simple Trusting Faith")
    {
      maxPages = 11;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "Talks to Jews and Non-Jews")
    {
      maxPages = 17;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "According to the Pattern")
    {
      maxPages = 11;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "A New Spirit")
    {
      maxPages = 19;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "The Parable of Telstar and Other Talks")
    {
      maxPages = 17;
      tMargin = 65;
      hFontSize = 18;
    }
  if(thisTitle == "The Holy Spirit in Our Lives Today")
    {
      maxPages = 10;
      tMargin = 65;
      hFontSize = 18;
    }


  hLines = document.getElementsByTagName("h1");
    if(hLines.length > 0 && tMargin > 0)
      {
        hLines[0].style.marginTop = tMargin;
        hLines[0].style.fontSize = hFontSize+"pt";
        if(thisTitle == "The Royal Priesthood")
          {
            hLines[1].style.fontSize = hFontSize+"pt";
          }
        if(thisTitle == "Adventure of Faith" || thisTitle == "The Kingdom of the Messiah")
          {
            if(hLines[1])
              {
                hLines[1].style.fontSize = hFontSize+"pt";
              }
          }
      }

  if(mode == "inside")
    {
      minWidth = "59";   
      maxWidth = "95";   
    }
  else
    {
      minWidth = "47";
      maxWidth = "95";
    }

  manage_nav_buttons();

  //if(caller.length > 0) 
  if(typeof(caller)!="undefined") 
    {
      document.all.search_alert.style.display = "block";
  //tTop = document.all.navtop.offsetHeight-3;
  tLeft = document.all.navtop.offsetLeft+3;

  tTop = document.all.topRow.offsetHeight-15+2;
  document.all.search_alert.style.top = tTop; //navtop is positioned at -13; the border is 3px
  document.all.search_alert.style.left = tLeft;
    }

}

function manage_nav_buttons()
{

  var str = window.location.href
  tmpArr = str.split("?");
  pageNo = parseInt(str.substr(tmpArr[0].length-6,2),10);
  
  if(pageNo == 0)
    {
      imgSrc = document.all.conteudo.src;              
      document.all.conteudo.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
      document.all.conteudo.style.cursor = "default";
    }

  if(pageNo > 0 && document.all.conteudo.src.indexOf('disabled') >= 0)
    {
      tSrc = document.all.conteudo.src
      document.all.conteudo.src = tSrc = tSrc.substr(0,tSrc.length-13)+".gif";
      document.all.conteudo.style.cursor = "hand";
    }
    
  //DISABLE NEXTP
  if(pageNo == maxPages)
    {
      imgSrc = document.all.nextP.src;              
      document.all.nextP.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
      document.all.nextP.style.cursor = "default";
    }
  //ENABLE NEXTP
  else if(document.all.nextP.src.indexOf('disabled') >= 0)
    {
      tSrc = document.all.nextP.src
      document.all.nextP.src = tSrc = tSrc.substr(0,tSrc.length-13)+".gif";
      document.all.nextP.style.cursor = "hand";
    }
  //DISABLE INCR
  if(document.all.mainDiv.style.width == maxWidth+"%")
    {
      imgSrc = document.all.incr.src;     
      document.all.incr.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
      document.all.incr.style.cursor = "default";
    }
  //DISABLE DECR
  if(document.all.mainDiv.style.width == minWidth+"%")
    {
      imgSrc = document.all.decr.src;              
      document.all.decr.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
      document.all.decr.style.cursor = "default";
    }
}


function test()
{
  if(mode == "inside")
    {
      init();
    }
}


function get_next_page()
{
  var str = window.location.href
  tmpArr = str.split("?");

  params = get_params();
  num = parseInt(str.substr(tmpArr[0].length-6,2),10);
  if(num < maxPages)
	{
	num++;
	if(num < 10)
		{
		  location.href = "page0"+num+".htm?"+params;
		}
	else
		{
		  location.href = "page"+num+".htm?"+params;
		}
	}
}

function get_prev_page()
{
  var str = window.location.href
  tmpArr = str.split("?");

  params = get_params();
  num = parseInt(str.substr(tmpArr[0].length-6,2),10);
  num--;
  if(num < 10)
    {
      if(num < 0)
        {
          location.href = "index.htm?"+params;
        }
      else
        {
          location.href = "page0"+num+".htm?"+params;
        }
    }
  else
    {
      location.href = "page"+num+".htm?"+params;
    }
}

function change_width(digit)
{
  obj = document.all.mainDiv;
  theWidth = parseInt(document.all.mainDiv.style.width);

  if(digit > 0) //increase
    {
      if(theWidth < maxWidth)
        {
          obj.style.width = theWidth+12+"%";
          mainWidth = obj.style.width;

         //DISABLE INCR
         if(obj.style.width == maxWidth+"%")
           {
             imgSrc = document.all.incr.src;     
             document.all.incr.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
             document.all.incr.style.cursor = "default";
           }
         //RESTORE DECR
         if(document.all.decr.src.indexOf('disabled') >= 0)
           {
             tSrc = document.all.decr.src
             document.all.decr.src = tSrc = tSrc.substr(0,tSrc.length-13)+".gif";
             document.all.decr.style.cursor = "hand";
           }
        }
    }
  else //decrease
    {
      if((theWidth <= 95) && (theWidth > minWidth))
        {
          obj.style.width = theWidth-12+"%";
          mainWidth = obj.style.width;

          //DISABLE DECR
          if(obj.style.width == minWidth+"%")
            {
              imgSrc = document.all.decr.src;              
              document.all.decr.src = imgSrc.substr(0,imgSrc.length-4)+"_disabled.gif"; ///HERE
              document.all.decr.style.cursor = "default";
            }

          //RESTORE INCR
          if(document.all.incr.src.indexOf('disabled') >= 0)
            {
              tSrc = document.all.incr.src
              document.all.incr.src = tSrc = tSrc.substr(0,tSrc.length-13)+".gif";
              document.all.incr.style.cursor = "hand";
            } 
        }
    }

   rHeight = document.all.rightDiv.clientHeight;
   lHeight = document.all.leftDiv.clientHeight;

   //adjust leftDiv height if needed
   if(rHeight > lHeight)
     {
       document.all.leftDiv.style.height = rHeight;
     }


}

window.onscroll = function()
{
var str = window.location.href
pos = str.indexOf('index.htm');
if(pos < 0)
  {
    var oNavBar = document.getElementById("navtop");
    var oSearchBar = document.getElementById("search_alert");
    if(mode == "inside")
      {
        oNavBar.style.top = document.body.scrollTop-15;
        if(typeof(caller)!="undefined") 
          {
            oSearchBar.style.top = document.body.scrollTop+tTop; //-31;
          }
      }
    else
      {
        oNavBar.style.top = document.body.scrollTop;
      }
  }
}

function go_there(page)
{
  params = get_params();
  location.href = page+"?"+params;
}

function get_params()
{
  if(typeof(caller)!="undefined") 
    params = "mode="+mode+"&width="+mainWidth+"&caller="+caller;
  else
    params = "mode="+mode+"&width="+mainWidth
  return params;
}

//added on 26Feb08
//creates a tChapter variable for pasting to an new window;
//a title tag is added with the chapter_title;
//a body tag is added with onload event handlers: window.print and window.close;
//the innerHTML of the includeone span is added;
//a link is added to the bottom of the file with chapter's URL
//the window is opened; content is written to the window; the window is reloaded;
//the window openes with the chapter and the print window opens;
//as soon as the user prints the chapter, the onload event executes the window.close method;
function print_chapter()
{
  var titleChapter = thisTitle+": "+thisChapter;
  if(thisTitle == "Voices of Concern")
    {
      bookAuthor = "Robert Meyers, Editor"; 
    }
  else
    {
      bookAuthor = thisAuthor;
    }
  str = location.href;
  tChapter = "<head><title>"+titleChapter+"</title>"
  tChapter += "<script>function hide_navBar(){document.all.top_nav.style.display = 'none';}</script></head>";
 
  tChapter += "<body onload='hide_navBar();window.print(\"myfile.htm\");window.close()'>";
  tChapter += "<div style='font-size:12pt;color:#C0C0C0'><i>"+thisTitle+"</i> by "+bookAuthor+"</div>";
  tChapter += document.all.mainDiv.innerHTML; //document.all.includeone.innerHTML;
  
  tChapter += "<pre><b><p>Downloaded on "+get_dateToday()+" from:<br><a href='"+str+"'>"+str+"</pre></p>";
  window.clipboardData.setData("Text", tChapter);
  var myWindow = window.open("","myWin");
  myWindow.document.write(tChapter);
  myWindow.location.reload(); //needed to fire the onload event within new window; since window is already open prior to insert;
}

function get_dateToday()
{
var d=new Date();
var theYear = d.getYear(); //2006
var theDate = d.getDate(); //day of month
var monthNum = d.getMonth(); //month: 0-11
var dateToday = (monthNum+1)+"/"+theDate+"/"+theYear;
return dateToday;
}