﻿var currentMap;
var currentForm;
var currentFormPrefix;

window.onload = function() {

    map1 = new VMMap(document.getElementById("mapIntroduction"));
    map1.drawMap(new VMLonLat(-2.520266, 53.946326), 5);
    map1.showMapTools();

    var visitedConsultation = false;

    $('tabConsultation').addEvent('click', function (event) {
        if (!visitedConsultation) {
            map2 = new VMMap(document.getElementById("mapConsultation"));
            map2.drawMap(new VMLonLat(-2.520266, 53.946326), 5);
            map2.showMapTools();
            visitedConsultation = true;
        }
    });

    map2 = new VMMap(document.getElementById("mapConsultation"));
    map2.drawMap(new VMLonLat(-2.520266, 53.946326), 5);
    
};

function geocoder_search() 
{
    geosearch = new VMGeoSearch();
    myaddress = new VMAddress();

    //myaddress.address = document.getElementById(currentFormPrefix + '-locator-address').value;
    //myaddress.city = document.getElementById(currentFormPrefix + '-locator-town').value;
    myaddress.zipCode = document.getElementById(currentFormPrefix + '-locator-postcode').value;
    myaddress.countryISOCode = document.getElementById(currentFormPrefix + '-locator-country').value;

    geosearch.addEventHandler("onCallBack", display_ambiguity);
    geosearch.search(myaddress);
}

function display_ambiguity() 
{
        if (geosearch.results.length < 1) 
        {
            alert("No results found. Please try another search.");
        } else {
            poi_definition(0);
        }
}

function poi_definition(idx) {
    poiDefinition = new VMPOIDefinition();
    poiDefinition.addEventHandler("onCallBack", poi_search);
    poiDefinition.getDefinition("149825");
}

function poi_search() {
    myPOIsearch = new VMPOISearch(poiDefinition);
    // only search for dermo analyser stores
    poiDefinition.setCriteria(3, 1, true);
    myPOIsearch.addEventHandler("onCallBack", poi_result);
    myPOIsearch.search(geosearch.results[0].coords);
}

function poi_result() {

    if (myPOIsearch.result.VMPOIs.length > 0) {
        myPOIlist = myPOIsearch.result;

        document.getElementById(currentMap).innerHTML = "";
        map = new VMMap(document.getElementById(currentMap));

        var poiBubbleHtml;

        for (i = 0; i < myPOIsearch.result.VMPOIs.length; i++) {
            var myPOIlayer;
            myicon = new VMIcon("/local/en-gb/img/mapicon1.png", -18, -20);

            //DISTANCE DISPLAY
            var distance = parseFloat("0.0");
            distance = myPOIsearch.result.VMPOIs[i].distance;
            if (distance < 1000) {
                distance = Math.round(distance * 0.914);
                distance += "yd";
            }
            else {
                distance = Math.round(distance / 160.9) / 10 + "mi";
            }
            //DISTANCE DISPLAY

            poiBubbleHtml = "<div style=\"width:200px\">";
            poiBubbleHtml += "<span class=\"titre2\">" + (i + 1) + "&nbsp;&nbsp;" + myPOIsearch.result.VMPOIs[i].name + "</span>";
            poiBubbleHtml += "<p style='text-align:left; width:70px;'>" + myPOIsearch.result.VMPOIs[i].address + "<br />";
            poiBubbleHtml += myPOIsearch.result.VMPOIs[i].zipCode + "&nbsp;-&nbsp;";
            poiBubbleHtml += myPOIsearch.result.VMPOIs[i].city + "</p>";
            poiBubbleHtml += "<div><a href=\"#\" class=\"bt\" onclick=\"showPoi('" + i + "');\">Zoom</a></div>";
            poiBubbleHtml += "</div>";

            myPOIlayer = new VMIconLayer(myPOIsearch.result.VMPOIs[i].coords, myicon, poiBubbleHtml);
            //myPOIlayer.setExpandLayer(poiBubbleHtml);
            map.addLayer(myPOIlayer);

            var myPOIlayer_number;
            myPOIlayer_number = new VMLayer(myPOIsearch.result.VMPOIs[i].coords, i, 3, -4);
            myPOIlayer_number.setHTML("<div style='color: White; padding: 5px 5px 5px 5px;'>" + (i + 1) + "</div>");
            map.addLayer(myPOIlayer_number);
        }

        map.drawMapFromLayers();
        map.activateSatelliteHybrid(true);
        map.activateShowPois(true);
        map.showMapTools();
    }
    else {
        alert("No results found. Please try another search.");
    }
}

function showPoi(poiNum) {
    document.getElementById(currentMap).innerHTML = "";

    map = new VMMap(document.getElementById(currentMap));
    map.drawMap(myPOIsearch.result.VMPOIs[poiNum].coords, 16);

    itiPoint = myPOIsearch.result.VMPOIs[poiNum].coords;

    myiconlayer = new VMIconLayer(myPOIsearch.result.VMPOIs[poiNum].coords, myicon);
    map.addLayer(myiconlayer);
    map.activateSatelliteHybrid(true);
    map.activateShowPois(true);
    map.showMapTools();
}