﻿var map;
var gBounds;
var baseIcon;
var blueIcon;
var smallMapControl = new GSmallMapControl();
var geocoder = new GClientGeocoder();
var resultsPage = false;
var counterReadyMarkers = 0;

function initializeMap() {
    baseIcon = new GIcon();
    blueIcon = new GIcon(G_DEFAULT_ICON);
    map = new GMap2(document.getElementById("google_map"));
    map.setCenter(new GLatLng(55, 12));
    map.addControl(smallMapControl);
    map.addControl(new GScaleControl());

    geocoder.getBaseCountryCode("dk");

    map.addControl(new GMapTypeControl());
    baseIcon.image = "/images/HomeMarker.png";
    //baseIcon.shadow = "/images/HomeMarker.png";
    baseIcon.iconSize = new GSize(27, 23);
    //baseIcon.shadowSize = new GSize(0, 0);
    baseIcon.iconAnchor = new GPoint(10, 10);
    baseIcon.infoWindowAnchor = new GPoint(10, -4);
    blueIcon.image = "/images/BlueMarker.png";

    initializeGeo(true);

    $(window).unload(function() { GUnload(); });
}

function initializeGeo(setMarkerOnMap) {
    if (isReadyAllMarkers() && setMarkerOnMap) {
        setMarkers();
        bindHomeList();
        return true;
    }
    counterReadyMarkers = 0;
    for (var i in homeList) {
        var home = homeList[i];
        if ((home.lat == 0 || home.lng == 0) || (abs(home.lat) > 85) || (abs(home.lng) > 180)) {
            getGeoFromAddress(home, setMarkerOnMap);
            continue;
        }
        counterReadyMarkers++;
        if (setMarkerOnMap) {
            setMarker(home);
            if (isReadyAllMarkers())
                bindHomeList();
        }
    }
}

function setMarkers() {
    for (var i in homeList) {
        setMarker(homeList[i]);
    }
}

function trackReadMore() {
    try { pageTracker._trackPageview("/tracking/forside/Kort_Las_mere"); }
    catch(e){}
}

function setMarker(home) {
    var anchorHtml = "<div></div>";
    if (resultsPage)
        anchorHtml = "<div style='width: 80; heigth: 100; float:left;'><img src='" + home.imgSrc + "' class='IconImg'/></div><div style='width: 150; heigth: 100; float: left;'><div style='width: 150; heigth: 25;  padding-left: 10px; margin-bottom: 8px;'>" + home.id + " - " + home.name + "</div><div style='width: 150; heigth: 25;  padding-left: 10px; margin-bottom: 8px;'>" + home.city + "</div><div style='width: 150; heigth: 25;  padding-left: 10px; margin-bottom: 8px;'>" + price + ": " + home.price + " " + currency + "</div><div style='width: 150; heigth: 25;  padding-left: 10px; margin-bottom: 3px;'><a class='InfoBoxRef' style='color: #990000;' onclick='trackReadMore();' href='" + home.href + "'>" + readMoreButton + "</a><br/></div><div style='width: 150; heigth: 25; padding-left: 10px;'>" + memoItemText + "</div></div>";
    home.gPoint = new GLatLng(home.lat, home.lng);
    home.marker = createMarker(home.gPoint, anchorHtml, baseIcon, home.id, home.name);
    map.addOverlay(home.marker);
}

function isReadyAllMarkers() {
    return counterReadyMarkers == homeList.length;
}

function getGeoFromAddress(home, setMarkerOnMap) {
    var fullAddress = home.address + ', ' + home.zipcode + ' ' + home.city;
    geocoder.getLatLng(fullAddress, function(point) {
        counterReadyMarkers++;
        if (!point) {
            if (resultsPage) {
                $('div#ResultsList a[@attId=' + home.id + ']').css("display", "none");
            }
            else {
                $("div#googleMapMini").css("display", "none");
                $("#googleLat").val(0);
                $("#googleLng").val(0);
                return false;
            }
        }
        else {
            home.lat = point.lat();
            home.lng = point.lng();
            if (setMarkerOnMap)
                setMarker(home);
            if (!resultsPage) {
                isFirstSearchGeo = false;
                $("#googleLat").val(home.lat);
                $("#googleLng").val(home.lng);
                onLoadSearchDetail();
            }
        }
        if (setMarkerOnMap && isReadyAllMarkers()) {
            bindHomeList();
        }
        return true;
    });
}

function bindHomeList() {

    if (homeList.length > 1) {
        gBounds = getBounds(homeList);
        zoom = map.getBoundsZoomLevel(gBounds);
        map.setCenter(gBounds.getCenter());
        map.setZoom(zoom);
    }
    if (homeList.length == 1) {
        map.setCenter(homeList[0].gPoint);
        map.setZoom(12);
    }

    if (resultsPage) {
        var slctd_marker_id = getMyCookie("selectedHomeId");
        if (slctd_marker_id != null)
            showMarkerWinInfo(slctd_marker_id);
    }
    else {
        $("div#googleMapMini").css("visibility", "visible");
        mapCopyLoad();
    }
}

function getBounds(fList) {
    var initialized = false;
    var curr_max_lat = 0;
    var curr_min_lat = 100;
    var curr_max_lng = 0;
    var curr_min_lng = 100;

    for (i in fList) {
        if (fList[i].lat == 0 || fList[i].lng == 0)
            continue;
        if (fList[i].lat > 85 || fList[i].lng > 180)
        {
            fList[i].lat = 0;
            fList[i].lng = 0;
            continue;
        }
        if (fList[i].lat > curr_max_lat)
            curr_max_lat = fList[i].lat;
        if (fList[i].lng < curr_min_lng)
            curr_min_lng = fList[i].lng;
        if (fList[i].lat < curr_min_lat)
            curr_min_lat = fList[i].lat;
        if (fList[i].lng > curr_max_lng)
            curr_max_lng = fList[i].lng;
        initialized = true;
    }

    if (!initialized) {
        curr_max_lat = curr_min_lat = 56;
        curr_min_lng = curr_max_lng = 12;
    }

    return new GLatLngBounds(new GLatLng(curr_max_lat, curr_min_lng), new GLatLng(curr_min_lat, curr_max_lng));
}

function abs(a) {
    if (a < 0)
        return a * (-1);
    else
        return a;
}
