﻿// Fonctions nécessaires à la recherche des centres de formation

var defaultAddress = 'QC, Canada';
var defaultZoom = 4;
var regionZoom = 8;

function createMarker(point, html) {

    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    var marker = new GMarker(point, baseIcon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;
}

function showAddress(address, html) {
    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                //alert(address + " not found 1");
                console.log(address + " not found");
            } else {
                var marker = createMarker(point, html);
                map.addOverlay(marker);
            }
        });
    }

function popupHTML(image, title, content) {
    var html = '<div class="CenterImage"><img src="' + image + '" width="52" height="52" border="0" alt="' + title + '" title="' + title + '" /></div>';
    html += '<div>' + content + '</div>';

    return html;
}

function updateMap() {
    if (map != null) {
        map.clearOverlays();

        //Read selectbox params
        var regions = document.getElementById("selRegion");
        var regionValue = regions.options[regions.selectedIndex].value;

        var typeFormation = document.getElementById("selTypeFormation");
        var typeFormationValue = typeFormation.options[typeFormation.selectedIndex].value;

        var chkFormation = document.getElementById("selTypeFormation");
        var chkFormationValue = "";
        for (i = 0; i < 100; i++) {
            var chkbox = document.getElementById('chk' + i);

            if (chkbox != null) {
                if (chkbox.checked) {
                    if (chkFormationValue.length == 0) {
                        chkFormationValue = chkFormationValue + chkbox.value;
                    }
                    else {
                        chkFormationValue = chkFormationValue + "," + chkbox.value;
                    }
                }
            }
            else {
                break;
            }
        }

        // Read the data from the webservice file
        var request = GXmlHttp.create();
        
        $mapfile = '/WS/wsCenters.asmx/GetCenters?regionId=' + regionValue + '&typeFormation=' + typeFormationValue + '&listeFormations=' + chkFormationValue;

        request.open("GET", $mapfile, true);

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = GXml.parse(request.responseText);

                // obtain the array of markers and loop through it
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                var html = null;
                var content = null;

                for (var i = 0; i < markers.length; i++) {
                    content = (markers[i].text) ? markers[i].text : (markers[i].textContent) ? markers[i].textContent : "";
                    html = popupHTML(markers[i].getAttribute("image"), markers[i].getAttribute("title"), content);

                    longitude = markers[i].getAttribute("longitude");
                    latitude = markers[i].getAttribute("latitude");

                    var point = new GLatLng(longitude, latitude);
                    var marker = createMarker(point, html)
                    map.addOverlay(marker);
                }

                if (regionValue != -1) {

                    var address = arrRegions[regions.options[regions.selectedIndex].value][1];

                    geocoder.getLatLng(
                        address,
                        function(point) {
                            if (!point) {
                                alert(address + " not found 2");
                            } else {

                                map.setCenter(point, regionZoom);
                            }
                        }
                    );
                }
            }
        }
        request.send(null);
    }
}

function listCenters() {
    if (map != null) {
        map.clearOverlays();

        geocoder.getLatLng(
            defaultAddress,
            function(point) {
                if (!point) {
                    alert(defaultAddress + " not found 3");
                } else {

                    map.setCenter(point, defaultZoom);
                }
            }
        );

        var typeFormation = document.getElementById("selTypeFormationListeComplete");
        var typeFormationValue = typeFormation.options[typeFormation.selectedIndex].value;

        var divListeComplete = document.getElementById("ListeComplete");

        var request = null;
        var currentRegion = null;

        // Read the data from the webservice file
        request = GXmlHttp.create();

        $mapfile = '/WS/wsCenters.asmx/GetAllCenters?typeFormation=' + typeFormationValue;

        request.open("GET", $mapfile, true);

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = GXml.parse(request.responseText);

                // obtain the array of markers and loop through it
                var regions = xmlDoc.documentElement.getElementsByTagName("region");

                divListeComplete.innerHTML = '';
                var html = null;
                var image = null;
                var title = null;
                var content = null;
                var region = null;
                var firstRegion = true;

                for (var r = 0; r < regions.length; r++) {
                    region = regions[r].getAttribute("id");

                    var markers = regions[r].getElementsByTagName("marker");

                    if (markers.length > 0) {

                        if (firstRegion) {
                            divListeComplete.innerHTML += '<h3>' + arrRegions[region][0] + '</h3>';
                            firstRegion = false;
                        }
                        else {
                            divListeComplete.innerHTML += '<h3 style="padding-top: 30px">' + arrRegions[region][0] + '</h3>';
                        }

                        for (var i = 0; i < markers.length; i++) {
                            image = markers[i].getAttribute("image");
                            title = markers[i].getAttribute("title");
                            content = (markers[i].text) ? markers[i].text : (markers[i].textContent) ? markers[i].textContent : "";

                            html = popupHTML(image, title, content);


                            longitude = markers[i].getAttribute("longitude");
                            latitude = markers[i].getAttribute("latitude");

                            var point = new GLatLng(longitude, latitude);
                            var marker = createMarker(point, html)
                            map.addOverlay(marker);

                            html = '<div class="ResultatCentre">';
                            html += '<img src="' + image + '" width="52" height="52" border="0" alt="' + title + '" title="' + title + '" />';
                            html += content;
                            html += '</div>';

                            divListeComplete.innerHTML += html;
                        }
                    }
                }
            }
        }
        request.send(null);
    }
    var sleep = function (naptime) {
        naptime = naptime;
        var sleeping = true;
        var now = new Date();
        var alarm;
        var startingMSeconds = now.getTime();
        
        while (sleeping) {
            alarm = new Date();
            alarmMSeconds = alarm.getTime();
            if (alarmMSeconds - startingMSeconds > naptime) { sleeping = false; }
        }
    }
}