var PopupWnd;
var WinPlanLegend;
var RootLegend = "http://www.geono.no/Tegnforklaring/";
var RootPlan ="http://www.geono.no/Plan/";
//    var XmlURL;       // = 'ArealplanerNordland.xml';
//    var XslURL;       // = '../../Template/Arealplan.xsl';
//    var xml;          // = loadXML(XmlURL);
//    var xsl;          // = loadXML(XslURL);


// Switch the visibility of nodes
function SwitchVisibility(e) {
  if (document.all) {
    if (e.children(0).children(1).className.indexOf("Group")==0) {
      mark = e.children(0).children(0).children(0);
      if (mark.src.toUpperCase().indexOf("PLUS.GIF")!=-1) {
        mark.src = "./Images/OpenedFolderMinus.gif";
        for (var i = 0; i < e.children(0).children(1).children.length; i++)
          //if (e.children(0).children(1).children(i).className=="e")
            e.children(0).children(1).children(i).style.display = "block";
      } else {
        mark.src = "./Images/ClosedFolderPlus.gif";
        for (var i = 0; i < e.children(0).children(1).children.length; i++) 
          e.children(0).children(1).children(i).style.display = "none";
      }
    }
  } else {
    try {
      if (e.childNodes[0].childNodes[2].className.indexOf("Group")==0) {
        mark = e.childNodes[0].childNodes[1].childNodes[0];
        if (mark.src.toUpperCase().indexOf("PLUS.GIF")!=-1) {
          mark.src = "./Images/OpenedFolderMinus.gif";
          for (var i = 0; i < e.childNodes[0].childNodes[2].childNodes.length; i++)
            if (e.childNodes[0].childNodes[2].childNodes[i].className=="e")
              e.childNodes[0].childNodes[2].childNodes[i].setAttribute("style","display:block");
        } else {
          mark.src = "./Images/ClosedFolderPlus.gif";
          for (var i = 0; i < e.childNodes[0].childNodes[2].childNodes.length; i++)
            if (e.childNodes[0].childNodes[2].childNodes[i].attributes!=null)
              e.childNodes[0].childNodes[2].childNodes[i].setAttribute("style","display:none");
        }
      }
    } catch (erreur) {
      try {
        if (e.childNodes[0].childNodes[1].className.indexOf("Group")==0) {
          mark = e.childNodes[0].childNodes[0].childNodes[0];
          if (mark.src.toUpperCase().indexOf("PLUS.GIF")!=-1) {
            mark.src = "./Images/OpenedFolderMinus.gif";
            for (var i = 0; i < e.childNodes[0].childNodes[1].childNodes.length; i++)
              if (e.childNodes[0].childNodes[1].childNodes[i].className=="e")
                e.childNodes[0].childNodes[1].childNodes[i].setAttribute("style","display:block");
          } else {
            mark.src = "./Images/ClosedFolderPlus.gif";
            for (var i = 0; i < e.childNodes[0].childNodes[1].childNodes.length; i++)
              if (e.childNodes[0].childNodes[1].childNodes[i].attributes!=null)
                e.childNodes[0].childNodes[1].childNodes[i].setAttribute("style","display:none");
          }
        }
      } catch (erreur) {
        return erreur;
      }
    }
  }
}

// Handle a mouse click
function cl(e)
{
  if (e) {    //MZ
    try {
      e = e.target;
      if (e.parentNode.className=="LSA") return;
      while ((e.className != "c")&&(e!=null)) e = e.parentNode;
      if (e==null) return;
      e = e.parentNode;
      SwitchVisibility(e);
    } catch (erreur) {
      return erreur;
    }
  } else {    //IE
    try {
      e = window.event.srcElement;
      if (e.parentElement.className=="LSA") return;
      while ((e.className != "c")&&(e!=null)) e = e.parentElement;
      if (e==null) return;
      e = e.parentElement;
      SwitchVisibility(e);
    } catch (erreur) {
      return erreur;
    }
  }
}

// Erase bogus link i nfo from the status window
function h()
{
  window.status=" ";
}

// Dummy function for expand/collapse link navigation - trap onclick events instead
function ex() 
{}

function DownloadDialog(aDescription,aUrl)
{
  var Chaine = "";
  var aFilename = aUrl.substr(0,aUrl.lastIndexOf("."));
  var aContent = document.getElementById("InfoContent");
  Chaine = "Download <b>" + aDescription + "</b><br>";
  Chaine += "<a href=" + RootPlan + aFilename + ".sid>MrSid file (.sid)</a><br>";
  Chaine += "<a href=" + RootPlan + aFilename + ".sdw>World file (.sdw)</a>";
  aContent.innerHTML = Chaine;
  ShowPopupWindow(PopupWnd);
}

function DownloadLink(aDescription, aUrl)
{
  var Chaine = "";
  var aFilename = aUrl.substr(0,aUrl.lastIndexOf("."));
  var aContent = document.getElementById("InfoContent");
  Chaine = "Link to the Karttjenester <b>" + aDescription + "</b><br/>Copy the entire link bellow<br/>";
  Chaine += aUrl + "<br/>or<br/>";
  Chaine += 'Open in a new <a href="#" onclick="ShowMap(\'' + aUrl + '\')">Window</a>';
  aContent.innerHTML = Chaine;
  ShowPopupWindow(PopupWnd);
}

function ShowLegend(aFilename,aName) {
  if ((WinPlanLegend==null)||(WinPlanLegend.closed)) {
    WinPlanLegend = window.open("","WinPlanLegend","width=800,height=600,scrollbars=yes,resizable=yes");
  }
  WinPlanLegend.document.open();
  WinPlanLegend.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><head><title>Tegnforklaring</title>');
  WinPlanLegend.document.writeln('<style type="text/css">');
  WinPlanLegend.document.writeln('@import url("../Styles/Global.css");')
  WinPlanLegend.document.writeln('@import url("../Styles/MainMenu.css");</style>');
  WinPlanLegend.document.writeln('</head>');
  WinPlanLegend.document.writeln('<body onload="window.focus()">');
  WinPlanLegend.document.writeln('<b>Tegnforklaring av </b><a href="' + RootLegend + aFilename + '">' + aName + '</a><br>');
  WinPlanLegend.document.writeln('<img src="' + RootLegend + aFilename + '">');
  WinPlanLegend.document.writeln('</body></html>');
  WinPlanLegend.document.close();
}

function ShowFile(aFilenameWithPath,aTitle) {
  NewWin = window.open(aFilenameWithPath,aTitle,"width=800,height=600,scrollbars=yes,resizable=yes");
}

function ShowMap(aURL) {
  window.open(aURL,"","width=1024,height=768,resizable=yes");
}

//Stupide?
function openMap(aUrl) {
  window.open(aUrl,"","scrollbars=yes,resizable=yes,width=1024,height=768");
}

// Set the onclick handler
document.onclick = cl;

