//<![CDATA[

/* 
Events in Google Maps einzeichnen
Stephan Karpischek kpi@kpi.at 2006
*/

var map = null;
var cmark = null;
var geocoder = null;
var b_lat = 52.534394;
var b_lng = 13.412247;
var b_zoom = 11;
/*
var iconneu = new GIcon(G_DEFAULT_ICON,'s.png');
iconneu.iconSize = new GSize(28, 30);
var icon2005 = new GIcon(G_DEFAULT_ICON,'2005.png');
icon2005.iconSize = new GSize(28, 30);
var icon2006 = new GIcon(G_DEFAULT_ICON,'2006.png');
var icon2007 = new GIcon(G_DEFAULT_ICON,'2007.png');
var icon2008 = new GIcon(G_DEFAULT_ICON,'2008.png');
var iconneu = new GIcon(G_DEFAULT_ICON,'neu.png');
var icon2005 = new GIcon(G_DEFAULT_ICON,'05.png');
var icon2006 = new GIcon(G_DEFAULT_ICON,'06.png');
var icon2007 = new GIcon(G_DEFAULT_ICON,'07.png');
var icon2008 = new GIcon(G_DEFAULT_ICON,'08.png');
*/
var iconneu = new GIcon(G_DEFAULT_ICON,'neuertatortpieker.png');
var icon2005 = new GIcon(G_DEFAULT_ICON,'image-001-test/2005pieker.png');
var icon2006 = new GIcon(G_DEFAULT_ICON,'image-001-test/2006pieker.png');
var icon2007 = new GIcon(G_DEFAULT_ICON,'image-001-test/2007pieker.png');
var icon2008 = new GIcon(G_DEFAULT_ICON,'image-001-test/2008pieker.png');
var icon2009 = new GIcon(G_DEFAULT_ICON,'image-001-test/2009pieker.png');
var icon2010 = new GIcon(G_DEFAULT_ICON,'image-001-test/2010pieker.png');
var icon2011 = new GIcon(G_DEFAULT_ICON,'image-001-test/2011pieker.png');
var icon2012 = new GIcon(G_DEFAULT_ICON,'image-001-test/2012pieker.png');
var actili = null;
var actim = null;
var current_district = 0;
var markers = [];

function getReq() {
  var xmlHttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
    try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
      xmlHttp = false;
    }
  }
  @end @*/

  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
  return xmlHttp;
}

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(b_lat,b_lng), b_zoom);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    //geocoder = new GClientGeocoder();
    showb();
  }
}

function load_scenes(year, district) {
    GDownloadUrl("scenes.php?y=" + year + "&d=" + district, function(data, responseCode) {
      p = eval(data);
      for (var i = 0; i < p.length; i++) {
        addpoint(p[i]);
        addli(p[i]);
      }
    });
}


function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address + ", Berlin",
      function(point) {
        if (!point) {
          alert(address + " in Berlin nicht gefunden");
        } else {
          map.setCenter(point,14);
/*              map.panTo(point);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address + " " + point.toUrlValue());
*/
        }
      }
    );
  }
}

function goto(lat,lng) {
  if (map) {
    map.setZoom(13);
    map.panTo(new GLatLng(lat,lng));
  }
}

function showb() {
  if (map) {
    map.clearOverlays();
    map.setZoom(b_zoom);
    map.panTo(new GLatLng(b_lat,b_lng));
    load_scenes(current_year,0);
  }
  table = document.getElementById('detail');
  while (table.childNodes[0]) {
    table.removeChild(table.childNodes[0]);
  }
  GDownloadUrl("details.php", function(data, responseCode) {
    p = eval(data);
    for (var i = 0; i < p.length; i++) {
      tr = createTR(p[i]);
      table.appendChild(tr);
    }
  });
}

function showd(district) {
  if (actim) {
    map.removeOverlay(actim);
    actim = null;
  }
  if (actili) {
    actili = null;
  }
  ul = document.getElementById('ul');
  while (ul.childNodes[0]) {
    ul.removeChild(ul.childNodes[0]);
  }
  table = document.getElementById('detail');
  while (table.childNodes[0]) {
    table.removeChild(table.childNodes[0]);
  }
  GDownloadUrl("details.php?d=" + district + "&y=" + current_year, function(data, responseCode) {
    p = eval(data);
    for (var i = 0; i < p.length; i++) {
      addli(p[i]);
      tr = createTR(p[i]);
      table.appendChild(tr);
    }
  });
}

function showy(year) {
  if (map) {
    map.clearOverlays();
    var ul = document.getElementById('ul');
    while (ul.childNodes[0]) {
      ul.removeChild(ul.childNodes[0]);
    }
    load_scenes(year,0);
    current_year = year;
    document.getElementById("current_year").innerHTML = year;

  }
}

function unsetmarker() {
  if (map) {
  if (cmark) {
    map.removeOverlay(cmark);
  }
  }
  if (document.getElementById) {
    document.getElementById("addform").style.display = "none";
    document.getElementById("detail").style.display = "block";
    document.getElementById("disclaim").style.display = "block";
  }
}

function setmarker() {
  var center = map.getCenter();
  cmark = new GMarker(center, {draggable:true,bouncy:false,title:'drag me!'});
  map.addOverlay(cmark);
  GEvent.addListener(cmark, "dragend", function getcmarkpoint() {
    document.forms[0].lat.value = cmark.getPoint().lat();
    document.forms[0].lng.value = cmark.getPoint().lng();
  });
  document.forms[0].lat.value = center.lat();
  document.forms[0].lng.value = center.lng();
  if (document.getElementById) {
    document.getElementById("detail").style.display = "none";
    document.getElementById("disclaim").style.display = "none";
    document.getElementById("addform").style.display = "block";
  }
}

function postform() {
  savereq = getReq();
  var form = document.f;
  var qs = new Array();
  for (var i=0;i<form.length;i++) {
    element = form.elements[i];  
    if (element.name != '' && element.name != 'undefined') {
      qs.push(element.name+'='+element.value);
    }
  }
  qs = qs.join('&');

  savereq.open('POST','add.php',true);
  savereq.onreadystatechange = addresponse;
  savereq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
  savereq.send(qs);
  return false;
}

function addresponse() {
  if (savereq.readyState == 4) {
    unsetmarker();
    var p = eval('(' + savereq.responseText + ')');
    addpoint(p);
    addli(p);
    sd(p.id);
  }
}

function sd(id) {
  if (actim) {
    map.removeOverlay(actim);
  }
  if (markers[id]) {
    point = markers[id].getPoint();
    actim = new GMarker(point,iconneu);
    map.addOverlay(actim);
  }
  if (actili) {
    actili.style.color = 'black';
/*    p = actili.lastChild;
    actili.removeChild(p);
    */
  }
  actili = document.getElementById(id);
  if (actili) {
    actili.style.color = 'red';
  }

  table = document.getElementById('detail');
  while (table.childNodes[0]) {
    table.removeChild(table.childNodes[0]);
  }

  var url = "detail.php?id=" + id;
  GDownloadUrl(url, function(data, responseCode) {
    details = eval('(' + data + ')');
    tr = createTR(details);
    table.appendChild(tr);
  });
}

function createTR(details) {
    tr = document.createElement('tr');
    td = document.createElement('td');
    p1 = document.createElement('p');
    p1.innerHTML = details.sdate + ', ' + details.place;
    td.appendChild(p1);
    p2 = document.createElement('p');
    p2.innerHTML = details.desc;
    td.appendChild(p2);
    p3 = document.createElement('p');
    p3.innerHTML = details.source;
    td.appendChild(p3);
    tr.appendChild(td);
    return tr;
}

function addpoint(p) {
    var point = new GLatLng(parseFloat(p.lat),
                            parseFloat(p.lng));
    if (p.y == '2005') {
      icon = icon2005;
    } else if (p.y == '2006') {
      icon = icon2006;
    } else if (p.y == '2007') {
      icon = icon2007;
    } else if (p.y == '2008') {
      icon = icon2008;
    } else if (p.y == '2009') {
      icon = icon2009;
    } else if (p.y == '2010') {
      icon = icon2010;
    } else if (p.y == '2011') {
      icon = icon2011;
    } else if (p.y == '2012') {
      icon = icon2012;
    }
    markers[p.id] = new GMarker(point,{icon:icon,title:p.t,year:p.y});
    GEvent.addListener(markers[p.id], "click", function() {
      goto(p.lat,p.lng);
      sd(p.id);
    });
    map.addOverlay(markers[p.id]);
}

function addli(p) {
  var ul = document.getElementById('ul');
  var li = document.createElement('li');
  li.id = p.id;
  li.innerHTML = '<a style="text-decoration:none" href="javascript:goto(' + p.lat + ', ' + p.lng + '); sd(' + p.id + ');">' + p.t + '</a>';
  ul.appendChild(li);
}



//]]>

