﻿var heatMap = null;
var heatMapInitted = false;

jQuery.fn.elementlocation = function() {
    var curleft = 0;
    var curtop = 0;

    var obj = this;

    do {
        curleft += obj.attr('offsetLeft');
        curtop += obj.attr('offsetTop');

        obj = obj.offsetParent();
    } while (obj.attr('tagName') != 'BODY');

    return ({ x: curleft, y: curtop });
};
function GetHeatMap() {
    var options = new VEMapOptions();
    options.LoadBaseTiles = false;

    heatMap = new VEMap("heatMap");
    heatMap.LoadMap(new VELatLong(136.0, 37.0), 1, VEMapStyle.Road, true, VEMapMode.Mode2D, true, 0, options);
    heatMap.SetCredentials("AoHiOE6KF3iNWiY2oanehot9wh3wZWKBlx4le9iVB-SRog8xF_aDHeVkXKV3c5nO");
    HeatMapAddOverlayTileLayer(1, false);
    heatMap.vemapcontrol.SetAnimationEnabled(false);
    heatMap.Resize();
    heatMap.HideScalebar();
    heatMap.HideDashboard();

    $("#heatMapImage").click(HeatMapImageShapeMouseClickHander);
    heatMap.AttachEvent("onendzoom", HeatMapEndZoomHandler);
}
function InitHeatMap() {
    if (!heatMapInitted) {
        GetHeatMap();
        $("#heatMapLoadLink").trigger("click");
        heatMapInitted = true;
    }
}
function HeatMapAddOverlayTileLayer(sectorId, deleteLayer) {
    if (deleteLayer) {
        heatMap.DeleteTileLayer("overlay");
    }

    var tileOverlaySourceSpec =
		new VETileSourceSpecification(
			"overlay", "/heatmaptiles/" + sectorId + "/%4", 1, null, 1, 5
		);

    heatMap.AddTileLayer(tileOverlaySourceSpec, true);
}
function HeatMapImageShapeMouseClickHander(e) {
    var location = $("#heatMapImage").elementlocation();
    var x = e.pageX - location.x;
    var y = e.pageY - location.y;

    var latLong = heatMap.PixelToLatLong(new VEPixel(x, y));

    HeatMapCountryClick(latLong.Latitude, latLong.Longitude)
}
function HeatMapEndZoomHandler(e) {
    var center = heatMap.GetCenter();
    var topLeft = heatMap.PixelToLatLong(new VEPixel(0, 0));
}
function HeatMapListAfterLoad() {
    //select first li item
    if($("#heatMapList li a.selected").length==0) $("#heatMapList li a:first").addClass("selected");


    //document.getElementById("heatMapImage").style.backgroundImage = "url(" + $("#heatMapInfo").attr("image") + ")";
    //$("#heatMapImage").attr("src", $("#heatMapInfo").attr("image"));
    // center and zoom first, otherwise we end up loading zoomed in tiles for the new layer that we don't need
    heatMap.SetCenterAndZoom(
        new VELatLong($("#heatMapInfo").attr("latitude"), $("#heatMapInfo").attr("longitude")),
        $("#heatMapInfo").attr("zoomLevel"));
    HeatMapAddOverlayTileLayer($("#heatMapInfo").attr("sectorid"), true);
    //FixIEHeatmapPng($("#heatMapInfo").attr("image"));
}

function HeatMapCountryClick(latitude, longitude) {
    $.ajax({
        url: "area/get-country/" + latitude + "/" + longitude,
        type: "GET",
        cache: false,
        dataType: "json",
        async: false,
        success: function(json) {
            if (json.name != "") {
                HeatMapOpenCountryInList(json.urlName, json.name, json.continentUrlName, json.continentName);
            }
        }
    });
}
function HeatMapOpenCountryInList(urlName, name, continentUrlName, continentName) {
    document.location.href = "/area/" + continentUrlName +
                "/" + urlName;
}

function FixIEHeatmapPng(iebg) {
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $("#heatMapImage")[0].style.backgroundImage = "none";
        $("#heatMapImage")[0].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
    }
} 
