
var maplayers          = new Array();
var iconPath        = "/idx/img/map-layers/";
var mapped_results  = [];

var FeaturedIcons   = new Array();
var FeaturedIconsBase = null;
FeaturedIconsBase            = new GIcon(G_DEFAULT_ICON);
FeaturedIconsBase.iconSize   = new GSize(12, 20);
FeaturedIconsBase.shadowSize = new GSize(0,0);
FeaturedIconsBase.iconAnchor = new GPoint(6, 20);

maplayers["School"]    = new MarkerLayer("School",     iconPath + "marker_blue.png");
maplayers["Church"]    = new MarkerLayer("Church",     iconPath + "marker_orange.png");
maplayers["Park"]      = new MarkerLayer("Park",       iconPath + "marker_green.png");
maplayers["Airport"]   = new MarkerLayer("Airport",    iconPath + "marker_purple.png");
maplayers["Hospital"]  = new MarkerLayer("Hospital",   iconPath + "marker_red.png");

function MarkerLayer(name, icon)
{
    this.name = name;
    this.markers = new Array();


    FeaturedIcons[name] = new GIcon(FeaturedIconsBase, icon);

    this.addMarker = function(marker, labelText)
    {
        if (mapped_results[labelText] == undefined) {
            marker.setTooltip(label);
            map.addOverlay(marker);
            GEvent.bindDom(marker.getEventTarget(), 'mouseover', marker, onMouseOver);
    	    GEvent.bindDom(marker.getEventTarget(), 'mouseout', marker, onMouseOut);
            labels[marker.getId()] = labelText;
            this.markers.push(marker)
            mapped_results[labelText] = true;
        }
    }

    this.getMarkerArray = function()
    {
        return this.markers;
    }

    this.markerCount = function()
    {
        return this.markers.length;
    }

    this.hideMarkers = function()
    {
        for (index in this.markers) {
            if (index == 'indexOf') continue;
            this.markers[index].hide();
        }
    }

    this.showMarkers = function()
    {
        for (index in this.markers) {
            if (index == 'indexOf') continue;
            this.markers[index].show();
        }
    }
}

function getLayerMarkers(markerLayer)
{
    var bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    $.getJSON("/idx/map/layer_markers.php", { "layer": markerLayer, "la1": sw.lat(), "la2": ne.lat(), "lo1": sw.lng(), "lo2": ne.lng() },

        function(data) {

            if (data.Results[0]) {

                var type = data.Results[0].type;

                for (i = 0; i < data.Results.length; i++) {
                    var newMarker = new BpMarkerLight(new GLatLng(data.Results[i].latitude, data.Results[i].longitude), {icon:FeaturedIcons[type]});
                    maplayers[type].addMarker(newMarker, data.Results[i].name);
                }

            }

        }

    );

}

function showLayer(lname)
{

    if (lname == null) {
        lname = getLayerFromCookie();
    }

    for (ltype in maplayers) {
        if (ltype == 'indexOf') continue;
        if (ltype == lname) {
            // add / show
            getLayerMarkers(lname);
            maplayers[ltype].showMarkers();
        } else {
            // hide
            maplayers[ltype].hideMarkers();
        }
    }

    var expires = new Date();
    expires.setTime(expires.getTime() + 604800000);
    document.cookie = 'layer=' + lname + '; expires=' + expires.toGMTString() + '; path=/idx/map';

}

function getLayerFromCookie()
{
    var lname = 'none';

    var keyPos = document.cookie.indexOf('layer=');
    if (keyPos != -1) {
        var scPos = document.cookie.indexOf(';', keyPos);
        var cookieData = document.cookie.substring(keyPos+6, scPos);
        lname = cookieData;
    }

    return lname;
}