var geocoder = null;
var Gselected_company_id='';
var wait_for_address=1;

var anchor_lats=new Array();
var anchor_longs=new Array();
var selected_zoom=15;

	
function oparate_cords(marker_id,lat,lng)
{
	nodes = $A(anchor_lats);	
	nodes.each(function(node,index){ if (node[1]==marker_id) {node[0]=lat;anchor_longs[index]=new Array(lng,marker_id);} });
	//alert(nodes.inspect());
}

function operate_address(marker_id,lat,lng,address,text,icon_src,icon_width,icon_height,draggable)
{
	if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
			  insert_marker(marker_id,lat,lng,text,icon_src,icon_width,icon_height,draggable)  	
			  return '';
            } else {
              map.setCenter(point, 15);
			  insert_marker(marker_id,point.lat(),point.lng(),text,icon_src,icon_width,icon_height,draggable);  		  
            }
          }
        );
      }
}

function insert_marker(marker_id,lat,lng,text,icon_src,icon_width,icon_height,draggable) 
{
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	if (icon_src!='')
	{		
		blueIcon.image = icon_src;
		blueIcon.printImage = icon_src;			
		blueIcon.iconSize = new GSize(icon_width,icon_height);
		blueIcon.iconAnchor = new GPoint(icon_width/2,icon_height);
		blueIcon.shadowSize = new GSize(0, 0);
		blueIcon.infoWindowAnchor = new GPoint(icon_width/2, 0);
		blueIcon.imageMap = [0,0,icon_width,0,icon_width,icon_height,0,icon_height];	//for clickable area	
	}
	var marker = new GMarker(new GLatLng(lat,lng)/*, {icon:blueIcon}*/);
	if (draggable==1)
	{
			marker = new GMarker(new GLatLng(lat,lng), {draggable: true/*, icon:blueIcon*/});
			GEvent.addListener(marker, "dragstart", function() {
			  map.closeInfoWindow();
			});
			GEvent.addListener(marker, "dragend", function() {
			  var rez = marker.getLatLng();
			  oparate_cords(marker_id,rez.lat(),rez.lng());
			});	 
	}
	if (text!='')
	{
		GEvent.addListener(marker,"mouseover", function() {marker.openInfoWindowHtml(text);});	
		GEvent.addListener(marker,"mouseout", function() {marker.closeInfoWindow();});	
	}
	map.addOverlay(marker);	
	map.setCenter(new GLatLng(lat, lng), selected_zoom);		
}

function createMarker(marker_id,lat,lng,address,text,icon_src,icon_width,icon_height,draggable) 
{	 
	if (address!='') operate_address(marker_id,lat,lng,address,text,icon_src,icon_width,icon_height,draggable);	
	else insert_marker(marker_id,lat,lng,text,icon_src,icon_width,icon_height,draggable); 	
}
	
function MyApplication(id, start_x, start_y, zoom)
{
  if (GBrowserIsCompatible())
  {
	map = new GMap2(document.getElementById("map_canvas_"+id));
	map.setCenter(new GLatLng(start_x, start_y), zoom);
	map.addControl(new GSmallMapControl()); //arrows lef-right
	map.addControl(new GMapTypeControl()); // view mode (satelite...)
	//map.addControl(new GLargeMapControl()); //left side zooming tool
	geocoder = new GClientGeocoder();	
  }
  
}

function initialize_googleMaps(id, start_x, start_y, zoom)
{
  var application = new MyApplication(id, start_x, start_y, zoom);
  selected_zoom = zoom;
}


function showAddress(address) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 15);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(address);
		}
	  }
	);
  }
}
