// Plot RETS Data On A Y! Map
// Version 1.0
// November 19th, 2007
// Author: Jason Levitt
// BSD license

var MAP = function(){
    var $ = {};
    // List of LatLons
    var geoPoints = [];
    // List of text addresses
    var addys = [];
    // Corresponding list of extra house data
    var houseData = [];
    // create custom marker image
    var imgSrc = './home.png';
    var markerImg = new YImage(imgSrc, new YSize(25, 27));
    // The RETS data in JSON format
    var dataFeed = "./data.js";
    
    return {
        init: function(mapDiv, addy){
            $.msgDiv = document.getElementById('messageDIV');
            // Create the map
            $.mo = new YMap(document.getElementById(mapDiv));
            // Event that continues initialization after map is drawn
            YEvent.Capture($.mo, EventsList.endMapDraw, MAP.continueInit);
            $.mo.addZoomLong();
            $.mo.addTypeControl();
            $.mo.disableKeyControls();
            // Do initial centering and drawing of map
            $.mo.drawZoomAndCenter(addy, 5);
        },
        continueInit: function(){
            // Go get the RETS data and geocode each point
            MAP.getTheData();
        },
        handleSuccess: function(o){
            if (o.responseText !== undefined) {
                // Turn the JSON data into a JS object
                // Only use eval when you trust the data source
                // Otherwise, use the JSON parser found at http://www.json.org/js.html
                var jObj = eval('(' + o.responseText + ')');
                var j = 0;
                 // This event will draw a pin as each address is geocoded
                YEvent.Capture($.mo, EventsList.onEndGeoCode, MAP.drawPins);
                while (jObj[j]) {
                    var jo = jObj[j];
                    // Create geo-codable addresses
                    var address = jo.HSN + ' ' + jo.STR + ', ' + jo.CIT + ', Illinois';
                    addys.push(address);
                    // Get extra house data and put it into an associative array, indexed
                    // by the address
                    houseData[address] = '<b>Type:</b> ' + jo.LongTYP + '<br /><b>Description:</b> ' + jo.Description + '<br /><b>Listing Price:</b> $' + jo.LP;
                    // Geocode the address which fires the onEndGeoCode event, calling drawPins
                    $.mo.geoCodeAddress(address);
                    j++;
                }
            }
        },
        handleFailure: function(o){
            if (o.responseText !== undefined) {
                $.msgDiv.innerHTML = "<b>Data Fetch Call Failed</b><br />";
                $.msgDiv.innerHTML += "<ul><li>Transaction id: " + o.tId + "</li>";
                $.msgDiv.innerHTML += "<li>HTTP status: " + o.status + "</li>";
                $.msgDiv.innerHTML += "<li>Status code message: " + o.statusText + "</li></ul>";
            }
        },
        getTheData: function(){
            var request = YAHOO.util.Connect.asyncRequest('GET', dataFeed, dataCallback);
            
        },
        drawPins: function(o){
            // Create a marker and add it to the map
            var marker = MAP.createMarker(o);
            $.mo.addOverlay(marker);
            // This is a way to tell if we're done drawing pins: the list of addys will
            // be the same length as the list of geoPoints. 
            if (geoPoints.length == addys.length) {
                // Turn off these events since the next two statements will fire them
                YEvent.Remove($.mo, EventsList.endMapDraw, MAP.continueInit);
                YEvent.Remove($.mo, EventsList.onEndGeoCode, MAP.drawPins);
                // Move the map center to the best zoom and center location
                var zoomAndCenterObj = $.mo.getBestZoomAndCenter(geoPoints);
                $.mo.drawZoomAndCenter(zoomAndCenterObj.YGeoPoint, zoomAndCenterObj.zoomLevel);
            }
        },
        createMarker: function(data){
        
            // Create our GeoPoint and add it to our list of GeoPoints
            var point = new YGeoPoint(data.GeoPoint.Lat, data.GeoPoint.Lon);
            geoPoints.push(point);
            
            // Create our Marker and set the smartWindow to red
            var marker = new YMarker(point, markerImg);
            marker.setSmartWindowColor('red');
            
            // add auto expand of title on mouse over marker
            marker.addAutoExpand(data.Address);
            
            // add the other house data in the bubble
            var swData = houseData[data.Address];
            
            // capture click on marker to open smart window
            YEvent.Capture(marker, EventsList.MouseClick, function(){
                marker.openSmartWindow(swData);
            });
            
            return marker;
        }
    };
}
();

// Register callback functions
var dataCallback = {
    success: MAP.handleSuccess,
    failure: MAP.handleFailure,
    scope: MAP
};

// Give the map the name of the div id and an initial location
window.onload = function(){
    MAP.init('ymap', 'Woodridge, Illinois');
};
