var layers          = new Array();
var iconPath        = "http://www.hartanovteam.com/idx/map/icons/feature_markers/";
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);

layers["School"]    = new MarkerLayer("School",     iconPath + "marker_blue.png");
layers["Church"]    = new MarkerLayer("Church",     iconPath + "marker_orange.png");
layers["Park"]      = new MarkerLayer("Park",       iconPath + "marker_green.png");
layers["Airport"]   = new MarkerLayer("Airport",    iconPath + "marker_purple.png");
layers["Hospital"]  = new MarkerLayer("Hospital",   iconPath + "marker_red.png");
layers["Locale"]    = new MarkerLayer("Locale",     iconPath + "marker_aqua.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) {
            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("http://www.hartanovteam.com/idx/map/features/layer_markers.php", { "layer": markerLayer, "la1": sw.lat(), "la2": ne.lat(), "lo1": sw.lng(), "lo2": ne.lng() }, 
    
        function(data) {
            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(type); 
            layers[type].showMarkers();
        } else {
            // hide
            layers[type].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;
}