function onPopupOpen() {
    var tempMarker = this;

    $('.marker-location-yes:visible').click(function () {
        var latlng = tempMarker.getLatLng();
        socket.compress(false).emit('suggested point', latlng);
        map.removeLayer(tempMarker);
        suggested_meetings[latlng.lat+latlng.lng] = new L.marker(latlng, {icon: new destIcon({iconUrl: '../images/blue-home.png'})})
                .on('popupopen',onPopupOpen).addTo(map).bindPopup('Votes: 1</span><br>Do you want to vote/unvote this meeting point?<p><button class="marker-vote">Unvote!</button></p>Delete the suggested meeting point?<p><button class="marker-delete-yes">Yes</button><button class="marker-no">No</button></p>');
        suggested_meetings[latlng.lat+latlng.lng].unvoted = true;
        suggested_meetings[latlng.lat+latlng.lng].votes = 1;
        suggested_meetings[latlng.lat+latlng.lng].owner = userName;
        checkMostVotedMeeting(latlng.lat,latlng.lng);
        suggested_meetings.group.addLayer(suggested_meetings[latlng.lat+latlng.lng]);
        limit_dests++;
        dest_marker = undefined;
    });

    $('.marker-delete-yes:visible').click(function () {
        var latlng = tempMarker.getLatLng();
        map.removeLayer(tempMarker);
        suggested_meetings.group.removeLayer(tempMarker);
        limit_dests--;

        //if the removed meeting is not the most voted
        if (mostVoted != latlng.lat+latlng.lng)
            socket.compress(false).emit('remove suggested point', latlng);
        else { //find the most voted meeting and send it and the removed
            var found = findMostVotedMeeting();
            if (found != undefined) {
                socket.compress(false).emit('remove suggested point', latlng, found.lat+found.lng);
                newMostVotedMeeting(found.lat, found.lng);
            }
            else {
                socket.compress(false).emit('remove suggested point', latlng);
                mostVoted = undefined;
            }
        }
        delete suggested_meetings[latlng.lat+latlng.lng];
    });

    //delete before set up the meeting point
    $('.mymarker-delete-yes:visible').click(function () {
        map.removeLayer(tempMarker);
        dest_marker = undefined;
    });

    $('.marker-no:visible').click(function () {
        tempMarker.closePopup();
    });

    $('.marker-vote:visible').click(function () {
        var latlng = tempMarker.getLatLng();

        if (suggested_meetings[latlng.lat+latlng.lng].unvoted) {
            socket.compress(false).emit('unvote meeting point', latlng);
            suggested_meetings[latlng.lat+latlng.lng].unvoted = false;
            suggested_meetings[latlng.lat+latlng.lng].votes--;
            tempMarker.setPopupContent(updateVote(tempMarker.getPopup().getContent(), -1).
                replace(/Vote!|Unvote!/,function(match) {return (match=="Vote!")?"Unvote!":"Vote!";}));
            tempMarker.fire('popupopen');
            if (latlng.lat+latlng.lng == mostVoted)
                findMostVotedMeeting();
        }
        else {
            socket.compress(false).emit('vote meeting point', latlng);
            suggested_meetings[latlng.lat+latlng.lng].unvoted = true;
            suggested_meetings[latlng.lat+latlng.lng].votes++;
            tempMarker.setPopupContent(updateVote(tempMarker.getPopup().getContent(), 1).
                replace(/Vote!|Unvote!/,function(match) {return (match=="Vote!")?"Unvote!":"Vote!";}));
            tempMarker.fire('popupopen');
            checkMostVotedMeeting(latlng.lat, latlng.lng);
        }
    });
}
//Function to handle events of opened marker popups
Function to handle events of opened marker popups
Function to handle events of opened marker popups
function onPopupOpen() {
    var tempMarker = this;
    $('.marker-location-yes:visible').click(function () {
        var latlng = tempMarker.getLatLng();
        socket.compress(false).emit('suggested point', latlng);
        map.removeLayer(tempMarker);
        suggested_meetings[latlng.lat+latlng.lng] = new L.marker(latlng, {icon: new destIcon({iconUrl: '../images/blue-home.png'})})
                .on('popupopen',onPopupOpen).addTo(map).bindPopup('Votes: 1</span><br>Do you want to vote/unvote this meeting point?<p><button class="marker-vote">Unvote!</button></p>Delete the suggested meeting point?<p><button class="marker-delete-yes">Yes</button><button class="marker-no">No</button></p>');
        suggested_meetings[latlng.lat+latlng.lng].unvoted = true;
        suggested_meetings[latlng.lat+latlng.lng].votes = 1;
        suggested_meetings[latlng.lat+latlng.lng].owner = userName;
        checkMostVotedMeeting(latlng.lat,latlng.lng);
        suggested_meetings.group.addLayer(suggested_meetings[latlng.lat+latlng.lng]);
        limit_dests++;
        dest_marker = undefined;
    });
    $('.marker-delete-yes:visible').click(function () {
        var latlng = tempMarker.getLatLng();
        map.removeLayer(tempMarker);
        suggested_meetings.group.removeLayer(tempMarker);
        limit_dests--;
        if (mostVoted != latlng.lat+latlng.lng)
            socket.compress(false).emit('remove suggested point', latlng);
        else { 
            var found = findMostVotedMeeting();
            if (found != undefined) {
                socket.compress(false).emit('remove suggested point', latlng, found.lat+found.lng);
                newMostVotedMeeting(found.lat, found.lng);
            }
            else {
                socket.compress(false).emit('remove suggested point', latlng);
                mostVoted = undefined;
            }
        }
        delete suggested_meetings[latlng.lat+latlng.lng];
    });
    $('.mymarker-delete-yes:visible').click(function () {
        map.removeLayer(tempMarker);
        dest_marker = undefined;
    });
    $('.marker-no:visible').click(function () {
        tempMarker.closePopup();
    });
    $('.marker-vote:visible').click(function () {
        var latlng = tempMarker.getLatLng();
        if (suggested_meetings[latlng.lat+latlng.lng].unvoted) {
            socket.compress(false).emit('unvote meeting point', latlng);
            suggested_meetings[latlng.lat+latlng.lng].unvoted = false;
            suggested_meetings[latlng.lat+latlng.lng].votes--;
            tempMarker.setPopupContent(updateVote(tempMarker.getPopup().getContent(), -1).
                replace(/Vote!|Unvote!/,function(match) {return (match=="Vote!")?"Unvote!":"Vote!";}));
            tempMarker.fire('popupopen');
            if (latlng.lat+latlng.lng == mostVoted)
                findMostVotedMeeting();
        }
        else {
            socket.compress(false).emit('vote meeting point', latlng);
            suggested_meetings[latlng.lat+latlng.lng].unvoted = true;
            suggested_meetings[latlng.lat+latlng.lng].votes++;
            tempMarker.setPopupContent(updateVote(tempMarker.getPopup().getContent(), 1).
                replace(/Vote!|Unvote!/,function(match) {return (match=="Vote!")?"Unvote!":"Vote!";}));
            tempMarker.fire('popupopen');
            checkMostVotedMeeting(latlng.lat, latlng.lng);
        }
    });
}
function onPopupOpen ( ) { var tempMarker = this ; $ ( '.marker-location-yes:visible' ) . click ( function ( ) { var latlng = tempMarker . getLatLng ( ) ; socket . compress ( false ) . emit ( 'suggested point' , latlng ) ; map . removeLayer ( tempMarker ) ; suggested_meetings [ latlng . lat + latlng . lng ] = new L . marker ( latlng , { icon : new destIcon ( { iconUrl : '../images/blue-home.png' } ) } ) . on ( 'popupopen' , onPopupOpen ) . addTo ( map ) . bindPopup ( 'Votes: 1</span><br>Do you want to vote/unvote this meeting point?<p><button class="marker-vote">Unvote!</button></p>Delete the suggested meeting point?<p><button class="marker-delete-yes">Yes</button><button class="marker-no">No</button></p>' ) ; suggested_meetings [ latlng . lat + latlng . lng ] . unvoted = true ; suggested_meetings [ latlng . lat + latlng . lng ] . votes = 1 ; suggested_meetings [ latlng . lat + latlng . lng ] . owner = userName ; checkMostVotedMeeting ( latlng . lat , latlng . lng ) ; suggested_meetings . group . addLayer ( suggested_meetings [ latlng . lat + latlng . lng ] ) ; limit_dests ++ ; dest_marker = undefined ; } ) ; $ ( '.marker-delete-yes:visible' ) . click ( function ( ) { var latlng = tempMarker . getLatLng ( ) ; map . removeLayer ( tempMarker ) ; suggested_meetings . group . removeLayer ( tempMarker ) ; limit_dests -- ; if ( mostVoted != latlng . lat + latlng . lng ) socket . compress ( false ) . emit ( 'remove suggested point' , latlng ) ; else { var found = findMostVotedMeeting ( ) ; if ( found != undefined ) { socket . compress ( false ) . emit ( 'remove suggested point' , latlng , found . lat + found . lng ) ; newMostVotedMeeting ( found . lat , found . lng ) ; } else { socket . compress ( false ) . emit ( 'remove suggested point' , latlng ) ; mostVoted = undefined ; } } delete suggested_meetings [ latlng . lat + latlng . lng ] ; } ) ; $ ( '.mymarker-delete-yes:visible' ) . click ( function ( ) { map . removeLayer ( tempMarker ) ; dest_marker = undefined ; } ) ; $ ( '.marker-no:visible' ) . click ( function ( ) { tempMarker . closePopup ( ) ; } ) ; $ ( '.marker-vote:visible' ) . click ( function ( ) { var latlng = tempMarker . getLatLng ( ) ; if ( suggested_meetings [ latlng . lat + latlng . lng ] . unvoted ) { socket . compress ( false ) . emit ( 'unvote meeting point' , latlng ) ; suggested_meetings [ latlng . lat + latlng . lng ] . unvoted = false ; suggested_meetings [ latlng . lat + latlng . lng ] . votes -- ; tempMarker . setPopupContent ( updateVote ( tempMarker . getPopup ( ) . getContent ( ) , - 1 ) . replace ( / Vote!|Unvote! / , function ( match ) { return ( match == "Vote!" ) ? "Unvote!" : "Vote!" ; } ) ) ; tempMarker . fire ( 'popupopen' ) ; if ( latlng . lat + latlng . lng == mostVoted ) findMostVotedMeeting ( ) ; } else { socket . compress ( false ) . emit ( 'vote meeting point' , latlng ) ; suggested_meetings [ latlng . lat + latlng . lng ] . unvoted = true ; suggested_meetings [ latlng . lat + latlng . lng ] . votes ++ ; tempMarker . setPopupContent ( updateVote ( tempMarker . getPopup ( ) . getContent ( ) , 1 ) . replace ( / Vote!|Unvote! / , function ( match ) { return ( match == "Vote!" ) ? "Unvote!" : "Vote!" ; } ) ) ; tempMarker . fire ( 'popupopen' ) ; checkMostVotedMeeting ( latlng . lat , latlng . lng ) ; } } ) ; }
Function to handle events of opened marker popups
Function to handle events of opened marker popups
//if the removed meeting is not the most voted //find the most voted meeting and send it and the removed //delete before set up the meeting point