﻿// JScript File
function createMarker(point,html) 
{
    var marker = new GMarker(point, CreateIcon());
    AddImageToggleEvent(marker, '');
    
    //add the click event if content is provided for the info window
    if (html != "") {
        GEvent.addListener(marker, 'mouseover', function() { marker.openInfoWindowHtml(html); });       
    }
    return marker; 
}

function createMarker(point, html, mlsID, link, listingType) {
    var marker = new GMarker(point, CreateIcon(listingType));
    AddImageToggleEvent(marker, mlsID, listingType);

    //add the click event if content is provided for the info window
    if (html != "") {
        var div = "<div style='text-align:right; padding:10px 10px 0px 0px'>" + html + "</div>";
        GEvent.addListener(marker, 'mouseover', function() { marker.openInfoWindowHtml(div); });
    }

    if (link != null && link != "") {
        GEvent.addListener(marker, 'click', function() { window.location = link; return false; });
        GEvent.addListener(marker, 'dblclick', function() { window.location = link; return false; });
    }
    
    return marker;
}

function createPoiMarker(point, html, poiID, count, type, icon)
{
    var marker = new GMarker(point, CreateLabelIcon(count, type, icon));

    if (_markerCollection != null)
        _markerCollection.push(marker);
    
    //add the click event if content is provided for the info window
    if (html != "") {
        var div = "<div class='smalltext1'>" + html + "</div>";

        if (_markerContent != null)
            _markerContent.push(div);

        GEvent.addListener(marker, 'mouseover', function() { marker.openInfoWindowHtml(div); });
    }
    return marker;
}

function SetZoomRange(mapType, min, max)
{    
    // Overwrite the getMinimumResolution() and getMaximumResolution() methods
    for (var i=0; i<mapType.length; i++) 
    {
        mapType[i].getMinimumResolution = function() {{return min;}}
        mapType[i].getMaximumResolution = function() {{return max;}}
    }
}

function AddImageToggleEvent(marker, mlsID, listingType) {
    var mouseoverImage;
    var mouseoutImage;
    
    //TODO: add athlete home icon
    if (listingType == "showcase") {
        mouseoverImage = "images/Icons/mapIcons/icon39.png";
        mouseoutImage = "images/Icons/mapIcons/icon47.png";
    }
    else if (listingType == "athlete") {
        mouseoverImage = "images/Icons/mapIcons/icon50.png";
        mouseoutImage = "images/Icons/mapIcons/icon58.png";
    }
    else {
        mouseoverImage = "images/Icons/mapIcons/icon4.png";
        mouseoutImage = "images/Icons/mapIcons/icon12.png";
    }

    GEvent.addListener(marker, "mouseover", function() {
        marker.setImage(mouseoverImage);
    });
    GEvent.addListener(marker, "mouseout", function() {
        marker.setImage(mouseoutImage);
    });

    GEvent.addListener(marker, "mouseover", function() { ToggleIcon("block", mlsID); });
    GEvent.addListener(marker, "mouseout", function() { ToggleIcon("none", mlsID); });   
}

function ToggleIcon(state, mlsID) {
    var listing = document.getElementById("marker_" + mlsID);

    if (listing != null)
        listing.style.display = state;
}

function CreateIcon(listingType) {
    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(32, 32);
    baseIcon.shadowSize = new GSize(56, 32);
    baseIcon.iconAnchor = new GPoint(16, 32);
    baseIcon.infoWindowAnchor = new GPoint(16, 0);
    //http://econym.org.uk/gmap/geicons.htm icon reference
    var image;
    
    //TODO: add athlete home icon
    if (listingType == "showcase") {
        image = "images/Icons/mapIcons/icon47.png";
    }
    else if (listingType == "athlete") {
        image = "images/Icons/mapIcons/icon58.png";
    }
    else {
        image = "images/Icons/mapIcons/icon12.png";
    }

    //var icon = new GIcon(baseIcon, image, null, "images/Icons/mapIcons/icon12s.png");
    var icon = new GIcon(baseIcon, image);
    return icon;
}

function CreateLabelIcon(count, type, icon) {
    if (type == "Airport") 
    {
        var baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(32, 32);
        baseIcon.shadowSize = new GSize(56, 32);
        baseIcon.iconAnchor = new GPoint(16, 32);
        baseIcon.infoWindowAnchor = new GPoint(16, 0);        
        var icon = new GIcon(baseIcon, "images/Icons/mapIcons/airport.png", null, "images/Icons/mapIcons/airports.png");
        return icon;
    }
    else 
    {
        var iconOptions = {};
        iconOptions.primaryColor = "#001E8F";
        iconOptions.strokeColor = "#000000";
        iconOptions.label = count;
        iconOptions.labelColor = "#ffffff";
        iconOptions.addStar = false;
        iconOptions.starPrimaryColor = "#FFFF00";
        iconOptions.starStrokeColor = "#0000FF";
        var icon = MapIconMaker.createLabeledMarkerIcon(iconOptions);
        return icon;
    }
}

function OpenPoi(lat, lon, count) {
    if (map != null) {
        map.panTo(new GLatLng(lat, lon));        
        //TODO:auto open the info window
    }
   
    if (_markerCollection != null) {
        try {
            var marker = _markerCollection[count - 1];
            
            if (_markerContent != null) {
                var content = _markerContent[count - 1];                
                marker.openInfoWindow(content);
            }
        }
        catch (err)
        { }
    }
}
