/*function load ()
{
  if (window.opener)
  {
    window.opener.sourceFilter.load ();
  }
}*/

function hbSourceFilter (settings, opener)
{
  this.settings = settings;
//  this.openedWindow = null;
  this.sourceGroups = new Object ();
//  this.opener = opener;

  var groupSettings = this.settings.get ("group");

  for (var g in groupSettings)
  {
    var group = groupSettings [g];
    var groupname = group ["name"];
    this.sourceGroups [groupname] = new Array ();
    this.sourceGroups [groupname].caption = group ["caption"];
    var sourceSettings = group ["source"];

    for (var s in sourceSettings)
    {
      var source = sourceSettings [s];
      this.sourceGroups [groupname].push (new hbSource (source));
    }
  }
}

hbSourceFilter.prototype.isInitialized = function ()
{
  return true;
}

hbSourceFilter.prototype.getSourceForLayer = function (layername)
{
  for (var g in this.sourceGroups)
  {
    var group = this.sourceGroups [g];

    for (var s = 0; s < group.length; ++s)
    {
      var source = group [s];

      if (source.hasLayer (layername))
      {
        return source;
      }
    }
  }

  return null;
}
/*
hbSourceFilter.prototype.openWindow = function ()
{
//  if (null != this.openedwindow)
//  {
//    for (var s in this.sources)
//    {
//      var source = this.sources [s];
//      source.resetvalue ();
//    }
//  }

  if ((null == this.openedWindow) || (true == this.openedWindow.closed))
  {
    this.openedWindow = window.open ("sourceFilter.html", "sourceFilterWindow",
        "width=448, height=704, resizable=yes, scrollbars=yes");

//    for (var s in this.sources)
//    {
//      var source = this.sources [s];
//      source.reset ();
//    }
  }
}*/
/*
hbSourceFilter.prototype.load = function ()
{
  var ff = this.openedWindow.document.getElementById ("filterForm");

  if (!ff)
  {
    ff = this.openedWindow.document.createElement ("form");
  }

  if (!ff)
  {
    return;
  }

  ff.id = "filterForm";
  ff.name = "selection";
  ff.action = "javascript:window.opener.sourceFilter.submit()";

  var table = this.openedWindow.document.createElement ("table");
  var rowCounter = 0;

  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];
    var captionElem = this.openedWindow.document.createTextNode (sourceGroup.caption);

    var row = table.insertRow (rowCounter++);
    var firstCol = row.insertCell (0);
  //  firstCol.style.whiteSpace = "nowrap";
  //  firstCol.appendChild (this.openedWindow.document.createElement ("hr"));
    var formattedFirstCol = this.openedWindow.document.createElement ("font");
    formattedFirstCol.color = "white";
    formattedFirstCol.face = "Arial";
    formattedFirstCol.size = "3";
    formattedFirstCol.appendChild (captionElem);
    var center = this.openedWindow.document.createElement ("center");
    var bold = this.openedWindow.document.createElement ("b");
    center.appendChild (bold);
    bold.appendChild (formattedFirstCol);
    firstCol.appendChild (center);

  //  var lastCol = row.insertCell (1);
  //  lastCol.appendChild (this.openedWindow.document.createElement ("hr"));

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      var sourceElem = source.getSourceElement (this.openedWindow.document);
      var captionElem = source.getCaptionElement (this.openedWindow.document);
      var filterElems = source.getFilterElements (this.openedWindow.document);

      var row = table.insertRow (rowCounter++);
      var firstCol = row.insertCell (0);
      var formattedFirstCol = this.openedWindow.document.createElement ("font");
      formattedFirstCol.color = "white";
      formattedFirstCol.face = "Arial";
      formattedFirstCol.size = "3";
      formattedFirstCol.appendChild (sourceElem);
      formattedFirstCol.appendChild (captionElem);
      firstCol.appendChild (formattedFirstCol);

      var lastCol = row.insertCell (1);
      lastCol.align = "right";

      if (filterElems)
      {
        for (var t in filterElems)
        {
          lastCol.appendChild (filterElems [t]);
        }
      }
    }

    row = table.insertRow (rowCounter++);
    firstCol = row.insertCell (0);
    firstCol.appendChild (this.openedWindow.document.createElement ("br"));
  }

  ff.appendChild (table);

  var emptyParagraph = this.openedWindow.document.createElement ("p");
  ff.appendChild (emptyParagraph);

  var submit = this.openedWindow.document.createElement ("input");
  submit.type = "submit";
  submit.value = "OK";
  ff.appendChild (submit);

  var emptyText = this.openedWindow.document.createTextNode (" ");
  ff.appendChild (emptyText);

  var cancel = this.openedWindow.document.createElement ("input");
  cancel.type = "button";
  cancel.value = "Annuleren";
  cancel.onclick = function () {window.sourceFilter.cancel()};
  ff.appendChild (cancel);

  this.openedWindow.document.body.appendChild (ff);

  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      source.refresh ();
    }
  }
}*/

hbSourceFilter.prototype.getActiveLayer = function ()
{
  var layer = new Array ();

  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      if (true == source.isChecked ())
      {
        var newLayer = source.getActiveLayer ();

        if ((newLayer) && (null != newLayer.layername) && (0 < newLayer.layername.length))
        {
          layer.push (newLayer);
        }
      }
    }
  }

  return layer;
}

hbSourceFilter.prototype.saveCookies = function ()
{
  //var cookies = this.opener.vcnlinfoCookies;
  var cookies = vcnlinfoCookies;

  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      if (true == source.isChecked ())
      {
        cookies.set ("source_" + source.getName (), "enabled");

        if ("" != source.getActiveFilterName ())
        {
          cookies.set ("filter_" + source.getName (), source.getActiveFilterName ());
        }
      }
      else
      {
        cookies.set ("source_" + source.getName (), "disabled");
      }
    }
  }
}
/*
hbSourceFilter.prototype.submit = function ()
{
  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      try
      {
        source.storeValue ();
      }
      catch (exception)
      {
        this.openedWindow.alert ("An error occured: " + exception.toString () + "\nPlease correct your input or close dialogue by cancelling submission.");
        return;
      }
    }
  }

  this.saveCookies ();

  var layer = new Array ();
  
  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      if (true == source.isChecked ())
      {
        var newLayer = source.getActiveLayer ();

        if ((newLayer) && (null != newLayer.layername) && (0 < newLayer.layername.length))
        {
          layer.push (newLayer);
        }
      }
    }
  }

  this.opener.map.redraw (layer);
  this.openedWindow.close ();
}*/
/*
hbSourceFilter.prototype.cancel = function ()
{
  if (null != this.openedWindow)
  {
    this.openedWindow.close ();
  }
}*/

hbSourceFilter.prototype.activateOnly = function (layer)
{
  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      source.activateOnly (layer);
    }
  }

  this.saveCookies ();
}

hbSourceFilter.prototype.activate = function (layer)
{
  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      source.activate (layer);
    }
  }

  this.saveCookies ();
}

hbSourceFilter.prototype.deactivate = function (layer)
{
  for (var g in this.sourceGroups)
  {
    var sourceGroup = this.sourceGroups [g];

    for (var s in sourceGroup)
    {
      var source = sourceGroup [s];

      if (false == source instanceof hbSource)
      {
        continue;
      }

      source.deactivate (layer);
    }
  }

  this.saveCookies ();
}
