var google_ad_client = "pub-7251478393613561";
var google_ad_channel = "8220287810";
var google_ad_width = 160;
var google_ad_height = 600;
var google_ad_format = "160x600_as";
var google_ad_type = "text_image";
var google_alternate_color = "79C6D0";
var google_color_border = "8898BF";
var google_color_bg = "FFFFFF";
var google_color_link = "0000CC";
var google_color_text = "000000";

var javaVersion = 'unk';
var appletReady = -1;
var actID = -1;
var diagramReady = -1;
var namesNumbered = 's';
var globNum = true;
var currCol = -1;

function getWindowHeight() {
  if (window.self && self.innerHeight)
    return self.innerHeight;
  if (document.documentElement && document.documentElement.clientHeight)
    return document.documentElement.clientHeight;
  return 600;
}

function getWindowWidth() {
  if (window.self && self.innerWidth)
    return self.innerWidth;
  if (document.documentElement && document.documentElement.clientWidth)
    return document.documentElement.clientWidth;
  return 600;
}

function getActId () {
  return actID;
}

function activate_wms() {
  var today = new Date()
  var expire = new Date()
  expire.setTime(today.getTime() + 1000*60*60*24*92)
  document.cookie = "wms=true; expires=" + expire.toGMTString()
}

function call_javainfo() {
  document.writeln('<applet id="viewer" mayscript codebase="../java" code="javainfo/JavaInfo.class" name="JavaInfo" width="0" height="0" vspace="0" hspace="0">')
  document.writeln('</applet>')
}

function load_diagram(country,lang) {
  var version = "unk";
  try { 
     version = document.JavaInfo.getVersion();
  } catch (ex) {
  }
  if ((version == "unk") || (version < '1.2'))
    return;
  document.writeln('<applet id="diagram" mayscript codebase="../java14" code="diagram/WorldCityMapDiagramApplet.class" archive="diagram.jar" name="DiagramApplet">')
  document.writeln('<param name="country" value="'+country+'" />')
  document.writeln('<param name="language" value="'+lang+'" />')
  document.writeln('<param name="rsc" value="worldcitydiagram_messages">')
  document.writeln('</applet>')
}

function start_mapviewer(file,country,maxscale,minscale,scale,basescale,width,height,lang) {
  var version = "unk";
  try { 
     version = document.JavaInfo.getVersion();
  } catch (ex) {
  }
  if ((version == "unk") || (version < '1.2')) {
    document.writeln('<p>For maps and diagrams, Java 1.2 or higher are required!</p>');
    return;
  }
  document.writeln('<applet id="mapviewer" mayscript codebase="../java14" code="mapviewer/WorldMapViewer.class" archive="countrymapviewer14.jar" name="ShowMap" ">')
  document.writeln('<param name="country" value="'+country+'">')
  document.writeln('<param name="url" value="Daten5/'+file+'.zip">')
  document.writeln('<param name="maxscale" value="'+maxscale+'">')
  document.writeln('<param name="minscale" value="'+minscale+'">')
  document.writeln('<param name="scale" value="'+scale+'">')
  document.writeln('<param name="basescale" value="'+basescale+'">')
  document.writeln('<param name="viewwidth" value="'+(width-190)+'">')
  document.writeln('<param name="viewheight" value="'+(height-10)+'">')
  document.writeln('<param name="language" value="'+lang+'">')
  document.writeln('<param name="provinces" value="yes">')
  document.writeln('<param name="browser" value="'+navigator.appname+'">')
  document.writeln('<param name="agent" value="'+navigator.userAgent+'">')
  document.writeln('<param name="configfile" value="worldcitymapviewer.zip">')
  document.writeln('<param name="rsc" value="worldcityviewer_messages">')
  document.writeln('</applet>')
}

function showHelp (langID) {
  window.open('../info/appletinfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
}

function setAppletReady () {
  appletReady = 1
}

function sym(id) {
  if (id != actID) {
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null)
        cell.style.backgroundColor = oldBackground
    }
    actID = id
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null) {
        oldBackground = cell.style.backgroundColor
        cell.style.backgroundColor = '#ffcc33'
        changeToMap();
      }
      else
        actID = -1
    }
  }
  if (appletReady == 1)
    document.ShowMap.movePosByApplet(id)
}

function showID (newActID) {
  if (newActID != actID) {
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null)
        cell.style.backgroundColor = oldBackground
    }
    actID = newActID
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null) {
        oldBackground = cell.style.backgroundColor
        cell.style.backgroundColor = '#ffcc33'
        cell.scrollIntoView()
      }
      else {
        actID = -1
      }
    }
  }
}

function Row (value, row) {
  this.value = value
  if (this.value == '...')
    this.value = -1;
  this.row = row
}

function value (cell, num) {
  if (!num)
    return cValue(cell);
  txt = cell.innerHTML;
  txt1 = "";
  for (var i=0; i<txt.length; i++) {
    ch = txt.charAt(i);
    if (ch != ",")
      txt1 += String.fromCharCode(txt.charCodeAt(i));
  }
  return txt1
}

function compare(a, b) {
  if (globNum) {
    var diff = b.value-a.value;
    if (diff != 0)
      return diff;
    else
      return a.row.getAttribute("ordNum")-b.row.getAttribute("ordNum")
  }
  if (a.value < b.value)
    return -1;
  else if (a.value == b.value)
    return a.row.getAttribute("ordNum")-b.row.getAttribute("ordNum")
  return 1
}

function sort (column, num) {
  var table = document.getElementById('tl');
  if (table == null)
    return;
  var tableBody = table.firstChild;
  if (tableBody.nodeName.toUpperCase() != "TBODY")
    tableBody = table.childNodes[1];
  if (tableBody.nodeName.toUpperCase() != "TBODY") {
    alert("For this browser, sorting is not supported!");
    return;
  }
  if (namesNumbered != 'f') {
    for (var i=0; i<table.rows.length-1; i++)
      table.rows[i].setAttribute('ordNum',i);
    namesNumbered = 'f';
  }
  globNum = num;
  var data = new Array();
  var max = table.rows.length-1;
  for (var i=1; i<max; i++)
    data[i-1] = new Row(cValue(table.rows[i].cells[column],num),table.rows[i]);
  data.sort(compare)
  for (var i=0; i<max-1; i++)
    tableBody.appendChild(data[i].row);
  tableBody.appendChild(table.rows[1]);
}



function show_diagram (id,minusRows) {
  if (diagramReady < 0)
    return;
  var table = document.getElementById(id);
  if (table == null)
    return;
  document.DiagramApplet.openDiagram(id)
}

function setDiagramAppletReady () {
  diagramReady = 0
}

function setDiagramAppletData () {
  setData('tl',1)
  document.DiagramApplet.openDiagram('tl')
}

function cValue (cell, num) {
  if (num) {
    txt = cell.innerHTML;
    txt1 = "0";
    for (var i=0; i<txt.length; i++) {
      ch = txt.charAt(i);
      if ((ch >= '0') && (ch <= '9'))
        txt1 += String.fromCharCode(txt.charCodeAt(i));
    }
    return parseInt(txt1,10);
  }
  else {
    if (cell.firstChild == null)
      txt = cell.innerHTML;
    else if (cell.firstChild.nodeName.toLowerCase() == "a")
      txt = cell.firstChild.firstChild.nodeValue;
    else
      txt = cell.innerHTML;
    return txt
  }
}

function setData (id,minusRows) {
  var table = document.getElementById(id);
  if (table == null)
    return;
  var max = table.rows.length-minusRows;
  var colNum = table.rows[0].cells.length;
  header = "ID##Name##"
  for (var j=2; j<colNum-1; j++) {
    var cellValue = table.rows[0].cells[j]
    header +=  cValue(cellValue,false)+ '##';
  }
  var cells = "";
  for (var i=1; i<max; i++) {
    cells += table.rows[i].cells[1].getAttribute('id') + "##";
    for (var j=1; j<colNum-1; j++)
      cells += cValue(table.rows[i].cells[j], j>3) + "##";
  }
  document.DiagramApplet.setData(id,header,cells,max,colNum-1)
  if (currCol >= 0)
    document.DiagramApplet.selectValueColumn(currCol);
}

function showDiagramHelp (lang) {
  window.open('../info/diagraminfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
}

function showObjectDiagram(id,type) {
  changeToDiagram();
}

function showLegendWindow(page, lang) {
  if (lang == 'de')
    window.open('legend/'+page+'_d.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width=550,height=990')
  else
    window.open('legend/'+page+'.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width=550,height=990')
}

function changeToDiagram() {
  var viewer = document.getElementById("mapviewer");
  viewer.style.visibility = 'hidden';
  var diagr = document.getElementById("diagram");
  diagr.style.visibility = 'visible';
  sym(-1)
}

function changeToMap() {
  var diagr = document.getElementById("diagram");
  diagr.style.visibility = 'hidden';
  var viewer = document.getElementById("mapviewer");
  viewer.style.visibility = 'visible';
}