  //<![CDATA[
  var map, geocoder;
  var coord, e_currentZoom, e_currentBounds;
  var done = false;
  var mapWidth;
  var gmarkers = []; 
  //var clusterer;
  //var point;
  
  function load_map(map_location_latitude, map_location_longitude, gmap_level, find_recent)
  {
  	 resizeMapDiv();
     if (GBrowserIsCompatible()) {
	   map = new GMap2($("map")); 

	   var company_id = $("COMPANY_ID").value;
	   
	   map.addMapType(G_PHYSICAL_MAP);
	   map.setMapType(G_PHYSICAL_MAP);

	   var point = new GLatLng(map_location_latitude, map_location_longitude);
	   map.setCenter(point, gmap_level);

       map.enableDoubleClickZoom();
	   map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
	  /* 
	   clusterer = new Clusterer(map);
	   clusterer.SetMaxVisibleMarkers(250);
	   clusterer.SetMinMarkersPerCluster(10);
*/
	   e_currentBounds = map.getBounds();
	   e_currentZoom = map.getZoom();
		
	   leftbound   = e_currentBounds.getSouthWest();
	   rightbound  = e_currentBounds.getNorthEast();
	   find_offices(leftbound, rightbound, find_recent, company_id);

/*	   GEvent.addListener(map, "moveend", function() {   
	   		e_currentBounds = map.getBounds();
	   	 	leftbound   = e_currentBounds.getSouthWest();
 		    rightbound  = e_currentBounds.getNorthEast();
//	   		alert(leftbound);
	        find_articles(leftbound, rightbound, "false", category_id, country_id, lang, keyword);
	    });*/
	   
     }
  }
  
  
  function find_offices(leftbound, rightbound, find_recent, company_id)
  {
	   //alert(uid);
	   new Ajax.Request('/include/API/get_offices.php', { 
				  method:'get',
				  parameters: {leftlat: leftbound.lat(), leftlng: leftbound.lng(), rightlat: rightbound.lat(), rightlng: rightbound.lng(), 
				  			   find_recent: find_recent, company_id: company_id},
				  onSuccess: function(transport)
				  			 {
							 	json = transport.responseText.evalJSON();
								//alert(uid);
								if (json.error == "error") {
									alert("wrong parameters");
								}
								else if (json.error == "debug") {
									alert(json.output);
								}
								else {
									//map.clearOverlays();
									json.offices.each(function(item) {
										//map.clearOverlays();
									  	display_office(item);
									});
									// if no recent option or recent articles found or (no article found and zoom is minimun)
									
									/*GEvent.clearListeners(map, "moveend");
								    GEvent.addListener(map, "moveend", function() {   
		   								e_currentBounds = map.getBounds();
								   	 	leftbound   = e_currentBounds.getSouthWest();
							 		    rightbound  = e_currentBounds.getNorthEast();
										//alert(keyword);
								        find_articles(leftbound, rightbound, "false", category_id, country_id, lang, keyword, uid);
								    });*/
								}
							 },
				  onFailure: function(){ alert('Something went wrong...') }
	   } );
  
  }
  
  function resizeMapDiv() {}

function createMarker(point,html,id) {
  //var marker = new GMarker(point);
  var marker = new PdMarker(point);

  marker.setId(id);
  marker.setUserData(html);
  GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
  });
  return marker;
}



function display_office(office) {
	//alert(office.OFFICE_ID);
	point = new GLatLng(office.LATITUDE, office.LONGITUDE);
//	alert(point);
//	ID = article.ID;
	str = office.OFFICE_NAME;
	str = str.replace(/[^а-я\w_-]/ig,'-');
	//str = str.replace(')','-');
	str = str.replace(/\s+/g,'-');
	str = str.replace(/\-{2}/g,'-');
	str = encodeURIComponent(str);

	//var marker = createMarker(point, '<a href="http://www.earthpublisher.com/office/' + str + '-' + office.OFFICE_ID + '/">' + office.OFFICE_NAME + '</a>', office.OFFICE_ID);
	var marker = createMarker(point, office.OFFICE_NAME, office.OFFICE_ID);
	map.addOverlay(marker);
	//clusterer.AddMarker(marker, '<a href="http://www.earthpublisher.com/office/' + str + '-' + office.OFFICE_ID + '/">' + office.OFFICE_NAME + '</a>');
}

function highlight_marker(id) 
{
	var marker = map.getMarkerById(id);
	html = marker.getUserData();
	var pt = marker.getPoint();
	map.panTo(pt);
	marker.openInfoWindowHtml(html);
}
//]]>
