var C = "ptkbdgfscvzjmnlrwy"
var V = "ieaou"

function emitField(name,val)
{
  document.write("<em>"+name+":</em> <strong>"+val+"</strong><p>")
}

function emitFooter()
{
  document.write(strAnchor("[Waldzell Home Page]","../../../index.html"))
  document.write(strAnchor("[Waldzell GBG]","../../index.html"))
  document.write(strAnchor("[Game Archive Index]","../index.html"))
}

function emitGlyphs(glyphs)
{
  var words = glyphs.split(" ")
  for (var w=0;w<words.length;++w)
  {
    var sylls = words[w].split("-")
    for (var s=0;s<sylls.length;++s)
      document.write(strBitmap(sylls[s]))
    if (w<words.length-1)
      document.write(strBitmap("SPC"))
  }
  document.write("<br>")
}

function emitQuotedField(name,val)
{
  emitField(name,"\""+val+"\"")
}

function isConsonant(ch)
{
  return (C.indexOf(ch,0) >= 0)
}

function isVowel(ch)
{
  return (V.indexOf(ch,0) >= 0)
}

function strAnchor(text,href)
{
  return "<a href=\""+href+"\">"+text+"</a>"
}

function strGameref(title,order)
{
  return "<a href=\"../Order"+order+"/"+title+".html\">"
    + "\""+title+"\" ("+order+")"
    + "</a>"
}

function strBitmap(syll)
{
  return "<img src=\"../../../Conlang/"+syll+".xbm\"> "
}

function strGlyph(glyphs)
{
}

function strMailto(email)
{
  return "&lt;"+strAnchor(email,"mailto:"+email)+"&gt;"
}

function Context(name)
{
  this.name = name
  this.links = new Array(0)
  this.link = ctxt_link
  this.emit = ctxt_emit
}

function ctxt_link(glyph,conlang,gloss)
{
  this.links[this.links.length] = new Link(glyph,conlang,gloss)
}

function ctxt_emit()
{
  document.write("<em>"+this.name+"</em><p>")
  for (var i=0;i<this.links.length;++i)
    this.links[i].emit()
}

function Move(player)
{
  this.player = player
  this.links = new Array(0)
  this.link = move_link
  this.emit = move_emit
}

function move_link(glyph,conlang,gloss)
{
  this.links[this.links.length] = new Link(glyph,conlang,gloss)
}

function move_emit(seq)
{
  if (this.player != "")
    document.write("<em>Move "+seq+" ("+this.player+")</em><p>")
  for (var i=0;i<this.links.length;++i)
    this.links[i].emit()
}

function Link(glyph,conlang,gloss)
{
  this.glyph = glyph
  this.conlang = conlang
  this.gloss = gloss
  this.emit = link_emit
}

function link_emit()
{
  emitGlyphs(this.glyph)
  document.write("<strong>"+this.conlang+"</strong><br>")
  document.write("<strong><em>"+this.gloss+"</em></strong><p>")
}

function Player(name,email)
{
  this.name = name
  this.email = email
  this.str = player_str
}

function player_str()
{
  return this.name + " " + strMailto(this.email)
}

function Xref(type,title,order)
{
  this.type = type
  this.title = title
  this.order = order
  this.str = xref_str
}

function xref_str()
{
  return this.type+" "+strGameref(this.title,this.order)
}

function Game()
{
  this.var_title = ""
  this.var_order = -1
  this.var_begun = ""
  this.var_ended = ""
  this.summary = "English"
  this.skelTitle = ""
  this.skelOrder = -1
  this.tempTitle = ""
  this.tempOrder = -1
  this.anlgTitle = ""
  this.anlgOrder = -1
  this.players = new Array(0)
  this.context = ""
  this.moves = new Array(0)
  this.xrefs = new Array(0)
  this.title = game_title
  this.order = game_order
  this.skeleton = game_skeleton
  this.template = game_template
  this.analogue = game_analogue
  this.begun = game_begun
  this.ended = game_ended
  this.player = game_player
  this.ctxt = game_ctxt
  this.ctxtlink = game_ctxtlink
  this.move = game_move
  this.link = game_link
  this.xref = game_xref
  this.emit = game_emit
  this.emitTitle = game_emitTitle
  this.emitHeader = game_emitHeader
  this.emitPlayers = game_emitPlayers
  this.emitSummary = game_emitSummary
  this.emitMoves = game_emitMoves
  this.emitXrefs = game_emitXrefs
  this.emitComments = game_emitComments
}

function game_title(s) {this.var_title = s}
function game_order(n) {this.var_order = n}
function game_begun(s) {this.var_begun = s}
function game_ended(s) {this.var_ended = s}

function game_skeleton(title,order)
{
  this.skelTitle = title
  this.skelOrder = order
}

function game_template(title,order)
{
  this.tempTitle = title
  this.tempOrder = order
}

function game_analogue(title,order)
{
  this.anlgTitle = title
  this.anlgOrder = order
}

function game_player(name,email)
{
  this.players[this.players.length] = new Player(name,email)
}

function game_ctxt()
{
  if ((this.var_order == 1) || (this.var_order == 3))
    this.context = new Context("Skeleton")
  else if (this.var_order == 2)
    this.context = new Context("Template")
}

function game_ctxtlink(glyph,conlang,gloss)
{
  this.context.link(glyph,conlang,gloss)
}

function game_move(player)
{
  this.moves[this.moves.length] = new Move(player)
}

function game_link(glyph,conlang,gloss)
{
  this.moves[this.moves.length-1].link(glyph,conlang,gloss)
}

function game_xref(type,title,order)
{
  this.xrefs[this.xrefs.length] = new Xref(type,title,order)
}

function game_emitTitle()
{
  document.write("<h1>Waldzell Game: \""+this.var_title+"\" ("
    +this.var_order+")</h1><hr>")
}

function game_emitPlayers()
{
  document.write("<ol>")
  for (var i=0;i<this.players.length;++i)
    document.write("<li>" + this.players[i].str())
  document.write("</ol>")
}
    

function game_emitHeader()
{
  document.write("<h2>Game Header</h2>")
  emitQuotedField("Title",this.var_title)
  emitField("Order",this.var_order)
  if ((this.var_order == 1) || (this.var_order == 3))
  {
    if (this.skelTitle == "")
      emitField("Skeleton","none")
    else
      emitField("Skeleton",strGameref(this.skelTitle,this.skelOrder))
  }
  else if (this.var_order == 2)
  {
    if (this.tempTitle == "")
      emitField("Template","none")
    else
      emitField("Template",strGameref(this.tempTitle,this.tempOrder))
    if (this.anlgTitle == "")
      emitField("Analogue","none")
    else
      emitField("Analogue",strGameref(this.anlgTitle,this.anlgOrder))
  }
  emitField("Begun",this.var_begun)
  emitField("Ended",this.var_ended)
  emitField("Players","")
  this.emitPlayers()
  document.write("<hr>")
}

function game_emitSummary()
{
  document.write("(<a href=\""+this.summary+"/"+this.var_title+".html\">")
  document.write("English Summary)</a>")
}

function game_emitMoves()
{
  document.write("<h2>Tablature ")
  this.emitSummary()
  document.write("</h2>")

  if (this.var_order > 0)
    if (typeof(this.context) == "object")
      this.context.emit()
  for (var i=0;i<this.moves.length;++i)
    this.moves[i].emit(i+1)
  document.write("<hr>")
}

function game_emitXrefs()
{
  document.write("<h2>Game Links</h2>")
  document.write("<em>This game is</em><strong><ul>")

  for (var i=0;i<this.xrefs.length;++i)
    document.write("<li>"+this.xrefs[i].str())

  document.write("</ul></strong><hr>")
}

function game_emitComments()
{
  document.write("<h2>Commentary</h2><hr>")
}

function game_emit()
{
  this.emitTitle()
  this.emitHeader()
  this.emitMoves()
  this.emitXrefs()
  this.emitComments()
  emitFooter()
}
