﻿var crossrefsorted = false;
var fargs;


function init() {
var n,sep;

 BrowserDetect.init(); // ==> BrowserDetect.browser, BrowserDetect.version, BrowserDetect.OS
 //      if (BrowserDetect.browser=="Explorer") alert("IE not supported\nbest viewed with Firefox");
 // else if (BrowserDetect.browser!="Firefox")  alert("best viewed with Firefox");
 if (BrowserDetect.browser!="Firefox")  alert("best viewed with Firefox");

 sep = location.href.indexOf('?');
 if (sep>0) fargs = location.href.substring(sep+1); else fargs = '';
 editCPod = fargs.length;
 generateTable(init0());
 closeModal();
}


function fformat(p) {
var q;
 if (p < 10) q = "00"+ p; else if (p < 100) q = "0"+ p; else q = p;
 return q;
}

function soundCompare (s,t) {
var v;

 v = stringCompare(correctedFinal(s),correctedFinal(t));
 if (v) return v; else return (stringCompare(s,t));
}


function finalCompare (s,t) {
var v;

 v = stringCompare(striptovowel(s),striptovowel(t));
 if (v) return v; else return (stringCompare(s,t));
}

function stringCompare (s,t) {
var a,b,c,i,m,n;

 m = s.length; n = t.length;
 if (n<m) n=m;
 for (i=0;i<n;i++) {
  c = s.charCodeAt(i)-t.charCodeAt(i);
  if (c) return c; 
 }
 return (m - t.length);
}


function sortAllPinyin(a,b)  { return (stringCompare(phoneticAll[a].pinyin,phoneticAll[b].pinyin)); }

function sortListPinyin(a,b) { return (stringCompare(phoneticList[a].pinyin,phoneticList[b].pinyin)); }

function sortListDate(a,b)   { return (phoneticList[a].CPod-phoneticList[b].CPod); }

function sortListScatter(a,b) {
var f;
 f = phoneticList[a].scattercoeff-phoneticList[b].scattercoeff
 if (f) return f; else return sortListPinyin(a,b);
}

function sortListStrokes(a,b) { 
var s;

 s = phoneticList[a].strokes-phoneticList[b].strokes
 if (s) return s; else return sortListPinyin(a,b);
}

function sortAllFinal(a,b) {
var f;

 f = stringCompare(phoneticAll[a].final,phoneticAll[b].final);
 if (f) return f; else return sortAllPinyin(a,b);
}

function sortListFinal(a,b) {
var f;

 f = stringCompare(phoneticList[a].final,phoneticList[b].final);
 if (f) return f; else return sortListPinyin(a,b);
}

function sortListSound(a,b) {
var f;

 f = stringCompare(phoneticList[a].corrected,phoneticList[b].corrected);
 if (f) return f; else return sortListPinyin(a,b);
}

function sortAllSound(a,b) {
var f;

 f = stringCompare(phoneticAll[a].corrected,phoneticAll[b].corrected);
 if (f) return f; else return sortAllPinyin(a,b);
}

function modulo(n,p) { return n-p*Math.floor(n/p);}


function generateTable(n) {

 showList(0);  // show table listed by Wieger rank
}

function charlistLength(s) {
var i,k,c;
var build;

  i = build = 0;
  for (i=k=0; k<s.length; k++) {
    c = s.charAt(k);
    if (!build) {
      i++;
      if (c == openpar) build++;
    }
    else if (c == closepar) build--; 
  }
  return i;
}


function totalSort(a,b) {
var p,q,t,u;

 p = charlistLength(phoneticList[a].charlist);
 q = charlistLength(phoneticList[b].charlist);

 if (p!= q) return (q-p); // first sort by size of character list desc.
 else {
   t = phoneticList[a].pinyin;
   u = phoneticList[b].pinyin;
   return (t-u); // then sort by pinyin asc.
 }
}

/*
function totalSort(a,b) {
var p,q,t,u;

 p = phoneticList[a].ntotal;
 q = phoneticList[b].ntotal;

 if (p!= q) return (q-p); // first sort by ntotal desc.
 else {
   u = p-phoneticList[a].nfirst;
   t = q-phoneticList[b].nfirst;
   return ((t-q)-(u-p)); // then sort by nfirst asc.
 }
}
*/

function linkFormat(s,sep) { // handles cases where no single unicode exist, notation (X+Y) or (X-Y) - remove parentheses
var k,c,r,b;
var build;

  r = ''; build = 0;
  for (k=0; k<s.length; k++) {
    c = s.charAt(k);
    if (!build) {
      if (c == openpar) {b = ''; build++; }
      else r+= '<a target=_blank href=http://www.nciku.com/search/all/' + c + '>' + c + '</a>'+sep;
    }
    else {
     if (c == closepar) {
       r+= '<a target=_blank href=http://www.nciku.com/search/all/' + b + '>' + b + '</a>'+sep;
       build--;
     }
     else  b += c;
   }
  }
  return r;
}

// findPos : courtesy of http://www.quirksmode.org/js/findpos.html

function findPos(obj) {
 var curleft = curtop = 0;
 if (obj.offsetParent) {
   do {
     curleft += obj.offsetLeft;
     curtop += obj.offsetTop;
   } while (obj = obj.offsetParent);
 }
 return [curleft,curtop];
}

// <A HREF=""onMouseOver="document.bgColor='white'">White</a>

function openModal(obj,i) {
var c,p,x,y,z;
var a,t,d,e,f,cp;
var ref,map;

   p  = phoneticList[i].ndex;
   c  = phoneticList[i].char;
   z  = phoneticList[i].pinyin;

   map = fformat(p) + phoneticList[i].pinyin + '.htm';

   t = findPos(obj);
   x = t[0];
   y = t[1];

   a = phoneticList[i].nfirst;
   t = phoneticList[i].ntotal;
   d  = Math.floor(10*phoneticList[i].pdistance)/10;
   e  = Math.floor(10*phoneticList[i].nscatter)/10;
   f  = Math.floor(10*phoneticList[i].scattercoeff)/10;
   cp = phoneticList[i].CPod;

   if (editCPod) ref = 'http://chinesepod.com/'+fargs+'/posts/edit/' + cp;
            else ref = 'http://chinesepod.com/community/conversations/post/' + cp;

   s = '<table border=0 cellspacing=2 cellpadding=1><tr><td valign=top>';
   s += '<tr><td></td><td></td>';
   s += '<td class=modef><b><a target="blank" href='+ map +'>&gt; map</a></b></td>';
   s += '<td class=closew><b> <a style="font: 9px courier; color:#000000;font: 9px courier; color:#FFFFFF;" href=Javascript:closeModal()>×</a> </b></td></tr>';
   s += '<tr><td valign=top>&nbsp;</td><td class=hanzi valign=top>' + linkFormat(phoneticList[i].charlist,'<br />')+ '</td><td valign=top>';
   s += '<table border=0 cellspacing=1 cellpadding=1>';
   s += '<tr><td class=modef>char.</td><td><b>' + c + '</b></td></tr>';
   s += '<tr><td class=modef>pinyin</td><td>' + z+ '</td></tr>';
   s += '<tr><td class=modef>final</td><td>' +  phoneticList[i].final+ '</td></tr>';
   s += '<tr><td class=modef>sound</td><td>'+ phoneticList[i].corrected + '</td></tr>';
   s += '<tr><td class=modef>alternates</td><td>'+ phoneticList[i].alternate + '</td></tr>';
   s += '<tr><td class=modef>strokes </td><td>' + phoneticList[i].strokes + '</td></tr>';
   s += '<tr><td class=modef>index</td><td span=modal>' + p + '</td></tr>';
   s += '<tr><td class=modef>CPod</td><td><a href="'+ref+'">'+cp+'</a></td></tr>';
   s += '<tr><td class=modef>map</td><td><a href='+ map + '>'+map+'</a></td></tr>';
   s += '<tr><td class=modef>scaterring</td><td>['+ f + ']=' + d + ' * ' + e + "\n"+ '</td></tr>';
   s += '<tr><td class=modef>unique</td><td>' + phoneticList[i].nchars + '</td></tr>';
   s += '<tr><td class=modef> </td><td>'+ t + ' : ' +  phoneticList[i].ncompound + '</td></tr>';
   s += '</table></td></tr>';
   s += '<tr><td></td></tr>';
   s += '</table>';

   document.getElementById('modal').innerHTML = s;
   document.getElementById('modal').style.position  = 'absolute';
   document.getElementById('modal').style.left = x+42;
   document.getElementById('modal').style.top  = y-28;
}

function closeModal() {
   document.getElementById('modal').style.left = 0;
   document.getElementById('modal').style.top  = 0;
   // document.getElementById('modal').style.height = 1;
   // document.getElementById('modal').style.width  = 1;
   document.getElementById('modal').innerHTML = '&nbsp;';
}


function showDetails(i) {
var c,d;

 p  = phoneticList[i].ndex;
 map = fformat(p) + phoneticList[i].pinyin + '.htm';

 d = '<a target="blank" href='+ map +' onMouseover=JavaScript:openModal(this,'+i+');>'; //  onMouseout=JavaScript:closeModal(this);>'; 
 c  = phoneticList[i].char;
 // only display simplified character, skip the rest except for character compounds where no unicode exists
 if ((c.indexOf('+')<0) && (c.indexOf('-')<0)) c = c.charAt(0); 
 d += c + '</a>';
 return d;
}


function scanList(srt) {
 var c,g,h,i,l,p,s,cp,ref;
 var scan = new Array;

 l = phoneticList.length;
 for (h=0;h<l;h++) scan[h] = h;
      if (srt == 1) scan.sort(totalSort);
 else if (srt == 2) scan.sort(sortListFinal);   // show table listed by pinyin final
 else if (srt == 5) scan.sort(sortListStrokes); // show table listed by number of character strokes
 else if (srt == 6) scan.sort(sortListSound);   // show table listed by number of final sound (corrected pinyin)
 // m = Math.floor(n/7);

 s = '<table border=0 cellspacing=2 cellpadding=2><tr><td valign=top nowrap><br />' + "\n";
 for (h=0;h<l;h++) {
   i = scan[h];
   p = phoneticList[i].ndex;
   c = phoneticList[i].char;
   // only display simplified character, skip the rest except for character compounds where no unicode exists
   if ((c.indexOf('+')<0) && (c.indexOf('-')<0))c = c.charAt(0);
   cp = phoneticList[i].CPod;
   if (editCPod) ref = 'http://chinesepod.com/'+fargs+'/posts/edit/' + cp;
            else ref = 'http://chinesepod.com/community/conversations/post/' + cp;

   s += '<tr>';
   s += '<td><b><a target=_blank href="' + fformat(p) + phoneticList[i].pinyin + '.htm?">' + showDetails(i) + '</a></b></td>';
   s += '<td><small>' +  phoneticList[i].pinyin + '</small></td>';
   s += '<td><small>' +  phoneticList[i].final + '</small></td>';
   s += '<td><small>' +  phoneticList[i].corrected + '</small></td>';
   s += '<td><small>' + phoneticList[i].nchars + ':</small></td>'+"\n";
   // charlistLength(phoneticList[i].charlist)
   s += '<td>' + linkFormat(phoneticList[i].charlist,'&nbsp;') + '</td>';
 }
 s += '</tr></table>';
 document.getElementById('wtable').innerHTML = s;
}

function showAll(srt) {
 var c,h,i,l,p,s,z;
 var scan = new Array;

 n = phoneticAll.length;
 for (h=0;h<n;h++) scan[h] = h;

        if (srt == 0) ;                          // do nothing, will show table listed by Wieger rank
   else if (srt == 1) scan.sort(sortAllPinyin);  // show table listed by pinyin
   else if (srt == 2) scan.sort(sortAllFinal);   // show table listed by pinyin final
   else if (srt == 6) scan.sort(sortAllSound);   // show table listed by number of final sound (corrected pinyin)

 m = Math.floor(n/5);

 if (BrowserDetect.browser=="Explorer") document.getElementById('xtotal').innerHTML = '&nbsp;';
 s = '<table border=0 cellspacing=4 cellpadding=4><tr>' + "\n";
 for (h=1;h<n;h++) {
   i  = scan[h];
   p  = phoneticAll[i].ndex;
   c  = phoneticAll[i].char;
   z  = phoneticAll[i].pinyin;
   cp = phoneticAll[i].CPod;
   if (modulo(h-1,m)==0) {
     if (h>1) s+= '</table></td>' + "\n";
     s+= '<td valign=top><table border=0 cellspacing=2 cellpadding=2>'+ "\n";
   }
   s += '<tr><td><small>('+p+')</small></td>'; 
   if (cp) s += '<td nowrap><b>' + showDetails(phoneticAll[i].list) + '</b> ';
      else s += '<td nowrap><b>'+c+ '</b> ';
   s += ' <small>'+z+'</small>';

   if (p>Wieger+1) s+= ' **';
   s += '</td></tr>' + "\n";
 }
 s += '</table></td>' + "\n";
 s += '</tr></table>';
 document.getElementById('wtable').innerHTML = s;
}


function showList(srt) {
 var c,d,e,f,h,i,l,p,s,z;
 var scan = new Array;

 n = phoneticList.length;
 for (h=0;h<n;h++) scan[h] = h;

        if (srt == 0) ;                           // do nothing, will show table listed by Wieger rank
   else if (srt == 1) scan.sort(sortListPinyin);  // show table listed by pinyin
   else if (srt == 2) scan.sort(sortListFinal);   // show table listed by pinyin final
   else if (srt == 3) scan.sort(sortListScatter); // show table listed by scattering coefficient
   else if (srt == 4) scan.sort(sortListDate);    // show table listed by date (surrogate : CPod post ref.)
   else if (srt == 5) scan.sort(sortListStrokes); // show table listed by number of character strokes
   else if (srt == 6) scan.sort(sortListSound);   // show table listed by number of final sound (corrected pinyin)

 m = Math.floor(n/8);

 s = '<p><small>'+ n + ' phonetics, '+tTotal + ' compounds, '+ (n+tTotal) + ' characters total, '+ cTotal +' unique</small></p>';
 if (BrowserDetect.browser!="Explorer"
) document.getElementById('xtotal').innerHTML = s;
 s = '<table border=0 cellspacing=4 cellpadding=4><tr>' + "\n";

 for (h=0;h<n;h++) {
   i  = scan[h];
   p  = phoneticList[i].ndex;
   z  = phoneticList[i].pinyin;
   if (modulo(h,m)==0) {
     if (h) s+= '</table></td>' + "\n";
     s+= '<td valign=top><table border=0 cellspacing=2 cellpadding=2>'+ "\n";
   }
   s += '<td nowrap><b>' + showDetails(i) + '</b> ';
   s += ' <small>'+z+'</small>';
   if (p>=Wieger) s+= ' **';
   s += '</td></tr>' + "\n";
 }
 s += '</table></td>' + "\n";
 s += '</tr></table>';
 document.getElementById('wtable').innerHTML = s;
}

function pinyinCrossref(srt) {
var s,i,j,k;
var ypinyin=new Array;

  j=0;
  for (xp in xpinyin) ypinyin[j++]=xp;
  if (BrowserDetect.browser=="Explorer") {
    s  = "This table lists phonetic characters that appear as phonetic elements \nin other characters with the corresponding pinyin entry.\n\n";
    s += "For instance, ? (f?n) is phonetic in character ? (pàn), so ? is also\nlisted against 'pan' in the table.";
    alert(s);
    s='';
  }
  else {
    s  = '<p><small>This table lists phonetic characters that appear as <b>phonetic elements in other characters</b> with the corresponding <br />pinyin entry. ';
    s += 'For instance, ? (f?n) is phonetic in character ? (pàn), so ? is also listed against <i>pan</i> in the table.</small></p>';
  }
       if (srt == 0) ;                            // do nothing, will show table listed by Wieger rank
  else if (srt == 1) ypinyin.sort(stringCompare); // sort by pinyin
  else if (srt == 2) ypinyin.sort(finalCompare);  // sort by pinyin finals
  else if (srt == 6) ypinyin.sort(soundCompare);  // sort by final sound

  n = ypinyin.length;
  m = Math.floor(n/3);
  s += '<table border=0 cellspacing=1 cellpadding=1><tr>';
  for (j=0; j<n; j++) {
   xp = ypinyin[j];
   xt = xpinyin[xp];
   if (modulo(j,m)==0) {
     if (j>0) s+= '</table></td>' + "\n";
     s+= '<td valign=top><table border=0 cellspacing=2 cellpadding=2>'+ "\n";
   }
   s+= '<tr><td><small>' + (j+1) + '<small></td><td><b>' + xp + '</b></td><td>'
   for (k=0; k<xt.length;k++) {
     i = rpinyin[xt[k]];
     p = phoneticList[i].ndex;
     // only display simplified character, skip the rest except for character compounds where no unicode exists
     if ((c.indexOf('+')<0) && (c.indexOf('-')<0)) c = c.charAt(0);
     z = phoneticList[i].pinyin;
     s += showDetails(i)+ '</a> &nbsp;';
     s+= "\n";
   }
   s+= "</td></tr>\n";
 }
 s+= "</table></td></tr></table>\n";
 document.getElementById('wtable').innerHTML = s;
}
