﻿
// 		FONCTION		 EVENEMENT
// 1.	googleMapApi --> onGoogleMapApiLoaded
//							 |
// 2.					 createMap --> onChange
//										  |
// 3.								   getData --> onDataLoaded
//												       |
// 4.											   addMarker --> onMarkerClick
//																	  |
// 5.															 openWindow  --> openWindow (à chaque changement de tab)




// Initialisation au chargement de la page
$(function() {
    //if (document.getElementById("carte")) {
    if ($("#carte").length > 0) 
    {
        googleMapApi.load(cleGoogleMap, "fr", onGoogleMapApiLoaded);
        //googleMapApi.load(cleGoogleMap, "fr", onGoogleMapApiLoaded);
    }
    else if ($("#carteSimple").length > 0) {    
        
        googleMapApi.load(cleGoogleMap, "fr", onGoogleMapApiLoaded);
    }     
    else
     {
     
        googleMapApi.load(cleGoogleMap, "fr", onGoogleMapApiLoadedTemp);
        //googleMapApi.load(cleGoogleMap, "fr", onGoogleMapApiLoaded);
    }
});

// -------------------------------------------------------------------------------------------------------------
// Une fois que l'API GoogleMap chargée
// -------------------------------------------------------------------------------------------------------------
var _RechercheEnCours = false;
var _map = null;
var _path = "/App_Themes/BonjourVoisinsNC/images/googlemap/"
_enableSelection = true;


// Change le type de la carte (terrain / satellite)
function setMapType(sat) {


   
       
        switch (sat) 
        {
            case 0:
                $('#btn_plan').attr("class", "btn_plan_hover");
                $('#btn_satellite').removeAttr("class");
                break;
            case 1:
                $('#btn_satellite').attr("class", "btn_satellite_hover");
                $('#btn_plan').removeAttr("class");
                break;
            default:
                $('#btn_plan').removeAttr("class");
                $('#btn_satellite').removeAttr("class");
                break;
        }
        
        if (_map != null) {
            _map.setMapType(sat);
        }
        else if (_mapGeoloc != null) {
        _mapGeoloc.setMapType(sat);
        }
    

}



// Change l'échelle
function setRadius(ctl) {
    _map.setRadius(ctl.value);
}

// Définit la direction actuelle pour le décalage
// attention à remettre à 0 cette valeur sur le onChange
var _panDir = 0;
var _panInit = 0;


function pan() {
    _map.panBy((_panDir ? 80 : -80), 0);
    _panDir = _panDir ? 0 : 1;
}




function getIcon(marker) {
    var _icon;
    // En fonction du type de point
    switch (marker.typePoint) {
        case 0: // Annonce
        case 1: // Voisin
            // On fonction du type du point
            switch (marker.typeVoisin) {
                case 0: _icon = { path: 'voisin', fileUp: 'voisin', size: { width: 28, height: 43 }, anchor: { x: 11, y: 32 }, windowAnchor: { x: 12, y: -7} }; break;
                case 1: _icon = { path: 'voisine', fileUp: 'voisine', size: { width: 28, height: 43 }, anchor: { x: 11, y: 32 }, windowAnchor: { x: 12, y: -7} }; break;
                case 2: _icon = { path: 'famille', fileUp: 'famille', size: { width: 42, height: 44 }, anchor: { x: 11, y: 32 }, windowAnchor: { x: 12, y: -7} }; break;
                case 100: _icon = { path: 'voisin', fileUp: 'voisin', size: { width: 28, height: 43 }, anchor: { x: 11, y: 32 }, windowAnchor: { x: 12, y: -7} }; break;
            }

            if (marker.typePoint == 0) {
                // on affiche soit les propositions soit les recherches, on prend le nombre d'annonce correspondante
                // si type annonce == 100 (les propositions et les recherches)

                if (marker.typeAnnonce == 100) {
                    _icon.fileUp = _icon.fileUp + 'rp';
                }
                else {
                    var _num = (marker.typeAnnonce == 0) ? marker.nbP : marker.nbR;
                    // On définit l'image					
                    _icon.fileUp = _icon.fileUp + (_num > 10 ? 10 : _num) + ((marker.typeAnnonce == 0) ? 'p' : 'r');
                }
            }
            break;
        case 2: // Adresse pratique
            _icon = { path: 'adresses', fileUp: marker.typePoint, size: { width: 42, height: 44 }, anchor: { x: 0, y: 0 }, windowAnchor: { x: 0, y: 0} };
            break;
    }
    // On rajoute le chemin complet
    _icon.path = _path + "pictos/" + _icon.path;
    _icon.fileOver = _icon.fileUp + '-on.png'
    _icon.fileUp = _icon.fileUp + '-off.png';


    // On renvoi l'icône
    return _icon;
}



function getLabel(type) {
    // En fonction du type de point
    switch (type) {
        case 0: return 'voisin';
        case 1: return 'voisine';
        case 2: return 'famille';
        case 100: return 'voisin';
    }
}


// -------------------------------------------------------------------------------------------------------------
// Si il y a plusieurs adresses correspondant à la recherche
// -------------------------------------------------------------------------------------------------------------

function onAddressSelection(addresses) {
    _c = 0;
    for (var _i = 0; _i < addresses.length; _i++) {
        if (addresses[_i].accuracy >= 4 && addresses[_i].accuracy <= 8 && addresses[_i].countryNameCode == "FR") _c++;
    }

    if (_c > 1) {

        _resultatsAdresses = "<div style='width:540px;background:#ffffff;padding:15px;margin-left:auto;margin-right:auto;'>";
        _resultatsAdresses += "<span style='font-size:14px;'>Afin d'afiner votre recherche sélectionnez le type d'annonce souhaité parmi :</span>";
        _resultatsAdresses += "<a id='btn_masquerSliderMap' style='float:right;' onclick='fermerLightBox();'/></a><br/><br/>";
        _resultatsAdresses += "<span style='font-size:13px;'><strong>" + _c + "</strong> au choix :</span><br/><br/>";

        for (var _i = 0; _i < addresses.length; _i++) {
            if (addresses[_i].accuracy >= 4 && addresses[_i].accuracy <= 8 && addresses[_i].countryNameCode == "FR")
                _resultatsAdresses += "- <a onClick='_map.setPosition2(" + addresses[_i].point.lat + ", " + addresses[_i].point.lng + ");fermerLightBox();' style='color:#4B90C1;'><strong>" + addresses[_i].address + "</strong></a><br/>";
        }
        _resultatsAdresses += "</div>";

        afficheLightBox(_resultatsAdresses);

    }
    else {
        _map.setPosition2(addresses[0].point.lat, addresses[0].point.lng);
        fermerLightBox();
    }
}

// -------------------------------------------------------------------------------------------------------------
// Ajoute la marque de l'utilisateur
// -------------------------------------------------------------------------------------------------------------

function addUserMark(lat, lng) {
    _map.addMarker(
		{ // Point
		    //lat	 : 46.5875354490414, 
		    //lng 	 : 0.3076171875
		    lat: lat,
		    lng: lng
		},
		{ // Icon
		    path: _path + 'pictos/profil',
		    fileUp: 'adresse-profil-off.png',
		    fileOver: 'adresse-profil-on.png',
		    size: { width: 20, height: 34 },
		    anchor: { x: 11, y: 32 }
		}
	);
}



// -------------------------------------------------------------------------------------------------------------
// Si il y a plusieurs adresses correspondant à la recherche
// -------------------------------------------------------------------------------------------------------------
var _localiteDejaRecherche = false;
function onRechercheLocalite(addresses) {

    
    
    _c = 0;
    for (var _i = 0; _i < addresses.length; _i++) {
        if (addresses[_i].accuracy >= 4 && addresses[_i].accuracy <= 8 && addresses[_i].countryNameCode == "FR") _c++;
    }

    if (_c > 1) {

        _resultatsAdresses = "<div style='width:540px;background:#ffffff;padding:15px;margin-left:auto;margin-right:auto;'>";
        _resultatsAdresses += "<span style='font-size:14px;'>Il existe plusieurs villes,  veuillez sélectionner la ville souhaitée parmi :</span>";
        _resultatsAdresses += "<a id='btn_masquerSliderMap' style='float:right;' onclick='fermerLightBox();'/></a><br/><br/>";
        _resultatsAdresses += "<span style='font-size:13px;'><strong>" + _c + "</strong> au choix :</span><br/><br/>";

        for (var _i = 0; _i < addresses.length; _i++) {
            if (addresses[_i].accuracy >= 4 && addresses[_i].accuracy <= 8 && addresses[_i].countryNameCode == "FR")
                _resultatsAdresses += "- <a onClick='_map.setPosition2(" + addresses[_i].point.lat + ", " + addresses[_i].point.lng + ");fermerLightBox();' style='color:#4B90C1;'><strong>" + addresses[_i].address + "</strong></a><br/>";
        }
        _resultatsAdresses += "</div>";

        _localiteDejaRecherche = true;

        
        afficheLightBox(_resultatsAdresses);

    }
    else {
        
    
        _localiteDejaRecherche = true;
        _map.setPosition2(addresses[0].point.lat, addresses[0].point.lng);
    }
}

function ListeAdressesExistes(adresse, callback) {

    //alert("ListeAdressesExistes callack = " + callback);
//var _adresses = null;



    if ($("#carte").length > 0) {
        _map.ListAddressExist(adresse, callback);

    }
    else if ($("#carteSimple").length > 0) {
    _map.ListAddressExist(adresse, callback);
    }
    else {        
        _tempMap.ListAddressExist(adresse, callback);
        //_tempMap.setAddress(adresse, 13, callback);
    }


//return false;  
    //return _adresses;

}

function onChangeTemp() {
}

var _tempMap = null;
function onGoogleMapApiLoadedTemp() {
 

    _tempMap = googleMapApi.createMap(
		"#carteTemp",
		"_tempMap",
		1,
		1,
		{
		    selection: {
		        //radius 			: 2800, // 2000 par défaut
		        dragIcon: {
		            path: _path + 'pictos/selection',
		            fileUp: "dragHandle-off.png",
		            fileOver: "dragHandle-on.png",
		            size: { width: 18, height: 18 },
		            anchor: { x: 9, y: 9 }
		        },
		        centerIcon: {
		            path: _path + 'pictos/selection',
		            fileUp: "centerHandle-off.png",
		            fileOver: "centerHandle-on.png",
		            size: { width: 20, height: 34 },
		            anchor: { x: 10, y: 34 }
		        },
		        enableSelection: false//, // true par défaut		        
		    },
		    onChange: onChangeTemp,
		    viewPort: {
		        mapControl: { y: 30 }
		    }
		}
	);
}

