var mapLoaded = false;
var mapVisible = false;
var mapQueryString = "";
var mapPointToLoad = 0;

$(document).ready(function(){
  catchUpSearch();
  catchUpCategory();
  $('#list-body a.user').openLayer(function(){
    $('a.creator').openCreator();
  });

  // Opisy kategorii
  $('#descriptions div.links a').click(function(e){
    if ($(this).attr('class')=='close') {
      $('#descriptions').hide('fast');
      return false;
    }
    $('#descriptions a').css('cursor', 'progress');
    showIndicator(e);
    $('#descriptions div.content').load("z-category-description.php"+shortAddress($(this).attr('href')), function() { 
      $('#descriptions a').css('cursor', 'pointer');
      hideIndicator();
    });
    return false;
  });

  // Przechwytujemy refresh
  refreshAction = function() {
    showIndicator();
    var p = $("a#link").attr("link");
    $("#list").load(p, function() {
      hideIndicator();
      catchUpList();
      catchUpHistory();
    });
    return false;
  };

})

function catchUpCategory() {
  if ($("#map").css("display")=="block") {
    $("#minimap span.more").hide();
    $("#minimap span.less").show();
  }
  else {
    $("#minimap span.less").hide();
    $("#minimap span.more").show();
  }
  $("#minimap").click(function() {
    if ($("#map").css("display")=="block")
      tHref = "hidemap";
    else
      tHref = "showmap";
    pageload(tHref);
    document.location = "#"+tHref;
  });
  $("#map-head a.layer").openLayer(function(){
    $("a.user").openLayer(function(){
      $("a.creator").openCreator();
    });
  });
  $("#map-head a.creator").openCreator();
}

function catchUpList() {
  // Przechwytuje zdarzenia na liście
  $("a.photo").openPhoto();
  $('#list-body a.creator').openCreator();
  $('#list-body a.user').openLayer(function(){
    $('a.creator').openCreator();
  });
  $('#list-head a, #list-foot a').click(function(e) {
//    $(window).scrollTop($('#list-head').offset().top);
    showIndicator(e);
    var p = shortAddress($(this).attr('href'));
    $("#list").load("z-poi-list.php"+p, function() { 
      hideIndicator();
      catchUpList();
      catchUpHistory();
    });
    return false;
  });
}

function catchUpCategoryTree() {
  $('#tree-box-m a').click(function(e){
    if ($("#map").css("display")!="block") return true;
    if (mapLoaded) {
      $("#map").show();
      $('#map').animate({height: 600}, "fast");
    }
    tHref = shortAddress($(this).attr('href'));
    mapQueryString = tHref;
    showIndicator(e);
//      window.mapframe.myMemo.queryString = tHref;
//      window.mapframe.myMemo.refresh();
    $("#tree-box-m").load("z-public-tree.php"+tHref, function(){
      catchUpCategoryTree();
      $("#list").load("z-poi-list.php"+tHref, function() { 
        catchUpList();
        catchUpHistory();
        hideIndicator();
      });
      $.get("z-title.php"+tHref,function(a){document.title=a;});
      $("#map-head").load("z-map-head.php"+tHref, catchUpCategory);
      $("#path").load("z-path.php"+tHref);
      $("#search-box").load("z-search.php"+tHref, catchUpSearch);
    });
    return false;
  });
}