var map = null;
var mapCfg = null;
var pointIndex = 0;
var timeOut = 5;

// {{{ modPopper
function modPopper(popper, marker, infoWindowContents) {
    var fn = null;

    if (infoWindowContents.length > 0) {
        fn = function() {
            marker.openInfoWindowHtml(infoWindowContents);
        };
    } else {
        fn = function() {
            map.closeInfoWindow();
        };
    }

    GEvent.addListener(marker, "click", fn);

    popper.href = "#map";
    popper.onclick = fn;
}
// }}}

// {{{ createMarker
function createMarker(mapPoint) {
    // _debug("creating marker: " + mapPoint.gpoint);
    
    if (mapPoint.gpoint == null) {
        _debug("cannot map null point!");
        return null;
    }
    
    var markerOpts = {"icon": G_DEFAULT_ICON};
    
    if (mapPoint.iconName != null) {
        markerOpts.icon = mapCfg.icons[mapPoint.iconName];
    }

    var marker = new GMarker(mapPoint.gpoint, markerOpts);

    var infoWindowContents = "";

    if (mapPoint.url != null) {
        infoWindowContents += "<a href=\"" + mapPoint.url + "\">";
        
        if (mapPoint.label != null) {
            infoWindowContents += mapPoint.label;
        } else {
            infoWindowContents += mapPoint.url;
        }
        
        infoWindowContents += "</a>";
    } else if (mapPoint.label != null){
        infoWindowContents += mapPoint.label;
    }
    
    if (mapPoint.blurb != null) {
        infoWindowContents += "<br />" + mapPoint.blurb;
    }
    
    // no poppers for points without labels
    if (mapPoint.label != null) {
        var li = document.createElement("li");

        var a = li.appendChild(document.createElement("a"));
        a.setAttribute("class", "markerPopper");
        a.appendChild(document.createTextNode(mapPoint.label));

        modPopper(a, marker, infoWindowContents);

        document.getElementById("clientList").appendChild(li);
    }
    
    return marker;
}
// }}}

// {{{ mapOnePoint
function mapOnePoint() {
    var moreToProcess = false;
    
    if (pointIndex < mapCfg.pointList.length) {
        var m = createMarker(mapCfg.pointList[pointIndex]);
        
        if (m != null) {
            map.addOverlay(m);
        }

        pointIndex += 1;
        moreToProcess = true;

        window.setTimeout(mapOnePoint, timeOut);
    }

    return moreToProcess;
}
// }}}

function _mapLoaded() {
    map.addControl(new GScaleControl());
    map.addControl(new GSmallMapControl());


    _debug("walking points");
    // walk over points
    pointIndex = 0;
    window.setTimeout(mapOnePoint, timeOut);

    // would walk polylines if we were supporting those :-)
}

// {{{ doOnLoad
function doOnLoad() {
    if (GBrowserIsCompatible()) {
        
        _debug("creating map");
        map = new GMap2(document.getElementById("mapDiv"));

        _debug("getting clients.xml");
        GDownloadUrl("/wp-content/themes/cafe54/" + "clients.xml", function(data, responseCode) {
            _debug("response code: " + responseCode);

            if (responseCode == 200) {
                var xml = GXml.parse(data);
                _debug("parsed xml; creating map config");
                
                // create new MapConfig from parsed XML document
                mapCfg = new MapConfig(xml);

                _debug("got map config; setting center point");
                // set the map center
                map.setCenter(mapCfg.centerPoint, mapCfg.zoomLevel);
                
                // try to speed up rendering in IE
                // http://www.panoramio.com/blog/?p=11
                // <img src=”http://www.google.com/mapfiles/shadow50.png” style=”display:none”>
                for (iconKey in mapCfg.icons) {
                    var imgElt = document.createElement("img");
                    imgElt.src = mapCfg.icons[iconKey].icon;
                    imgElt.style.display = "none";
                    document.body.appendChild(imgElt);

                    var imgShadowElt = document.createElement("img");
                    imgShadowElt.src = mapCfg.icons[iconKey].shadow;
                    imgShadowElt.style.display = "none";
                    document.body.appendChild(imgShadowElt);
                }

                if (map.isLoaded()) {
                    _mapLoaded();
                } else {
                    GEvent.addListener(map, "load", _mapLoaded);
                }
            } else {
                alert(responseCode);
            }
        });
        
    }
}
// }}}

