var GMapsInit,map,markerClusterer;
var markers = [];

function initAdds() {
  $('.ad_c').css('cursor','pointer').click(function(event) {
      window.open($(this).find('a').attr('href'),$(this).attr('id'),'width=805,height=600,resizable=1,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0');
      return false;
  });
}

function showMarkerInfo(marker) {
  window.open($('#map_show').attr('href')+'?number='+marker.getTitle(),$(this).attr('id'),'width=805,height=600,resizable=1,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0');
}

function initMap() {
  $('#map_canvas').each(function() {
    if (GBrowserIsCompatible() && (!GMapsInit)) {
      var coords = $(this).html();
      $(this).html('').css('display','block').css('width','762px').css('height','480px');
      var markerHide = true;

      coords = eval(coords);
      for(var i=0;i<coords.length;i++) {
        markers[i] = new GMarker(new GLatLng(coords[i].x,coords[i].y),{draggable: false, title: coords[i].nr});
      }

      map = new google.maps.Map2(document.getElementById("map_canvas"),{});
      map.setCenter(new google.maps.LatLng(38.963745,35.243322), 6);
      map.setUIToDefault();
      /*map.disableScrollWheelZoom();*/

      GEvent.addListener(map, "click", function(overlay) {
        if(overlay !== null) {
          showMarkerInfo(overlay);
        }
      });

      markerClusterer = new MarkerClusterer(map,markers,{maxZoom:14});

      GMapsInit = true;
      //window.geocoder = new GClientGeocoder();
    }
  });
}

$(document).ready(function() {
  $('#maintabs li').click(function() {
    $(this).addClass('current').siblings().removeClass('current');
    $('.maintabcontent').children().remove();
    $('.maintabcontent').append('<span class="loader">'+_('Loading...')+'</span>');
    var url = $(this).children('a').attr('href');
    $.get(url,null,function(data, status) {
      $('.maintabcontent').children().remove();
      $('.maintabcontent').html(data);
      GMapsInit = false;
      initAdds();
      initMap();
    },'html');
  });

  initAdds();

  $('#main').css('min-height',Math.max($('#location_bar').height()+$('#servicebox').height()+50,$('#main').height())+'px');
  $('#location_bar div.container').height($('#main').height()-230-43-60);
  $('#main .maintabcontent').css('min-height',$('#main').height()-$('#main .maintabcontent').position().top-28);
});

