﻿// File JScript
var map = null;
var pointLayer = null;
var polygonLayer = null;
var myWidth = 0, myHeight = 0;
var zoomMax = 19;
var zoomMin = 7;
var mapPanStartPoint = null;
var mapPanIntermediate = null;
var mapPanRestrictionDistance = 100;
var restrizioni = true;
var parentSize = true;

//---------- Funzioni di base ------------------------------------------------------------

function GetMap(divName)
{
    if(parentSize){
        GetParentSize();
        
        document.getElementById(divName).style.width = myWidth + 'px';
        document.getElementById(divName).style.height = myHeight + 'px';
    }
    
    var center = new VELatLong(43.52266348752663, 11.156616210937502);
    
    map = new VEMap(divName);
    map.SetDashboardSize("small"); //normal, small, tiny
    map.LoadMap(center, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
    
    //Aggiunta eventi per restrizioni mappa
    map.AttachEvent('onendzoom', restrictionZoom);
    map.AttachEvent('onendpan', restrictionPan); 
    map.AttachEvent('onstartpan', onStartPan);
    mapPanStartPoint = map.GetCenter();

    //se non viene disabilitato limita i punti visualizzati, 
    //a discapito della precisione dei bordi e della qualità grafica
    map.EnableShapeDisplayThreshold(false);
}

function SetDivMenu(divName){
    var menuWidth = 500;
    var menuHeight = 20;
    var menuPadding = 5;
    var menuBGColor = '#A8A5EE';
    var menuBorder = '1px solid #000';
    var divMenu = document.getElementById(divName);
    
    if(divMenu!=null){
        divMenu.style.top = menuPadding;
        divMenu.style.left = myWidth - (menuWidth + menuPadding);
        divMenu.style.width = menuWidth;
        divMenu.style.height = menuHeight;
        divMenu.style.border = menuBorder;
        divMenu.style.backgroundColor = menuBGColor;
        //divMenu.innerHTML = 'prova';
        divMenu.innerText = 'prova';
        
    }
}

function SetView(shplayer){
    map.SetMapView(shplayer.GetBoundingRectangle());
}

function SetViewRectangle(MaxLat, MaxLon, MinLat, MinLon){
    var TopLeftLatLong = new VELatLong(MinLat, MinLon);
    var BottomRightLatLong = new VELatLong(MaxLat, MaxLon);
    var rect = new VELatLongRectangle(TopLeftLatLong, BottomRightLatLong);
    map.SetMapView(rect);
}

function AddPoint(lat,lon,title,description,icon){
    try {
        point = new VELatLong(lat,lon);
        shape = new VEShape(VEShapeType.Pushpin, point);
        shape.SetTitle(title);
        if(icon!="")shape.SetCustomIcon(icon);
        shape.SetDescription(description);
        pointLayer.AddShape(shape);
    } 
    catch (err)
    {
       alert(err);
    }
}

//---------- Gestione punti utilizzando georss ------------------------------------------------------------

//(non funzionante in quanto non gestisce le immagini
//function AddBankPointLayer(code,selectedLayer){
//    var now = new Date()
//    var url = "RSSBankPointHandler.ashx?id=" + code + "&d="+now;
//    var layerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, url, selectedLayer);
//    map.ImportShapeLayerData(layerSpec,null,true);
//}

//---------- Restrizioni spostamento e zoom ------------------------------------------------------------

function restrictionZoom(e){
    if(restrizioni){
        var currentZoom = e.zoomLevel;

        if(currentZoom > zoomMax) {
            map.SetZoomLevel(zoomMax);
        }
        else if(currentZoom < zoomMin) {
            map.SetZoomLevel(zoomMin);
        }
    }
}

function restrictionPan(e){
    if(restrizioni){
        var distance = GeoCodeCalc.CalcDistance(
            mapPanStartPoint.Latitude,
            mapPanStartPoint.Longitude,
            map.GetCenter().Latitude,
            map.GetCenter().Longitude,
            GeoCodeCalc.EarthRadiusInKilometers
            );

        if (distance > mapPanRestrictionDistance){
            map.SetCenter(mapPanIntermediate);
        }
    }
}

function onStartPan(e){
    mapPanIntermediate = map.GetCenter();
}

var GeoCodeCalc = {};
GeoCodeCalc.EarthRadiusInMiles = 3956.0;
GeoCodeCalc.EarthRadiusInKilometers = 6367.0;
GeoCodeCalc.ToRadian = function(v) { return v * (Math.PI / 180);};
GeoCodeCalc.DiffRadian = function(v1, v2) {
return GeoCodeCalc.ToRadian(v2) - GeoCodeCalc.ToRadian(v1);
};
GeoCodeCalc.CalcDistance = function(lat1, lng1, lat2, lng2, radius) {
return radius * 2 * Math.asin( Math.min(1, Math.sqrt( ( Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) * Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
};

//---------- Recupera le dimensioni della finestra ------------------------------------------------------------

function GetParentSize() {
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
}

//---------- Varie ------------------------------------------------------------

function RGB2Html(red, green, blue)
{
    return RGBToHex(red)+RGBToHex(green)+RGBToHex(blue);
}

function RGBToHex(rgb) {
    var char = "0123456789ABCDEF";
    return String(char.charAt(Math.floor(rgb / 16))) + String(char.charAt(rgb - (Math.floor(rgb / 16) * 16)));
} 