﻿function adresse(_adresseid, _nom, _adresse, _GoogleMapLatLng, _container, _removeMustDelete)
{
    this.adresseid = _adresseid;
    this.nom = _nom;
    this.adresse = _adresse;
    this.GoogleMapLatLng = _GoogleMapLatLng;
    
    var mapMarker;

    var adresseDIV = new Element('li');
    adresseDIV.id = 'list_' + _adresseid;

    // Name
    var myName = new Element('a');
    myName.update(_nom);
    myName.className = 'nomadresse';
    myName.onclick = function() { window.open('http://www.tourismeduplessis.com/index2.php?option=com_members&popup=1&print=1&layout=print&page=&view=memberinfos&memberid=' + _adresseid,'','status=0,toolbar=0,width=500, height=600,scrollbars=1,resizable=0');  };
    
    // Move handle
    var myHandle = new Element('a');
    myHandle.className = 'handle';
    myHandle.setStyle({
        display: 'none'
    });
    
    // Add/remove button
    var myAdRemove = new Element('a');
    myAdRemove.className = 'addremove';
    myAdRemove.observe('click',function (event) { addRemoveAdresse(_adresseid, _removeMustDelete); return false; });

    switch (_container)
    {
        case 'ListeAdressesRecherche':
        {
            myAdRemove.update('+');
            break;
        }
        case 'ListeAdressesSelection':
        {
            myAdRemove.update('-');
            break;
        }
    }
    
    adresseDIV.insert(myAdRemove);
    adresseDIV.insert(myName);
    adresseDIV.insert(myHandle);
    
    this.adresseDIV = adresseDIV;
      
    $(_container).insert(adresseDIV);
    
    this.addSelection = function() {   
        mapMarker = createMarker(_GoogleMapLatLng, _nom, '<b>' + _nom + '</b><br>' + _adresse);
        
        map.addOverlay(mapMarker);
        mapMarker.setImage('images/darkgreen_Marker.png');
        myAdRemove.update('-');
        myHandle.setStyle({
            display: 'inline-block'
        });
    }

    this.removeSelection = function() {
        map.removeOverlay(mapMarker);
        myAdRemove.update('+');
    }    
    
    function findCoordinates(myAdresse)
    {
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(myAdresse,
            function(point)
            {
                if (!point)
                {
                    alert($('txtAdresse').value + " est introuvable");
                }
                else
                {
                    _longitude = point.lng();
                    _latitude = point.lat();

                    this.longitude = _longitude;
                    this.latitude = _latitude;
                    this.GoogleMapLatLng = point;
                }
            }
        );
    }
}
