////////////////////////////////////////////////////////////////////
// Continent class
Continent.CLASS_NAME = 'Continent';

// Static vars/objs
Continent.htObj = new Hashtable();
Continent.htContinentRegion = new Hashtable();

// XML constructor
function Continent(id, name, numConids) {
  this.id = id;
  this.name = name;
  this.mapFile = "../images/countries/map"+this.id+".gif";

  this.numConids = numConids;

  Continent.htObj.put(this.id, this);
}

// Static method to hide the regions of this continent
Continent.hideRegions = function(continentId) {
  var htRegion = Continent.htContinentRegion.get(continentId);
  if (htRegion == undefined) {
    return;
  }

  // Change the onclick event to show the regions
  var tdClk = $('click'+continentId);
  tdClk.setAttribute("href", "javascript:Continent.showRegions('" + continentId +"')");

 var areaSelectTbl = $('areaSelectTbl');

  var tblRow = $('Continent'+continentId);
  var rowIndex = tblRow.rowIndex+1;
  for (var k in htRegion.hash) {
    Region.hideCountries(k);
  }

  for (var k in htRegion.hash) {
    areaSelectTbl.deleteRow(rowIndex);
  }

}

// Static method to show the countries of this continent
Continent.showCountries = function(continentId) {
  var htCountry = Region.htRegionCountry.get(continentId);
  if (htCountry == undefined) {
    return;
  } 
    
  // Change the onclick event to hide the regions
  var tdClk = $('click'+continentId);
  tdClk.setAttribute("href", "javascript:Continent.hideRegions('" + continentId +"')");

  var areaSelectTbl = $('areaSelectTbl');

  var tblRow = $('Continent'+continentId);
  var rowIndex = tblRow.rowIndex+1;
  var i=0;
  for (var k in htCountry.hash) {
    var c = Country.htObj.get(k);
    var row = areaSelectTbl.insertRow(rowIndex+i);
    row.setAttribute("id", "Country"+k);
    row.setAttribute("class", "white");
    row.setAttribute("className", "white");

    var cell = row.insertCell(0);
    cell.innerHTML = "&nbsp;";
    cell = row.insertCell(1);
    cell.innerHTML = '&nbsp;';
    cell = row.insertCell(2);
    cell.innerHTML = c.name;
    cell = row.insertCell(3);
    cell.innerHTML = c.numConids;
    cell.setAttribute("align", "right");
    cell = row.insertCell(4);
    cell.innerHTML = '<input type="checkbox">';

    i++;
  }
}

// Static method to show the regions of this continent
Continent.showRegions = function(continentId) {
  var htRegion = Continent.htContinentRegion.get(continentId);
  if (htRegion == undefined) {
    return;
  }

  // Change the onclick event to hide the regions
  var tdClk = $('click'+continentId);
  tdClk.setAttribute("href", "javascript:Continent.hideRegions('" + continentId +"')");

  var areaSelectTbl = $('areaSelectTbl');

  var tblRow = $('Continent'+continentId);
  var rowIndex = tblRow.rowIndex+1;
  var i=0;
  for (var k in htRegion.hash) {
    var r = Region.htObj.get(k);
    var row = areaSelectTbl.insertRow(rowIndex+i);
    row.setAttribute("id", "Region"+k);
    row.setAttribute("class", "white");
    row.setAttribute("className", "white");

    var cell = row.insertCell(0);
    cell.innerHTML = "&nbsp;";
    cell = row.insertCell(1);
    cell.innerHTML = '<a id="click' + k + '" href="javascript:Region.showCountries(\'' + k + '\')">' + r.name + '</a>';
    cell = row.insertCell(2);
    cell.innerHTML = "&nbsp";
    cell = row.insertCell(3);
    cell.innerHTML = r.numConids;
    cell.setAttribute("align", "right");
    cell = row.insertCell(4);
    cell.innerHTML = '<input type="checkbox">';

    i++; 
  }
}

// Static method to map regions to continents
Continent.addRegion = function(continentId, regionId) {
  var htRegion = Continent.htContinentRegion.get(continentId);
  if (htRegion == undefined) {
    htRegion = new Hashtable();
    Continent.htContinentRegion.put(continentId, htRegion);
  }
  htRegion.put(regionId, regionId);
}

Continent.prototype.getText = function() {
  var func = 'Continent.showRegions';
  if (this.id == '_ME' || this.id == '_SA' || this.id == '_OTH') {
    func = 'Continent.showCountries';
  }
  var text = '<tr class="white" id="Continent' + this.id + '"><td><a id="click' + this.id + '" href="javascript:' + func + '(\'' + this.id + '\')">' + this.name + '</a></td><td>&nbsp;</td><td>&nbsp;</td>' +
             '<td align=right>' + this.numConids + '</td><input type="checkbox"></td></tr>';
  return text;
}

// Static method
Continent.getSelectText = function() {
  var text = "<div style=\"width:100%; height:100%;background-color:white; overflow:auto\">";
  text += '<table class="resultsTbl" id="areaSelectTbl" style="margin-bottom:0px; width:100%;">';
  text += '<tr class="underlying"><th>Continent</th><th>Region</th><th>Country</th><th width=1%>#</th><th width=1%>&nbsp;</th></tr>';

  for (var k in Continent.htObj.hash) {
    var c = Continent.htObj.get(k);
    text += c.getText();
  }
  text += "</table></div>";
  return text;
}

