// Connection library: connect_entry.js   beta version 0.6  June 2000
// 
// Copyright (C) 2003 Jean-Hugues Rety and Robert Kendall
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public 
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//
// This library provides javascript tools for 
// hypertext literary writing in html.
//
// Important: this version is a prototype, functionalities may be 
// modified in future versions.
//
// Documentation: http://www.wordcircuits.com/connect
//
// e-mail: kendall@wordcircuits.com
//
//
// You can freely use and distribute this library provided that 
// this notice remains included. 
// (c) 2000 Robert Kendall and Jean-Hugues Rety.
//

// The parameter below refers to the first node of the hypertext:
if (typeof(firstNode) == "undefined")
  var firstNode = "javascript:void(0)";

if (typeof(connectWin) == "undefined")
 var connectWin = window;

if (typeof(outputWin) == "undefined")
 var outputWin = window;

function h_thisFile ()
// Returns the file name of the outputWin window location
{
var url = outputWin.location.href;
var i = url.lastIndexOf (connectWin.baseDirectory);
return (url.substring (i + connectWin.baseDirectory.length, url.length));
}


function displayHist()
{
var h = connectWin.histArray();
var text = "";
for (var i=0 ; i < h.length ; i++)
  {text = h[i];
   if (typeof(connectWin.titleArray[h[i]]) != "undefined")
     text += " - " + connectWin.titleArray[h[i]];
   outputWin.document.write("  " + (i+1) + ": " + text + "<BR>");
  }
}

function popHist()
{
var offset = connectWin.h_currHist.lastIndexOf("*", connectWin.h_currHist.length -2);
connectWin.h_currHist = connectWin.h_currHist.substring(0, offset +1);
if (connectWin.h_currHist == "") 
  {
  connectWin.h_deleteCookie (connectWin.histCookieName + connectWin.h_nbCookies.toString());
  connectWin.h_nbCookies--;
  connectWin.h_setCookie(connectWin.histCookieName + 'Nb', connectWin.h_nbCookies.toString());
  connectWin.h_hist = connectWin.histValue();
  connectWin.h_currHist = connectWin.h_cookieVal (connectWin.histCookieName + connectWin.h_nbCookies.toString() );
  connectWin.h_histEmpty = (connectWin.h_hist == "" || connectWin.h_hist == "*");
  }
else
  {
  connectWin.setCurrHist (connectWin.h_currHist);
  connectWin.h_hist = connectWin.histValue();
  connectWin.h_histEmpty = (connectWin.h_hist == "" || connectWin.h_hist == "*");
  }
}

 function h_thisDirectory ()
 {var url = outputWin.location.href;
 return (url.substring (0,url.lastIndexOf("/")));
 }
 
 function h_makeUrl(nnode)
 {if (nnode == 'javascript:void(0)')
    return (nnode);
  if (nnode.substring(nnode.lastIndexOf('/'),nnode.length).indexOf('.') == -1)
    nnode += connectWin.h_filesExtComp;
  if (nnode.charAt(0) == '/')
   if (connectWin.baseDirectory != '/') 
     return(connectWin.baseDirectory + nnode);
   else
     return(nnode.substring(1,nnode.length));
  else
    return(h_thisDirectory() + '/' + nnode);
 }

var bookmarkMessage = "This work uses Word Circuits <a href='http://wordcircuits.com/connect'>Connection Muse</a><br> to save your place in the reading.<br>You can return to this page anytime<br>to resume reading exactly where you left off.";

function soWhat() 
{
var l_h_hist = connectWin.h_hist;
var offsetr = l_h_hist.lastIndexOf("*", l_h_hist.length -2);
var targetr = connectWin.makeFileName(l_h_hist.substring(offsetr +1, l_h_hist.length -1));
outputWin.document.write("<table border='0' align='center' cellpadding='10' cellspacing='0'><tr><td>");
outputWin.document.write("You have returned to a reading in progress. Choose an option:<BR><BR>"); 
outputWin.document.write("<UL><LI><A href='" + h_makeUrl('/' + targetr) + "' onclick='popHist();'>Resume the reading where you left off</A></LI>");
outputWin.document.write("<LI><A href='" + h_makeUrl(firstNode) + "' onclick='connectWin.deleteHist();'>Start over and delete the current history</A></LI></UL><BR><BR>");
outputWin.document.write("Below is the current history:<BR><BR>");
displayHist();
outputWin.document.write("<br><br>" + bookmarkMessage)
outputWin.document.write("</td></tr></table>");
}

function comeIn()
{
outputWin.document.write("<table border='0' align='center' cellpadding='10' cellspacing='0'><tr><td>");
outputWin.document.write("<CENTER><A href='" + h_makeUrl(firstNode) + "'>Begin</A></CENTER>");
outputWin.document.write("<br><br>" + bookmarkMessage)
outputWin.document.write("</td></tr></table>");
}

function entry()
{
if (! connectWin.histEmpty())
  {var h_cur = connectWin.h_currHist;
  var offset = h_cur.lastIndexOf("*", h_cur.length -2);
  var target = connectWin.makeFileName(h_cur.substring(offset +1, h_cur.length -1));
  if (target == h_thisFile())
    popHist();
  }
if (connectWin.histEmpty()) comeIn();
else soWhat();
}

















