// Global settings
var imagesDir = "images/";

var _today = new Date();
var _dayOfWeek = _today.getDay(); // Monday = 1
var _month = _today.getMonth(); // 0-based
var _day = _today.getDate();
if (_day < 10) _day = "0" + _day;
var _year = _today.getFullYear();

var _monthNames = new Array();
_monthNames[0] = "January";
_monthNames[1] = "February";
_monthNames[2] = "March";
_monthNames[3] = "April";
_monthNames[4] = "May";
_monthNames[5] = "June";
_monthNames[6] = "July";
_monthNames[7] = "August";
_monthNames[8] = "September";
_monthNames[9] = "October";
_monthNames[10] = "November";
_monthNames[11] = "December";

function getImageUrl(url)
{
  return (imagesDir + url);
}

function Event(eventType)
{
  this.eventType = eventType;
  this.month;
  this.day;
  this.year;
  this.place;
  this.description;
  this.location;
  this.notes;
  this.images = new Array();
  this.citations = new Array();
}

function getImageTag(url, width, height)
{
  var str = "<A HREF=\"" + getImageUrl(url) + "\"><IMG SRC=\"" + getImageUrl(url + "\"");
  if (typeof(width) != "undefined")
  {
    str = str + " WIDTH=\"" + width + "\"";
  }
  if (typeof(height) != "undefined")
  {
    str = str + " HEIGHT=\"" + height + "\"";
  }
  str = str + " BORDER=\"0\"></A>";

  return str;
}

function getThumbnailTag(image, width)
{
  if (typeof(image) == "undefined")
  {
    return "";
  }

  if (typeof(width) == "undefined")
  {
    width = image.width;
  }

  var str = "<A HREF=\"" + getImageUrl(image.url) + "\"><IMG SRC=\"" + getImageUrl(image.url + "\"");
  if (typeof(width) != "undefined")
  {
    str = str + " WIDTH=\"" + width + "\"";
  }

  var height = getScaledHeight(image, width);
  str = str + " HEIGHT=\"" + height + "\"";

  str = str + " BORDER=\"0\"></A>";

  return str;
}

function getScaledHeight(image, width)
{
  return image.height * width / image.width;
}

function setDate(object, month, day, year)
{
  object.month = month;
  object.day = day;
  object.year = year;
}

// Font info
var font = "Arial";
var fontSize = 10;

function getFont(fontOffset)
{
  return (fontSize + offset);
}

var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthAbbrevs = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function getLifespanStr(person)
{
  var str = "";

  if (typeof(person.birthInfo.year) != "undefined")
  {
    str = str + person.birthInfo.year;
  }
  str = str + "-";
  if (typeof(person.deathInfo.year) != "undefined")
  {
    str = str + person.deathInfo.year;
  }

  return str;
}

function getFullNameStr(person)
{
  var str = "";

  if (typeof(person.firstName) != "undefined")
  {
    str = person.firstName;
  }

  if (typeof(person.middleName) != "undefined")
  {
    if (str != "")
    {
      str = str + " ";
    }
    str = str + person.middleName;
  }

  if (typeof(person.lastName) != "undefined")
  {
    if (str != "")
    {
      str = str + " ";
    }
    str = str + person.lastName;
  }

  if (person.suffix !="")
  {
    str = str + ", " + person.suffix;
  }

  return str;
}

function getProperNameStr(person)
{
  var str = "";

  if (typeof(person.lastName) != "undefined" && person.lastName != "")
  {
    str = person.lastName;
  }
  else
  {
    str = "Unknown";
  }

  if (typeof(person.firstName) != "undefined" && person.firstName != "")
  {
    if (str != "")
    {
      str = str + ", ";
    }
    str = str + person.firstName;

    if (typeof(person.middleName) != "undefined" && person.middleName != "")
    {
      str = str + " " + person.middleName;
    }
  }

  if (person.suffix !="")
  {
    str = str + ", " + person.suffix;
  }

  return str;
}

function getFullDateStr(dateInfoObject)
{
  var str = "";

  if (typeof(dateInfoObject.month) != "undefined"
      && dateInfoObject.month != "")
  {
    str = months[dateInfoObject.month];

    if (typeof(dateInfoObject.day) != "undefined"
        && dateInfoObject.day != "")
    {
      str = str + " " + dateInfoObject.day;
    }
  }

  if (typeof(dateInfoObject.year) != "undefined"
      && dateInfoObject.year != "")
  {
    if (str != "" && typeof(dateInfoObject.day) != "undefined"
        && dateInfoObject.day != "")
    {
      str = str + ",";
    }
    str = str + " " + dateInfoObject.year;
  }

  return str;
}

function getAbbrevFullDateStr(dateInfoObject)
{
  var str = "";

  if (typeof(dateInfoObject.month) != "undefined"
      && dateInfoObject.month != "")
  {
    str = monthAbbrevs[dateInfoObject.month];

    if (typeof(dateInfoObject.day) != "undefined"
        && dateInfoObject.day != "")
    {
      str = str + " " + dateInfoObject.day;
    }
  }

  if (typeof(dateInfoObject.year) != "undefined"
      && dateInfoObject.year != "")
  {
    if (str != "")
    {
      str = str + ", ";
    }
    str = str + dateInfoObject.year;
  }

  return str;
}

function getFullDateStrWithPrep(dateInfoObject)
{
  var str = getStandardDateStr(dateInfoObject);

  var prep = " ";
  if (typeof(dateInfoObject.day) != "undefined"
      && dateInfoObject.day != "")
  {
    prep = " on ";
  }
  else if (typeof(dateInfoObject.year) != "undefined"
           && dateInfoObject.year >= "1000"
           && dateInfoObject.year <= "2999")
  {
    prep = " in ";
  }

  return (prep + str);
}

function getStandardDateStr(dateInfoObject)
{
  var str = "";

  if (typeof(dateInfoObject.day) != "undefined"
      && dateInfoObject.day != "")
  {
    if (dateInfoObject.day < 10)
    {
      str = str + "0";
    }
    str = str + dateInfoObject.day;
  }

  if (typeof(dateInfoObject.month) != "undefined"
      && dateInfoObject.month != "")
  {
    if (str != "")
    {
      str = str + " ";
    }
    str = str + monthAbbrevs[dateInfoObject.month];
  }

  if (typeof(dateInfoObject.year) != "undefined"
      && dateInfoObject.year != "")
  {
    if (str != "")
    {
      str = str + " ";
    }
    str = str + dateInfoObject.year;
  }

  return str;
}

function arrayContains(arr, str)
{
  var result = -1;

  for (var i = 0; i < arr.length; i++)
  {
    if (arr[i] == str)
    {
      result = i + 1;
      break;
    }
  }

  return result;
}
