var divBubble;

window.onload = function () {
  divBubble = document.getElementById("bubblediv")
  divBubble.cpWidth = divBubble.style.width;

  var arrTextLinks = document.getElementById("text").getElementsByTagName("a");
  for (var i = 0; i < arrTextLinks.length; i++) {
    if (arrTextLinks[i].className.indexOf("bubble-") > -1) {
      arrTextLinks[i].onclick = function () {
        bubble(this.className.split("bubble-")[1].split(" ")[0]);
      }
    }
  }

  arrTRs = document.getElementsByTagName("tr");
  for (var i = 0; i < arrTRs.length; i++) {
    arrTDs = arrTRs[i].getElementsByTagName("td");
    if (arrTDs.length > 1) {
      if (!arrTDs[arrTDs.length - 1].style.textAlign.length > 0) {
        arrTDs[arrTDs.length - 1].style.textAlign = "right";
      }

      arrTDs[arrTDs.length - 1].style.paddingLeft = "10px";
    }
    if (arrTDs.length > 2) {
      if (!arrTDs[arrTDs.length - 2].style.textAlign.length > 0) {
        arrTDs[arrTDs.length - 2].style.textAlign = "right";
      }

      arrTDs[arrTDs.length - 2].style.paddingLeft = "10px";
    }
  }

  bubble();

  divBubble.getElementsByTagName("div")[0].childNodes[1].onclick = function () {
    de_bubble();
    divBubble.style.display = "none";
  }

  setInterval(poll_hash, 500);
}

var strLastHash = window.location.hash.replace("#", "");
function poll_hash() {
  if (window.location.hash.replace("#", "") != strLastHash) {
    bubble();
  }
}

function find_pos(obj) {
  var intCurrLeft = 0;
  var intCurrTop = 0;
  if (obj.offsetParent) {
    intCurrLeft = obj.offsetLeft;
    intCurrTop = obj.offsetTop;
    while (obj = obj.offsetParent) {
      intCurrLeft += obj.offsetLeft;
      intCurrTop += obj.offsetTop;
    }
  }

  return [intCurrLeft, intCurrTop];
}

function make_collapsible() {
  arrPs = document.getElementById("bubblediv-inner").getElementsByTagName("p");
  for (var i = 0; i < arrPs.length; i++) {
    if (arrPs[i].parentNode.className == "contact" && (arrPs[i].parentNode.parentNode.className == "contacts" || arrPs[i].parentNode.parentNode.className == "innercontacts")) {
      arrPs[i].style.backgroundImage = "url('images/arrow_contacts_right.gif')";
      arrPs[i].parentNode.getElementsByTagName("div")[0].style.display = "none";

      if (arrPs[i].parentNode.parentNode.className != "innercontacts" && window.location.hash.split("-")[1] && arrPs[i].innerHTML.toLowerCase().replace(/-/g, "") == decodeURIComponent(window.location.hash.split("-")[1])) {
        arrPs[i].parentNode.getElementsByTagName("div")[0].style.display = "block";

        window.scrollTo(find_pos(arrPs[i])[0], (find_pos(arrPs[i])[1] - 4));
      }

      arrPs[i].onclick = function () {
        elemInfo = this.parentNode.getElementsByTagName("div")[0];
        if (elemInfo.style.display == "block") {
          this.style.backgroundImage = "url('images/arrow_contacts_right.gif')";
          elemInfo.style.display = "none";

          if (this.parentNode.parentNode.className != "innercontacts" && window.location.hash.split("-")[1] && this.innerHTML.toLowerCase().replace(/-/g, "") == decodeURIComponent(window.location.hash.split("-")[1])) {
            window.location.hash = window.location.hash.replace("#", "").split("-")[0];
            strLastHash = window.location.hash.replace("#", "");
          }
        } else {
          this.style.backgroundImage = "url('images/arrow_contacts_down.gif')";
          elemInfo.style.display = "block";

          if (this.parentNode.parentNode.className != "innercontacts") {
            strNewHash = window.location.hash.replace("#", "").split("-")[0] + "-" + encodeURIComponent(this.innerHTML.toLowerCase().replace(/-/g, ""));
            window.location.hash = strNewHash;
            strLastHash = window.location.hash.replace("#", "");
          }
        }
      }
    }
  }
}

function de_bubble() {
  divBubbleInner = document.getElementById("bubblediv-inner");

  if (divBubbleInner.lastChild) {
    divBubbleInner.lastChild.style.display = "none";
    document.getElementById("text").appendChild(divBubbleInner.lastChild);
  }
}

function bubble(idHash) {
  if (idHash) {
    window.location.hash = idHash;
  }

  strLastHash = window.location.hash.replace("#", "");

  if (window.location.hash) {
    var divNewBubbleContents = document.getElementById("bubble-" + window.location.hash.replace("#", "").split("-")[0]);

    if (divNewBubbleContents) {
      if (parseInt(divNewBubbleContents.style.width) > 0) {
        divNewBubbleContents.cpWidth = divNewBubbleContents.style.width;
        divNewBubbleContents.style.width = "auto";
      }

      if (parseInt(divNewBubbleContents.cpWidth) > 0) {
        divBubble.style.width = divNewBubbleContents.cpWidth;
      } else {
        divBubble.style.width = divBubble.cpWidth;
      }

      de_bubble();

      document.getElementById("bubblediv-inner").appendChild(divNewBubbleContents);

      arrNewBubbleContentsDivs = divNewBubbleContents.getElementsByTagName("div");
      for (var i = 0; i < arrNewBubbleContentsDivs.length; i++) {
        if (arrNewBubbleContentsDivs[i].className.indexOf("database-") > -1) {
          ajax_query(arrNewBubbleContentsDivs[i].className.split("database-")[1].split(" ")[0], i);
        }
      }

      divNewBubbleContents.style.display = "block";
      divBubble.style.display = "inline";

      make_collapsible();
    }
  }
}


function insert_mail(straccount, strdomain, strtopdomain) {
  var strmailaddress = straccount + "@" + strdomain + "." + strtopdomain; 
  document.write("<a href=\"mailto:" + strmailaddress + "\">" + strmailaddress + "</a>");
}

function insert_query(intDivNum) {
  divInsertThis = document.getElementById("bubblediv-inner").lastChild.getElementsByTagName("div")[intDivNum];

  if (divInsertThis.cpAjaxRequest.readyState == 4) {
    divInsertThis.innerHTML = divInsertThis.cpAjaxRequest.responseText;

    make_collapsible();
  }
}

function ajax_query(strKeyword, intDivNum) {
  divInsertThis = document.getElementById("bubblediv-inner").lastChild.getElementsByTagName("div")[intDivNum];

  try {
    divInsertThis.cpAjaxRequest = new XMLHttpRequest();
  } catch(e) {
    try {
      divInsertThis.cpAjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        divInsertThis.cpAjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        alert("Din browser understøtter ikke Ajax");

        return false;
      }
    }
  }

  eval("divInsertThis.cpAjaxRequest.onreadystatechange = function () { insert_query(" + intDivNum + "); }");

  divInsertThis.cpAjaxRequest.open("GET", "query.asp?q=" + encodeURIComponent(strKeyword), true);
  divInsertThis.cpAjaxRequest.send(null);
}