var middlepoint=null;
var count_point=0;
var zoom=8;
var map=null;
var geocoder=null;
var allobject=0;
var currentobject=0;
function SetPointOfAddress(map,geocoder,address,title,icontype,showinfotab)			
			{
  			geocoder.getLocations(address, 	geocoder.getLatLng(address,function(point){
				if (!point) 
						{
							adressarray=address.split(',');
							address=adressarray[0]+","+adressarray[1];
							zoom=11;
							SetPointOfAddress(map,geocoder,address,title,icontype,showinfotab);
							
						}
						else
						{
							var infoTabs = [new GInfoWindowTab("     ", "<b>"+title+"</b>"+'<DIV style="font-size:11px;">'+address+'</DIV>')];
							
							addMark(map,point,title,infoTabs,icontype,showinfotab);
							
						}
			
			}));
			}
			
function SetPointOfCor(map,geocoder,address,googlex,googley,title,icontype,showinfotab)			
			{
  			var point;
  			point=new GLatLng(googlex,googley)
				if (!point) 
						{
							zoom=11;
							SetPointOfAddress(map,geocoder,address,title,icontype,showinfotab);	
						}
						else
						{
							var infoTabs = [new GInfoWindowTab("     ", "<b>"+title+"</b>"+'<DIV style="font-size:11px;">'+address+'</DIV>')];
							
							addMark(map,point,title,infoTabs,icontype,showinfotab);
							
						}  			
			}
						
									function addMark(map,point,title,infoTabs,icontype,showinfotab)
									{
										
				
										if (icontype==1)   
										{
											var marker = CreateMarkerHouse(point,title,0);//new GMarker(point);
										}	
										if (icontype==2)   
										{
											var marker = CreateMarkerPlot(point,title,0);//new GMarker(point);
										}
										GEvent.addListener(marker, "click", function() {marker.openInfoWindowTabsHtml(infoTabs);});
										map.addOverlay(marker);
											var points=ExtractPoint(point);
											var ext_middlepoint=ExtractPoint(middlepoint);
											if(ext_middlepoint[0]>0)
											{
												ext_middlepoint[0]=(ext_middlepoint[0]+points[0])/2;	
												count_point=count_point+1;
											}
											else
											{
												ext_middlepoint[0]=points[0];
											}
											if(ext_middlepoint[1]>0)
											{
												ext_middlepoint[1]=(ext_middlepoint[0]+points[1])/2;	
												count_point=count_point+1;
											}
											else
											{
												ext_middlepoint[1]=points[1];
											}
										middlepoint=new GLatLng(ext_middlepoint[0],ext_middlepoint[1]);
										if(showinfotab)
										{
											marker.openInfoWindowTabsHtml(infoTabs);
											
										}
										
										Window.setTimeout('WriteToStatusBar',1);
									}
									function WriteToStatusBar()
									{
										window.status='загружено '+currentobject+' из '+allobject+' объектов';
									}
									function CreateMarkerPlot(point, texts, ids) 
									{
												var myicon = new GIcon();
												myicon.image = "icon/plot.png";
												myicon.shadow = "icon/plot.png";
												myicon.iconSize = new GSize(24,27);
												myicon.shadowSize = new GSize(24,27);
												myicon.iconAnchor = new GPoint(24,27);
												myicon.infoWindowAnchor = new GPoint(10,7);
												
								
												var marker = new GMarker(point,{"title":texts});
												//"icon":myicon,
												GEvent.addListener(marker, "click", function() 
												{
													//showAddressNoZoom(ids);
												});
												
												return marker;
									}
								
									function CreateMarkerHouse(point, texts, ids) 
									{
												var myicon = new GIcon();
												myicon.image = "icon/house.png";
												myicon.shadow = "icon/house.png";
												myicon.iconSize = new GSize(30,43);
												myicon.shadowSize = new GSize(30,43);
												myicon.iconAnchor = new GPoint(30,43);
												myicon.infoWindowAnchor = new GPoint(10,7);
												var marker = new GMarker(point,{"title":texts});
												//"icon":myicon,
												GEvent.addListener(marker, "mouseover", function() 
												{
													//showAddressNoZoom(ids);
												});
												
												return marker;
									}									
									
									function CreateMap(div,center_x,center_y)
									{
										var map=null; 
										if (GBrowserIsCompatible()) 
      										{
      											        map = new GMap2(document.getElementById(div));
														map.setCenter(new GLatLng(center_x,center_y), zoom, G_NORMAL_MAP);
														map.addControl(new GLargeMapControl());
														map.addControl(new GMapTypeControl());
														map.addControl(new GScaleControl());
														map.enableDoubleClickZoom();
														var geocoder = new GClientGeocoder();

															map.setCenter(GLatLng(center_x,center_y), 8, G_NORMAL_MAP);

														
      										}
      										return map;
									}


function load() 
{

      
	  map=CreateMap("map",55.750303644490394,37.624053955078125);
	  geocoder = new GClientGeocoder();

}

function ExtractPoint(point)
{
	var middle_points= new Array(null,null)
	if(point!=null)
	{
		var str_point=point.toString();
		str_point=str_point.replace('(','');
		str_point=str_point.replace(')','');
		middle_points=str_point.split(',');
		return middle_points;
	}
	else
	{
		return middle_points;	
	}
}
									function SetObject(map,geocoder,adress_list)
									{
															
															var count=adress_list.length;
															map.clearOverlays();
															allobject=count;
															for(i=1;i<count;i++)
															{
																currentobject=i;
																if(count==2)
																{
																	var showinfotab=true;
																	zoom=16;
																}
																else
																{
																	var showinfotab=false;
																	zoom=10;
																}
																
																if(adress_list[i][0]!='')
																{
																 if(adress_list[i][4]==1)	
																 {
																	if(adress_list[i][5]>0&&adress_list[i][6]>0)
																	{
																		SetPointOfCor(map,geocoder,adress_list[i][0],adress_list[i][5],adress_list[i][6],'<a href="/shop.php?id='+adress_list[i][3]+'" style="font-size:12px;font-weight:bold;color:blue;" target="_blank">'+adress_list[i][1]+'</a>',adress_list[i][2],showinfotab);								
																	}
																	else
																	{
																 		SetPointOfAddress(map,geocoder,adress_list[i][0],'<a href="/shop.php?id='+adress_list[i][3]+'" style="font-size:12px;font-weight:bold;color:blue;" target="_blank">'+adress_list[i][1]+'</a>',adress_list[i][2],showinfotab);
																	}	
																	
																 }
																 else
																 {
																 	//alert(adress_list[i][4]);
																 }
																} 
															}
															document.getElementById('loading').style.display='none';
									}
									function b()
									{
										return 0;
									}
function CreateHTMLListShops($list)
{
var count=$list.length;	
var table='';
	table='<TABLE width=100% align=left >';
	for(i=1;i<count;i++)
	{
		if($list[i][0]!='')
		{
			table=table+'<TR><TD><DIV style="margin-bottom:5px;font-size:10px;">'+i+': <a href="/shop.php?id='+$list[i][3]+'" style="font-size:12px;color:blue;" target="_blank">'+$list[i][1]+'</a></DIV><div style="font-size:11px;">'+$list[i][0]+'</DIV></TD></TR>';	
		}
	}
	table=table+'</TABLE>';
	return table;
}
