var total;
var deleteFromUserPackString = '';
var addToUserPackString = '';

$(window).ready(function() {
  ustaw_tabelke();
});

$(window).resize(function() {
  ustaw_tabelke();
});

function searchBhv(){

$('.ajaxLink').click (function () {
		var href = $(this).attr ('href');
		href.replace ('&ajax','');
		href += '&ajax';
		var target = 'ajxcnt_1';
		$.ajax({
		  type: "GET",
		  url: href,
		  data: '',
		  success: function(msg){

			$('#'+target).html (msg);
			searchBhv();
			ieSomethingStrangeFix();
		},
			error: function (xhr, ajaxOptions, thrownError){
				alert ('Błąd!');
			}
		});
		return false;
	});
  $('.markAll').click (function () {

  });

  $('.mark').click (function (){
    markButton ($(this));
  });
  $('.addByCategory').click (function () {
    var id = $(this).attr('id').split('_')[1];
    if ($(this).attr("checked")){
      var action = 'set';
    } else { action = 'unset'; }
    $.ajax({
      type: "GET",
      url: "ajax/markToPack.php",
      data: "search="+searchId+"&cat="+id+"&action="+action,
      success: function(msg){
	parseAddToPackAnswer(msg);
      }
    });
  });
  /*
	$("#searchInput").tooltip({
		bodyHandler: function() {
			return $("<div></div>").html('<p><b>Wpisz czego szukasz np.: </b><br /><i>Stacje benzynowe Mazowieckie <br />Sport Częstochowa</i></p>');
		},
		track:true,
		delay: 1000,
		showURL: false
	});

	$("*[id!=searchInput]").tooltip({
		track:true,
		delay: 1000,
		showURL: false
	});
*/
	//go to page on Enter
	$('#pagerInput, #map_pagerInput').keypress(function(e) {
        var c = e.which ? e.which : e.keyCode;
		if (c == 13) {
            this.blur();
			var nextPage = this.value;
            var url = location.href;
			if (url.indexOf('page=') >= 0) {
				var gotoPage = url.replace(/page=[0-9]+/,'page='+nextPage);
			} else {
				var gotoPage = url + '&page=' + nextPage;
			}
			location.href = gotoPage;
        }
    });
    $('#mapall').click (function () {

      showAllOnTheMap();
    });

}
$(function (){
  searchBhv();
});

function markButton (o) {
    var id  =o.attr('id').split('_')[2];

    if (o.hasClass('del')){
	var dta = 'poiid='+id+'&action=unset&search='+searchId;
    }
    if (o.hasClass('add')){
	var dta = 'poiid='+id+'&action=set&search='+searchId;
    }
    if (o.hasClass('addall')){
      var dta = 'all&action=set&search='+searchId;
      o.removeClass ('addall');
      o.addClass ('delall');
    } else {
      if (o.hasClass('delall')){
	var dta = 'all&action=unset&search='+searchId;
	o.removeClass ('delall');
	o.addClass ('addall');
      }
    }
    $.ajax({
      type: "GET",
      url: "ajax/markToPack.php",
      data: dta,
      success: function(msg){
	parseAddToPackAnswer(msg);
      }
    });
  }


function markPoi (id) {

}
var mapPage = 1;
var mapPageLength = 100;
function showAllOnTheMap () {
     $('#result_table').hide('fast');
      $('#resultMap').show('slow');
    showAllPoiOnTheMap();
}
function showAllPoiOnTheMap () {
 	for (var a=(mapPage-1)*mapPageLength; a<pois.length && a<(mapPage)*mapPageLength; a++){
	  console.log (a);
	  var poi = pois[a].split(";");
	  if (poi[4] == 1){
	    var icon = new MapgoIcon();
	    icon.lon = poi[0];
	    icon.lat = poi[1];
	    icon.width = 58; icon.height = 53; icon.off_x=-34; icon.off_y=-25;
	    icon.image = "http://www.mapgo.pl/mapgo/gfx/mapgo_pointer.png"
	    object_list.push(icon);
	  }
	  var icon = new MapgoIcon();
	  icon.lon = poi[0];
	  icon.poi = true;
	  icon.id = poi[3];
	  icon.lat = poi[1];
	  icon.width = 24; icon.height = 24;
	  icon.descr = "<strong>"+poi[5]+"</strong><br />"+poi[6]+" "+poi[7];
	  if (poi[4] == 1){
	    icon.descr += "<button id = 'mapMark_"+poi[3]+"' class = 'mark del' onclick='markButton ($(this))'></button>";
	  } else {
	    icon.descr += "<button id = 'mapMark_"+poi[3]+"'  class = 'mark add' onclick='markButton ($(this))'></button>";
	  }
	  icon.descr += "<button id = 'mapMark_"+poi[3]+"'  class = 'mark map' onclick='markButton ($(this))'></button>";
	  icon.image = adres_ikon_mapgo+poi[2]+"001.png"
	  object_list.push(icon);
      }
	mapobject.removeLayerByName ('sample_objects');
	mapobject.addIconsToMap(object_list,"sample_objects",true);

	 $('#map_pcount').html (parseInt (pois.length / mapPageLength) +1 );
	 $('#map_firstPage').click (function () {mapGotoPage (1)});
	 $('#map_nextPage').click (function () {mapGotoPage (mapPage+1)});
	 $('#map_prevPage').click (function () {mapGotoPage (mapPage-1)});
	 $('#map_lastPage').click (function () {mapGotoPage (parseInt (pois.length / mapPageLength) +1)});
	$('#map_goBack').click (function () { $('#resultMap').hide('fast'); $('#result_table').show('slow'); });
}
function mapGotoPage (page) {
  $('#map_pcount').html (page);
  $('#mapResult button').css('display','block');
  if (page==1) {
    $('#map_prevPage, #map_firstPage').css('display','none');
  }
  if (page == parseInt (pois.length / mapPageLength) +1){
   $('#map_nextPage, #map_lastPage').css('display','none');
  }
  mapPage = page;
  showAllPoiOnTheMap();
}
function parseAddToPackAnswer (msg) {
  var answers = msg.split (';');
  for (var a=0; a<answers.length; a++){
    var answer = answers[a];
    var t = answer.split (' ');

    if (t[0] == 'A'){
      //$('#pack_'+t[1]).html(deleteFromUserPackString);
      $('#pack_'+t[1]).removeClass ('add');
      $('#pack_'+t[1]).addClass ('del');
      $('#mapMark_'+t[1]).removeClass ('add');
      $('#mapMark_'+t[1]).addClass ('del');
    }
    if (t[0] == 'D'){
      //$('#pack_'+t[1]).html(addToUserPackString);
      $('#pack_'+t[1]).removeClass ('del');
      $('#pack_'+t[1]).addClass ('add');
      $('#mapMark_'+t[1]).removeClass ('del');
      $('#mapMark_'+t[1]).addClass ('add');
    }
    if (t[0] == 'T') {
      $('#markedToAdd').html("Zaznaczono do dodania do zestawu <span>" + t[1] + " " + obiekty(t[1]) + " </span>" );
      total = t[1];
    }
    if (t[0] == 'E') {
      if (t[1] == '1') { alert ('Musisz być zalogowanym by dodać POI do zestawu'); }
    }
  }
}

var cats = Array ();
$(function () {

		$('#typyWynikow input[type=submit]').css('display','none');
		$('#typyWynikow input').click (function () {
		cats = Array ();
			$('#typyWynikow input[type=checkbox]:checked').each (function (i,o){
				cats.push (parseInt ($(o).attr('id').split('_')[1]));
			});
			refreshTable();
		});

});
function refreshTable () {
  var target = 'ajxcnt_1';
		var href = 'poichange.php?';
		if (total > 0){
		  //	alert ('Komunikat');
		  total = 0;
		}
		$('#markedToAdd').html("Zaznaczono do dodania do zestawu <span>0 obiektów</span>" );

		$('#typyWynikow input[type=hidden]').each (function (i,o) {
		  if ($(o).attr('name') != 'page'){
			   href += $(o).attr('name')+'='+$(o).attr('value')+'&';
			 }
		});
		for (var a=0; a<cats.length; a++){
		  href += 'cat_'+cats[a]+'=1&';
		}
		href += 'ajax';
		$.ajax({
		  type: "GET",
		  url: href,
		  data: '',
		  success: function(msg){
			   $('#'+target).html (msg);
			   searchBhv();
						ustaw_tabelke();
		  },
		  error: function (xhr, ajaxOptions, thrownError){
				  alert ('Błąd!');
		  }
		});
		if (typeof (ieSomethingStrangeFix) != 'undefined'){
		  ieSomethingStrangeFix();
		}
}

function obiekty (liczba) {
  if (liczba == 1) { return 'obiekt'; }
if (liczba<10){
  if (liczba.substr(-1) == 2 || liczba.substr(-1) == 3 || liczba.substr(-1) == 4){
    return 'obiekty';
  }
  }
  return 'obieków';
}

/* funkcja ustawiająca wymiary tabelki w wyszukwiarce */
function ustaw_tabelke(){
  var s = szerokosc-r_bok;
  $('.ruchoma_tabela').each(function(index) {
    if($(this).hasClass('tb1') == true){
      $(this).css('width',(s-50)+'px');
	 }
    else if($(this).hasClass('tb2') == true){
      $(this).css('width',(s-310)+'px');
	 }
    else if($(this).hasClass('tb3') == true){
      $(this).css('width',(s-300)+'px');
	 }
  });
}

