	var map = null;
    var layer;
    var shape;
    var xmlDoc;
    var latlongs = new Array();
    var descs = new Array();
    var photourls = new Array();    
    var strXML;
	var pinIcon = "/images/icon_map_pushpin.gif";
    var sessionedVEMapState;

    function importXML()
    {
        try //Internet Explorer
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(strXML);
        }
        catch(e)
        {
            try //Firefox, Mozilla, Opera, etc.
            {
                xmlDoc = document.implementation.createDocument("","",null);
                xmlDoc.async = false;
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(strXML, "application/xml");
            }
            catch(e)
            {
                alert(e.message);
                return;
            }
        }
    }
    
    //add the pins to the map;
    function addPins()
    {  
        for(var j=0;j<latlongs.length;j++)
        {
            var pin = new VEShape(VEShapeType.Pushpin,latlongs[j]);
            
            //set the pin icon
            pin.SetCustomIcon(pinIcon);
            
            //set the property description displayed on mouseover;
            pin.SetDescription(descs[j]);
            
            //add the shaped(pins) to the map;
            map.AddShape(pin);    
        }
    }
    
    //read data from the xml file;
    //'latlongs' array stores latitude and longitude of pins;
    //'descs' array stores property descriptions;
    //'photourls' array stores property photo urls;
    function getXmlData() 
    {
        importXML();
        var nodeList = xmlDoc.getElementsByTagName("listing");  
        var browserName = navigator.appName;
        
        for(var i=0; i< nodeList.length; i++) 
        {   
			var id = nodeList[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
			var listingType = nodeList[i].getElementsByTagName("listingType")[0].childNodes[0].nodeValue;
			var photoURL = nodeList[i].getElementsByTagName("photoURL")[0].childNodes[0].nodeValue;
			var address = nodeList[i].getElementsByTagName("address")[0].childNodes[0].nodeValue;
			//var city = nodeList[i].getElementsByTagName("city")[0].childNodes[0].nodeValue;
			//var zip = nodeList[i].getElementsByTagName("zip")[0].childNodes[0].nodeValue;
			//var state = nodeList[i].getElementsByTagName("state")[0].childNodes[0].nodeValue;
			var lat = nodeList[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue;
			var lng = nodeList[i].getElementsByTagName("long")[0].childNodes[0].nodeValue;
			var price = nodeList[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
			var status = nodeList[i].getElementsByTagName("status")[0].childNodes[0].nodeValue;
			var beds = nodeList[i].getElementsByTagName("beds")[0].childNodes[0].nodeValue;
			var baths = nodeList[i].getElementsByTagName("baths")[0].childNodes[0].nodeValue;
			var squareSize = nodeList[i].getElementsByTagName("squareSize")[0].childNodes[0].nodeValue;
			var detailsURL = nodeList[i].getElementsByTagName("detailsURL")[0].childNodes[0].nodeValue;     
			var priceMeaning = '(' + nodeList[i].getElementsByTagName("priceMeaning")[0].childNodes[0].nodeValue + ')'; 
        
            //adding data to the arrays
            latlongs[i] = new VELatLong(lat,lng);
            if (listingType == "FSBO") listingType = "For Sale by Owner";
            if (listingType == "FSBR") listingType = "For Sale by Realtor";

            if ((listingType == "For Sale by Owner") || (listingType == "For Sale by Realtor")||(listingType == "Foreclosure")||(price == "unpublished")) 
            descs[i] = '<h4>' + listingType + '</h4>' +
				'<span class="text-large text-quinary">' + price + '</span><br/>' +
				'<div><img class="float-left" src=' + photoURL + ' width="85px" height="65px" />' +
                '<span class="text-large text-primary-darkest">' + address + '</span><br/>' +
                'Bedrooms: ' + beds + '<br/>' +
                'Bathrooms: ' + baths + '<br/>' +
                'Square Feet: ' + squareSize + '</div>' +
                '<a class="more-link text-small float-right" href=' + detailsURL + '>View details</a>';   
            else   
				descs[i] = '<h4>' + listingType + ' | ' + status + '</h4>' +
				'<span class="text-large text-quinary">' + price + '</span> ' + priceMeaning + '<br/>' +
				'<div><img class="float-left" src=' + photoURL + ' width="85px" height="65px" />' +
                '<span class="text-large text-primary-darkest">' + address + '</span><br/>' +
                'Bedrooms: ' + beds + '<br/>' +
                'Bathrooms: ' + baths + '<br/>' +
                'Square Feet: ' + squareSize + '</div>' +
                '<a class="more-link text-small float-right" href=' + detailsURL + '>View details</a>';     
        }       
    }   
    
    //calculates the latitude of the center point of the map 
    function getMapCenterLat() {
        var sum = 0 ;
        for(var j=0;j<latlongs.length;j++) {
            sum = sum + Number(latlongs[j].Latitude);
        }
        return (sum/latlongs.length);
    }
    
    //calculates the longitude of the center point of the map 
    function getMapCenterLong() {
        var sum = 0;
        for(var j=0;j<latlongs.length;j++) {
            sum = sum + Number(latlongs[j].Longitude);
        }
        return (sum/latlongs.length);
    }
    
    function openMap()
    {
        $("#hack").hide();
        $("#ve-map").slideDown("slow");
        $("#ve-map").css("height","248px");
        $("#ve-map").css("backgroundColor","transparent");
        
        if ($("#map-show").is(":visible"))
        {
            $("#map-show").hide();
            $("#map-hide").show();
            saveVEMapState("open");
        }
        getXmlData(); 
        var mapCenterLatLong = new VELatLong(getMapCenterLat(), getMapCenterLong()); //the center point of the map;
        map = new VEMap("ve-map");
        map.SetDashboardSize("normal"); //set the map dashboard size: tiny, small, normall;
        map.LoadMap();
        
        map.SetMouseWheelZoomToCenter(false); //map zoom to the cursor position on the screen, not to the center; 
        map.SetMapStyle(VEMapStyle.Road); //set map style: Road, Aerial, Hybrid, Birdseye;
        
        addPins();
        
        //latlongs.push(mapCenterLatLong); //add the center point of the to the pins array for a better poisitining;
        map.SetMapView(latlongs); //set map for best view based on pin locations;
    }
    
    function closeMap()
    {
		$("#ve-map").slideUp("slow");
		if ($("#map-hide").is(":visible"))
		{
			$("#map-hide").hide();
			$("#map-show").show();
			saveVEMapState("closed");
		}
		$("#hack").show();
    }
    
	//Simple Ajax, without callback function
	//Author: Michaelj
	function saveVEMapState(state)
	{
		request = createRequest();
		
		if (request == null) {
			alert("Unable to create request");
		}
		else
		{
			var url = "/ajax-server/saveVEMapState.aspx?state=" + state;
			
			request.open("GET", url, true);
			request.send(null);
		}
	}