﻿// UI component id
var _grlTxtAddressID = null;
var _grlTxtAddressWaterMarkText = null;
var _grlBtnAfficherID = null;
var _grlBtnInverserID = null;
var _grlBtnEnvoyerCourrielID = null;
var _grlBtnImprimerID = null;
var _grlBtnAfficherLocalisateurID = null;
var _grlLblDeID = null;
var _grlLblDeInvertID = null;
var _grlLblAID = null;
var _grlLblAInvertID = null;

// variable
var _grlCurrentControl = null;
var _grlLanguageID = "3084";
var _grlStartLatLong = null;
var _grlEndLatLong = null;
var _grlLocationName = null;
var _grlIsInverted = null;
var _grlIsAdminView = false;
var _grlMap = null;
var _grlModalPopup = null;
var _grlLocalisateur = null;
var _grlGR = null;
var _grlProcessMessageTimeoutID = null;
var _grlAdminPushPin = null;
var _grlYULAddress = null;
var _grlYULLat = null;
var _grlYULLong = null;
var _grlLocalisateurZoomLevel = null;
var _grlGuideRoutierZoomLevel = null;

/* -------- Common -------- */
function _grl_onUnknownError() {
    _grl_cacherMessage();
    _grl_afficherMessageErreur(util_getMessage(_commonUnknownErrorMsg_FR, _commonUnknownErrorMsg_EN, _grlLanguageID));
}

function _grl_onShow() {
    _grlMap.load();
    
    if (_grlIsAdminView) {
        var vemap = _grlMap.get_instance();
        vemap.AttachEvent("onmousemove", Function.createDelegate(this, _grl_onMouseMove));
        vemap.AttachEvent("onclick", Function.createDelegate(this, _grl_onMouseClick));
        
        _grlModalPopup.displayMessagePanel();
        
        var lblInstruction = $get("lblInstruction");
        lblInstruction.style.display = "block";
    }
    if (_grlCurrentControl == "localisateur") {
        _grl_afficherLocalisateur();
    }
    else {
        _grl_afficherGuideRoutier();
    }
}

function _grl_onClose() {
    try {
        if (_grlMap.isLoaded()) {
            if (_grlCurrentControl == "localisateur") {
                _grlLocalisateur.dispose();
                
                var pMap = $get("pMap");
                pMap.style.height = "100%";
                
                var imgLegende = $get("imgLegende");
                imgLegende.style.display = "none";
            }
            else {
                _grlGR.dispose();
            }
            if (_grlIsAdminView) {
                var vemap = _grlMap.get_instance();
                vemap.DetachEvent("onmousemove", Function.createDelegate(this, _grl_onMouseMove));
                vemap.DetachEvent("onclick", Function.createDelegate(this, _grl_onMouseClick));
            }
            _grlMap.dispose();
        }
        _grlCurrentControl = "localisateur";
        }
    catch(ex) {
        // do nothing.
    }
}

function _grl_onPreResize() {
    var pMapContainer = $get(_grlMap.get_elementID());
    if (pMapContainer != null) {
        pMapContainer.style.width = "1px";
        pMapContainer.style.height = "1px";
    }
}

function _grl_onResize(width, height) {
    if (width != null && height != null) {
        var w = new Number(width.replace("px", ""));
        var h = new Number(height.replace("px", ""));
        
        if (w > 0 && h > 0) {
            var pMapContainer = $get(_grlMap.get_elementID());
            if (pMapContainer != null) {
                pMapContainer.style.width = w + "px";
                pMapContainer.style.height = h + "px";
            }
           
            if (_grlCurrentControl == "localisateur") {
                _grlLocalisateur.resize(w, h);
            }
            else {
                _grlGR.resize(w, h);
                _grl_resizePanneauTrajet();
            }
        }
    }
}

function _grl_onMouseMove(e) {
    var vemap = _grlMap.get_instance();
    // - 25 to y coordonate to fix a probleme with the pushpin image.
    var pixel = new VEPixel(e.mapX, e.mapY - 25);
    var latLong = vemap.PixelToLatLong(pixel);
    
    _grlModalPopup.displayMessagePanel();
    
    var lblCurrentLatLong = $get("lblCurrentLatLong");
    lblCurrentLatLong.innerHTML = latLong.Latitude + " / " + latLong.Longitude
    lblCurrentLatLong.style.display = "block";
}

function _grl_onMouseClick(e) {
    if (e.rightMouseButton) {
        var vemap = _grlMap.get_instance();
        // - 25 to y coordonate to fix a probleme with the pushpin image.
        var pixel = new VEPixel(e.mapX, e.mapY - 25);
        var latLong = vemap.PixelToLatLong(pixel);
        
        _grlModalPopup.displayMessagePanel();
        
        var lblCopyLatLong = $get("lblCopyLatLong");
        lblCopyLatLong.innerHTML = "Lat: " + latLong.Latitude + "<br />Long: " + latLong.Longitude
        lblCopyLatLong.style.display = "block";
        
        // clear all the current pushpins that are not on a shape.
        if (_grlAdminPushPin != null) {
            vemap.DeleteShape(_grlAdminPushPin);
        }
        // add a pushpin at the location clicked.
        _grlAdminPushPin = new VEShape(VEShapeType.Pushpin, latLong);
        _grlAdminPushPin.SetCustomIcon("<img src='" + util_getUrl('/UserControls/VEControls/Pushpin/pushpinPic.png') + "' alt='flag' class='png'/>");
        vemap.AddShape(_grlAdminPushPin);
    }
}

function _grl_onModalPopupInit(instance) {
    _grlModalPopup = instance;
}

function _grl_afficherMessageTraitement() {
    if (_grlProcessMessageTimeoutID != null) {
        clearTimeout(_grlProcessMessageTimeoutID);
    }
    _grlProcessMessageTimeoutID = window.setTimeout(Function.createDelegate(this, __grl_afficherMessageTraitement), 500)
}

function __grl_afficherMessageTraitement() {
    _grlProcessMessageTimeoutID = null;
    _grlModalPopup.displayMessagePanel();
    
    var lblTraitement = $get("lblTraitement");
    lblTraitement.innerHTML = util_getMessage(_commonLoadingMsg_FR, _commonLoadingMsg_EN, _grlLanguageID);
    lblTraitement.style.display = "block";
    
    _grlModalPopup.disable(); 
}

function _grl_cacherMessageTraitement() {
    if (_grlProcessMessageTimeoutID != null) {
        clearTimeout(_grlProcessMessageTimeoutID);
        _grlProcessMessageTimeoutID = null;
    }
    if (!_grlIsAdminView) {
        _grlModalPopup.hideMessagePanel();
    }
    var lblTraitement = $get("lblTraitement");
    lblTraitement.style.display = "none";
    
    _grlModalPopup.enable();
}

function _grl_afficherMessageErreur(msg) {
    _grlModalPopup.displayMessagePanel();
    
    var lblErreur = $get("lblErreur");
    lblErreur.innerHTML = msg;
    lblErreur.style.display = "block";
}

function _grl_cacherMessageErreur() {
    if (!_grlIsAdminView) {
        _grlModalPopup.hideMessagePanel();
    }
    var lblErreur = $get("lblErreur");
    lblErreur.style.display = "none"; 
}

function _grl_afficherMessageStatut(msg) {
    _grlModalPopup.displayMessagePanel();
    
    var lblMessage = $get("lblMessage");
    lblMessage.innerHTML = msg;
    lblMessage.style.display = "block";
}

function _grl_cacherMessageStatut() {
    if (!_grlIsAdminView) {
        _grlModalPopup.hideMessagePanel();
    }
    var lblMessage = $get("lblMessage");
    lblMessage.style.display = "none"; 
}

function _grl_cacherMessage() {
    _grl_cacherMessageTraitement();
    _grl_cacherMessageErreur();
    _grl_cacherMessageStatut();
}

/* -------- Localisateur -------- */
function grl_afficherLocalisateurService(levelID, zoneID, categoryID, serviceName) {
    // display the modal popup.
    var modal = _grlModalPopup.getModalPopup();
    modal.show();
    _grl_afficherMessageTraitement();
    // open the menu hierarchy to the specified service and display the service.
    _grlLocalisateur.localizeService(levelID, zoneID, categoryID, serviceName, Function.createDelegate(this, _grl_onDisplayLevelCallback));
}

function _grl_afficherLocalisateur() {
    var pLocalisateurTitre = $get("pLocalisateurTitre");
    var pLocalisateurMenu = $get("pLocalisateurMenu");
    var pGuideRoutierTitre = $get("pGuideRoutierTitre");
    var pGuideRoutierMenu = $get("pGuideRoutierMenu");
    var pMap = $get("pMap");
    var pLegende = $get("pLegende");
    
    var pContainerMenu = $get("pContainerMenu");
    if (pContainerMenu != null) {
        pContainerMenu.style.width = "200px";
    }
    var pLocalisateurMenu = $get("pLocalisateurMenu");
    if (pLocalisateurMenu != null) {
        pLocalisateurMenu.style.width = "200px";
    }
    
    _grlCurrentControl = "localisateur";
    _grlModalPopup.resize();
    
    pLocalisateurTitre.style.display = "block";
    pLocalisateurMenu.style.display = "block";
    pGuideRoutierTitre.style.display = "none";
    pGuideRoutierMenu.style.display = "none";
    pLegende.style.display = "block";

    //hauteur de l'image légende = 77
    var pMapHeight = pMap.offsetHeight - 77;
    if (pMapHeight >= 0) {
        pMap.style.height = pMapHeight + "px";
    }
    
    _grl_cacherMessage();
    _grl_afficherFromTo();
    
    if (_grlMap.isLoaded()) {
        _grlMap.clear();
        
        _grlGR.dispose();
        _grlLocalisateur.initialize();
        
        var vemap = _grlMap.get_instance();
        vemap.SetCenterAndZoom(new VELatLong(_grlYULLat, _grlYULLong), _grlLocalisateurZoomLevel);
    }
}

function grl_cliquerLevel(levelID) {
    _grl_afficherMessageTraitement();
    _grlLocalisateur.displayLevel(levelID, Function.createDelegate(this, _grl_onDisplayLevelCallback));
}

function grl_cliquerZone(levelID, zoneID) {
    _grl_afficherMessageTraitement();
    _grlLocalisateur.displayZone(levelID, zoneID, Function.createDelegate(this, _grl_onDisplayZoneCallback));
}

function grl_cliquerService(levelID, zoneID, categoryID, serviceName) {
    _grl_afficherMessageTraitement();
    _grlLocalisateur.displayService(levelID, zoneID, categoryID, serviceName, Function.createDelegate(this, _grl_onDisplayServiceCallback));
}

function _grl_onDisplayLevelCallback(url, hideLegende) {
    _grl_cacherMessage();
    
    var imgLegende = $get("imgLegende");
    if (url != null && url != "") {
        imgLegende.src = url;
        imgLegende.style.display = "block";
    }
    else {
        if (hideLegende) {
            imgLegende.style.display = "none";
        }
    }
}

function _grl_onDisplayZoneCallback() {
    _grl_cacherMessage();
}

function _grl_onDisplayServiceCallback() {
    _grl_cacherMessage();
}

/* -------- Guide Routier -------- */
function grl_afficherGuideRoutierItineraire(start) {
    // display the modal popup.
    var modal = _grlModalPopup.getModalPopup();
    modal.show();
    // show the guide routier.
    _grl_afficherGuideRoutier();
    // affecter au textbox la valeur de l'address de départ.
    var txtAddress = $get(_grlTxtAddressID);
    txtAddress.value = start;
    // trace the specified itinerary.
    grl_tracerRoute(start, _grlYULAddress);
}

function _grl_afficherGuideRoutier() {
    var pLocalisateurTitre = $get("pLocalisateurTitre");
    var pLocalisateurMenu = $get("pLocalisateurMenu");
    var pGuideRoutierTitre = $get("pGuideRoutierTitre");
    var pGuideRoutierMenu = $get("pGuideRoutierMenu");
    var pMap = $get("pMap");
    var imgLegende = $get("imgLegende");
    var pDirection = $get("pDirection");
    var btnGuideRoutierBtnInverser = $get(_grlBtnInverserID);
    
    var pContainerMenu = $get("pContainerMenu");
    if (pContainerMenu != null) {
        pContainerMenu.style.width = "260px";
    }
    var pGuideRoutierMenu = $get("pGuideRoutierMenu");
    if (pGuideRoutierMenu != null) {
        pGuideRoutierMenu.style.width = "260px";
    }     
    
    _grlCurrentControl = "guideRoutier";
    _grlModalPopup.resize();   
    
    pLocalisateurTitre.style.display = "none";
    pLocalisateurMenu.style.display = "none";
    pGuideRoutierTitre.style.display = "block";
    pGuideRoutierMenu.style.display = "block";
    
    imgLegende.style.display = "none";

    btnGuideRoutierBtnInverser.style.display = "none";
    pDirection.innerHTML = "";
    
    var twbeAddress = $find("tbweStart");
    twbeAddress.set_Text("");
    
    pMap.style.height = "100%";
    
    _grl_disableOutils();
    _grl_cacherPanneauCourriel();
    _grl_cacherMessage();
    
    if (_grlMap.isLoaded()){
        _grlMap.clear();
        _grlLocalisateur.dispose();
        _grlGR.initialize();
        
        var vemap = _grlMap.get_instance();
        vemap.SetCenterAndZoom(new VELatLong(_grlYULLat, _grlYULLong), _grlGuideRoutierZoomLevel);
    }
}

function grl_tracerRoute(start, end) {
    var isOk = true;
    
    _grl_cacherMessage();
    
    if (start == null || start == "" || start == _grlTxtAddressWaterMarkText) {
        isOk = false;
    }
    else if (end == null || end == "") {
        isOk = false;
    }
    if (isOk) {
        _grl_afficherMessageTraitement();
        _grlGR.traceRoute(start, end, false, Function.createDelegate(this, _grl_onRouteTraced));
    }
    else {
        _grl_afficherMessageErreur(util_getMessage(_grlEmptyAddressMsg_FR, _grlEmptyAddressMsg_EN, _grlLanguageID));
    }
    
    _grlIsInverted = false;
}

function grl_tracerInverse() {
    _grl_cacherMessage();
    _grl_afficherMessageTraitement();
    _grlIsInverted = !_grlIsInverted;
    _grlGR.traceRoute(_grlEndLatLong, _grlStartLatLong, _grlIsInverted, Function.createDelegate(this, _grl_onRouteTraced));
    
    if (_grlIsInverted) {
        _grl_afficherToFrom();
    }
    else {
        _grl_afficherFromTo();
    }
}

function _grl_onRouteTraced(html, startLatLong, endLatLong, error) {
    var pDirection = $get("pDirection");
    var btnInverser = $get(_grlBtnInverserID);
    
    _grl_cacherMessage();
    
    _grlStartLatLong = startLatLong;
    _grlEndLatLong = endLatLong;

    if (error != null && error != "") {
//        pDirection.innerHTML = "";
        btnInverser.style.display = "none";
    
        _grl_afficherMessageErreur(error);
//        _grl_disableOutils();
        _grl_cacherPanneauCourriel();
    }
    else {
        pDirection.innerHTML = html;
        btnInverser.style.display = "block";
        
        _grl_enableOutils();
    }
}

function grl_envoyerCourriel(emailAddress) {
    var isOk = true;
    
    _grl_cacherMessage();
    
    if (emailAddress == null || emailAddress == "") {
        _grl_afficherMessageErreur(util_getMessage(_grlEmptyEmailMsg_FR, _grlEmptyEmailMsg_EN, _grlLanguageID));
        isOk = false;
    }
    if (isOk) {
        if (!util_validateEmail(emailAddress)) {
            _grl_afficherMessageErreur(util_getMessage(_grlInvalidEmailMsg_FR, _grlInvalidEmailMsg_EN, _grlLanguageID));
            isOk = false;  
        }
    }
    if (isOk) {
        _grl_afficherMessageTraitement();
        
        var url = util_encodeUrl(util_getUrl(_grl_buildPrintUrl()));
        _grlGR.sendDirectionMail(url, emailAddress, Function.createDelegate(this, _grl_envoyerCourrielCallback));
    }
}

function _grl_envoyerCourrielCallback(success) {
    _grl_cacherMessage();

    if (success) {
        _grl_afficherMessageStatut(util_getMessage(_grlEmailSentMsg_FR, _grlEmailSentMsg_EN, _grlLanguageID));
    }
    else {
        _grl_afficherMessageErreur(util_getMessage(_grlCannotSendEmailMsg_FR, _grlCannotSendEmailMsg_EN, _grlLanguageID));
    }
}

function grl_imprimer() {
    _grl_cacherMessage();
    
    var title = util_getMessage("Imprimer", "Print", _grlLanguageID);
    var url = util_encodeUrl(util_getUrl(_grl_buildPrintUrl()));
    
    window.open(url, title, "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=800, height=800");
}

function _grl_buildPrintUrl() {
    var isInvertedStr = "";
    var startName = "";
    var endName = "";
    
    if (_grlIsInverted) {
        isInvertedStr = "true";
        startName = "Aéroport Montréal-Trudeau";
        
        var txtAddress = $get(_grlTxtAddressID);
        endName = txtAddress.value;
    }
    else {
        isInvertedStr = "false";
        endName = "Aéroport Montréal-Trudeau";
        
        var txtAddress = $get(_grlTxtAddressID);
        startName = txtAddress.value;
    }
    
    var ret = "/UserControls/VEControls/Print.aspx?slat=" + _grlStartLatLong.Latitude + "&slon=" + _grlStartLatLong.Longitude + "&elat=" + _grlEndLatLong.Latitude + "&elon=" + _grlEndLatLong.Longitude + "&inverted=" + isInvertedStr + "&lan=" + _grlLanguageID + "&sval=" + startName + "&eval=" + endName;
    return ret;
}
    
function _grl_afficherPanneauCourriel() {
    var pEnvoiCourriel = $get("pEnvoiCourriel");
    pEnvoiCourriel.style.display = "block"; 
    
    var txtCourriel = $find("tbweCourriel");
    txtCourriel.set_Text("");
    
    _grl_resizePanneauTrajet();
}

function _grl_cacherPanneauCourriel() {
    var pEnvoiCourriel = $get("pEnvoiCourriel");
    pEnvoiCourriel.style.display = "none";
    
    var txtCourriel = $find("tbweCourriel");
    txtCourriel.set_Text("");
    
    _grl_resizePanneauTrajet();
}

// fonction de calcul : hauteur de pTrajet (où s'affichent les directions)
function _grl_resizePanneauTrajet() {
    var pGRMenu = $get("pGuideRoutierMenu");
    var fieldsetGRMenu = $get("fieldsetGRMenu");
    var GRCriteres = $get("GRCriteres");
    var pTrajet = $get("pTrajet");
  
    pTrajet.style.height = "1px";
  
    var h = pGRMenu.offsetHeight - util_getOffsetTop(pGRMenu) - util_getOffsetBottom(pGRMenu) - util_getOffsetTop(fieldsetGRMenu) - util_getOffsetBottom(fieldsetGRMenu) - GRCriteres.offsetHeight;
    h = h - util_getOffsetTop(pTrajet) - util_getOffsetBottom(pTrajet);
  
    if (h >= 0) {
        pTrajet.style.height = h + "px";
    }
}

function _grl_disableOutils() {
    var btnImprimer = $get(_grlBtnImprimerID);
    var btnEnvoyerCourriel = $get(_grlBtnEnvoyerCourrielID);
    
    btnImprimer.href = "javascript:;";
    btnImprimer.className = "disabled";
    
    btnEnvoyerCourriel.href = "javascript:;";
    btnEnvoyerCourriel.className = "disabled";
}

function _grl_enableOutils() {
    var btnImprimer = $get(_grlBtnImprimerID);
    var btnEnvoyerCourriel = $get(_grlBtnEnvoyerCourrielID);
    
    btnImprimer.href = "javascript:grl_imprimer();";
    btnImprimer.className = "";
    
    btnEnvoyerCourriel.href = "javascript:_grl_afficherPanneauCourriel();";
    btnEnvoyerCourriel.className = "";
}

function _grl_afficherFromTo() {
    var lblDe = $get(_grlLblDeID);
    var lblDeInvert = $get(_grlLblDeInvertID);
    var lblA = $get(_grlLblAID);
    var lblAInvert = $get(_grlLblAInvertID);
    
    lblDe.style.display = "block";
    lblDeInvert.style.display = "none";
    lblA.style.display = "block";
    lblAInvert.style.display = "none";
}

function _grl_afficherToFrom() {
    var lblDe = $get(_grlLblDeID);
    var lblDeInvert = $get(_grlLblDeInvertID);
    var lblA = $get(_grlLblAID);
    var lblAInvert = $get(_grlLblAInvertID);
    
    lblDe.style.display = "none";
    lblDeInvert.style.display = "block";
    lblA.style.display = "none";
    lblAInvert.style.display = "block";
}

