// ===================================================================
// Class for formatting bible reference
//

var Bible20;
if (!Bible20) {
  Bible20 = {};
}
else if (typeof Bible20 != "object") {
  throw new Error("Bible20 already exists and is not an object");
}

if (!Bible20.Bible) {
  Bible20.Bible = {};
}
else if (typeof Bible20.Bible != "object") {
  throw new Error("Bible20.Bible already exists and is not an object");
}


Bible20.Bible.BibleRefFormatter = function()
{
//  try {
//  }
//  catch (e) {
//    alert("Bible20.BibleBibleRefFormatter: " + e);
//  }
}

Bible20.Bible.BibleRefFormatter._MAP = {
// TODO 2010-03-05 low HS - multilanguage bible book names
  "Gn"  : ["1. Mose"],
  "Ex"  : ["2. Mose"],
  "Lv"  : ["3. Mose"],
  "Nu"  : ["4. Mose"],
  "Dt"  : ["5. Mose"],
  "Jos" : ["Josua", "Jos."],
  "Jdc" : ["Richter", "Ri."],
  "Rth" : ["Rut"],
  "1Sm" : ["1. Samuel", "1. Sam."],
  "2Sm" : ["2. Samuel", "2. Sam."],
  "1Rg" : ["1. K\u00f6nige", "1. K\u00f6n."],
  "2Rg" : ["2. K\u00f6nige", "2. K\u00f6n."],
  "1Chr": ["1. Chronik", "1. Chr."],
  "2Chr": ["2. Chronik", "2. Chr."],
  "Esr" : ["Esra"],
  "Neh" : ["Nehemia", "Neh."],
  "Esth": ["Ester"],
  "Job" : ["Hiob"],
  "Ps"  : ["Psalm", "Ps."],
  "Prv" : ["Spr\u00fcche", "Spr."],
  "Eccl": ["Prediger", "Pred."],
  "Ct"  : ["Hoheslied", "Hohesl."],
  "Is"  : ["Jesaja", "Jes."],
  "Jr"  : ["Jeremia", "Jer."],
  "Thr" : ["Klagelieder", "Klg."],
  "Ez"  : ["Hesekiel", "Hes."],
  "Dn"  : ["Daniel", "Dan."],
  "Hos" : ["Hosea", "Hos."],
  "Joel": ["Joel"],
  "Am"  : ["Amos", "Am."],
  "Ob"  : ["Obadja", "Ob."],
  "Jon" : ["Jona"],
  "Mch" : ["Micha"],
  "Nah" : ["Nahum", "Nah."],
  "Hab" : ["Habakuk", "Hab."],
  "Zph" : ["Zefanja", "Zef."],
  "Hgg" : ["Haggai", "Hagg."],
  "Zch" : ["Sacharja", "Sach."],
  "Ml"  : ["Maleachi", "Mal."],
  "Mt"  : ["Matth\u00e4us", "Mt."],
  "Mc"  : ["Markus", "Mk."],
  "L"   : ["Lukas", "Lk."],
  "J"   : ["Johannes", "Joh."],
  "Act" : ["Apostelgeschichte", "Apg."],
  "R"   : ["R\u00f6mer", "R\u00f6m."],
  "1K"  : ["1. Korinther", "1. Kor."],
  "2K"  : ["2. Korinther", "2. Kor."],
  "G"   : ["Galater", "Gal."],
  "E"   : ["Epheser", "Eph."],
  "Ph"  : ["Philipper", "Phil."],
  "Kol" : ["Kolosser", "Kol."],
  "1Th" : ["1. Thessalonicher", "1. Thess."],
  "2Th" : ["2. Thessalonicher", "2. Thess."],
  "1T"  : ["1. Timotheus", "1. Tim."],
  "2T"  : ["2. Timotheus", "2. Tim."],
  "Tt"  : ["Titus", "Tit."],
  "Phm" : ["Philemon", "Phm."],
  "H"   : ["Hebr\u00e4er", "Hebr."],
  "Jc"  : ["Jakobus", "Jak."],
  "1P"  : ["1. Petrus", "1. Petr."],
  "2P"  : ["2. Petrus", "2. Petr."],
  "1J"  : ["1. Johannes", "1. Joh."],
  "2J"  : ["2. Johannes", "2. Joh."],
  "3J"  : ["3. Johannes", "3. Joh."],
  "Jd"  : ["Judas", "Jd."],
  "Ap"  : ["Offenbarung", "Offb."]
};

Bible20.Bible.BibleRefFormatter._getBookShort = function(book)
{
  var MAP = Bible20.Bible.BibleRefFormatter._MAP;
  var result = MAP[book];
  return result && (result[1] || result[0]);
}

Bible20.Bible.BibleRefFormatter._getBook = function(book)
{
  var MAP = Bible20.Bible.BibleRefFormatter._MAP;
  var result = MAP[book];
  return result && result[0];
}

Bible20.Bible.BibleRefFormatter.prototype.toString = function()
{
  return "Bible20.Bible.BibleRefFormatter";
}

// FormatShort(book[, chapterNumber[, start[, end]]])
// returns short user-friendly representation of bible reference to book, chapter, verse or verse range
// if given, chapterNumber, start and end must be non-0.
Bible20.Bible.BibleRefFormatter.prototype.FormatShort = function(book, chapterNumber, start, end)
{
  var result = Bible20.Bible.BibleRefFormatter._getBookShort(book);
  if (result) {
    var sep = "";
    if (Bible20.Bible.BookNames.isSingleChapterBookName(book)) {
      sep = " ";
    }
    else if (chapterNumber) {
      result += " " + chapterNumber;
      sep = ",";
    }
    if (chapterNumber && start) {
      result += sep + start;
      if (end && end != start) {
        result += "-" + end;
      }
    }
  }
  return result;
}

// Format(book[, chapterNumber[, start[, end]]])
// returns user-friendly representation of bible reference to book, chapter, verse or verse range
// if given, chapterNumber, start and end must be non-0.
Bible20.Bible.BibleRefFormatter.prototype.Format = function(book, chapterNumber, start, end)
{
  var result = Bible20.Bible.BibleRefFormatter._getBook(book);
  if (result) {
    var sep = "";
    if (Bible20.Bible.BookNames.isSingleChapterBookName(book)) {
      sep = " ";
    }
    else if (chapterNumber) {
      result += " " + chapterNumber;
      sep = ",";
    }
    if (chapterNumber && start) {
      result += sep + start;
      if (end && end != start) {
        result += "-" + end;
      }
    }
  }
  return result;
}

