// Mapa
var myMap, tHref;
function initLite() {
  $.getJSON("c/point.php"+tHref, function(json){
    if (json.status!='0') {
      myMap = new TargeoLite({e: 'map',w: '410',h: '300',x: json.x,y: json.y,z: '19'});
      $("#point-map").show();
      showPoints(json);
    }
  });
}
function showPoints(json) {
  if (!json.i) json.i = 0;
  if (json.ox!=json.x || json.oy!=json.y)
    myMap.createPoint(
      'old-poi-on-map',
      {x:parseFloat(json.ox),y:parseFloat(json.oy)},
      {url:'http://i.miplo.pl/'+json.o_i+'.bmp',w:16,h:16,x:0,y:29,a:false}
    );
  myMap.createPoint(
    'poi-on-map',
    {x:parseFloat(json.x),y:parseFloat(json.y)},
    {url:'http://i.miplo.pl/'+json.i+'.bmp',w:16,h:16,x:0,y:29,a:false}
  );
  $("#old-poi-on-map").css({"background-image":"url('http://g.miplo.pl/poi-normal.gif')","padding":"3px 3px 10px 3px"});
  $("#poi-on-map").css({"background-image":"url('http://g.miplo.pl/poi-"+json.status+".gif')","padding":"3px 3px 10px 3px"});
}
function addNote(e) {
  $('#new-note').attr({'disabled':'disabled'});
  showIndicator(e);
  $.post('/c/note.php', {
    'p': $('#p').val(),
    's': $('#sugg').val(),
    'n': $('#n').val()
  }, function(r){
    hideIndicator(e);
    if (r=='OK') {
      $("#point-user").load("z-point-user.php"+tHref, function(){
        $("#point-user div.rounded").corner();
        $("#point-user .creator").openCreator();
        $("#point-user .layer").openLayer();
        $("#new-note").click(addNote);
      });
    }
    else alert(r);
    $('#new-note').removeAttr('disabled');
  });
}

$(document).ready(function(){
  // zmienne z adresu
  tHref = shortAddress($(location).attr('href'));

  // notatka
  $("#new-note").click(addNote);

  // Przechwytujemy refresh
  refreshAction = function(a) {
    if (a=='del') {
      document.location = 'index.php';
      return;
    }
    $('#indicator').hide();
    $("#point-data").load("z-point-data.php"+tHref, function(){
      $("#point-data div.rounded").corner();
    });
    $("#point-user").load("z-point-user.php"+tHref, function(){
      $("#point-user div.rounded").corner();
      $("#point-user .creator").openCreator();
      $("#point-user .layer").openLayer();
      $("#new-note").click(addNote);
    });
    $.getJSON("c/point.php"+tHref, function(json){
      if (json.status!='0') {
        myMap.destroyPoint('poi-on-map');
        myMap.destroyPoint('old-poi-on-map');
        showPoints(json);
        myMap.moveTo({x:json.x,y:json.y},0);
      }
    });
  };

  // Opisy punktów
  $('#descriptions div.links a').click(function(e){
    showIndicator(e);
    $('#descriptions div.content').load("z-point-description.php"+shortAddress($(this).attr('href')), hideIndicator);
    return false;
  });

  // Mapa
  jQuery.getScript("http://maps.targeo.pl/Targeo.html?vn=1_9_1&v=lite&k=NzhkMzZlNWIwNGM5OGJhNzRhMDExNGFlYzBhNGRlMTdkZGE0NmExOA==&f=initLite");
})