var filmlist=new Array(),langsw=0,maxcla=-1,date,spacing ;
var alpha='abcdefghijklmnopqrstuvwxyz0\u00a3' ; 
var linklist=new Array(),linkcount=new Array() ; 
var month=['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] ; 
var sortfield=
  ['title','','year','language','director','','','length','','','','score','date viewed','','',''] ;
var langopt=['usual title','original title'] ; 

var i,j,ditto=document.createElement('td') ; 
ditto.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"' ;

if(pagetype=='avengers') 
{ sortfield[3] = 'director' ; 
  sortfield[4] = 'writer' ; 
  sortfield[2] = 'date broadcast' ; 
  sortfield[7] = '' ; 
} 

for(i=0;i<13;i++) if(i==1||sortfield[i]!='') 
{ linklist[i] = new Array() ; 
  linkcount[i] = new Array() ; 
  if(sortfield[i]=='language') linklist[i] = ['english','french','french*','italian','other'] ;
  else if(sortfield[i]=='year') for(j=1900;j<=2000;j+=10) linklist[i].push(j) ; 
  else if(sortfield[i]=='date broadcast') for(j=1965;j<=2010;j++) linklist[i].push(j) ; 
  else if(sortfield[i]=='length') for(j=60;j<=200;j+=10) linklist[i].push(j) ; 
  else if(sortfield[i]=='score') for(j=1;j<=10;j++) linklist[i].push(j) ; 
  else if(sortfield[i]=='date viewed') for(j=2007;j<=2011;j++) linklist[i].push(j) ; 
  else for(j=0;j<28;j++) { linklist[i][j] = alpha.charAt(j) ; if(i>2&&j==25) break ; } 
  for(j=0;j<linklist[i].length;j++) linkcount[i][j] = 0 ; 
}

if(pagetype=='dvds') spacing = 1 ; else spacing = 0 ; 

// langsw=0 for usual, 1 for orig
// sortcol = 0 for title

function whatletter(c)
{ var ind ; 
  if(c.splitfield=='') return -2 ; 
  ind = alpha.indexOf(c.splitfield) ;  
  if(ind==-1) return 26 ; else return ind ;
}
function updatecount(v,i)
{ var j,n=linklist[i].length ; 
  if(i==3&&v=='italian*') v = 'italian' ; 
  for(j=0;j<n;j++) if(linklist[i][j]==v||(i==3&&j==n-1)) linkcount[i][j] += 1 ;  
}
function flatten(p)
{ var s='' ; 
  function thisflatten(p)
  { if(p.nodeType==3) s += p.data.toLowerCase() ; 
    else for(var i=0;i<p.childNodes.length;i++) thisflatten(p.childNodes[i]) ; 
  }
  thisflatten(p) ; 
  return s ; 
}
/* ------------------------------------------ htmlcell -------------------------------------------- */

// an html cell comprises a sort field, a split field, and a cell field for displaying, 
// consisting of several spans, a 'high' field specifying which field should be 
// highlighted, and a 'wanted' field saying whether the cell should be printed.
// with opt=0, p0 is empty and p1 provides sort and split fields as well as content
// with opt=1, p1 is the sort field and its first letter the split field
//             (used for titles, director names etc)
// with opt=2, p0 is empty, p1 is the sort field, and the splitfield is 10*floor(p1/10)
//             (used for year of release of films)
// with opt=3, p0 is empty, p1 is the date of viewing (=sortfield); the splitfield is the month
// with opt=4, p0 is empty, p1 is the sort field, and the splitfield is its month component
//             (used for date of first showing of tv programs)
// with opt=5, p0 is Christian name, p1 is surname which is both sort field and split field
//             (used for director and writer names of tv programs)
//
// note that with opt=1, the procedure for extracting a prefix has unwanted results if
// the p1 field begins with an html tag

function htmlcell(p0,p1,opt)
{ var pfx='',lhs,mid,yy,mm,rhs=document.createElement("span") ; 
  function prefix(p)
  { if(pfx!='') return ; 
    else if(p.nodeType!=3) for(var i=0;i<p.childNodes.length;i++) prefix(p.childNodes[i]) ; 
    else { pfx = p.data.charAt(0) ; p.data = p.data.slice(1,p.data.length) ; } 
  }
  this.cell = document.createElement("td") ;
  this.wanted = 1 ; 
  if(opt==3||opt==4)
  { this.sortfield = p1 ; 
    this.linkfield = Math.floor(p1/10000) ; 
    mm = Math.floor(p1/100) ;
    this.splitfield = mm ; 
    if(opt==4)
    { lhs = document.createElement("span") ; 
      lhs.appendChild(document.createTextNode(p1%100+' ')) ; 
      this.cell.appendChild(lhs) ; 
      this.high = 1 ; 
    }
    else this.high = 0 ; 
    yy = this.linkfield % 100 ; 
    if(yy<10) yy = '0' + yy ; 
    rhs.appendChild(document.createTextNode(month[mm%100] + ' \u2019' + yy)) ; 
    this.cell.appendChild(rhs) ; 
    return ; 
  }

  this.high = opt ; 
  if(p0!='')
  { lhs = document.createElement("span") ; 
    lhs.innerHTML = p0 ; 
    this.cell.appendChild(lhs) ; 
  }
  else if(this.high>0) this.high -= 1 ; 

  // the special case opt!=1 is for the straddling director fields in toview
  if(p1==''&&opt!=1) { rhs.innerHTML = '&nbsp;' ; this.high = -1 ; } 
  else rhs.innerHTML = p1 ; 
  if(typeof(p1)=='number') this.sortfield = p1 ; else this.sortfield = flatten(rhs) ; 
  if(opt==0||opt==5) this.splitfield = this.sortfield ; 
  else if(opt==1)
  { prefix(rhs) ; 
    mid=document.createElement("span") ;
    mid.appendChild(document.createTextNode(pfx)) ; 
    this.cell.appendChild(mid) ; 
    this.splitfield = pfx = pfx.toLowerCase() ;
    if(pfx=='\u00a3') { this.sortfield = 'zz1' + this.sortfield ; } 
    else if(alpha.indexOf(pfx)<0||alpha.indexOf(pfx)>=26) 
    { this.splitfield = '0' ; this.sortfield = 'zz0' + this.sortfield ; } 
  }
  else if(opt==2) { this.splitfield = 10 * Math.floor(p1/10) ; this.high = 0 ; } 
  this.cell.appendChild(rhs) ; 
  if(opt==5) { this.linkfield = this.splitfield.charAt(0) ; this.high = 1 ; } 
  else this.linkfield = this.splitfield ; 
}
function addcell(c,opt)
{ var cc ; 
  if(opt&&c.high>=0)
  { cc = c.cell.cloneNode(true) ; 
    cc.childNodes[c.high].setAttribute("style","border:1px dotted gray;font-weight:bold") ; 
    return cc ; 
  } 
  return c.cell ;
}
/* ------------------------------------------ make cells -------------------------------------------- */

function makecrit(rt,te,gu)
{ var cell=document.createElement("td"),rtel,teel,guel ;
  rtel = document.createTextNode(rt) ;     
  teel = document.createElement("font") ; 
  teel.setAttribute("color","#880000") ; 
  teel.appendChild(document.createTextNode(te)) ;
  guel = document.createElement("font") ; 
  guel.setAttribute("color","#004080") ; 
  guel.appendChild(document.createTextNode(gu)) ;
  if(rt>0)
  { cell.appendChild(rtel) ;
    if(te>0||gu>0)
    { cell.appendChild(document.createTextNode(":")) ;
      if(te>0) cell.appendChild(teel) ; else cell.appendChild(guel) ; 
    }
  }
  else if(te>0)
  { cell.appendChild(teel) ;
    if(gu>0)
    { cell.appendChild(document.createTextNode(":")) ;
      cell.appendChild(guel) ; 
    }
  }
  else if(gu>0) cell.appendChild(guel) ;
  else cell.innerHTML = '&nbsp;' ;
  return cell ; 
}
/* ----------------------------------------- constructor ------------------------------------------- */

function film(the,title,le,titre,year,lang,subt,dirx,dirs,genre,cert,len,rt,te,gu,rem,
              crit,mark,tcmcrit,tcmmark,seen,id)
{ var i,ind,yy ; 
  this.data = new Array() ; 

  // date of viewing/subtable
  // class is for partitioning, serialno for sorting, day is for sorting, date is for display
  // yearseen is for linking to
  if(seen<10000) 
  { this.cla = seen ; 
    if(seen>0) this.serialno = filmlist.length ; else this.serialno = 0 ; 
    if(seen>maxcla) maxcla = seen ; 
    this.data[12] = new htmlcell('','',0) ; 
    this.data[12].cell.setAttribute('class','r') ; 
    if(pagetype=='toview') this.data[12].wanted = 0 ; 
  } 
  else
  { this.data[12] = new htmlcell('',seen,3) ; 
    this.data[12].cell.setAttribute('align','right') ; 
    this.data[12].cell.setAttribute('class','r') ; 
    this.cla = this.serialno = -2 ; 
  }

  // titles
  if(title!='') this.data[0] = new htmlcell(the,title,1) ; 
  if(titre=='') this.data[1] = this.data[0] ; 
  else this.data[1] = new htmlcell(le,titre,1) ; 
  if(title=='') this.data[0] = this.data[1] ; 

  // year
  if(year<3000) this.data[2] = new htmlcell('',year,2) ; 
  else this.data[2] = new htmlcell('',year,4) ; 
  this.data[2].cell.setAttribute('align','right') ; 

  // language/director
  if(pagetype=='avengers') this.data[3] = new htmlcell(lang,subt,5) ;
  else
  { if(lang!='(silent)'&&lang!='English'&&subt!='English') 
    { this.data[3] = new htmlcell('',lang+'*',0) ; 
      this.data[3].cell.setAttribute('style','cursor:help') ; 
      if(subt=='') subt = 'no' ;
      this.data[3].cell.setAttribute('title',subt+' subtitles') ; 
    } 
    else this.data[3] = new htmlcell('',lang,0) ; 
  }

  // director/writer
  if(pagetype=='avengers') this.data[4] = new htmlcell(dirx,dirs,5) ;
  else this.data[4] = new htmlcell(dirx,dirs,1) ; 
  ind = whatletter(this.data[4]) ; 
  if(this.cla>0) this.data[4].cell.setAttribute("style","text-align:center;vertical-align:middle") ;
  if(this.data[4].sortfield==''&&this.cla>0) this.data[4].wanted = 0 ; 

  this.data[5] = new htmlcell('',genre,0) ; 

  if(cert=='') this.data[6] = new htmlcell('','(nc)',0) ; 
  else this.data[6] = new htmlcell('',cert,0) ; 
  this.data[6].cell.setAttribute('align','center') ; 

  this.data[7] = new htmlcell('',len,2) ; 
  this.data[7].cell.setAttribute('align','right') ; 
  ind = this.data[7].splitfield ; 

  // guide crits
  this.data[8] = new htmlcell('','',1) ; 
  this.data[8].cell = makecrit(rt,te,gu) ; 
  if(pagetype=='avengers') for(ind=5;ind<=8;ind++) this.data[ind].wanted = 0 ; 

  // remark
  this.data[9] = new htmlcell('',rem,0) ; 
  if(pagetype!='avengers') 
  { this.data[9].cell.setAttribute("class","r") ; 
    if(pagetype=='dvds'&&crit==''&&mark==0) this.data[9].wanted = 0 ; 
  } 

  this.data[15] = new htmlcell('','(tcm)',0) ; 
  this.data[15].cell.setAttribute("class","r") ;

  for(i=10;i<16;i+=3,crit=tcmcrit,mark=tcmmark)
  { // criticism
    this.data[i] = new htmlcell('','',1) ;
    if(crit!=''||mark>0)
    { this.data[i].cell = document.createElement("td") ;
      this.data[i].cell.setAttribute("style","padding-left:1em;font-size:84%") ; 
      this.data[i].cell.setAttribute("colspan","7") ; 
      this.data[i].cell.innerHTML = crit ; 
    }
    // score
    if(mark==0) 
    { this.data[i+1] = new htmlcell('','',0) ; 
      this.data[i+1].sortfield = this.data[i+1].splitfield = 0 ; 
    } 
    else this.data[i+1] = new htmlcell('',mark,0) ; 
    this.data[i+1].cell.setAttribute('align','right') ; 
    if(i==10&&pagetype=='avengers') this.data[i].wanted = 0 ; 
    else if((crit==''&&mark==0)||(i==13&&(crit==''||mark==0)))  
    this.data[i].wanted = this.data[i+1].wanted = 0 ;
    if(i==13&&this.data[i].wanted==0) this.data[15].wanted = 0 ; 
  }

  if(this.cla<=0) for(i=0;i<16;i++) if(i==1||sortfield[i]!='') updatecount(this.data[i].linkfield,i) 

  this.id = id ; 
} 
function                       addfilm(the,title,le,titre,year,lang,subt,dirx,dirs,genre,cert,len,
                                       rt,te,gu,rem,crit,mark,tcmcrit,tcmmark,seen,id)
{ filmlist[filmlist.length] = new film(the,title,le,titre,year,lang,subt,dirx,dirs,genre,cert,len,
                                       rt,te,gu,rem,crit,mark,tcmcrit,tcmmark,seen,id) ;  
}
function addprog(the,title,date,dirx,dirs,writerx,writers,rem,mark,seen)
{ var f = 
    new film(the,title,'','',date,dirx,dirs,writerx,writers,'','',0,0,0,0,rem,'',mark,'',0,seen,'') ; 
  filmlist[filmlist.length] = f ; 
}
/* ------------------------------------------ rankfilms --------------------------------------------- */

function rankfilms(f,g)
{ var a,b,eq,j,aa,bb ; 

  // outer sort on class - but if both negative, only if the sort field is 12
  a = f.cla ; 
  b = g.cla ; 
  if(pagetype!='avengers') if(a>=0||b>=0||sortcol==12)
  { if(a<b) return -2 ; else if(a>b) return 2 ; 
    
  // and serial number to ensure that the order of items within a set is fixed
    a = f.serialno ; 
    b = g.serialno ; 
    if(a<b) return -1 ; else if(a>b) return 1 ; 
  }

  // main sort on sortcol
  a = f.data[sortcol].sortfield ;
  b = g.data[sortcol].sortfield ;
  if(f.data[sortcol].splitfield==g.data[sortcol].splitfield) eq = 1 ; else eq = 2 ; 
  if(sortcol==3) 
  { aa = a.charAt(a.length-1) ; 
    bb = b.charAt(b.length-1) ; 
    if(aa=='*'&&bb!='*') return 2*rev ; 
    if(aa!='*'&&bb=='*') return -2*rev ; 
  }
  if(a<b) return -rev*eq ; else if(a>b) return rev*eq ; 


  // if main sort is on director, inner sort on year
  if(sortcol==4&&pagetype!='avengers') 
  { a = f.data[2].sortfield ; 
    b = g.data[2].sortfield ; 
    if(a<b) return -1 ; else if(a>b) return 1 ; 
  }

  // inner sort on title
  if(sortcol>1&&pagetype!='avengers')
  { a = f.data[langsw].sortfield ; 
    b = g.data[langsw].sortfield ; 
    if(a<b) return -1 ; else if(a>b) return 1 ; 
  }

  // tiebreak on year/date broadcast
  if(f.data[2].sortfield<g.data[2].sortfield) return -1 ; 
  else if(f.data[2].sortfield>g.data[2].sortfield) return 1 ; else return 0 ; 
}
function sortfilms(colno) 
{ if(colno==sortcol||(sortcol==1&&colno==0)) rev = -rev ; 
  else 
  { if(colno==11||colno==12) rev = -1 ; else rev = 1 ; 
    if(colno==0) sortcol = langsw ; else sortcol = colno ; 
  } 
  filmlist.sort(rankfilms) ; 
  printfilms("filmlist") ; 
}
function settitle(opt)
{ if(langsw!=opt) 
  { if(sortcol==langsw) sortcol = opt ; 
    langsw = opt ; 
    filmlist.sort(rankfilms) ; 
    printfilms("filmlist") ; 
  } 
}
/* ------------------------------------------ printfilms -------------------------------------------- */

function printfilms(id)
{ var i,j,k,body,table,crit,row,cell,img,ntimes,child,pfx,prev,link,linkname,flag,n,extra,nprint=0 ; 

  body = document.createElement("tbody") ;

  // create the list of sort options
  table = document.createElement("table") ; 
  table.setAttribute("cellspacing",0) ; 
  table.setAttribute("cellpadding",0) ; 
  table.setAttribute("width","100%") ; 
  row = document.createElement("tr") ;
  cell = document.createElement("td") ;
  if(pagetype=='avengers') cell.appendChild(document.createTextNode('Sort episodes by:')) ;
  else cell.appendChild(document.createTextNode('Sort films by:')) ; 
  cell.setAttribute("style","font-family:palatino;font-weight:bold;"+
                            "margin:2px 0;font-size:100%;border:0;padding-right:20px") ;
  row.appendChild(cell) ; 

  cell = document.createElement("td") ;
  cell.setAttribute("style","font-family:arial;margin:2px 0;font-size:100%;border:0") ; 
  cell.setAttribute("colspan",2) ;  
  for(i=0;i<sortfield.length;i++) if(sortfield[i]!='')
  { link = document.createElement('a') ; 
    link.setAttribute('href','javascript:void sortfilms('+i+')') ; 
    link.appendChild(document.createTextNode(sortfield[i])) ;
    if(sortcol!=i) link.setAttribute("style","text-decoration:none;border:1px dotted #fffdf4") ; 
    else link.setAttribute("style","text-decoration:none;border:1px dotted gray") ;
    cell.appendChild(link) ; 
    if(pagetype=='toview'&&sortfield[i]=='length') break ;
    if(i<12) cell.appendChild(document.createTextNode(' | ')) ;
  }
  row.appendChild(cell) ; 
  table.appendChild(row) ;

  // create options for usual/original title
  if(pagetype!='avengers')
  { row = document.createElement("tr") ;
    cell = document.createElement("td") ;
    cell.appendChild(document.createTextNode('Show:')) ; 
    cell.setAttribute
      ("style","font-family:palatino;font-weight:bold;margin:2px 0;font-size:100%;border:0") ; 
    row.appendChild(cell) ; 

    cell = document.createElement("td") ;
    cell.setAttribute("style","font-family:arial;margin:2px 0;font-size:100%;border:0") ; 
    for(i=0;i<2;i++) 
    { link = document.createElement('a') ; 
      link.setAttribute('href','javascript:void settitle('+i+')') ; 
      link.appendChild(document.createTextNode(langopt[i])) ;
      if(langsw!=i) link.setAttribute("style","text-decoration:none;border:1px dotted #fffdf4") ; 
      else link.setAttribute("style","text-decoration:none;border:1px dotted gray") ;
      cell.appendChild(link) ; 
      if(i==0) cell.appendChild(document.createTextNode(' | ')) ;
    }
    row.appendChild(cell) ; 

    // create link to key
    cell = document.createElement("td") ;
    cell.setAttribute("style","font-family:arial;margin:2px 0;font-size:100%;border:0") ; 
    cell.setAttribute("align","right") ; 
    link = document.createElement('a') ; 
    link.setAttribute('href','#key') ; 
    link.appendChild(document.createTextNode('key')) ;
    cell.appendChild(link) ; 
    row.appendChild(cell) ; 

    table.appendChild(row) ;
  }

  row = document.createElement("tr") ;
  cell = document.createElement("td") ;
  cell.setAttribute("colspan",9) ;
  cell.setAttribute("style","border:0;padding-bottom:6px") ;  
  cell.appendChild(table) ; 
  row.appendChild(cell) ; 
  body.appendChild(row) ;

  row = document.createElement("tr") ;
  cell = document.createElement("td") ;

  // links to splits of the table by linkfield
  for(i=0;i<linklist[sortcol].length;i++) 
  { if(rev==1) j = i ; else j = (linklist[sortcol].length-1) - i ; 
    ntimes = linkcount[sortcol][j] ;
    if(ntimes==0) continue ; 
    linkname = linklist[sortcol][j] ;
    if(linkname=='0') linkname = '0-9' ; 
    if(sortcol==2&&pagetype!='avengers') linkname += 's' ; 
    if(nprint>0) cell.appendChild(document.createTextNode(' | ')) ;
    flag = 1 ;  
    if(sortcol==3&&pagetype!='avengers') 
    { if((rev==1&&i==linklist[sortcol].length-1)||(rev==-1&&i==0)) flag = 0 ; }
    if(flag) 
    { child = document.createElement('a') ; 
      child.setAttribute('href','#'+linklist[sortcol][j]) ; 
      child.appendChild(document.createTextNode(linkname)) ; 
    }
    else child = document.createTextNode(linkname) ;
    cell.appendChild(child) ; 
    if((sortcol==2)||(sortcol>10)||(sortcol==3&&pagetype!='avengers'))
    cell.appendChild(document.createTextNode(' ('+ntimes+')')) ; 
    nprint = 1 ; 
  }
  cell.setAttribute("colspan","9") ; 
  cell.setAttribute("align","center") ; 
  cell.setAttribute("class","tra") ; 
  row.appendChild(cell) ; 
  body.appendChild(row) ;

  // build the table proper

  for(i=0;i<filmlist.length;i++)
  { f = filmlist[i] ;  
    if(i==0) ntimes = 2 - spacing ; 
    else { ntimes = rankfilms(prev,f) ; if(ntimes<0) ntimes = -ntimes ; } 
// if(i>0) document.write('('+prev.data[0].sortfield+'+'+f.data[0].sortfield+'='+ntimes+')') ; 
    // make double line at the top
    if(ntimes==0) ntimes = 1 ; 
    extra = 0 ; 
    if(ntimes==1&&sortcol==4) // lots of code to group by director
    { if(i<filmlist.length-1&&filmlist[i+1].data[4].sortfield==f.data[4].sortfield)
        if(i==0||filmlist[i-1].data[4].sortfield!=f.data[4].sortfield)
          extra = 1 ; 
    }
    if(ntimes+extra==1&&sortcol==4)
    { if(i>1&&filmlist[i-1].data[4].sortfield!=f.data[4].sortfield)
        if(filmlist[i-1].data[4].sortfield==filmlist[i-2].data[4].sortfield)
          extra = 1 ; 
    }
    if(i>0&&prev.cla==0&&f.cla>0) ntimes += 1 ; 
    // print the spacer
    for(j=0;j<ntimes+spacing+extra-1;j++)
    { row = document.createElement("tr") ;
      cell = document.createElement("td") ;
      img = document.createElement("img") ;
      img.setAttribute("src","1x1.gif") ; 
      img.setAttribute("width","1") ; 
      img.setAttribute("height","1") ; 
      cell.appendChild(img) ; 
      cell.setAttribute("colspan","9") ; 
      cell.setAttribute("class","r0") ; 
      row.appendChild(cell) ; 
      body.appendChild(row) ;
    }

    row = document.createElement("tr") ;
    if(f.cla<=0&&(i==0||f.data[sortcol].linkfield!=prev.data[sortcol].linkfield)) 
      row.setAttribute("id",f.data[sortcol].linkfield) ; 

    // print the cells
    flag = f.cla<=0&&(i==0||ntimes==2) ; 
    for(j=0;j<=15;j++)
    { if((j<2&&j!=langsw)||f.data[j].wanted==0) continue ; 
      if(j==10||j==13) { body.appendChild(row) ; row = document.createElement("tr") ; }
      if(j==langsw&&f.id!='') cell.setAttribute('id',f.id) ; 
      if((j==3||j==4)&&sortcol==j&&i>0&&filmlist[i-1].data[j].sortfield==f.data[j].sortfield)
      { cell = document.createElement('td') ; 
        cell.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"' ; 
      }
      else cell = addcell(f.data[j],flag&&sortcol==j) ; 
      if(j==4)
      { for(k=i+1;k<filmlist.length&&filmlist[k].data[4].sortfield=='';k++) ; 
        if(k>i+1) cell.setAttribute("rowspan",k-i) ; 
      } 
      row.appendChild(cell) ; 
    }

    body.appendChild(row) ; 
    prev = f ; 
  }
  table = document.getElementById(id) ; 
  table.replaceChild(body,table.getElementsByTagName("tbody")[0]) ;
}
function showdate()
{ document.write('<p style="font-size:65% ; text-align:right">'+date) ; } 

