package com.sharewithagent.map {
    import com.google.maps.*;
    import com.google.maps.overlays.*;
    import com.google.maps.services.*;
    import com.sharewithagent.net.CommunicatorEvent;
    
    import flash.display.*;
    import flash.events.DataEvent;
    import flash.events.Event;
    import flash.geom.*;
    import flash.text.*;
    
    public class AgentMarker extends Sprite {
        public static var READY:String = "ready";
        
        private var parentMap:Map;
        //private var parentMapView:MapView;
        private var mapPin:Sprite;
        private var geocoder:ClientGeocoder;
        private var infoWindow:Sprite;
        private var marker:Marker
        private var mapMarker:MapMarker
        private var geocoded:Boolean = false;
        private var dontCenter:Boolean;
        
        public var agent:Object;
        public var lat:Number = 0;
        public var lon:Number = 0;
        public var point:LatLng;
        public var ready:Boolean = false;
        public var hasLetters:Array = new Array();
        public var letter:String;
        public var geocodeCallback:Function;
        
        public function AgentMarker(parentMap:Map, agent:Object, letter:String, dontCenter:Boolean = false) {
            super();
            
            this.letter = letter;
            this.parentMap = parentMap;
            this.dontCenter = dontCenter;
            this.agent = agent;

            geocoder = new ClientGeocoder(new ClientGeocoderOptions({
                language: 'en',
                countryCode: 'US'
            }));
            
            run();
        }
        
        public function run():void {
            parentMap.clearOverlays();
            geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, processGeocoder);
            geocoder.geocode(agent.address+", "+agent.city+" "+agent.state);
        }
        
        private function showInfo(e:DataEvent):void {
            dispatchClick();
        }
        
        public function dispatchClick():void {
            dispatchEvent(new CommunicatorEvent('clicked', {marker:mapMarker, pin:this, agent:agent}));
        }
        
        private function processGeocoder(e:GeocodingEvent):void {
            mapMarker = new MapMarker(letter);
            mapMarker.latlong = e.response.placemarks[0].point;
            
            this.lat = e.response.placemarks[0].point.lat();
            this.lon = e.response.placemarks[0].point.lng();
            
            mapMarker.addEventListener('button_clicked', showInfo);
            marker = new Marker(
                e.response.placemarks[0].point,
                new MarkerOptions({
                    hasShadow:false,
                    icon: mapMarker
                })
            );
            
            parentMap.addOverlay(marker);
            mapMarker.marker = marker;
            point = e.response.placemarks[0].point;
            
            //if (!dontCenter) parentMap.setCenter(e.response.placemarks[0].point, 13, MapType.NORMAL_MAP_TYPE);
            
            dispatchEvent(new CommunicatorEvent(CommunicatorEvent.PIN_PLACED, {marker:mapMarker, pin:this, agent:agent}));
            geocoded = true;
            
            if (geocodeCallback != null) geocodeCallback();
        }

        
    }
}