Department.prototype._websites = null;

Department.prototype.GetWebsites = function() {
  if (this._websites == null)
  {
    this._websites = new Array();
    for (var i = 0; i < this.websites.length; i++)
    {
      var url = this.websites[i];
      var target = null;
      var index = url.indexOf(";");
      if (index != -1)
      {
        target = url.substring(index + 1);
        url = url.substring(0, index);
      }
      this._websites[i] = new Object();
      this._websites[i].url = url.indexOf("http://") != -1 ? url : (GetRootUrl() + url);
      this._websites[i].target = target;
    }
  }
  return this._websites;
}

Department.prototype.GetName = function() {
  var websites = this.GetWebsites();
  if (websites.length > 0)
  {
    return ('<a href="' + websites[0].url + '"' + (websites[0].target != null ? (' target="' + websites[0].target + '"') : "") + '>' + this.name + '</a>');
  }
  else
  {
    return this.name;
  }
}

var _closeBtnImgOnMsOver = new Image();
_closeBtnImgOnMsOver.src = GetRootUrl() + "images/ms_btn_close_over.gif";
var _closeBtnImgOnMsOut = new Image();
_closeBtnImgOnMsOut.src = GetRootUrl() + "images/ms_btn_close_out.gif";

function CloseBtnOnMsOver(img)
{
  img.src = _closeBtnImgOnMsOver.src;
}

function CloseBtnOnMsOut(img)
{
  img.src = _closeBtnImgOnMsOut.src;
}

OfficeBuilding.prototype._depts = null;

OfficeBuilding.prototype.AddDept = function(dept) {
  if (this._depts == null)
  {
    this._depts = new Array();
  }
  this._depts[this._depts.length] = dept;
}

OfficeBuilding.prototype.GetHtml = function() {
  if (this._depts == null || this._depts.length == 0)
  {
    return null;
  }
  var html;
  if (this.image)
  {
    html = '<table cellpadding="0" cellspacing="0" class="OffcBldInfo">'
         + '<tr>'
         +   '<td valign="top">'
         +     '<b>' + this.name + '</b>'
         +     (this.address ? ('<div>' + this.address + '</div>') : "")
         +     (this.zip ? ('<div>Hampton, VA ' + this.zip + '</div>') : "")
         +   '</td>'
         +   '<td align="right" valign="top" rowspan="2"><img src="images/' + this.image + '" alt="' + this.name + '" title="' + this.name + '"></td>'
         +   '<td align="right" valign="top" rowspan="2"><a href="javascript:HideDepartments()" onmouseover="CloseBtnOnMsOver(this.firstChild)" onmouseout="CloseBtnOnMsOut(this.firstChild)"><img src="' + _closeBtnImgOnMsOut.src + '" width="13" height="13" border="0" alt="Close Departments Information" title="Close Departments Information"></a></td>'
         + '</tr>'
         + '<tr>'
         +   '<td valign="bottom"><div>Departments:</div></td>'
         + '</tr>'
         + '<tr>'
         +   '<td colspan="3">';
  }
  else
  {
    html = '<div class="OffcBldInfo">'
         + '<a href="javascript:HideDepartments()" onmouseover="CloseBtnOnMsOver(this.firstChild)" onmouseout="CloseBtnOnMsOut(this.firstChild)"><img src="' + _closeBtnImgOnMsOut.src + '" width="13" height="13" align="right" border="0" alt="Close Departments Information" title="Close Departments Information"></a>'
         + '<b>' + this.name + '</b>'
         + (this.address ? ('<div>' + this.address + '</div>') : "")
         + (this.zip ? ('<div>Hampton, VA ' + this.zip + '</div>') : "")
         + '<div>Departments:</div>';
  }
  html += '<div class="DeptsInfo"><ul>';
  for (var i = 0; i < this._depts.length; i++)
  {
    html += '<li>' +  this._depts[i].GetName();
    if (this._depts[i].services.length > 0)
    {
      html += '<div class="Services">Services:</div><ul>';
      for (var j = 0; j < this._depts[i].services.length; j++)
      {
        html += '<li>' + this._depts[i].services[j] + '</li>';
      }
      html += '</ul>';
    }
    html += '</li>';
  }
  html += '</ul></div>';
  if (this.image)
  {
    html += '</td></tr></table>';
  }
  else
  {
    html += '</div>';
  }
  return html;
}

var _departmentsOut = null;
var _currOffcBlds = null;

function InitOffcBlds(location)
{
  _currOffcBlds = new Array();
  var i = 0, j, k;
  for (j = 0; j < _offcBlds.length; j++)
  {
    if (_offcBlds[j].location == location)
    {
      _currOffcBlds[i] = _offcBlds[j];
      for (k = 0; k < _departments.length; k++)
      {
        if (_departments[k].offcBldId == _currOffcBlds[i].id)
        {
          _currOffcBlds[i].AddDept(_departments[k]);
        }
      }
      i++;
    }
  }
  _departmentsOut = document.createElement("div");
  _departmentsOut.id = "OffcBldInfoOut";
  document.getElementsByTagName("body")[0].appendChild(_departmentsOut);
}

function ShowDepartments(offcBldName)
{
  var offcBld = null, html;
  for (var i = 0; i < _currOffcBlds.length; i++)
  {
    if (_currOffcBlds[i].name == offcBldName)
    {
      offcBld = _currOffcBlds[i];
      break;
    }
  }
  if (offcBld == null || (html = offcBld.GetHtml()) == null)
  {
    HideDepartments();
    return;
  }
  _departmentsOut.innerHTML = html;
  var clientWidth = document.all ? document.body.clientWidth : window.innerWidth;
  var clientHeight = document.all ? document.body.clientHeight : window.innerHeight;
  _departmentsOut.style.left = (_mouseX + _departmentsOut.offsetWidth) > clientWidth ? (_mouseX - _departmentsOut.offsetWidth) : _mouseX;
  _departmentsOut.style.top = ((_mouseY + _departmentsOut.offsetHeight) > (clientHeight - 32) ? (_mouseY - _departmentsOut.offsetHeight) : _mouseY) + (document.all ? document.body.scrollTop : 0);
  _departmentsOut.style.visibility = "visible";
}

function HideDepartments()
{
  _departmentsOut.style.visibility = "hidden";
}
