// xLibrary scripts: credit - Michael Foster - http://www.cross-browser.com
// Covered by the GNU Lesser public license:

// All other code, Copyright 2006 © The Restored Church of God. All Rights Reserved.

function attach_file( strURL ) {
  script = document.createElement( 'script' );
  script.src = strURL;
  document.getElementsByTagName( 'head' )[0].appendChild( script );
}

function CreateXMLHttp() {
  if (typeof XMLHttpRequest != "undefined") {
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    var Versions = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp" ];
    for (var i=0; i<Versions.length; i++) {
      try {
        var objXmlHttp = new ActiveXObject(Versions[i]);
        return objXmlHttp;
      } catch (oError) {
        // Throw away
      }
    }
  }
}

function AjaxReadVerse(strURL, strWinID)
{
  var Ajax = CreateXMLHttp();
  Ajax.open("get", strURL, true);
  Ajax.onreadystatechange = function()
  {
    if (Ajax.readyState == 4)
    {
      if (Ajax.status == 200)
      {
        WriteContent(strWinID, Ajax.responseText);
        InitSlick(strWinID);
        }
      }
    };
  Ajax.send(null);
}

function AjaxNextVerse(strURL, strWinID)
{
  var Ajax = CreateXMLHttp();
  Ajax.open("get", strURL, true);
  Ajax.onreadystatechange = function()
  {
    if (Ajax.readyState == 4)
    {
      if (Ajax.status == 200)
      {
        var resultArr = Ajax.responseText.split("|");
        RefreshTitle(strWinID, resultArr[0], resultArr[1], resultArr[2]);
        WriteContent(strWinID, resultArr[3]);
      }
    }
  };

  Ajax.send(null);
}

function AjaxLoadArchive(strURL, divTo) {
  var Ajax = CreateXMLHttp();
  Ajax.open("get", strURL, true);
  Ajax.onreadystatechange = function() {
    if (Ajax.readyState == 4) {
      if (Ajax.status == 200) {
        WriteContent(divTo, Ajax.responseText);
      }
    }
  };

  Ajax.send(null);
}

// ** INCLUDES **
// note: requires x_code, x_event and x_drag to be included first
var strEnv = '';
if (env && env != 'prod') {
  strEnv = 'env='+env+'&';
}

// initialize
var highZ = 3;
var lngX = 0;
var lngY = 0;
xAddEventListener(document, 'mousemove', CaptureXY, false);
function CaptureXY(e) {
  var evt = new xEvent(e);
  lngX = evt.pageX;
  lngY = evt.pageY;
}


// ** FUNCTIONS **
// create window
function CreateWindow(x,y,strID,title,content) {
  // -- Create outer Window ---------
  var divWin=document.createElement('div');
  divWin.id = strID;
  divWin.className = 'verseBoxWrapper';
  // --------------------------------

  // --
  // Close
  var aClose=document.createElement('a');
  aClose.id = strID + 'CloseBtn';
  aClose.className = 'closeVerse close';
  aClose.innerHTML = 'Close Window';
  aClose.title = 'Close';
  aClose.href = '#';
  divWin.appendChild(aClose);

  // --
  // verseBox
  var divVerseBoxWin=document.createElement('div');
  divVerseBoxWin.id = strID + 'VerseBox';
  divVerseBoxWin.className = 'verseBox';
  divWin.appendChild(divVerseBoxWin);

  // --
  // Title Bar
  var divBar = document.createElement('h6');
  divBar.id = strID + 'Bar';
  divBar.innerHTML = title;
  divVerseBoxWin.appendChild(divBar);

  // --
  // -- div Navigation
  var divSlicknav = document.createElement('div');
  divVerseBoxWin.appendChild(divSlicknav);

  // --
  // -- ul Navigation
  var ulSlicknav = document.createElement('ul');
  divSlicknav.appendChild(ulSlicknav);

  // ---------------------------------------------------
  // --
  // Previous Chapter
  var liPreviousChapter = document.createElement('li');
  ulSlicknav.appendChild(liPreviousChapter);

  var aPreviousChapter=document.createElement('a');
  aPreviousChapter.id = strID + 'PreviousChapterBtn';
  aPreviousChapter.title = 'Previous Chapter';
  aPreviousChapter.href = '#';
  aPreviousChapter.disabled = false;
  aPreviousChapter.className = 'scriptBtnPreviousChapter';
  liPreviousChapter.appendChild(aPreviousChapter);

  var imgPreviousChapter = document.createElement('img');
  imgPreviousChapter.id = strID + 'PreviousChapterImg';
  imgPreviousChapter.alt = 'first';
  imgPreviousChapter.src = '/images/icon_first.gif';
  aPreviousChapter.appendChild(imgPreviousChapter);

  // ---------------------------------------------------
  // --
  // Previous Verse
  var liPrevious = document.createElement('li');
  ulSlicknav.appendChild(liPrevious);

  var aPrevious=document.createElement('a');
  aPrevious.id = strID + 'PreviousBtn';
  aPrevious.title = 'Previous Verse';
  aPrevious.href = '#';
  aPrevious.disabled = false;
  aPrevious.className = 'scriptBtnPreviousVerse';
  liPrevious.appendChild(aPrevious);

  var imgPrevious = document.createElement('img');
  imgPrevious.id = strID + 'PreviousImg';
  imgPrevious.alt = 'previous';
  imgPrevious.src = '/images/icon_previous.gif';
  aPrevious.appendChild(imgPrevious);

  // ---------------------------------------------------
  // --
  // Next Verse
  var liNext = document.createElement('li');
  ulSlicknav.appendChild(liNext);

  var aNext=document.createElement('a');
  aNext.id = strID + 'NextBtn';
  aNext.title = 'Next Verse';
  aNext.href = '#';
  aNext.disabled = false;
  aNext.className = 'scriptBtnNextVerse';
  liNext.appendChild(aNext);

  var imgNext = document.createElement('img');
  imgNext.id = strID + 'NextImg';
  imgNext.alt = 'next';
  imgNext.src = '/images/icon_next.gif';
  aNext.appendChild(imgNext);

  // ---------------------------------------------------
  // --
  // Next Chapter
  var liNextChapter = document.createElement('li');
  ulSlicknav.appendChild(liNextChapter);

  var aNextChapter=document.createElement('a');
  aNextChapter.id = strID + 'NextChapterBtn';
  aNextChapter.className = 'last';
  aNextChapter.title = 'Next Chapter';
  aNextChapter.href = '#top';
  aNextChapter.disabled = false;
  aNextChapter.className = 'scriptBtnNextChapter';
  liNextChapter.appendChild(aNextChapter);

  var imgNextChapter = document.createElement('img');
  imgNextChapter.id = strID + 'NextChapterImg';
  imgNextChapter.alt = 'last';
  imgNextChapter.src = '/images/icon_last.gif';
  aNextChapter.appendChild(imgNextChapter);

  // --------------------------------------------------
  // --
  // Content Area
  var pContent = document.createElement('p');
  pContent.id = strID + 'Content';
  pContent.innerHTML = content;
  pContent.Tag = strID;
  pContent.setAttribute("style", "clear:both;");
  divVerseBoxWin.appendChild(pContent);

  // --
  // Footer Bar
  var divFooter = document.createElement('div');
  divFooter.id = strID + 'Footer';
  divFooter.className = 'winFooterBar';

  // --
  // Assemble Window Components --
  //divWin.appendChild(divFooter);
  //divWin.appendChild(aClose);
  //divWin.appendChild(divResize);

  // --------------------------------

  // !! Attach Window to Document !!
  document.getElementsByTagName('body')[0].appendChild(divWin);
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  // Set Up Window
  xMoveTo(divWin,x,y);

  // MARK xResizeTo(divContent, xWidth(divContent), xHeight(divWin)-xHeight(divFooter)-xHeight(divBar));

  // Attach Drag Scripts
  xEnableDrag(strID + 'Bar', SubDivOnDragStart, BarOnDrag, null);
  xEnableDrag(strID, SubDivOnDragStart, BarOnDrag, null);
  xEnableDrag(strID + 'VerseBox', SubDivOnDragStart, BarOnDrag, null);
  //xEnableDrag(strID + 'ResizeBtn', SubDivOnDragStart, ResizeOnDrag, null);

  // --
  // Attach Actions
  //divMax.onclick = MaxBtnOnClick;
  aClose.onclick = WinClose;
  aPreviousChapter.onclick = PreviousChapter;
  aPrevious.onclick = PreviousVerse;
  aNext.onclick = NextVerse;
  aNextChapter.onclick = NextChapter;
  //divMore.onclick = MoreClick;

  // !! Show !!
  xShow(divWin);

  return divWin;
}

function ViewIMG(strID, strFilename, intWidth, intHeight, title, strSource, strCaption) {
  divWin = xGetElementById(strID);
  if (divWin) {
    // This makes window appear at mouse location
    lngToX = lngX - 15 - (intWidth / 3);
    lngToY = lngY - 8 - (intHeight / 3);
    xShow(divWin);
    xMoveTo(divWin,lngToX, lngToY);
    return;
  }

  divWin=document.createElement('div');
  divWin.id = strID;
  divWin.className = 'winIMGBox';

  var divClose=document.createElement('div');
  divClose.id = strID + 'CloseBtn';
  divClose.className = 'winBtn';
  divClose.innerHTML = 'X';
  divClose.title = 'Close';

  // Content Area
  var divContent = document.createElement('div');
  divContent.id = strID + 'Content';
  divContent.className = 'winIMGContent';
  divContent.innerHTML = '<img src="' + strFilename + '" width="' + intWidth + '" height="' + intHeight + '" />';
  divContent.Tag = strID;

  divWin.width = intWidth;

  // --
  // Title Bar
  var divBar = document.createElement('div');
  divBar.id = strID + 'Bar';
  divBar.className = 'winBar';
  if (title != '') {
    divBar.innerHTML = title;
  } else {
    divBar.innerHTML = '&nbsp;';
  }

  if (strCaption != '') {
    var pCaption = document.createElement('p');
    pCaption.className = "caption";
    pCaption.innerHTML = strCaption;
    divContent.appendChild(pCaption);
  }

  if (strSource != '') {
    var pSource = document.createElement('p');
    pSource.className = "source";
    pSource.innerHTML = strSource;
    divContent.appendChild(pSource);
  }

  // Footer Bar
  var divFooter = document.createElement('div');
  divFooter.id = strID + 'Footer';
  divFooter.className = 'winFooterBar';

  divWin.appendChild(divClose);
  divWin.appendChild(divBar);
  divWin.appendChild(divContent);
  divWin.appendChild(divFooter);

  // !! Attach Window to Document !!
  document.getElementsByTagName('body')[0].appendChild(divWin);

  // This makes window appear at mouse location
  lngToX = lngX - 15 - (intWidth / 3);
  lngToY = lngY - 8 - (intHeight / 3);

  //xResizeTo(divContent, intWidth, intHeight + 150);

  // Set Up Window
  xMoveTo(divWin,lngToX,lngToY);

  intWidth = parseInt(intWidth) + 9;
  divWin.style.width = intWidth + 'px';

  xEnableDrag(strID + 'Bar', SubDivOnDragStart, BarOnDrag, null);
  //xEnableDrag(strID, SubDivOnDragStart, BarOnDrag, null);

  // Attach Close Script
  divClose.onclick = WinClose;

  // !! Show !!
  xShow(divWin);

  return divWin;
}

function SubDivOnDragStart(ele, mx, my) {
  xZIndex(ele.id, highZ++);
}

function BarOnDrag(ele, mdx, mdy) {
  strID = ele.parentNode.parentNode.id;
  xMoveTo(strID, xLeft(strID) + mdx, xTop(strID) + mdy);
}

function ResizeOnDrag(ele, mdx, mdy) {
  strID = ele.parentNode.id;
  xResizeTo(strID, xWidth(strID) + mdx, xHeight(strID) + mdy);
}

function MaxBtnOnClick() {
  strID = this.parentNode.id;
  var div = xGetElementById(strID);
  if (div.maximized) {
    div.maximized = false;
    this.title = 'Maximize';
    xResizeTo(div, div.prevW, div.prevH);
    xMoveTo(div, div.prevX, div.prevY);
  } else {
      this.title = 'Restore';
      div.prevW = xWidth(div);
      div.prevH = xHeight(div);
      div.prevX = xLeft(div);
      div.prevY = xTop(div);
      xMoveTo(div, xScrollLeft(), xScrollTop());
      div.maximized = true;
      xResizeTo(div, xClientWidth(), xClientHeight());
  }
}

function WinClose() {
  var divWin = xGetElementById(this.parentNode.id);
  xHide(divWin);
  return false;
}

function ReEnable(strWin) {
  divWin = xGetElementById(strWin);
  divWin.disabled = false;
}

function PreviousChapter() {
  if (this.disabled == false) {
    this.disabled = true;
    setTimeout("ReEnable('" + this.id + "')", 600);
    var strWin = this.parentNode.parentNode.parentNode.parentNode.parentNode.id
    var divWin = xGetElementById(strWin);
    strBook = divWin.strBook;
    strChapter = divWin.strChapter
    strVerse = divWin.strVerse;
    AjaxNextVerse('/php/previouschapter.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWin);
  }
  return false;
}

function PreviousVerse() {
  if (this.disabled == false) {
    this.disabled = true;
    setTimeout("ReEnable('" + this.id + "')", 600);
    var strWin = this.parentNode.parentNode.parentNode.parentNode.parentNode.id
    var divWin = xGetElementById(strWin);
    strBook = divWin.strBook;
    strChapter = divWin.strChapter
    strVerse = divWin.strVerse;
    AjaxNextVerse('/php/previousverse.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWin);
  }
  return false;
}

function NextVerse() {
  if (this.disabled == false) {
    this.disabled = true;
    setTimeout("ReEnable('" + this.id + "')", 600);
    var strWin = this.parentNode.parentNode.parentNode.parentNode.parentNode.id
    var divWin = xGetElementById(strWin);
    strBook = divWin.strBook;
    strChapter = divWin.strChapter
    strVerse = divWin.strVerse;
    AjaxNextVerse('/php/nextverse.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWin);
  }
  return false;
}

function NextChapter() {
  if (this.disabled == false) {
    this.disabled = true;
    setTimeout("ReEnable('" + this.id + "')", 600);
    var strWin = this.parentNode.parentNode.parentNode.parentNode.parentNode.id
    var divWin = xGetElementById(strWin);
    strBook = divWin.strBook;
    strChapter = divWin.strChapter
    strVerse = divWin.strVerse;
    AjaxNextVerse('/php/nextchapter.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWin);
  }
  return false;
}

function MoreClick() {
  //alert('More was clicked!');
}

var slickWindowReady = false;
YAHOO.util.Event.onDOMReady(function(){
  slickWindowReady = true;
});
function slickWindow(strBook,strChapter,strVerse) {
  if (!slickWindowReady) {
    return;
  }
  strID = strBook + '-' + strChapter + '-' + strVerse;
  strWinID = 'Win' + strID;

  if (strVerse == 'ALL') {
    strDisplay = strBook + ' ' + strChapter + ' (KJV)';
  } else {
    strDisplay = strBook + ' ' + strChapter + ':' + strVerse + ' (KJV)';
  }

  // This makes window appear at mouse location
  lngToX = lngX - 15;
  lngToY = lngY - 8;

  /*
  divTarget = xGetElementById(strID);
  lngToX = xLeft(divTarget);
  lngToY = xTop(divTarget);
  */

  var objWin = xGetElementById(strWinID);
  if (!objWin) {
    objWin = CreateWindow(lngToX, lngToY, strWinID, strDisplay, 'Loading...');

    // basically storing as variables (who wants to use a class, anyway? :P)
    objWin.strBook = strBook;
    objWin.strChapter = strChapter;
    objWin.strVerse = strVerse;

    AjaxReadVerse('/php/readverse.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWinID);
  } else {
    xShow(objWin);
    xMoveTo(objWin,lngToX, lngToY);
    objWin.strBook = strBook;
    objWin.strChapter = strChapter;
    objWin.strVerse = strVerse;

    AjaxReadVerse('/php/readverse.php?'+strEnv+'strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse, strWinID);

    RefreshTitle(strWinID, strBook, strChapter, strVerse);
  }

  lngDocWidth = xWidth(document);
  lngWinWidth = xWidth(objWin);
  lngWinHeight = xHeight(objWin);

  if (lngToX > (lngDocWidth - lngWinWidth)) {
    xMoveTo(objWin, lngDocWidth-lngWinWidth, lngToY);
  }

//  attach_file('/php/readverse.php?'+strEnv+'strWin=' + strWinID + '&strBook=' + strBook + '&strChapter=' + strChapter + '&strVerse=' + strVerse);
}

function WriteContent(strWinID, strContent) {
  divVerseBoxWin = xGetElementById(strWinID + 'VerseBox');
  divContent = xGetElementById(strWinID + 'Content');
  divContent.innerHTML = strContent;

  // remove static height class ('scroll'), and then measure height, add scroll class if needed & add an extra new line for whitespace
  divContent.parentNode.className = 'verseBox';
  pHeight = divContent.scrollHeight;
  if ( pHeight > 126 ) { // 6 lines: 110, 7 lines: 126
    divContent.innerHTML = strContent + '<br />&nbsp;';
    divContent.parentNode.className = 'verseBox scroll';
  } else {
    divContent.innerHTML = strContent;
    divContent.parentNode.className = 'verseBox';
  }

  // Scroll to top of p
  divContent.scrollTop = 0;
}

function InitSlick(strID) {
  objWin = xGetElementById(strID);
  objBar = xGetElementById(strID + 'Bar');
  objFooter = xGetElementById(strID + 'Footer');
  objContent = xGetElementById(strID + 'Content');
  //xResizeTo(objWin, xWidth(objWin), xHeight(objContent) + xHeight(objBar) + xHeight(objFooter));
  //xResizeTo(objWin, xWidth(objWin), xHeight(objWin));
}

function RefreshTitle(strID, strBook, strChapter, strVerse) {
  objWin = xGetElementById(strID);
  objWin.strBook = strBook;
  objWin.strChapter = strChapter;
  objWin.strVerse = strVerse;
  objBar = xGetElementById(strID + 'Bar');

  if (objWin.strVerse == 'ALL') {
    objBar.innerHTML = objWin.strBook + ' ' + objWin.strChapter + ' (KJV)';
  } else {
    objBar.innerHTML = objWin.strBook + ' ' + objWin.strChapter + ':' + objWin.strVerse + ' (KJV)';
  }
}


