
var dash = '-';
var comma = ',';
var crlf = "\n";
var nbsp = '&nbsp;';

var totalPages = 202;
var sectionPages = 0;

var screenView = true; // screen resolution (800x600) or full size view (1400x1880)
var currentSection = -1;
var currentPage = 0;
var sectionView = -1;

var imageRoot = ''; // 'http://cursives.org/';  location of image files (/cursives, /cursivescreen)
var textRoot  = '';                       // location of text files (/cursivetext)

var textFiles = textRoot+'cursivetext/';
var fullFiles = imageRoot+'cursives/';
var screenFiles = imageRoot+'cursivescreen/';

nsection = 15;
sectionD = 3; // D : liste des formes
sections = [
 ['A - Cover pages'],
 ['B - Préface'],
 ['C - Instructions'],
 ['D - Liste des formes cursives'],
 ['E - Tableaux synoptiques'],
 ['F - Caractères alphabétiques japonais'],
 ['G - Tableau des équivalences des caractéristiques'],
 ['H - Index des caractères corrects'],
 ["I - Caractères difficiles à trouver dans l'index"],
 ['J - Liste de parties finales'],
 ['K - Liste de parties initiales'],
 ["L - Exemple d'étude analytique d'un texte cursif"],
 ["M - Récapitulation des moyens d'investigation"],
 ['N - Notes et errata'],
 ['O - Table des matières'],
 ['T - pages with text only']];

var section = new Array;
var alltext = new Array;
var sectcat = new Array;

var sect = new Array;

sect[0]=['A',0,10]; // 0 as first page : indicates pages have no printed number
sect[1]=['B',5,6];  // 5 is first page number, 6 last page number (in print book)
sect[2]=['C',7,8];
sect[3]=['D',9,119];
sect[4]=['E',121,135];
sect[5]=['F',136,137];
sect[6]=['G',137,142];
sect[7]=['H',143,172];
sect[8]=['I',173,176];
sect[9]=['J',177,190];
sect[10]=['K',191,197];
sect[11]=['L',197,200];
sect[12]=['M',200,200];
sect[13]=['N',201,201];
sect[14]=['O',202,202];

duplicates = 3; // pages 137, 197 and 200 listed under 2 sections

// section T (full text) : see below

section[15] = [
'A-001-000','A-002-000','A-003-000','A-004-000','A-005-000','A-006-000','A-007-000','A-008-000','A-009-000','A-011-000',
'B-001-005','B-002-006',
'C-001-007','C-002-008',
'D-001-009',
'E-001-121','E-002-122','E-003-123','E-004-124','E-008-128','E-009-129','E-012-132','E-015-135',
'F-001-136','F-002-137',
'G-001-137','G-002-138',
'H-001-143',
'I-001-173',
'J-001-177',
'K-001-191','K-004-194','K-007-197',
'L-001-197','L-002-198','L-003-199','L-004-200',
'M-001-200',
'N-001-201',
'O-001-202'];

// p. D-001-009 starts with at page 9 with 'forme' 1 until 'forme' 47, etc.

var formep = 9; // starting page of 'formes'

var formes = [
47,112,172,237,300,360,414,470,530,590,650,712,773,839,901,960,
1024,1080,1141,1198,1255,1313,1373,1435,1501,1566,1627,1687,1749,1806,1870,1936,1999,
2063,2131,2194,2252,2317,2381,2448,2513,2582,2644,2704,2761,2820,2880,2946,
3008,3071,3137,3200,3264,3294,3365,3435,3511,3588,3666,3741,3818,3894,3970,
4042,4118,4192,4268,4339,4415,4488,4560,4632,4706,4775,4842,4911,4977,
5052,5126,5187,5247,5323,5390,5458,5523,5591,5664,5730,5796,5859,5924,5987,
6051,6121,6191,6259,6333,6403,6470,6532,6598,6663,6728,6791,6861,6935,
7002,7070,7133,7203,7259];

var fulltext = new Array;
/* 
  fulltext['A-005-000'] = '0,4'; etc. 
  this will be generated from section[15] array by initFulltext()
*/

function initFulltext() {
var a,c,f,g,h,i,j,k,m,n;

 a = 'A'.charCodeAt(0);
 k = nsection; // last section
 for(i=0;i<section[k].length;i++) {
  f = section[k][i];
  g = f.split(dash);
  // m = f[0].charCodeAt(0) - a;
  m = f.charCodeAt(0) - a;
  n = parseInt(g[1],10)-1;
  fulltext[f] = m+comma+n;
 }
}

function selectSection(n) {
 currentSection=n;
 loadSubmenus();
}

function toggleImageText(i,j,t) {
var s,x,c;



  s = '<small>'+nbsp+nbsp+nbsp;
  // show txt link if exists
  c = sectcat[i];
  if (t) s+= '<a href="Javascript:imageView('+i+','+j+')">image view</a>';
  else {
    s+= '<a href="Javascript:textView('+i+','+j+',0)">text view</a>'+nbsp+nbsp+nbsp;
    // s+= '<a href="Javascript:textView('+i+','+j+',1)">all <b>'+c+'</b>-text</a>';
    if(i==nsection) {
      x=section[nsection][j];
      c=x[0];
      i=c.charCodeAt(0)-'A'.charCodeAt(0);
    }
    s+= '<a href="'+textFiles+alltext[i]+'.htm" target="_blank">all <b>'+c+'</b>-text</a>';
  }
  s+= nbsp+nbsp+nbsp+'</small>';
  document.getElementById('view').innerHTML = s;
}

function showTextNavig(i,j) {
var e,f,h,n,s,x;
var prv,ths,nxt;

  h=0; x=false;
  for (f in fulltext) 
    if (h==j) { ths=f; x=true; h++; } else if (x) { nxt=f; break; } else { prv = f; h++; }


  s = '<small>';
  // i always == 15
  n = section[i].length;
  e = n-1;
  if (j==0) {           // no last
    s+= '<b>'+ths+'</b>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j+1)+')">'+nxt+'</a>';
    s+= nbsp+nbsp+'<a href="Javascript:selectPage('+i+','+e+')">&gt;&gt;&gt;</a>';
  }
  else if (j==e) {       // no next

    s+= '<a href="Javascript:selectPage('+i+','+0+')">&lt;&lt;&lt;</a>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j-1)+')">'+prv+'</a>';
    s+= nbsp+nbsp+'<b>'+ths+'</b>'+nbsp+nbsp;
  }
  else {
    s+= '<a href="Javascript:selectPage('+i+','+0+')">&lt;&lt;&lt;</a>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j-1)+')">'+prv+'</a>';
    s+= nbsp+nbsp+'<b>'+ths+'</b>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j+1)+')">'+nxt+'</a>';
    s+= nbsp+nbsp+'<a href="Javascript:selectPage('+i+','+e+')">&gt;&gt;&gt;</a>';
  }
  s+= '</small>';
  document.getElementById('navig').innerHTML = s;
  // if (fulltext[ths] == undefined) document.getElementById('view').innerHTML = nbsp; else 
  toggleImageText(i,j,true);
}

function showNavig(i,j) {
var e,n,s;
var prv,ths,nxt;



  s = '<small>';
  n = section[i].length;
  e = n-1;
  ths = section[i][j];
  if (n==1) ;                // no last/next
  else if (j==0) {           // no last
    nxt = section[i][j+1];
    s+= '<b>'+ths+'</b>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j+1)+')">'+nxt+'</a>';
    s+= nbsp+nbsp+'<a href="Javascript:selectPage('+i+','+e+')">&gt;&gt;&gt;</a>';
  }
  else if (j==e) {       // no next

    prv = section[i][j-1];
    s+= '<a href="Javascript:selectPage('+i+','+0+')">&lt;&lt;&lt;</a>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j-1)+')">'+prv+'</a>';
    s+= nbsp+nbsp+'<b>'+ths+'</b>'+nbsp+nbsp;
  }
  else {
    prv = section[i][j-1];
    nxt = section[i][j+1];
    s+= '<a href="Javascript:selectPage('+i+','+0+')">&lt;&lt;&lt;</a>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j-1)+')">'+prv+'</a>';
    s+= nbsp+nbsp+'<b>'+ths+'</b>'+nbsp+nbsp;
    s+= '<a href="Javascript:selectPage('+i+','+(j+1)+')">'+nxt+'</a>';
    s+= nbsp+nbsp+'<a href="Javascript:selectPage('+i+','+e+')">&gt;&gt;&gt;</a>';
  }
  s+= '</small>';
  document.getElementById('navig').innerHTML = s;
  s = nbsp+nbsp+'<small>p.'+(j+sect[i][1])+'</small>';
  if (i==sectionD) {

    s+= nbsp+nbsp+'<small>f.';
    if(j>0) s+= (1+formes[j-1])+'-'+formes[j]; else s+= 1+'-'+formes[j];
    s+= '</small>';
  }
  document.getElementById('details').innerHTML = s;
  if (fulltext[ths] == undefined) document.getElementById('view').innerHTML = nbsp; else toggleImageText(i,j,false);
}

/*
function autoResize(id){
 var newheight;
 var newwidth;

 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
 if(document.getElementById){
   newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
   newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth;
 }
 document.getElementById(id).height= (newheight) + "px";
 document.getElementById(id).width= (newwidth) + "px";
}
*/

function readUTFfile(f) {
 //
 // http://robloche.free.fr/javascript/tuto_xhr/tuto_xhr.html
 //
 //
 // readyState
 //
 // 0 (uninitialized)	non initialisé
 // 1 (loading)	début du transfert des données
 // 2 (loaded)	données transférées
 // 3 (interactive)	les données reçues sont accssibles en partie
 // 4 (complete)	les données sont complètement accessibles}
 //
 if(window.XMLHttpRequest) { // Firefox 
	   vocab_object      = new XMLHttpRequest(); 
 }
 else if(window.ActiveXObject) { // Internet Explorer 
	   vocab_object      = new ActiveXObject("Microsoft.XMLHTTP"); 
 }
 else { // XMLHttpRequest non supporté par le navigateur 
	   alert("your browser cannot handle XMLHTTPRequest..."); 
	   return;
 }
 vocab_object.open("GET", f, true); 
 vocab_object.onreadystatechange = loadUTFfile;
 vocab_object.send(null);
}

function loadUTFfile() { 
  if(vocab_object.readyState == 4) 
    document.getElementById('content').innerHTML = '<pre>'+vocab_object.responseText+'</pre>';

  // [+=] else alert('error:'+xhr_object.readyState);
}

function textView(i,j,k) {
var s;

 readUTFfile(textFiles+section[i][j]+'.txt');
 // f = section[i][j]+'.txt';
 // s = '<iframe width="1024px" ID="itext" height="600px" frameborder="no" src="'+textFiles+f+'" onload=autoResize("itext")>';
 // document.getElementById('content').innerHTML = s;
 s = nbsp;
 document.getElementById('view').innerHTML = s;
 document.getElementById('options').innerHTML = s;
 toggleImageText(i,j,true);
}


function showOptions() {
var s;


  s = '<small>';
  if (screenView) s+= '<a href=Javascript:toggleView()>full size view</a>'; 
             else s+= '<a href=Javascript:toggleView()>screen size</a>'; 
  s+= '</small>';
  document.getElementById('options').innerHTML = s;
}

function toggleView() {
var i,j,s;

  i = sectionView;
  screenView = !screenView;
  if (i<0) ;
  else {
    j = currentPage;
    if (screenView) p= screenFiles; else p=fullFiles;
    s = '<img src="'+p+section[i][j]+'.png">';
    document.getElementById('content').innerHTML = s;
    // showNavig(i,j);
  }
  showOptions();
}

function imageView(i,j) {
var s;
  if (screenView) p= screenFiles; else p=fullFiles;
  s = '<img src="'+p+section[i][j]+'.png">';
  document.getElementById('content').innerHTML = s;
  toggleImageText(i,j,false);
  showOptions();
}

function selectPage(i,j) {

  sectionView = i;
  currentPage = j;
  if (i==(nsection-1)) {
    textView(i,j,0);
    showTextNavig(i,j);
  }
  else {
    sectionView = i;
    currentPage = j;
    imageView(i,j);
    showNavig(i,j);
  }
}

function z3(n) { if(n<10) return '00'+n; else if(n<100) return '0'+n; else return n; }

function initArray(i) {
var c,h,i,j,k,m;
var a = new Array;

  c=sect[i][0];
  h=sect[i][1];
  m=sect[i][2];
  k=0;
  for(j=h;j<=m;j++) {
    a[k++]=c+ dash +z3(k) +dash +z3(h);
    if (h) h++;
  }
  alltext[i]=c+ dash +z3(0) +dash +z3(0);
  sectcat[i]=c;
  return a;
}

function initArrays() {
var a,i,n;
 n=0;
 for(i=0;i<nsection;i++) {
  a=initArray(i);
  n+=a.length;
  section[i]=a;
 }
 totalPages=n-duplicates;
}

function selectValue(v) {
var i,j,k;

  k = v.indexOf(comma);
  i=v.substr(0,k);
  j=v.substr(k+1);
  selectPage(parseInt(i,10),parseInt(j,10));
}

function loadSubmenus() {
var s;

  s = '<form>';
  s+= '<select onChange="Javascript:selectValue(this.options[this.selectedIndex].value)">' +crlf;
  if (currentSection<0) {     // all content
    s+= '<option value="">'+ totalPages +' pages</option>' +crlf;
    // loop thru all content
    for(i=0;i<sect.length;i++) {
      a=section[i];
      for(j=0;j<a.length;j++) s+= '<option value="'+i+comma+j+'">'+ a[j] +'</option>' +crlf;
    }
  }
  else { // section-only content
    s+= '<option value="-1">'+ section[currentSection].length +' pages</option>' +crlf;
    // loop thru section content
    i=currentSection;

    a=section[i];
    for(j=0;j<a.length;j++) s+= '<option value="'+i+comma+j+'">'+ a[j] +'</option>' +crlf;
    // if (a.length==1) selectPage(i,0);
    selectPage(i,0);
  }
  s+= '</select>'+crlf;
  s+='</form>';
  document.getElementById('submenu').innerHTML = s;
}

function loadPage(t) {
var a,b,i,j,p,f,t;

 f = document.nsearch.fsearch.value;
 p = document.nsearch.psearch.value;
 if (f.length) {
   t = formes.length;
   f = parseInt(f,10);
   p = parseInt(p,10);
   if (isNaN(f)) ; else if (f>formes[t]) ; else {
     a=1;
     for(i=1;i<t;i++) {
      b=formes[i];
      if ((f>=a)&&(f<=b)) {p=i+formep; break;} // found 'forme'
      a=b;
     }
   }
 }
 if (isNaN(p)) ; else if (p>totalPages) ; else if (p<5) ; else {
   for(i=1;i<sect.length;i++) {
    a = sect[i][1];
    b = sect[i][2];
    if ((p>=a)&&(p<=b)) {j=p-a; break;} // found section
   }   
   selectPage(i,j);
 }
 return false;
}

function loadSearchboxes() {
var s;

  s = '<form name=nsearch> <small>';
  s+= 'p.<input type="text" style="width: 21px; font-size: 8pt;" name="psearch"> ';
  s+= 'f.<input type="text" style="width: 28px; font-size: 8pt;" name="fsearch"> ';
  s+= '<input type="submit" style="width: 21px; font-size: 8pt;" value=">>" onClick="return loadPage();">';
  s+= '</small></form>'+crlf;
  document.getElementById('search').innerHTML = s;
}

function loadMenus() {
var a,s;
var i,j;

  s = '<form>';
  s+= '<select onChange="Javascript:selectSection(this.options[this.selectedIndex].value)">' +crlf;
  s+= '<option value="">Select a Section -</option>' +crlf;
  for(j=0;j<sections.length;j++) {
    s+= '<option value="'+j+'">'+ sections[j] +'</option>' +crlf;
  }
  s+= '</select>' +nbsp+crlf;
  s+='</form>';
  document.getElementById('menu').innerHTML = s;
  loadSubmenus();
  loadSearchboxes();
  document.getElementById('navig').innerHTML = nbsp;
}


function downloadText() {
var i,k,s;

 s = '<small>text list :';
 s += '</small><br /><span class=downloads>';
 k = sections.length-1; // last section
 for(i=0;i<alltext.length;i++) s+= '<a target="_blank" href="'+textFiles+alltext[i]+'.htm">'+alltext[i]+'</a><br />' + crlf;
 for(i=0;i<section[k].length;i++) s+= '<a target="_blank" href="'+textFiles+section[k][i]+'.txt">'+section[k][i]+'</a><br />' + crlf;
 s+= '</span>';
 document.getElementById('content').innerHTML = s;
}

function downloadImages(fullsize) {
var a,i,j,s;

 s = '<small>';
 if (fullsize) s+= 'full size :'; else s+= 'screen size :'; 
 s += '</small><br /><span class=downloads>';
 for(i=0;i<sect.length;i++) {
  a=section[i];
  for(j=0;j<a.length;j++) {
   s+= '<a target="_blank" href="';
   if (fullsize) s+=fullFiles; else s+=screenFiles;
   s+= a[j]+'.png">'+a[j]+'</a><br />' + crlf;
  }
 }
 s+= '</span>';
 document.getElementById('content').innerHTML = s;
}

function downloadLists() {
var s,sp;

 sp = nbsp+nbsp+nbsp;
 s = '<small>downloads :' + sp;
 s+= '<a href=Javascript:downloadImages(false)>screen size</a>' + sp;
 s+= '<a href=Javascript:downloadImages(true)>full size</a>' + sp;
 s+= '<a href=Javascript:downloadText()>text list</a>' + sp;
 s+= '<a target="_blank" href="index.js">jscript</a>'; 
 s+= '</small>';
 document.getElementById('navig').innerHTML = s;
 document.getElementById('options').innerHTML = nbsp;
 document.getElementById('view').innerHTML = nbsp;
 <!-- document.getElementById('content').innerHTML = '<img border=0 src="images/A-001-000-thumb.png">'; -->
}

function init () {
 initArrays();
 initFulltext();
 loadMenus();
 downloadLists();
}

