function PolyControl(controlDiv,map){this.pc_state=false;this.map=map;this.polyg=false;this.polyg_c=false;var thisObj=this;controlDiv.style.padding='5px';var controlUI=document.createElement('DIV');controlUI.style.backgroundColor='white';controlUI.style.borderStyle='solid';controlUI.style.borderWidth='2px';controlUI.style.cursor='pointer';controlUI.style.textAlign='center';controlUI.title='Click to draw a search polygon';controlDiv.appendChild(controlUI);this.setState=function(s){var controlText=document.createElement('DIV');controlText.style.fontFamily='Arial,sans-serif';controlText.style.fontSize='12px';controlText.style.paddingLeft='4px';controlText.style.paddingRight='4px';while(controlUI.hasChildNodes()){controlUI.removeChild(controlUI.lastChild);}
if(s){controlText.innerHTML='Cancel/Clear Polygon Search';controlText.style.fontWeight='bold';var mOpts={draggable:false};this.map.setOptions(mOpts);this.polyg=new PolygonCreator(this.map);this.pc_state=true;}else{controlText.innerHTML='Draw Polygon Search';controlText.style.fontWeight='normal';if(this.polyg){this.polyg.destroy();this.polyg=false;this.polyg_c=false;var mOpts={draggable:true};this.map.setOptions(mOpts);google.maps.event.trigger(this.map,'idle');}
this.pc_state=false;}
controlUI.appendChild(controlText);}
this.getPolyPoints=function(){if(this.pc_state&&this.polyg){return this.polyg.getPlots();}
return false;}
this.getPolyState=function(){var vertices=this.getPolyPoints();if(vertices){var st='';vertices.getAt(0).forEach(function(value,index){st=st+value.lat()+","+value.lng()+'|';});return st;}
return false;}
this.restorePolyState=function(st){var dots=new Array();var vertices=st.split('|');for(i=0;i<vertices.length;i++){if(vertices[i].length>0){var pnt=vertices[i].split(',');dots.push(new rDot(new google.maps.LatLng(pnt[0],pnt[1])));}}
var pnt=vertices[0].split(',');dots.push(new rDot(new google.maps.LatLng(pnt[0],pnt[1])));this.setState(true);this.polyg.pen.polygon=new Polygon(dots,this.map);var mOpts={draggable:true};this.map.setOptions(mOpts);}
google.maps.event.addDomListener(controlUI,'click',function(){if(thisObj.pc_state){thisObj.setState(false);}else{thisObj.setState(true);}});this.setState(false);}
function PolygonCreator(map){this.map=map;this.pen=new Pen(this.map);var thisObj=this;this.event=google.maps.event.addListener(thisObj.map,'click',function(event){thisObj.pen.draw(event.latLng);});this.getPlots=function(){if(null!=this.pen.polygon){return this.pen.polygon.getPlots();}
return false;}
this.destroy=function(){this.pen.deleteMis();if(null!=this.pen.polygon){this.pen.polygon.remove();}
google.maps.event.removeListener(this.event);}}
function Pen(map){this.map=map;this.listOfDots=new Array();this.polyline=null;this.polygon=null;this.currentDot=null;this.draw=function(latLng){if(null!=this.polygon){alert('Please click Cancel/Clear Polygon Search to draw another polygon or return to regular map search.');}
else{if(this.currentDot!=null&&this.listOfDots.length>1&&this.currentDot==this.listOfDots[0]){this.drawPloygon(this.listOfDots);}
else{if(null!=this.polyline){this.polyline.remove();}
var dot=new Dot(latLng,this.map,this);this.listOfDots.push(dot);if(this.listOfDots.length>1){this.polyline=new Line(this.listOfDots,this.map);}}}}
this.drawPloygon=function(listOfDots,color,des,id){this.polygon=new Polygon(listOfDots,this.map);this.deleteMis();var mOpts={draggable:true};this.map.setOptions(mOpts);google.maps.event.trigger(this.map,'idle');}
this.deleteMis=function(){$.each(this.listOfDots,function(index,value){value.remove();});this.listOfDots.length=0;if(null!=this.polyline){this.polyline.remove();this.polyline=null;}}
this.cancel=function(){if(null!=this.polygon){(this.polygon.remove());}
this.polygon=null;this.deleteMis();}
this.setCurrentDot=function(dot){this.currentDot=dot;}}
function Dot(latLng,map,pen){this.latLng=latLng;this.parent=pen;var marker_image=new google.maps.MarkerImage(APP_M_IMG+'polymark.jpg',new google.maps.Size(12,12),new google.maps.Point(0,0),new google.maps.Point(2,2));this.markerObj=new google.maps.Marker({position:this.latLng,icon:marker_image,map:map});this.addListener=function(){var parent=this.parent;var thisMarker=this.markerObj;var thisDot=this;google.maps.event.addListener(thisMarker,'click',function(){parent.setCurrentDot(thisDot);parent.draw(thisMarker.getPosition());});}
this.addListener();this.getLatLng=function(){return this.latLng;}
this.getMarkerObj=function(){return this.markerObj;}
this.remove=function(){this.markerObj.setMap(null);}}
function rDot(latLng,map){this.latLng=latLng;this.getLatLng=function(){return this.latLng;}}
function Line(listOfDots,map){this.listOfDots=listOfDots;this.map=map;this.coords=new Array();this.polylineObj=null;if(this.listOfDots.length>1){var thisObj=this;$.each(this.listOfDots,function(index,value){thisObj.coords.push(value.getLatLng());});this.polylineObj=new google.maps.Polyline({path:this.coords,strokeColor:"#0D70B3",strokeOpacity:1.0,strokeWeight:2,map:this.map});}
this.remove=function(){this.polylineObj.setMap(null);}}
function Polygon(listOfDots,map){this.listOfDots=listOfDots;this.map=map;this.coords=new Array();var thisObj=this;$.each(this.listOfDots,function(index,value){thisObj.coords.push(value.getLatLng());});this.polygonObj=new google.maps.Polygon({paths:this.coords,strokeColor:"#0D70B3",strokeOpacity:0.8,strokeWeight:1,fillColor:"#0D70B3",fillOpacity:0.35,map:this.map});this.remove=function(){this.polygonObj.setMap(null);}
this.getPlots=function(){return this.polygonObj.getPaths();}}
