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

var FeaturedIcons   = new Array();
var FeaturedIconsBase = null;
FeaturedIconsBase            = new GIcon(G_DEFAULT_ICON);
FeaturedIconsBase.shadow     = '/img/map/ico-map-shadow.png';
FeaturedIconsBase.iconSize   = new GSize(14, 17);
FeaturedIconsBase.shadowSize = new GSize(17,17);
FeaturedIconsBase.iconAnchor = new GPoint(0, 0);

layers["School"]    = new MarkerLayer("School",     iconPath + "ico-map-school.png");
layers["Church"]    = new MarkerLayer("Church",     iconPath + "ico-map-church.png");
layers["Park"]      = new MarkerLayer("Park",       iconPath + "ico-map-park.png");
layers["Airport"]   = new MarkerLayer("Airport",    iconPath + "ico-map-airport.png");
layers["Hospital"]  = new MarkerLayer("Hospital",   iconPath + "ico-map-hospital.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, onMouseOverLayer);
    	    GEvent.bindDom(marker.getEventTarget(), 'mouseout', marker, onMouseOutLayer);
            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) {
            this.markers[index].hide();
        }
    }

    this.showMarkers = function()
    {
        for (index in this.markers) {
            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]});
                    layers[type].addMarker(newMarker, data.Results[i].name);
                }

            }

        }

    );

}

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

    for (type in layers) {
        if (type == lname) {
            // add / show
            getLayerMarkers(lname);
            layers[type].showMarkers();
        } else {
            // hide
            layers[type].hideMarkers();
        }
    }

    var expires = new Date();
    expires.setTime(expires.getTime() + 3600);
    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;
}

function onMouseOverLayer() {
  ilabel = labels[this.getId()];
  label.setHtml(ilabel);
  label.setPoint(this.getTooltipPoint());
  label.show();
}

function onMouseOutLayer() {
  label.hide();
}