    //global vars
	var DEFAULT_ZOOM = 11  
	var DEFAULT_CIRCLE_RADIUS_MILES = 5  
	var GMAP_MAP_DIV = "gmap_default_container";
	var GMAP_STYLE_DIV = "gmap_style_container";
    var INFO_START = "start";
    var INFO_DIR_TO = "directionsTo";
    var INFO_DIR_FROM = "directionsFrom";
    var INFO_STREET_VIEW = "streetView";
    var CSS_MAP_SHOW = "gMapOverlayVisible";
    var CSS_MAP_HIDE = "gMapOverlayHidden";
    var CSS_MAP_EMBED = "gMapEmbedded";
    var ORANGE_COLOR_HEX = "#FF9933";
    var RED_COLOR_HEX = "#FF0000";
    var GREEN_COLOR_HEX = "#00FF00";
    var BLUE_COLOR_HEX = "#0000FF";

    var posting = 1;
    var orangeIcon;
    var greenIcon;
    var bluIcon
    var circleOverlays = new Array();
    var currentPanoramaDiv = null;

    
//=============================================
//object to store markers added to map
	function markerInfo() 
	{
	    this.enteredAddr = null;
	    this.marker =  null;
	    this.selectLink = null;
	    this.jobTitleHtml = null;
	    this.infoWindowTextToDir = null;
	    this.infoWindowTextFromDir = null;
	    this.infoWindowText = null;
	    this.infoWindowTextStreetView = null;
	    this.drivingAddress = null;
	    this.city = null;
	    this.stateCode = null;
	    this.zipCode = null;
	    this.visible = true;
	    this.geoHash = null;
	};


//object to allow interfacing with GMap on client
	var googleMapClient =
	{
	    //PROPERTIES
	    gMap: null,
	    gGeocoder: null,
	    gLocArray: new Array(),
	    gMarkerBounds: null,
	    gUseLocDiv: false,
	    isEmbedded: false,
	    currentRefTargetName: "",
    	
	    //"PUBLIC" METHODS
        show: function(mapRefPointObjName) {
            this._show(mapRefPointObjName);
        },
        hide: function() {
            this._hide();
        },
        toggleVisibility: function(mapRefPointObjName) {
            this._toogleMapVisibility(mapRefPointObjName);
        },
	    resetResizedMap: function(showDefaultMarker) {
	        this._resetResizedMap(showDefaultMarker);
	    }, 
	    centerOnMarkerGroup: function(){
	        this._centerOnMarkerGroup();
	    },
	    addMarkerToMap: function(latVal, longVal, markerTitle, windowText, openWindowText,driveAddress){
	        this._addMarkerToMap(latVal, longVal, markerTitle, windowText, openWindowText,driveAddress);
	    },
	    loadGMap: function(showLovDiv, mapDivName) {
	        this._loadGMap(showLovDiv, mapDivName);
	    },
	    showToHereDirections: function(indexValue) {
	        this._showInfoText(this.gLocArray[indexValue], INFO_DIR_TO);
	    },
	    showFromHereDirections: function(indexValue) {
	        this._showInfoText(this.gLocArray[indexValue], INFO_DIR_FROM);
	    },
	    showStreetView: function(indexValue) {
	        this._showInfoText(this.gLocArray[indexValue], INFO_STREET_VIEW);
	    },

	    focusOnMarker: function(indexValue) {
	        this._focusOnMarker(this.gLocArray[indexValue], INFO_START);
	        //this._focusOnMarker(this.gLocArray[indexValue], INFO_STREET_VIEW);
        },
 	    initGMapContainer: function() {
	        this._initGMapContainer();
	    },
 	    apply: function(postingId) {
	        posting = postingId;
	        getApplicationUrl();
	    },
       
        //"PRIVATE" METHODS
        
        _initGMapContainer: function() {
           var divObj = document.getElementById(GMAP_STYLE_DIV);
           if(divObj)
           {
              if(this.isEmbedded)
              {
                divObj.className=CSS_MAP_EMBED;
                closeLink = document.getElementById("mapCloseLink");
                if(closeLink)
                {
                    closeLink.style.visibility = "hidden";
                    closeLink.style.display = "none";
                }
              }
              else
              {
                divObj.className=CSS_MAP_HIDE;
              }
            }
            
        },

	    //initializes the google map control
        _loadGMap : function (showLocDiv, mapDivName) 
        {
          if(!GBrowserIsCompatible())
          {
            //note: bug with firefox 1.5 and GMap requires the negative check
          }
          else
          {    //init map

//          alert(document.getElementById(mapDivName));
              this.gMarkerBounds = new GLatLngBounds();
              this.gMap = new GMap2(document.getElementById(mapDivName),{googleBarOptions: {suppressZoomToBounds:true, onMarkersSetCallback:doGoogleBarAddMarkerComplete}});
              this.gMap.setCenter(new GLatLng(0, 0), 1);
              this.gMap.addControl(new GLargeMapControl());
              this.gMap.addControl(new GScaleControl());
              this.gMap.addControl(new GMapTypeControl());
              this.gMap.addMapType(G_PHYSICAL_MAP);    

              this.gMap.enableGoogleBar();
              
                this.gMap.enableDoubleClickZoom();
                //this.gMap.enableContinuousZoom();
                //this.gMap.enableScrollWheelZoom();

                this.gMap.setZoom(DEFAULT_ZOOM);        
              
              
                //var streetOverlay = new GStreetviewOverlay();
                //this.gMap.addOverlay(streetOverlay);
 
              
              this.gUseLocDiv = showLocDiv;
              
              

              
              window.onunload = unloadGMap;
              window.onresize = onWindowGMapResize;
                //searches
                orangeIcon = new GIcon();
                orangeIcon.image = "/images/icons/mm_20_orange.png";
                orangeIcon.shadow = "/images/icons/mm_20_shadow.png";
                orangeIcon.iconSize = new GSize(12, 20);
                orangeIcon.shadowSize = new GSize(22, 20);
                orangeIcon.iconAnchor = new GPoint(6, 20);
                orangeIcon.infoWindowAnchor = new GPoint(5, 1);
                //postings
                greenIcon = new GIcon();
                greenIcon.image = "/images/icons/mm_20_green.png";
                greenIcon.shadow = "/images/icons/mm_20_shadow.png";
                greenIcon.iconSize = new GSize(12, 20);
                greenIcon.shadowSize = new GSize(22, 20);
                greenIcon.iconAnchor = new GPoint(6, 20);
                greenIcon.infoWindowAnchor = new GPoint(5, 1);
                //visitors
                blueIcon = new GIcon();
                blueIcon.image = "/images/icons/mm_20_blue.png";
                blueIcon.shadow = "/images/icons/mm_20_shadow.png";
                blueIcon.iconSize = new GSize(12, 20);
                blueIcon.shadowSize = new GSize(22, 20);
                blueIcon.iconAnchor = new GPoint(6, 20);
                blueIcon.infoWindowAnchor = new GPoint(5, 1);
                
                circleOverlays[ORANGE_COLOR_HEX ] = null;
                circleOverlays[GREEN_COLOR_HEX] = null;
                circleOverlays[BLUE_COLOR_HEX] = null;
                circleOverlays[RED_COLOR_HEX] = null;
           }
        },

	    // Creates a marker at the given point with the given markerText
        _createMarker : function (point, markerText, indexValue, markerIcon) {
	        var marker = new GMarker(point, {icon:markerIcon,title:markerText});
	        //add onClick handler to show the marker's info window when clicked
            GEvent.addListener(marker, "click", function() {googleMapClient.focusOnMarker(indexValue);});
            //register the marker with gmarkBounds so that map will know the viewable limit of the markers
	        this.gMarkerBounds.extend(marker.getPoint());
            return marker;
	    },
    	
    	
	    //add a marker to the map control
        _addMarkerToMap: function (latVal, longVal,markerTitle,windowText, openWindowText,driveAddress)
        {
        
        
            var iconToUse;
            var city, state, zip
            var existingMarkerInfo = null;

            existingMarkerInfo = this.gLocArray[latVal.toString() + longVal.toString()];                       
            if(existingMarkerInfo != null)
            {
                existingMarkerInfo.jobTitleHtml += '<br/>' + windowText;
                this.gLocArray[latVal.toString() + longVal.toString()] = existingMarkerInfo;
            }
            else  //new location -- add to map and local array of info objects
            {
                point = new GLatLng(latVal,longVal);
                
                switch(markerTitle)
                {
                case "search":
                    iconToUse= orangeIcon;
                    break;
                case "post":
                    iconToUse= greenIcon;
                    break;
                case "visitor":
                    iconToUse= blueIcon;
                    break;
                default:
                    iconToUse= orangeIcon;
                    break;
                }
               
                var marker = this._createMarker(point, markerTitle, this.gLocArray.length, iconToUse);
                this.gMap.addOverlay(marker);
                if(this.gLocArray.length ==0)
                {
                    this.gMap.setCenter(this.gMarkerBounds.getCenter());

                }
                 
                //init local markerInfo object
                var localMarker = new markerInfo();
                localMarker.selectLink = markerTitle;
                localMarker.marker = marker;
                localMarker.drivingAddress = driveAddress;
                localMarker.city = city;
                localMarker.stateCode = state;
                localMarker.zipCode = zip;
                localMarker.jobTitleHtml = windowText;
                localMarker.infoWindowText = this._generateInfoWinText(INFO_START, "",  this.gLocArray.length, latVal, longVal,driveAddress);
                localMarker.infoWindowTextToDir = this._generateInfoWinText(INFO_DIR_TO, "",  this.gLocArray.length, latVal, longVal,driveAddress);
                localMarker.infoWindowTextFromDir = this._generateInfoWinText(INFO_DIR_FROM, "",  this.gLocArray.length, latVal, longVal,driveAddress);
                localMarker.infoWindowTextStreetView = this._generateInfoWinText(INFO_STREET_VIEW, "",  this.gLocArray.length, latVal, longVal,driveAddress);
                //localMarker.geoHash = latVal.toString() + longVal.toString(); //used to support searching for existing markers at same location
                
                this.gLocArray[this.gLocArray.length] = localMarker;
                this.gLocArray[latVal.toString() + longVal.toString()] = this.gLocArray[this.gLocArray.length-1];
                if(openWindowText){this._focusOnMarker(localMarker, INFO_START)};
            }
        
        },
        //build the different viwes of the window info text balloon
        _generateInfoWinText: function(textType, winText, indexValue, latVal, longVal, driveAddress)
        {
            var returnString = winText;
//            if (driveAddress != "")
//            {
                switch (textType)
                {
                case INFO_START:
                    returnString += '<br /><br /><a href="javascript:googleMapClient.showStreetView('+indexValue+')">Street View</a>';
                    returnString += '<br /><b>Directions:</b>: <a href="javascript:googleMapClient.showToHereDirections('+indexValue+')">To here</a> - <a href="javascript:googleMapClient.showFromHereDirections('+indexValue+')">From here</a>'
                    break;
                case INFO_DIR_TO:
                    returnString += '<br /><br /><a href="javascript:googleMapClient.showStreetView('+indexValue+')">Street View</a>';
                    returnString += '<br /><b>Directions:</b> <b>To here</b> - <a href="javascript:googleMapClient.showFromHereDirections(' + indexValue + ')">From here</a>' +
                   '<br>Start address:<form id="mapDir" action="http://maps.google.com/maps" method="get" target="_blank">' +
                   '<input type="text" SIZE=20 MAXLENGTH=40 name="saddr" id="saddr" value="" />' +
                   '<INPUT value="Go" TYPE="SUBMIT">' +
                   '<input type="hidden" name="daddr" value="' +driveAddress +'"/></form>';
                
                    break;
                case INFO_DIR_FROM:
                    returnString += '<br /><br /><a href="javascript:googleMapClient.showStreetView('+indexValue+')">Street View</a>'
                    returnString += '<br /><b>Directions:</b>: <a href="javascript:googleMapClient.showToHereDirections(' + indexValue + ')">To here</a> - <b>From here</b>' +
                   '<br>End address:<form  id="mapDir" action="http://maps.google.com/maps" method="get"" target="_blank">' +
                   '<input type="text" SIZE=20 MAXLENGTH=40 name="daddr" id="daddr" value="" />' +
                   '<INPUT value="Go" TYPE="SUBMIT">' +
                   '<input type="hidden" name="saddr" value="' + driveAddress + '"/></form>';
                    break;
                default: 
                    break;
                }; 
//            }
            
            if(textType == INFO_STREET_VIEW)
            {
            
                returnString += '<br /><br /><b>Directions:</b>: <a href="javascript:googleMapClient.showToHereDirections('+indexValue+')">To here</a> - <a href="javascript:googleMapClient.showFromHereDirections('+indexValue+')">From here</a>'
                returnString += '<div name="infoPano" id="infoPano" style="display:block;width: 300px; height: 200px"></div>';

            }
            return returnString
            
        },
        
        
        //center on marker in gLocArray 
        _centerOnAddr: function (linkText)
        {
	      for (i=0; i<this.gLocArray.length;i++)
	      {
	        if (this.gLocArray[i].selectLink == linkText)
	        {
                this._focusOnMarker(this.gLocArray[i], INFO_START);
	        }
	      }
        },
        
        //when the map div's display is toggled from none to block/inline
        //the map forgets a lot about itself
        _resetResizedMap: function (showDefaultMarker)
        {

            this.gMap.checkResize();
            if(showDefaultMarker){this._focusOnMarker(this.gLocArray[0], INFO_START);}
        },
        
        //center on the group of markers currently available
        _centerOnMarkerGroup : function()
        {
          
            this.gMap.closeInfoWindow();
            this.gMap.setCenter(this.gMarkerBounds.getCenter());
            if(this.gMap.getBoundsZoomLevel(this.gMarkerBounds) < DEFAULT_ZOOM)
            {
                this.gMap.setZoom(this.gMap.getBoundsZoomLevel(this.gMarkerBounds));
            }else{
                this.gMap.setZoom(DEFAULT_ZOOM);
            }
        },
        
        //will open the marker's info window
        //in addition to opening the window, this function will draw a 
        //circle around the marker
        _focusOnMarker: function (selMarkerInfo, textType)
        {
         
            this.gMap.setCenter(selMarkerInfo.marker.getPoint(), DEFAULT_ZOOM);
            this._showInfoText(selMarkerInfo, textType);
         
            switch(selMarkerInfo.selectLink)
            {
            case "search":
                drawCircle(selMarkerInfo.marker.getPoint(), DEFAULT_CIRCLE_RADIUS_MILES, ORANGE_COLOR_HEX );
                break;
            case "post":
                drawCircle(selMarkerInfo.marker.getPoint(), DEFAULT_CIRCLE_RADIUS_MILES, GREEN_COLOR_HEX);
                break;
            case "visitor":
                drawCircle(selMarkerInfo.marker.getPoint(), DEFAULT_CIRCLE_RADIUS_MILES, BLUE_COLOR_HEX);
                break;
            default:
                drawCircle(selMarkerInfo.marker.getPoint(), DEFAULT_CIRCLE_RADIUS_MILES, ORANGE_COLOR_HEX );
                break;
            
            
            }

            
               

        },
        //will show selected info window
        _showInfoText: function (selMarkerInfo, textType)
        {
        //alert(selMarkerInfo.infoWindowText)
            var infoText = selMarkerInfo.drivingAddress + '<br/>' + selMarkerInfo.jobTitleHtml;
            var inputFocusElem = '';
            var markerLatLng = '';
            
            switch (textType)
            {
            case INFO_START:
                 infoText += selMarkerInfo.infoWindowText;
                break;
            case INFO_DIR_TO:
                infoText += selMarkerInfo.infoWindowTextToDir;
                inputFocusElem = 'saddr';
                break;
            case INFO_DIR_FROM:
                infoText += selMarkerInfo.infoWindowTextFromDir;
                inputFocusElem = 'daddr';
                break;
            case INFO_STREET_VIEW:
                infoText += selMarkerInfo.infoWindowTextStreetView;
                markerLatLng = selMarkerInfo.marker.getPoint();
                break;
            };    
            selMarkerInfo.marker.openInfoWindowHtml(infoText, {maxWidth:500});
            if (inputFocusElem != '')
            {
                setTimeout("document.getElementById('"+ inputFocusElem + "').focus();", 100); //give time to render/register element
            }
             if(textType == INFO_STREET_VIEW)
             {
                  setTimeout('showStreetView(' +markerLatLng.lat() + ',' +markerLatLng.lng() + ',"infoPano");', 1000 ); //give time to render/register element
             }
        },


        //show/ hide the map div container
        _toogleMapVisibility : function (mapRefPointObjName, showMap)
        {
            var divObj = document.getElementById(GMAP_STYLE_DIV);
            var nodeHeight = 12;
            
            if(divObj.className==CSS_MAP_HIDE || (divObj.className==CSS_MAP_SHOW && googleMapClient.currentRefTargetName != mapRefPointObjName))
            {
                //show it
                this._show(mapRefPointObjName);
            }       
            else
            {
                this._hide();
            }
       },
        _show: function(mapRefPointObjName)
        {
            var divObj = document.getElementById(GMAP_STYLE_DIV);
            var nodeHeight = 12;
            var node = document.getElementById(mapRefPointObjName);
            googleMapClient.currentRefTargetName = mapRefPointObjName;
            if (node)
            {


                if(node.offsetHeight)
                {
                    nodeHeight = node.offsetHeight
                }
                var x=0; var y=0; 
                var lastLeftOffset = 0; var lastTopOffset = 0;
	            if (node.offsetParent){
			        while (node.offsetParent){
                        lastLeftOffset = node.offsetLeft;
                        lastTopOffset = node.offsetTop;
    			        x+=node.offsetLeft;
	    		        y+=node.offsetTop;
		    	        node=node.offsetParent;
		            }
		            x= x-lastLeftOffset;
		            y= y-lastTopOffset;
	            } else if (node.x) {
		            x+=node.x;
		            y+=node.y;
	            }
               divObj.className=CSS_MAP_SHOW;
               divObj.style.left=(x)+"px";
               divObj.style.top=y+nodeHeight+"px";
               this._resetResizedMap(true);
            }       

        },

        _hide: function()
        {
            var divObj = document.getElementById(GMAP_STYLE_DIV);
            googleMapClient.currentRefTargetName = "";
            divObj.className=CSS_MAP_HIDE;           
        }
       
    };
        
        

    ////////////////////////////////
    //Callback functions / event handlers
    function onWindowGMapResize()
    {
        if(googleMapClient.currentRefTargetName != "")
        {
            googleMapClient.show(googleMapClient.currentRefTargetName );
        }
    
    }

  
    //clean up and release client side resources
    function unloadGMap()
    {
        GUnload();
    }
   
    function gMap_onCallbackComplete(result, context)
    {

        var results = result.split('|');
       //0 - action, 1 - url, 2 - target
        if (results[2] == '_blank')
        {
           var popUpWin = window.open(results[1], results[2]);
        }
        else
        {
            document.location.href = results[1];
        }

    }       
    
    
    ///////////////////////////////////////////
    //circle overlays
    //////////////////////////////////////////
    function clearCircles()
    {
       googleMapClient.gMap.removeOverlay(circleOverlays[ORANGE_COLOR_HEX ]);
       googleMapClient.gMap.removeOverlay(circleOverlays[GREEN_COLOR_HEX]);
       googleMapClient.gMap.removeOverlay(circleOverlays[BLUE_COLOR_HEX]);
       googleMapClient.gMap.removeOverlay(circleOverlays[RED_COLOR_HEX]);
       
    }
    function drawCircle(point, radius, colorToUse) {
          //alert("drawCircle@"+point+" r="+radius); 
          if(circleOverlays[colorToUse] != null)
          {
          //alert("removing " + colorToUse); 
          googleMapClient.gMap.removeOverlay(circleOverlays[colorToUse]); 
          }
          var cColor = colorToUse;
          var cWidth = 5;
          var Cradius = radius;   
          var d2r = Math.PI/180; 
          var r2d = 180/Math.PI; 
          var Clat = (Cradius/3963)*r2d; 
         //alert("point.lat()="+point.lat()+" point.lng()="+point.lng());  
          var Clng = Clat/Math.cos(point.lat()*d2r); 
          var Cpoints = []; 
          for (var i=0; i < 33; i++) 
          { 
            var theta = Math.PI * (i/16); 
            var CPlng = point.lng() + (Clng * Math.cos(theta)); 
            var CPlat = point.lat() + (Clat * Math.sin(theta)); 
            var P = new GLatLng(CPlat,CPlng);
            googleMapClient.gMarkerBounds.extend(P);
            Cpoints.push(P); 
          }
             //alert("circle@"+point+" color="+cColor+" width="+cWidth+" length="+Cpoints.length);
            circleOverlays[colorToUse] = new GPolyline(Cpoints,cColor,cWidth)
            //alert('adding circle')
            googleMapClient.gMap.addOverlay(circleOverlays[colorToUse]); 
    } 

    /////////////////////////////////////////////////////////////
    //Helper functions
    /////////////////////////////////////////////////////////////
    
    //will show a street view panaroma, if available in the specified div
    function showStreetView(lat, lng, divElementName)
    {
            latLng = new GLatLng(lat, lng);
            currentPanoramaDiv = divElementName;
            panorama = new GStreetviewPanorama(document.getElementById(currentPanoramaDiv));
            panorama.setLocationAndPOV(latLng, {"yaw":0});  //due north - should be var

            GEvent.addListener(panorama, "error", handlePanoramaError);
    }
    
    function handlePanoramaError(errorCode)
    {
        var divObj = document.getElementById(currentPanoramaDiv);
        
        switch(errorCode)
        {
        case 600 : //GStreetviewPanorama.ErrorValues.NO_NEARBY_PANO:
            divObj.innerHTML = "<br/><br/><b>No street view is available.</b>";
            break;
        case 603 : //GStreetviewPanorama.ErrorValues.FLASH_UNAVAILABLE:
            divObj.innerHTML = "<br/><br/><b>Flash player is needed to view street view.</b>";
            break;

        }
        
    }
    function doGoogleBarAddMarkerComplete(result)
    {
        if(result.length == 1)
        {
            drawCircle(result[0].marker.getPoint(), DEFAULT_CIRCLE_RADIUS_MILES, RED_COLOR_HEX);
        }
    }


