
function GoogleMapPolygon(googleMap,prefix,polyLine,polyFill,polyOpacity)
{var polyOpacityOrig=polyOpacity;var markers=[];var polygon=null;var poly=[];var hideInfoWindow;var timeoutEvent;return{instantiateIcon:function()
{url=window.location.toString();url=url.substring(0,url.indexOf("/",url.indexOf("cms/")+4)+1);icon=new GIcon();icon.image=url="/images/projects/polygon_marker.png";icon.iconSize=new GSize(12,20);icon.iconAnchor=new GPoint(6,20);},addMarker:function(lat,lng)
{var self=this;var markerIndex=markers.length;var marker=null;if(null==lat||null==lng)
{markerCenter=googleMap.get('center');}
else
{markerCenter=new GLatLng(parseFloat(lat),parseFloat(lng));}
marker=new GMarker(markerCenter,{icon:icon,draggable:true});googleMap.get('map').addOverlay(marker);marker.enableDragging();GEvent.addListener(marker,'dragend',function(){self.markerMoveEvent(markerIndex);});markers[markerIndex]=marker;this.addPoly(lat,lng);this.markerMoveEvent(markerIndex);},addPoly:function(lat,lng)
{var point=new GLatLng(parseFloat(lat),parseFloat(lng));poly.push(point);},updatePoly:function(index,lat,lng)
{var point=new GLatLng(parseFloat(lat),parseFloat(lng));poly[index]=point;},draw:function()
{var gm=googleMap.get('map');if(poly.length<3)
{if(polygon)
{gm.removeOverlay(polygon);}
return;}
poly.push(poly[0]);if(polygon)
{gm.removeOverlay(polygon);}
polygon=new GPolygon(poly,polyLine,3,1,polyFill,polyOpacity);gm.addOverlay(polygon);poly.pop();},removeLastmMarker:function()
{$('#'+prefix+'_marker_'+(markers.length-1)+'_latitude').remove();$('#'+prefix+'_marker_'+(markers.length-1)+'_longitude').remove();googleMap.get('map').removeOverlay(markers[markers.length-1]);markers.pop();poly.pop();this.draw();},markerMoveEvent:function(index)
{this.updateInput(prefix+'_marker_'+index+'_latitude',markers[index].getPoint().lat());this.updateInput(prefix+'_marker_'+index+'_longitude',markers[index].getPoint().lng());this.updatePoly(index,markers[index].getPoint().lat(),markers[index].getPoint().lng());this.draw();},updateInput:function(id,value)
{if(undefined==$('#'+id).val())
{$('<input type="hidden" name="'+id+'" id="'+id+'" value="'+value+'" />').appendTo('form');}
$('#'+id).val(value);},loadMarkers:function()
{var lat=null;var lng=null;var marker=null;for(var index=0;index<100;index++)
{if(undefined==$('#'+prefix+'_marker_'+index+'_latitude').val())
{index=100;break;}
lat=$('#'+prefix+'_marker_'+index+'_latitude').val();lng=$('#'+prefix+'_marker_'+index+'_longitude').val();this.addMarker(lat,lng);}
this.draw();},set:function(name,value)
{switch(name)
{case'polyLine':polyLine=value;break;case'polyFill':polyFill=value;break;case'polyOpacity':polyOpacity=value;break;default:alert(name+" not implemented.");exit();}},registerPolygonMouseOverHandler:function(html)
{var self=this;GEvent.addListener(polygon,"mouseover",function(){clearTimeout(timeoutEvent);timeoutEvent=setTimeout(function(){polyOpacity=0.7;self.draw();googleMap.openMapInfoWindow(polygon.getBounds().getCenter(),html);},100);});GEvent.addListener(polygon,"mouseout",function(){timeoutEvent=setTimeout(function(){if(polygon.getBounds().getCenter().lat()==googleMap.get('infoWindow').getPoint().lat()&&polygon.getBounds().getCenter().lng()==googleMap.get('infoWindow').getPoint().lng())
{googleMap.hideMapInfoWindow();}
polyOpacity=polyOpacityOrig;self.draw();self.registerPolygonMouseOverHandler(html);},1500);});}}}