var changesStyle = "editor.css";
var changesFilter = "all.css";
var trackPageChanges = true;
var checkPageConsistency = true;
var useAccessKeys = true;
var addCorrectionsToTop = true;

var bookCode = "";

var invalidMode   = -1;
var bookMode      = 0;
var changesMode   = 1;
var graphMode     = 2;
var downloadsMode = 3;
var optionsMode   = 4;
var helpMode      = 5;

var lastMode = -1;

var bookLocations = new Object( );
bookLocations[ "01fftd" ]  = "xhtml/lw/01fftd";
bookLocations[ "02fotw" ]  = "xhtml/lw/02fotw";
bookLocations[ "03tcok" ]  = "xhtml/lw/03tcok";
bookLocations[ "04tcod" ]  = "xhtml/lw/04tcod";
bookLocations[ "05sots" ]  = "xhtml/lw/05sots";
bookLocations[ "06tkot" ]  = "xhtml/lw/06tkot";
bookLocations[ "07cd" ]    = "xhtml/lw/07cd";
bookLocations[ "08tjoh" ]  = "xhtml/lw/08tjoh";
bookLocations[ "09tcof" ]  = "xhtml/lw/09tcof";
bookLocations[ "10tdot" ]  = "xhtml/lw/10tdot";
bookLocations[ "11tpot" ]  = "xhtml/lw/11tpot";
bookLocations[ "12tmod" ]  = "xhtml/lw/12tmod";
bookLocations[ "13tplor" ] = "xhtml/lw/13tplor";
bookLocations[ "14tcok" ]  = "xhtml/lw/14tcok";
bookLocations[ "15tdc" ]   = "xhtml/lw/15tdc";
bookLocations[ "16tlov" ]  = "xhtml/lw/16tlov";
bookLocations[ "17tdoi" ]  = "xhtml/lw/17tdoi";
bookLocations[ "18dotd" ]  = "xhtml/lw/18dotd";
bookLocations[ "19wb" ]    = "xhtml/lw/19wb";
bookLocations[ "20tcon" ]  = "xhtml/lw/20tcon";
bookLocations[ "21votm" ]  = "xhtml/lw/21votm";
bookLocations[ "22tbos" ]  = "xhtml/lw/22tbos";
bookLocations[ "23mh" ]    = "xhtml/lw/23mh";
bookLocations[ "24rw" ]    = "xhtml/lw/24rw";
bookLocations[ "25totw" ]  = "xhtml/lw/25totw";
bookLocations[ "26tfobm" ] = "xhtml/lw/26tfobm";
bookLocations[ "27v" ]     = "xhtml/lw/27v";
bookLocations[ "28thos" ]  = "xhtml/lw/28thos";
bookLocations[ "01gstw" ]  = "xhtml/gs/01gstw";
bookLocations[ "02tfc" ]   = "xhtml/gs/02tfc";
bookLocations[ "03btng" ]  = "xhtml/gs/03btng";
bookLocations[ "04wotw" ]  = "xhtml/gs/04wotw";
bookLocations[ "01hh" ]    = "xhtml/fw/01hh";
bookLocations[ "02smr" ]   = "xhtml/fw/02smr";
bookLocations[ "03oz" ]    = "xhtml/fw/03oz";
bookLocations[ "04cc" ]    = "xhtml/fw/04cc";
bookLocations[ "mc" ]      = "xhtml/misc/mc";
bookLocations[ "rh" ]      = "xhtml/misc/rh";

var bookPublished = new Object( );
bookPublished[ "01fftd" ]  = true;
bookPublished[ "02fotw" ]  = true;
bookPublished[ "03tcok" ]  = true;
bookPublished[ "04tcod" ]  = true;
bookPublished[ "05sots" ]  = true;
bookPublished[ "06tkot" ]  = true;
bookPublished[ "07cd" ]    = true;
bookPublished[ "08tjoh" ]  = true;
bookPublished[ "09tcof" ]  = true;
bookPublished[ "10tdot" ]  = true;
bookPublished[ "11tpot" ]  = true;
bookPublished[ "12tmod" ]  = true;
bookPublished[ "13tplor" ] = true;
bookPublished[ "14tcok" ]  = true;
bookPublished[ "15tdc" ]   = true;
bookPublished[ "16tlov" ]  = true;
bookPublished[ "17tdoi" ]  = false;
bookPublished[ "18dotd" ]  = false;
bookPublished[ "19wb" ]    = false;
bookPublished[ "20tcon" ]  = false;
bookPublished[ "21votm" ]  = false;
bookPublished[ "22tbos" ]  = false;
bookPublished[ "23mh" ]    = false;
bookPublished[ "24rw" ]    = false;
bookPublished[ "25totw" ]  = false;
bookPublished[ "26tfobm" ] = false;
bookPublished[ "27v" ]     = false;
bookPublished[ "28thos" ]  = false;
bookPublished[ "01gstw" ]  = true;
bookPublished[ "02tfc" ]   = true;
bookPublished[ "03btng" ]  = true;
bookPublished[ "04wotw" ]  = true;
bookPublished[ "01hh" ]    = false;
bookPublished[ "02smr" ]   = false;
bookPublished[ "03oz" ]    = false;
bookPublished[ "04cc" ]    = false;
bookPublished[ "mc" ]      = false;
bookPublished[ "rh" ]      = true;

// Interface ///////////////////////////////////////////////////////////
    
function showBook( ) {
  controls.updatePageSelection( );
  var book = getCurrentBook( );
  var sect = getCurrentSection( );

  if( book != "" && sect != "" ) {
    var success = true;
    try {
      setBookLocation( book, sect );
    }
    catch(e) { success = false; }
    return (success);
  }
  else return false;
}

function showChanges( ) {
  controls.updatePageSelection( );
  var book = getCurrentBook( );
  var sect = getCurrentSection( );
  if( isNaN( sect ) ) { }
  else { sect = "sect" + sect; }

  if( book != "" && sect != "" ) {
    var success = true;
    try {
      setCurrentLocation( "editor/changes/index.html?book=" + book + ";sect=" + sect + ";" );
    }
    catch(e) { success = false; }
    return (success);
  }
  else return false;
}

function showGraph( ) {
  controls.updatePageSelection( );
  var book = self.controls.getBook( );

  if( book != "" ) {
    var success = true;
    try {
      setCurrentLocation( "svg/" + book + ".svgz" );
    }
    catch(e) { success = false; }
    return (success);
  }
  else return false;
}

function showDownloads( ) {
  controls.updatePageSelection( );
  var book = self.controls.getBook( );

  if( book != "" ) {
    var success = true;
    try {
      setCurrentLocation( "editor/main.html#" + book );
    }
    catch(e) { success = false; }
    return (success);
  }
  else return false;
}

function showOptions( ) {
  controls.updatePageSelection( );
  var success = true;
  lastMode = getMode( );

  try {
    setCurrentLocation( "editor/options.html" );
  }
  catch(e) { success = false; }
  return (success);
}

function showHelp( ) {
  controls.updatePageSelection( );
  var success = true;
  lastMode = getMode( );

  try {
    setCurrentLocation( "editor/help.html" );
  }
  catch(e) { success = false; }
  return (success);
}

function showLastMode( ) {
  switch( lastMode ) {
    case bookMode:      return showBook( );
    case changesMode:   return showChanges( );
    case graphMode:     return showGraph( );
    case downloadsMode: return showDownloads( );
  }
}

function getCurrentBook( ) {
  var currLocation = getCurrentLocation( );
  var separator = "/";
  var locationTokens = new Array( );
  locationTokens = currLocation.split( separator );

  if( locationTokens.length > 1 ) {
    var dirString = new String( locationTokens[ locationTokens.length - 2 ] );
    var fileString = new String( locationTokens[ locationTokens.length - 1 ] );
  }
  else return controls.getBook( );

  switch( getMode( ) ) {
    case bookMode:
      return dirString;
    case changesMode:
      //      return fileString.substring( fileString.indexOf( "book=" ) + 5, fileString.indexOf( ";", fileString.indexOf( "book=" ) ) );
      return main.getBook( );
    case graphMode:
      return fileString.substring( 0, fileString.indexOf( ".svg" ) );
    case downloadsMode:
      if( fileString.indexOf( "#" ) != -1 ) {
        return fileString.substring( fileString.indexOf( "#" ) + 1, fileString.length );
      }
      else {
        return controls.getBook( );
      } 
    default:
      return controls.getBook( );
  }
}

function getCurrentSection( ) {
  var currLocation = getCurrentLocation( );
  var separator = "/";
  var locationTokens = new Array( );
  locationTokens = currLocation.split( separator );

  if( locationTokens.length > 1 ) {
    var fileString = new String( locationTokens[ locationTokens.length - 1 ] );
  }
  else return controls.getSection( );

  if( getMode( ) == bookMode ) {
    if( fileString.indexOf( "sect" ) != -1 ) {
      return fileString.substring( fileString.indexOf( "sect" ) + 4, fileString.indexOf( ".htm" ) );
    }
    else {
      return fileString.substring( 0, fileString.indexOf( ".htm" ) );
    }
  }
  else if( getMode( ) == changesMode ) {
    /*    if( fileString.indexOf( "sect=" ) != -1 ) {
      return fileString.substring( fileString.indexOf( "sect=" ) + 5, fileString.indexOf( ";", fileString.indexOf( "sect=" ) ) );
      }*/
    return main.getSection( );
  }
  else return controls.getSection( );
}

function setBook( bookcode, section ) {
  if( getMode( ) == bookMode ) {
    setBookLocation( bookcode, section );
  }
  else if( getMode( ) == changesMode ) {
    if( !isNaN( section ) /* isaN */ ) { section = "sect" + section; }
    main.setBook( bookcode, section );
    //    setCurrentLocation( "editor/changes/index.html?book=" + bookcode + ";sect=" + section + ";" );
  }
  else if( getMode( ) == graphMode ) {
    setCurrentLocation( "svg/" + book + ".svg" );
  }
  else if( getMode( ) == downloadsMode ) {
    setCurrentLocation( "editor/main.html#" + bookcode );
  }
}

////////////////////////////////////////////////////////////////////////

function setBookLocation( bookcode, section ) {
  var tempLocation = bookLocations[ bookcode ];
  if( viewedOnline( ) && !bookPublished[ bookcode ] ) {
    tempLocation = "sanctum/" + tempLocation;
  }

  if( isNaN( section ) ) {
    setCurrentLocation( tempLocation + "/" + section + ".htm" );
  }
  else {
    setCurrentLocation( tempLocation + "/sect" + section + ".htm" );
  }
}

function getMode( ) {
  var newLocation = getCurrentLocation( );
  var separator = "/";
  var locationTokens = new Array( );
  locationTokens = newLocation.split( separator );

  if( locationTokens.length > 1 ) {
    var dirString = new String( locationTokens[ locationTokens.length - 2 ] );
    var fileString = new String( locationTokens[ locationTokens.length - 1 ] );

    if( dirString == "svg" ) {
      return graphMode;
    }
    else if( dirString == "changes" ) {
      return changesMode;
    }
    else if( dirString == "editor" ) {
      if( fileString.indexOf( "main.html" ) != -1 ) return downloadsMode;
      else if( fileString.indexOf( "options.html" ) != -1 ) return optionsMode;
      else if( fileString.indexOf( "help.html" ) != -1 ) return helpMode;
      else return invalidMode;
    }
    else if( locationTokens.length > 3 && locationTokens[ locationTokens.length - 4 ] == "xhtml" ) {
      return bookMode;
    }
    else return invalidMode;
  }
  else return invalidMode;
}

function getCurrentLocation( ) {
  return new String( "" + self.main.location );
}

function setCurrentLocation( partialPath ) {
  self.main.location = baselocation + partialPath;
}

function queryString( string ) {
  string = string.substring( 1 ).split( ";" );
  for( var i = 0; i < string.length; i++ ) {
    string[ i ] = string[ i ].split( "=" );
    this[ string[i][0] ] = unescape( string[ i ][ 1 ] );
  }
}

function viewedOnline( ) {
  var thisLocation = new String( "" + window.location );
  return ( thisLocation.indexOf( "http://" ) != -1 );
}

function codify( string ) {
  string = new String( "" + string );
  string = string.replace( /\n/g, "\\n" );
  string = string.replace( /\r/g, "\\r" );
  string = string.replace( /\'/g, "\\\'" );
  string = string.replace( /=/g, "\\=" );

  return string;
}

function readUserPrefs( ) {
  var cookieName = new String( "UserPrefs=" );
  var cookies = new String( "" + document.cookie );
  var position = cookies.indexOf( cookieName );

  if( position != -1 ) {
    position += cookieName.length;
    var end = cookies.indexOf( ";", position );
    if( end == -1 ) end = cookies.length;
    var prefsCookie = cookies.substring( position, end );
    if( prefsCookie != "" ) {
      eval( unescape( prefsCookie ) );
    }
  }
  return true;
}

function writeUserPrefs( ) {
  var cookieName = new String( "UserPrefs=" );
  var cookieValue = new String( "" );

  cookieValue += "top.changesStyle='" + codify( top.changesStyle ) + "';";
  cookieValue += "top.changesFilter='" + codify( top.changesFilter ) + "';";
  if( top.trackPageChanges ) cookieValue += "top.trackPageChanges=true;";
  else cookieValue += "top.trackPageChanges=false;";
  if( top.checkPageConsistency ) cookieValue += "top.checkPageConsistency=true;";
  else cookieValue += "top.checkPageConsistency=false;";
  if( top.useAccessKeys ) cookieValue += "top.useAccessKeys=true;";
  else cookieValue += "top.useAccessKeys=false;";
  if( top.addCorrectionsToTop ) cookieValue += "top.addCorrectionsToTop=true;";
  else cookieValue += "top.addCorrectionsToTop=false;";

  if( (cookieValue.length + cookieName.length) > 4096 ) { // cookie + name must be less than 4 KB
    alert( "Warning: user options info has exceeded maximum storage length." );
  }

  var date = new Date();
  date.setTime( date.getTime() + 1000*60*60*24*365*1 ); // 1 year hence

  document.cookie = cookieName + escape( cookieValue ) + "; expires=" + date.toGMTString();

  return true;
}

////// Run at load

if( self.location.href != top.location.href ) top.location = self.location;

var qString = new queryString( location.search );
if( qString[ "book" ] ) {
  bookCode = qString[ "book" ];
}

var baselocation = new String( "" + window.location );
if( baselocation.lastIndexOf( "editor/" ) != -1 ) {
  baselocation = baselocation.substring( 0, baselocation.lastIndexOf( "editor/" ) );
}
else {
  alert( "Can't figure out the base URL." );
}

readUserPrefs( );
