
function Role_getTargetName () {
  return this.targName;
}

function Role_getHostName () {
  return this.hostName;
}

function Role (targName, hostName) {
  this.getTargetName = Role_getTargetName;
  this.getHostName = Role_getHostName;
  this.targName = targName;
  this.hostName = hostName;
}

 
// stripTag strips variant number (eg. #15) from end of string.

function DialogueElement_stripTag (str) {
   result = new String();
   count = 0;                                                1
   while (count < str.length) {
     chr = str.charAt(count);
     if (chr == '#') {
       count++;
       chr = str.charAt(count);
       while ((count < str.length) && (chr >= '0') && (chr <= '9')) {
         count++;
         chr = str.charAt(count);
       }
     }
     result = result + chr;
     count++;
   }
   return result;
}

// stripTie strips tie chrs (~) from string.

function DialogueElement_stripTie (str) {
   result = new String ();
   count = 0;
   while (count < str.length) {
     chr = str.charAt(count);
     if (chr != '~') {
       result = result + chr;
     }
     else {
       result = result + ' ';
     }
     count++;
   }
   return result;
}

function DialogueElement_getShowString () {
  return this.ShowStr;
}

function DialogueElement_getVocabEntry () {
  return this.vocabEntry;
}

function DialogueElement_getNoteNo () {
  return this.noteNo;
}

function DialogueElement_getTiedString () {
  return this.tiedStr;
}

function DialogueElement (elemStr, keyStr, noteNo, voc) {
  this.voc = voc;
  this.stripTag = DialogueElement_stripTag;
  this.stripTie = DialogueElement_stripTie;
  this.getShowString = DialogueElement_getShowString;
  this.getVocabEntry = DialogueElement_getVocabEntry;
  this.getNoteNo = DialogueElement_getNoteNo;
  this.getTiedString = DialogueElement_getTiedString;
  this.elemStr = elemStr;
  this.tiedStr = this.stripTag(this.elemStr);
  this.ShowStr = this.stripTie(this.tiedStr);
  this.vocabEntry = this.voc.findKey(keyStr);
  if (this.vocabEntry == null) {
    alert ("failed to find vocab entry for "+keyStr);
  }
  this.noteNo = noteNo;
}

var punctuation = new String(' ,;.!?[]Ąż:"');

function DialogueLine_parseTarget (targStr) {
  nchr = 0;
  while (nchr < targStr.length) {
    elemStr = new String();
    keyStr = new String();
    noteNo = 0;
    while ((nchr < targStr.length) && (targStr.charAt(nchr) != ' ')) {
       chr = targStr.charAt(nchr);
       if ((chr == '\\') && (nchr < targStr.length-1)) {
         elemStr = elemStr + targStr.charAt(nchr+1);
         keyStr  = keyStr + targStr.charAt(nchr+1);
         nchr += 2;
         continue;
       }
       if (chr == '%') {
         nchr++;
         while ((nchr < targStr.length) && (targStr.charAt(nchr) >= '0') &&
           (targStr.charAt(nchr) <= '9')) {
           noteNo = noteNo * 10 + targStr.charAt(nchr);
           nchr++;
         }
       }
       else {
         elemStr = elemStr + chr;
         if (punctuation.indexOf(chr) < 0) {
           keyStr = keyStr + chr;
         }
         nchr++;
       }
    }
    while ((nchr < targStr.length) && (targStr.charAt(nchr) == ' ')) {
      elemStr = elemStr + ' '
      nchr++;
    }
    element = new DialogueElement (elemStr, keyStr, noteNo, this.voc);
    this.elements[this.elements.length] = element;
  }
}

function DialogueLine_getImageURL() {
  return this.imageURL;
}

function DialogueLine_getSoundURL () {
  return this.sndURL;
}

function DialogueLine_getElementCount () {
  return this.elements.length;
}

function DialogueLine_getElement (n) {
  return this.elements[n];
}

function DialogueLine_getRole () {
  return this.role;
}

function DialogueLine_getHostText () {
  return this.hostStr;
}

function DialogueLine (role, targStr, hostStr, imageURL, sndURL, voc) {
  this.parseTarget = DialogueLine_parseTarget;
  this.getImageURL = DialogueLine_getImageURL;
  this.getSoundURL = DialogueLine_getSoundURL;
  this.getElementCount = DialogueLine_getElementCount;
  this.getElement = DialogueLine_getElement;
  this.getRole = DialogueLine_getRole;
  this.getHostText = DialogueLine_getHostText;
  this.elements = new Array ();
  this.voc = voc;
  this.role = role;
  this.parseTarget (targStr);
  this.hostStr = hostStr;
  this.imageURL = imageURL;
  this.sndURL = sndURL;
}



function UnitDialogue_addRole (roleNo, targName, hostName) {
  this.roles[roleNo-1] = new Role (targName, hostName);
}

function UnitDialogue_getRole (roleNo) {
  if (roleNo <= this.roles.length) {
    return this.roles[roleNo-1];
  }
  else
    return null;
}

function UnitDialogue_addNote (noteNo, noteURL) {
  this.notes[noteNo-1] = noteURL;
}

function UnitDialogue_getNote (noteNo) {
  return this.notes[noteNo-1];
}

function UnitDialogue_addLine (roleNo, targStr, hostStr, imageURL, sndURL) {
  role = this.getRole(roleNo);
  line = new DialogueLine (role, targStr, hostStr, imageURL, sndURL, this.voc);
  this.lines[this.lines.length] = line;
}

function UnitDialogue_getLineCount () {
  return this.lines.length;
}

function UnitDialogue_getLine (lineNo) {
  if ((lineNo > 0) && (lineNo <= this.lines.length)) {
    return this.lines[lineNo-1];
  }
}

function UnitDialogue_getImageURL () {
  return this.imageURL;
}
  
function UnitDialogue (title, imageURL, voc) {
  this.voc = voc;
  this.addNote = UnitDialogue_addNote;
  this.getRole = UnitDialogue_getRole;
  this.addRole = UnitDialogue_addRole;
  this.addLine = UnitDialogue_addLine; 
  this.getNote = UnitDialogue_getNote;
  this.getLineCount = UnitDialogue_getLineCount;
  this.getLine = UnitDialogue_getLine;
  this.getImageURL = UnitDialogue_getImageURL;
  this.roles = new Array ();
  this.lines = new Array ();
  this.notes = new Array ();
  this.title = title;
  this.imageURL = imageURL;
}


