var markers = [];
var map=null;
var filterContainer=document.getElementById('filter_container');
//var addressContainer=filterContainer.appendChild(document.createElement('div'));
var addressContainer;
//var places={json_encode object=$googlemap->getPlaces()};if (places===null)	places=[];
//var structures={json_encode object=$googlemap->getStructures()};

function createMarker(placeIndex)
{			
	if (places[placeIndex].sw && places[placeIndex].ne && places[placeIndex].zl)
	{
		if (! markers[placeIndex])	
		{
			markerOptions = {};	
			if (places[placeIndex].icon)			
			{	
			 	var baseIcon = new GIcon(G_DEFAULT_ICON);		
				var customIcon = new GIcon(baseIcon);
				if (places[placeIndex].shadowsize)
				{
					sizeres=places[placeIndex].shadowsize.split(',');
	      			customIcon.shadowSize = new GSize(sizeres[0], sizeres[1]);				
				}
				if (places[placeIndex].iconsize)
				{
					sizeres=places[placeIndex].iconsize.split(',');
	      			customIcon.iconSize = new GSize(sizeres[0], sizeres[1]);				
				}
				if (places[placeIndex].iconanchor)
				{
					sizeres=places[placeIndex].iconanchor.split(',');
	      			customIcon.iconAnchor = new GPoint(sizeres[0], sizeres[1]);									
				}
				if (places[placeIndex].infowindowanchor)
				{
					sizeres=places[placeIndex].infowindowanchor.split(',');
	      			customIcon.infoWindowAnchor = new GPoint(Number(sizeres[0]), Number(sizeres[1]));									
				}
				if (places[placeIndex].imagemap)
				{
					customIcon.imageMapType='rect';
					sizeres=places[placeIndex].imagemap.split(',');					
					customIcon.imageMap=sizeres;				
				}
				customIcon.image = places[placeIndex].icon;
				if (places[placeIndex].shadowicon)
					customIcon.shadow = places[placeIndex].shadowicon;
				markerOptions = {icon:customIcon};
			}
			
			markers[placeIndex]=new GMarker(new GLatLng(places[placeIndex].sw,places[placeIndex].ne),markerOptions);
			
			GEvent.addListener(markers[placeIndex], "click", function() {
	    		markers[placeIndex].openInfoWindowHtml(places[placeIndex].info+'<br/>'+places[placeIndex].address);
	  		});
	  	}		
		map.addOverlay(markers[placeIndex]);
	}
}			

function createFilterElement(element,td)
{
	switch (element.type)
	{
		case 'combo':
			td.appendChild(document.createTextNode(element.label));
			el=td.appendChild(document.createElement('select'));
			el.setAttribute('name',element.name);
			el.setAttribute('id',element.name);
			attachElementEvent([el],['change'],applyFilters);
			{
				/*placeIndexes=getPlaceIndexesByFilterNameValue(places,this.name,this.value)
				for (var i=0;i<placeIndexes.length;i++)
					createMarker(placeIndexes[i]);*/
				
			};
			for (var i=0;i<(filterValues=getFilterValuesByName(element.name)).length;i++)
			{
				optionObject=document.createElement('option');
				optionObject.text=filterValues[i];
				optionObject.value=filterValues[i];
				if(document.all && !window.opera)	el.add(optionObject);
				else	el.add(optionObject,null);
			}
		break;
	}
}

function applyFilters()
{
	map.clearOverlays();
	var helper=places;
	if (structures && structures.filters)
	{
		for (var i=0;i<(filterData=structures.filters.data).length;i++)
		{	
			helper=getPlaceIndexesByFilterNameValue(helper,filterData[i].name,document.getElementById(filterData[i].name).value);		
		}
	}
	
	for (var i=0;i<helper.length;i++)
		createMarker(getPlaceIndex(helper[i]));
		
	refreshAddressList(helper);
}

function refreshAddressList(placeList)
{
	if (document.getElementById('addresscontainer'))
		document.getElementById('addresscontainer').parentNode.removeChild(document.getElementById('addresscontainer'));
	addressContainer=document.createElement('table');
	addressContainer.id='addresscontainer';
	addressContainer.appendChild(tbody=document.createElement('tbody'));
	
	for (var i=0;i<placeList.length;i++)		
	{
		
		if (placeList[i].sw && placeList[i].ne)
		{
			/*
			addressContainer.appendChild(div=document.createElement('div'));			
			div.innerHTML=placeList[i].info;
			div.place=placeList[i];
			div.onclick=function(){setCenterByPlaceId(getPlaceIndex(this.place),16); return false;};*/
			
			tr=tbody.appendChild(document.createElement('tr'));
			td0=tr.appendChild(document.createElement('td'));
			td1=tr.appendChild(document.createElement('td'));
			td0.appendChild(document.createTextNode(((placeList[i].num>0)?placeList[i].num:i)));
			td1.appendChild(infoDiv=document.createElement('div'));
			infoDiv.innerHTML=(placeList[i].info);
			
			td1.place=placeList[i];
			td1.onclick=function(){setCenterByPlaceId(getPlaceIndex(this.place),16); return false;};
		}
	}
	filterContainer.appendChild(addressContainer);
}

function setCenterByPlaceId(placeIndex,zoomLevel)
{
	map.setCenter(new GLatLng(places[placeIndex].sw,places[placeIndex].ne),zoomLevel);
}

function getFilterValuesByName(filterName)
{
	res=[];
	for (var i=0;i<places.length;i++)
	{
		if (places[i].filters)
		{
			for (var a=0;a<places[i].filters.length;a++)
			{
				if (places[i].filters[a].name==filterName)
				{
					res[res.length]=places[i].filters[a].value;					
				}
			}
		}
	}		
	return (arrayUnique(res));
}

function getPlaceIndexesByFilterNameValue(placeList,filterName,value)
{
	res=[];
	for (var i=0;i<placeList.length;i++)
	{
		if (placeList[i].filters)
		{
			for (var a=0;a<placeList[i].filters.length;a++)
			{
				if (placeList[i].filters[a].name==filterName && placeList[i].filters[a].value==value)
				{
					res[res.length]=placeList[i];
				}
			}
		}
	}		
	return res;
}

function getPlaceIndex(placeObject)
{
	for (var i=0;i<places.length;i++)
		if (places[i]===placeObject)	return i;
	return -1;
}

function initFilters(filters)
{
	if (! filters) return;
	
	td=filterContainer.appendChild(document.createElement('TBODY')).appendChild(document.createElement('TR')).appendChild(document.createElement('TD'));
	
	for (var i=0;i<filters.data.length;i++)
		createFilterElement(filters.data[i],td)		
}

function getStructure(structureName)
{
	if (structures && structures[structureName])
		return structures[structureName].data;
}

function getDataByName(structure,name)
{
	if (structure)
		for (var i=0;i<structure.length;i++)
			if (structure[i].name==name)
				return structure[i];
}
//javascript:alert(getDataByName(getStructure('design'),'siteurl'));
	
function initialize() 
{
      if (GBrowserIsCompatible()) 
      {
      	map = new GMap2(document.getElementById("map_canvas"));			      	
        map.addControl(new GLargeMapControl());
        if (res=getDataByName(getStructure('design'),'disablemapcontrol')) { }
        else	map.addControl(new GMapTypeControl());
        
		map.enableScrollWheelZoom();        
        
		if (res=getDataByName(getStructure('design'),'mapcenter'))    		
			map.setCenter(new GLatLng(res.sw, res.ne),Number(res.zl));
		else
			map.setCenter(new GLatLng("47.2", "18.8"),7);
			
		if (res=getDataByName(getStructure('design'),'maptype'))
			switch (res.value)
			{
				case 'G_SATELLITE_MAP':	map.setMapType(G_SATELLITE_MAP); 	break;
				case 'G_HYBRID_MAP':	map.setMapType(G_HYBRID_MAP); 		break;
			}			
			
        if (structures)	initFilters(structures.filters);
        
        applyFilters();
        
		for (var i=0;i<places.length;i++)
			createMarker(i);	        
	  }
}
