;
ec.google || (function($)
{
   var _google = {

      /*
      global parameters
       */
      params: {
         animationSpeed: 700,
         canEdit: false,
         currentMap: null,
         ecMapId:-1,
         placedEcMarkers: [],
         infoWindow: null
      },
      /* ****************************************************************
      initialize the google map
      */
      init: function(ecMapId)
      {
         // create new EcMap object
         ec.google.params.currentMap = new ec.google.EcMap(ecMapId);
         // create new google map info window
         ec.google.params.infoWindow = new google.maps.InfoWindow();

         // click event to focus google map on given location
         $('.toLocation').click(function(event)
         {
            event.preventDefault();
            fundament.ec.google.codeAddress('searchAddress',
            {
               'feedbackId':'googleLocationResult',
               'defaultAddressSuffix':'Nederland',
               'latResultInput':'googleMap_lat',
               'lngResultInput':'googleMap_lng',
               'displayOnMap':true,
               'centerOnMap':true
            });
         });
         // click event to show all markers in the EcMap on the google map.
         $('.showAllMarkers').click(function(event){
            event.preventDefault();
            ec.google.params.currentMap.showMarkers();
         });
         // when submitting the google map form fill in the form with the current google map values (center, zoom, etc).
         $('#editGoogleMap').submit(function()
         {
            ec.google.fillFormWithMapValues();
         });
         // click event to add a new facility group to the page.
         $('.createNewFacilityGroup').click(function(event) {
            event.preventDefault();
            ec.google.addNewFacilityGroup(this);
         });
         // common click event to close the element with a close button.
         $('.closeWindow').click(function() {
            $(this).parent().hide();
         });

      },
      /* ****************************************************************
       initialize the google map managers (slightly different than for users).
       */
      initManageFacilities: function()
      {
         // user is allowed to manage facilities
         ec.google.params.canEdit = true;
         $("#editFacilityGroupForm").hide();
         // click events to the facility groups.
         ec.google.addFacilityGroupEvents();
      },
      /* ****************************************************************
       initialize the google map users (slightly different than for managers).
       */
      initSelectFacilities: function()
      {
         // hide the listed markers of group
         $('.facilityMarkersGroup').hide();
         // click event to show the facilities of a group
         $(".facilityGroup .facilityGroupHead").click(function(event) {
            event.preventDefault();
            ec.google.animateFacilityGroupSlide($(this).parent().children(".facilityMarkers"));
         });
         // click event to show only the selected facility on the map.
         $('.facilityMarker').click(function(event){
            event.preventDefault();
            var markerToShow = ec.google.params.currentMap.getMarker({id: $(".markerId", this).val()});
            ec.google.params.infoWindow.setContent(ec.google.getMarkerInfoWindowContent(markerToShow));
            ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, markerToShow.marker);

         });
      },
      /* ****************************************************************
       initialize the google map for voorzieningen
       */
      initVoorzieningen: function()
      {
         $('.facilityMarkers li').hide();

         $('#showFacilityGroups').hide();
         $('#hideFacilityGroups').click(function(event) {
            event.preventDefault();
            var newMarkersHeight = $('.facilityMarkers').height() + $('.facilityGroups').height();
            $('.facilityGroups').hide();
            $('.facilityMarkers').css({height:newMarkersHeight});
            $('#hideFacilityGroups').hide();
            $('#showFacilityGroups').show();
         });
         $('#showFacilityGroups').click(function(event) {
            event.preventDefault();
            $('.facilityGroups').show();
            var newMarkersHeight = $('.facilityMarkers').height() - $('.facilityGroups').height();
            $('.facilityMarkers').css({height:newMarkersHeight});
            $('#hideFacilityGroups').show();
            $('#showFacilityGroups').hide();
         });
         $('.facilityGroupCheckbox').click(function(event) {

            var groupIdStr = $(this).attr('name');
            var groupId = groupIdStr.substr("groupId_".length); // strip of the "groupId_" prefix
            var showMarkers = $(this).attr('checked');
            if (showMarkers)
            {
               $('.facilityMarkers .'+groupIdStr).show();
               ec.google.params.currentMap.showMarkers({groupId:groupId});
            }
            else
            {
               $('.facilityMarkers .'+groupIdStr).hide();
               ec.google.params.currentMap.hideMarkers({groupId:groupId});
            }
         });
         // click event to show only the selected facility on the map.
         $('.facilityMarker').click(function(event){
            event.preventDefault();
            $('.facilityMarker').css({backgroundColor:''});
            $(this).css({backgroundColor:'#DEDEDE'});
            var markerToShow = ec.google.params.currentMap.getMarker({id: $(".markerId", this).val()});
            ec.google.params.infoWindow.setContent(ec.google.getMarkerInfoWindowContent(markerToShow));
            ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, markerToShow.marker);

         });
      },
      /**
       * fills the edit form with the current values of the google map.
       */
      fillFormWithMapValues: function()
      {
         var mapCenter = fundament.ec.google.theMap.getCenter();
         $('#googleMap_lat').val(mapCenter.lat());
         $('#googleMap_lng').val(mapCenter.lng());
         $('#googleMap_zoom').val(fundament.ec.google.theMap.getZoom());
      },
      /**
       * shows a list of available icons to choose from (positioned at the clicked icon).
       * When a icon is chosen it updates the clicked icon.
       *
       * @param clickedIcon this icon will be changed if a new facility icon is selected.
       * @param _params extra params can contains the following:
       * {
       *    ecMarkerToUpdate: null, // if passed this marker will be updated with the new icon.
       *    ecMarkerGroupId: null,  // if passed all markers in this group will be updated with the new icon.
       * }
       */
      showFacilityIcons: function(clickedIcon, _params)
      {
         var params = {
            ecMarkerToUpdate: null,
            ecMarkerGroupId: null
         };
         $.extend(params, _params);

         // position the group of available icons to select from
         var facilityIconsOffset = $(clickedIcon).offset();
         $('#facilityIcons').css({
            left: facilityIconsOffset.left+20,
            position: "absolute",
            top: facilityIconsOffset.top+20,
            width: "177px"
         });
         // show the available icons
         $('#facilityIcons').show();

         $('#facilityIcons .icon').unbind('click');
         $('#facilityIcons #useOwnIconUrl').unbind('click');
         // add click event to selected icon.
         $('#facilityIcons .icon').click(function()
         {
            ec.google.updateIcon(params, $(this).attr('src'), clickedIcon);
         });
         $('#facilityIcons #useOwnIconUrl').click(function()
         {
            var ownIconForm = $(this).parent();
            ec.google.updateIcon(params, $(".ownIconUrl", ownIconForm).val(), clickedIcon);
         });
      },
      updateIcon: function (params, newIcon, clickedIcon)
      {
         if (params.ecMarkerToUpdate)
         {
            params.ecMarkerToUpdate.marker.setIcon(newIcon);
            ec.google.storeMarker(params.ecMarkerToUpdate);
         }
         if (params.ecMarkerGroupId)
         {
            ec.google.updateFacilityGroup(params.ecMarkerGroupId, newIcon)
         }
         // update the icon on google map of the clicked icon.
         $(clickedIcon).attr('src', newIcon);
         $(clickedIcon).attr('title', $(this).attr('title'));
         $('.groupName').val($('#newFacilityGroup .icon').attr('title'));
         $('#facilityIcons').hide();
      },

      /**
       * Shows a 'popup' with a form to create a new facility group.
       * @param clickedElement
       */
      addNewFacilityGroup: function(clickedElement)
      {
         var clickedElementOffset = $(clickedElement).offset();
         // show the form
         $('#newFacilityGroup').show();
         // position the form
         $('#newFacilityGroup').css({
            left: clickedElementOffset.left,
            position: "absolute",
            top: clickedElementOffset.top + $(clickedElement).height()
         });
         // click event to select new icon for the form.
         $('#newFacilityGroup .icon').click(function(event)
         {
            event.preventDefault();
            ec.google.showFacilityIcons(this, {});
         });
         // click event to actually add the new form to the page.
         $('#newFacilityGroup .addNewFacilityGroup').click(function(event)
         {
            event.preventDefault();
//            var newFacilityGroup = '<div class="facilityGroup">'+
//                                    '<div class="facilityGroupHead">'+
//                                    '<input type="checkbox" class="facilityGroupCheckbox" name="groupId_" />&nbsp;'+
//                                    '<span class="groupName">'+$('#newFacilityGroup .groupName').val()+'</span>&nbsp;'+
//                                    '<a href="#" class="addToMap" title="Plaats op kaart">toevoegen</a>&nbsp;'+
//                                    '<a href="#" class="editMarkerGroup" title="edit group">bewerk</a>&nbsp;'+
//                                    '<a href="#" class="addToMap" title="Plaats nieuwe marker op de kaart">Zet op de kaart</a></li>';
//                                    '</div>';
             var newFacilityGroup = $('#newFacilityGroupDiv .facilityGroup').clone();
             $(".groupName", newFacilityGroup).html($('#newFacilityGroup .groupName').val());
             $("input.groupName", newFacilityGroup).val($('#newFacilityGroup .groupName').val());
             $(".changeIcon", newFacilityGroup).replaceWith('<div>Het icoon kan pas gewijzigd worden na het toevoegen van een marker en het verversen van de pagina</div>');
             $(".removeFacilityGroup", newFacilityGroup).remove();
            if (!$(".facilityGroups .groups").exists())
            {
               $(".facilityGroups").append('<div class="groups"></div>');
            }

            $(".facilityGroups .groups").prepend(newFacilityGroup);
            ec.google.addFacilityGroupEvents();
            $('#newFacilityGroup').hide();
         });
      },
      updateFacilityGroup: function(facilityGroupId, newIconUrl){
         // get all markers of the selected group on the map
         var ecGroupMarkers = ec.google.params.currentMap.getMarker({groupId:facilityGroupId});
         for (var i=0; i<ecGroupMarkers.length; i++)
         {
            var markerToUpdate = ecGroupMarkers[i];
            markerToUpdate.marker.icon = newIconUrl;
            ec.google.storeMarker(markerToUpdate);
         }
         setTimeout("location.reload(true)", 1000);
      },
      removeFacilityGroup: function(facilityGroupId)
      {
         var toBeRemoved = confirm("Weet U zeker dat u deze groep wilt verwijderen? Alle markers van deze groep worden dan ook direct verwijderd van de google kaart!");
         if (toBeRemoved)
         {
             ec.google.params.infoWindow.close();
            // get all markers of the selected group on the map
            var ecGroupMarkers = ec.google.params.currentMap.getMarker({groupId:facilityGroupId});
            var markerIds = [];
            var mapId = _google.params.ecMapId;
            for (var i=0; i<ecGroupMarkers.length; i++)
            {
               var ecMarkerToRemove = ecGroupMarkers[i];
               mapId = ecMarkerToRemove.ecMapId;
               ecMarkerToRemove.marker.setMap(null);
               ec.google.params.currentMap.removeMarker(ecMarkerToRemove);
               markerIds.push(ecMarkerToRemove.ecMarkerId);
            }
             $.post("/googleMaps/deleteMarker", {mapId:mapId, 'markerIds':markerIds, XMLOnly:true});

            $("#facilityGroup_"+facilityGroupId).remove();
         }
         return toBeRemoved;
      },

      /**
       * adds the following events to all facility groups
       * 1) click event to place a new marker of the a group on the map
       * 2) click event to select a new icon for a group
       * 3) click event to update a group name
       */
      addFacilityGroupEvents: function()
      {
         // unbind previous events cause groups are dynamically added and this method is called every time a new group is added.
         $('.facilityGroups .facilityGroup .addToMap').unbind('click');
         $('.facilityGroups .facilityGroup .addToMap').click(function(event)
         {
            event.preventDefault();
            var groupIdStr = $("input", $(this).parent()).attr("name");
            var groupId = groupIdStr.substr("groupId_".length);
            var groupName = $(".groupName", $(this).parent()).html();
            var icon = '';
            var ecGroupMarkers = ec.google.params.currentMap.getMarker({groupId:groupId});
            if (ecGroupMarkers && ecGroupMarkers.length > 0) {
                var aGroupMarker = ecGroupMarkers[0];
                icon = aGroupMarker.marker.icon;
            }
            ec.google.addNewEcMarker({title:groupName, groupName:groupName, groupId:groupId, icon:icon});
         });
         $('.facilityGroups .editMarkerGroup').unbind('click');
         $('.facilityGroups .editMarkerGroup').click(function(event)
         {
             $("#sendFacilityGroupForm").unbind('click');
             $("#cancelFacilityGroupForm").unbind('click');
             $("#editFacilityGroupForm .removeFacilityGroup").unbind('click');
             $('#editFacilityGroupForm .changeIcon').unbind('click');

             var facilityGroupElement = $(this).parent();
             var groupIdStr = $("input", facilityGroupElement).attr("name");
             var groupId = groupIdStr.substr("groupId_".length);
//             var iconUrl = $('.facilityGroupIcon', facilityGroupElement).attr('name');
//             $("#editFacilityGroupForm img").attr('src',iconUrl);

             var facilityGroupElementOffset = $(facilityGroupElement).offset();
             $('#editFacilityGroupForm').css({
                left: facilityGroupElementOffset.left+20,
                position: "absolute",
                top: facilityGroupElementOffset.top+20,
                width:"180px"
             });
             $("#editFacilityGroupForm").show();
             $("#sendFacilityGroupForm").click(function(event){
                event.preventDefault();
                var groupName = $("#editFacilityGroupForm .groupName").val();
                ec.google.updateMarkerGroup(groupId, groupName);
                $(".groupName", facilityGroupElement).html(groupName);
                $("#editFacilityGroupForm").hide();
             });
             $("#cancelFacilityGroupForm").click(function(event){
                event.preventDefault();
                $("#editFacilityGroupForm").hide();
             });
             $('.updateFacilityGroup').click(function(event) {
                event.preventDefault();
             });
             $('#editFacilityGroupForm .removeFacilityGroup').click(function(event) {
                event.preventDefault();
                if (ec.google.removeFacilityGroup(groupId))
                {
                   $("#editFacilityGroupForm").hide();
                }
             });
             $('#editFacilityGroupForm .changeIcon').click(function(event)
             {
                event.preventDefault();
                ec.google.showFacilityIcons(this, {ecMarkerGroupId: groupId});
             });

         });

      },
      /**
       * Animates to show or hide the facilities of a group.
       * @param facilities the facilities to show or hide.
       */
      animateFacilityGroupSlide: function(facilities)
      {
         //if ($(answer).css("height") == '0' || $(answer).css("height") == '0pt')
         if ($(facilities).is(":hidden") )
         {
            ec.google.animateShowFacilityGroup(facilities);
         }
         else
         {
            ec.google.animateHideFacilityGroup(facilities);
         }
      },
      /**
       * @param facilities the facilities to show
       */
      animateShowFacilityGroup: function(facilities)
      {
         // remember the original height
         var originalHeight = $(facilities).outerHeight();
         // set the height to zero and hide the overflow. This will still not show the answer even if the element is not hidden anymore.
         $(facilities).css({height:'0', overflow:'hidden'});
         // remove the hidden property, the element is still not visible due to the command above.
         $(facilities).show();
         // remove the click event, user is allow to click again when animation is finished.
         $(".facilityGroupHead", facilities).unbind("click");
         $(facilities).animate({height:originalHeight}, ec.google.params.animationSpeed, function(){
            // bind the click event, user must be able to close the answer.
            $(".facilityGroupHead", facilities).click(function() {ec.google.animateHideFacilityGroup(facilities);});
            $(facilities).css({height:null, overflow:'hidden'});
         });
      },
      /**
       * @param facilities the facilities to hide
       */
      animateHideFacilityGroup: function(facilities)
      {
         // remember the original height
         var originalHeight = $(facilities).height();
         // remove the click event, user is allow to click again when animation is finished.
         $(".facilityGroupHead", facilities).unbind("click");
         $(facilities).animate({height:0}, ec.google.params.animationSpeed, function() {
            $(facilities).hide();
            $(facilities).height(originalHeight);
            // bind the click event, user must be able to show the answer.
            $(".facilityGroupHead", facilities).click(function() {ec.google.animateShowFacilityGroup(facilities);});
         });
      },
      /**
       * constructs a new EcMarker and stores it.
       * @param params possible values:
            {
               draggable: true,                             // if set to true the added icon is draggable on the map
               description: '',                             // a description of the marker (shown in the info window)
               groupName: 'Default',                        // name of the group of which the marker is part (defaults to'Default')
               icon: '',                                    // the icon of the marker
               map: map,                                    // the map on which the marker will be placed.
               position: map.getCenter(),                   // the position of the marker (default is in the center of the map)
               title: 'voorziening',                        // the title of the marker
               ecMarkerId: -1,                              // the persistent id of the ec marker.
               ecMapId: ec.google.params.currentMap.ecMapId // the persistent id of the ec map.
            };
       */
      addNewEcMarker: function(params)
      {
         var map = ec.google.params.currentMap.googleMap;
         var ecMarkerParams =
         {
            draggable: true,
            description: '',
            groupName: 'Default',
            icon: '',
            map: map,
            position: map.getCenter(),
            title: 'voorziening',
            ecMarkerId: -1,
            ecMapId: ec.google.params.currentMap.ecMapId
         };
         $.extend(ecMarkerParams, params);
         // Creating a marker
         var ecMarker = new ec.google.ECMarker(ecMarkerParams);
         ecMarker = ec.google.storeMarker(ecMarker);
      },
      /**
       * adds a new marker to the map.
       * @param params
       */
      addEcMarker: function(params)
      {
         var map = ec.google.params.currentMap.googleMap;
         var ecMarkerParams =
         {
            draggable: false,
            description: '',
            icon: '',
            map: map,
            position: map.getCenter(),
            title: 'voorziening',
            ecMarkerId: -1,
            ecMarkerGroupId: -1,
            ecMapId: ec.google.params.currentMap.ecMapId
         };
         $.extend(ecMarkerParams, params);
         if (params.latitude && params.longitude)
         {
            ecMarkerParams.position = new google.maps.LatLng(params.latitude, params.longitude);
         }
         if (ec.google.params.canEdit)
         {
            ecMarkerParams.draggable = true;
         }
         var ecMarker = new ec.google.ECMarker(ecMarkerParams);
         ec.google.params.currentMap.putMarker(ecMarker);
         ec.google.addEventsToMarker(ecMarker, ecMarkerParams);
      },
      /**
       * persists the ec Marker in the ec database and adds it to the ec.google.params.currentMap javascript object
       * @param ecMarker the ec marker to store
       */
      storeMarker: function(ecMarker)
      {
         var marker = ecMarker.marker;
         var postData = {
            XMLOnly: true,
            id: ecMarker.ecMapId,
            groupId:ecMarker.groupId,
            groupName: ecMarker.groupName,
            latitude: marker.position.lat(),
            longitude: marker.position.lng(),
            title: marker.title,
            description: ecMarker.description.replace(/\n/g, '<br />'),
            iconUrl: marker.icon};
         if (ecMarker.ecMarkerId == -1)
         {
            // this is a new insert
            $.post("/googleMaps/storeMarker", postData, function(data)
            {
               var markerId = $("ECResponse > module > action > list > ECItemCollection > items > marker > id", data).text();
               ecMarker.ecMarkerId = markerId;
               ec.google.params.currentMap.putMarker(ecMarker);
               ec.google.addEventsToMarker(ecMarker);
//               ec.google.getMarkerInfoWindowContent(ecMarker);
//               ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, ecMarker.marker);
//               ec.google.editMarker(markerId);
            });
         }
         else
         {
            // this is an update
            $.extend(postData, { markerId: ecMarker.ecMarkerId });
            $.post("/googleMaps/storeMarker", postData, function(data)
            {
//               var markerId = $("ECResponse > module > action > list > ECItemCollection > items > marker > id", data).text();
//               ecMarker.ecMarkerId = markerId;
            });
         }
      },
      /**
       * Builds up the content of an ec marker to be displayed in an google info window
       * @param ecMarker the ec marker
       * @param _params valid values:
       */
      getMarkerInfoWindowContent: function (ecMarker)
      {
         var infoWindowObjectObj = $("<div class='facilityContent'/>");
         infoWindowObjectObj.append("<div class='facilityTitle'><strong>"+ecMarker.marker.title+"</strong></div>");
         if (ec.google.params.canEdit)
         {
            var infoWindowMenu = $("<ul/>");
            var editLink = $("<a href='#' title='bewerk' class='editFacilityMarker' >Bewerk </a>").click(function() {
                  ec.google.editMarker(ecMarker.ecMapId,ecMarker.ecMarkerId);
               });
            var editLinkListItem = $("<li/>").append(editLink);
            infoWindowMenu.append(editLinkListItem);
            var removeLink = $("<a href='#' title='verwijder' class='removeFacilityMarker' >Verwijder </a>").click(function() {
                  ec.google.removeMarker(ecMarker.ecMapId,ecMarker.ecMarkerId);
               });
            var removeLinkListItem = $("<li/>").append(removeLink);
            infoWindowMenu.append(removeLinkListItem);
            infoWindowObjectObj.append(infoWindowMenu);
         }
         infoWindowObjectObj.append("<div class='facilityDescription'><p>"+ecMarker.description+ "</p></div>");
         return infoWindowObjectObj[0];
      },
      /**
       * adds the drag event to the marker so it can be dragged around the google map and
       * adds the click event to display the info window of a marker.
       * @param ecMarker the ec marker
       * @param _params valid values:
       */
      addEventsToMarker: function(ecMarker, _params)
      {
         google.maps.event.addListener(ecMarker.marker, 'click', function()
         {
            ec.google.params.infoWindow.setContent(ec.google.getMarkerInfoWindowContent(ecMarker));
            ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, ecMarker.marker);
         });
         // stores the marker when it's dragged.
         google.maps.event.addListener(ecMarker.marker, 'dragend', function()
         {
            ec.google.storeMarker(ecMarker);
         });
      },
      /**
       * shows a info window with a form to edit the properties of a marker.
       *
       * @param ecMapId the persistent ec map id on which the marker is present
       * @param ecMarkerId the persistent ec marker id
       */
      editMarker: function(ecMapId, ecMarkerId) {
         var ecMarker = ec.google.params.currentMap.getMarker({id:ecMarkerId});
         // add click event to store the edited marker.
         var saveLink = $('<a href="#" title="bewaar" class="facilitySave">bewaar</a>').click(function() {
            ecMarker.marker.title = $('.facilityContent .facilityTitle input').val();
            ecMarker.description = $('.facilityContent .facilityDescription textarea').val();
            ec.google.storeMarker(ecMarker);
            ec.google.params.infoWindow.setContent(ec.google.getMarkerInfoWindowContent(ecMarker));
            ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, ecMarker.marker);
            });
         var ecMarkerIcon = "http://www.google.com/intl/en_us/mapfiles/ms/icons/red-dot.png";
         if (ecMarker.marker.icon.length > 0)
         {
            ecMarkerIcon = ecMarker.marker.icon;
         }
//         var markerIcon = $('<img class="changeMarkerIcon" src="'+ecMarkerIcon+'" alt="'+ecMarker.marker.title+'" />').click(function() {
//            ec.google.showFacilityIcons(this, {updateMap: false, ecMarkerToUpdate: ecMarker});
//         });
         $('.facilityContent .facilityTitle strong').replaceWith('<input type="text" name="facilityTitle" value="'+ecMarker.marker.title+'"/>');
         $('.facilityContent .facilityDescription p').replaceWith('<textarea  class="facilityDescription" rows="8" cols="36" name="facilityDescription">'+ecMarker.description+'</textarea>');
//         $('.facilityContent').append(markerIcon);
//         $('.facilityContent').append("Klik om icon te kiezen.<br/>");
         $('.facilityContent').append(saveLink);
         ec.google.params.infoWindow.setContent($('.facilityContent')[0]);
         ec.google.params.infoWindow.open(ec.google.params.currentMap.googleMap, ecMarker.marker);
      },
      changeMarkerIcon: function()
      {

      },
      /**
       * removes the marker from the map and alert the user if he is sure about it.
       *
       * @param ecMapId the persistent ec map id on which the marker is present
       * @param ecMarkerId the persistent ec marker id
       */
      removeMarker: function(ecMapId, ecMarkerId)
      {
         if (confirm("Weet U zeker dat u deze marker wilt verwijderen?"))
         {
            ec.google.removeMarkerSilent(ecMapId, ecMarkerId);
         }
      },
      /**
       * removes the marker from the map. The user doesn't get any warning
       *
       * @param ecMapId the persistent ec map id on which the marker is present
       * @param ecMarkerId the persistent ec marker id
       */
      removeMarkerSilent: function(ecMapId, ecMarkerId)
      {
         ec.google.params.infoWindow.close();
         //var marker = ec.google.params.placedEcMarkers[ecMapId].ecMarkerId;
         var ecMarker = ec.google.params.currentMap.getMarker({id:ecMarkerId});
         ecMarker.marker.setMap(null);
         $.post("/googleMaps/deleteMarker", {mapId:ecMapId, markerId:ecMarkerId, XMLOnly:true});
         ec.google.params.currentMap.removeMarker(ecMarker);
      },
      updateMarkerGroup: function(groupId, groupName)
      {
         $.post("/googleMaps/updateMarkerGroup", {id:groupId, name:groupName, XMLOnly:true});
      },
      /**
       * ECMarker object representing an ec marker
       * @param ecMarkerParams
       */
      ECMarker: function(ecMarkerParams)
      {
         // persistent ec map id
         this.ecMapId = ecMarkerParams.ecMapId;
         // persistent ec marker id
         this.ecMarkerId = ecMarkerParams.ecMarkerId;
         // description of the marker
         this.description = ecMarkerParams.description;
         // groupId of which the marker is part
         this.groupId = ecMarkerParams.groupId;
         // groupName of which the marker is part @TODO move the groupName to it's own object (OOP :-) )
         this.groupName = ecMarkerParams.groupName;
         // a google marker object.
         this.marker = new google.maps.Marker(ecMarkerParams);
      },
      /**
       * ECMap object representing an ec map
       *
       * It contains serveral functions to manipulate the map.
       *
       * putMarker: adds a new ec marker to the map
       * getMarker: gets a marker or array of markers from the map, matching the pasted like parms
       * showMarkers: show only those markers matching the like params
       * removeMarker: removes a marker from the map
       * hideAllMarker: no markers are visiable on the google map any more.
       * @param ecMapId
       */
      EcMap: function(ecMapId)
      {
         this.googleMap = fundament.ec.google.getECGoogleMap();
         this.ecMapId = ecMapId;
         this.ecMarkers = [];
         /**
          * @param ecMarker the ec marker to add the this map
          */
         this.putMarker = function(ecMarker)
         {
            this.ecMarkers.push(ecMarker);
         };
         /**
          * @param likeParams // valid values are
          * {
          *    id: 1 // a unique persistent ec marker id
          *    iconSrc: 'url of icon' // a url of the icon src, all icons with this iconSrc are returned.
          *    groupId: 1 // a unique persistent ec markerGroup id.
          * }
          * @return a single marker or array of markers matching the like params
          */
         this.getMarker = function(likeParams)
         {
            var foundMarkers;
            if (likeParams.id)
            {
               for (i=0; i<this.ecMarkers.length; i++)
               {
                  if (this.ecMarkers[i].ecMarkerId == likeParams.id) {foundMarkers = this.ecMarkers[i]; }
               }
            }
            if (likeParams.iconSrc)
            {
               foundMarkers = [];
               for (i=0; i<this.ecMarkers.length; i++)
               {
                  if (this.ecMarkers[i].marker.icon == likeParams.iconSrc) {foundMarkers.push(this.ecMarkers[i]);}
               }
            }
            if (likeParams.groupId)
            {
               foundMarkers = [];
               for (i=0; i<this.ecMarkers.length; i++)
               {
                  if (this.ecMarkers[i].marker.ecMarkerGroupId == likeParams.groupId) {foundMarkers.push(this.ecMarkers[i]);}
               }
            }
            return foundMarkers;
         };
         /**
          * show all markers on the google map matching the likeParams, if no lieParams are
          * specified all markers are shown.
          *
          * @param likeParams // valid values are
          * {
          *    id: 1 // a unique persistent ec marker id
          *    iconSrc: 'url of icon' // a url of the icon src, all icons with this iconSrc are returned.
          *    groupId: 1 // a unique persistent ec markerGroup id.
          * }
          */
         this.showMarkers = function(likeParams) {
//            this.hideMarkers();
            var markersToShow;
            if (likeParams)
            {
               markersToShow = this.getMarker(likeParams);
            }
            else
            {
               markersToShow = this.ecMarkers;
            }
            if (markersToShow instanceof ec.google.ECMarker)
            {
               markersToShow.marker.setMap(this.googleMap);
            }
            else
            {
               for (i=0; i<markersToShow.length; i++)
               {
                  markersToShow[i].marker.setMap(this.googleMap);
               }
            }
         };
         /**
          * @param markerToRemove remove this marker from the map.
          */
         this.removeMarker = function(markerToRemove) {
            for (i=0; i<this.ecMarkers.length; i++)
            {
               if (this.ecMarkers[i].ecMarkerId == markerToRemove.ecMarkerId) {
                  this.ecMarkers.splice(i,1);
               }
            }
         };
         /**
          * hide all markers on the google map matching the likeParams if no likeParams are
          * specified all markers are hidden.
          *
          * @param likeParams // valid values are
          * {
          *    id: 1 // a unique persistent ec marker id
          *    iconSrc: 'url of icon' // a url of the icon src, all icons with this iconSrc are returned.
          *    groupId: 1 // a unique persistent ec markerGroup id.
          * }
          */
         this.hideMarkers = function(likeParams) {
            var markersToHide;
            if (likeParams)
            {
               markersToHide = this.getMarker(likeParams);
            }
            else
            {
               markersToHide = this.ecMarkers;
            }
            if (markersToHide instanceof ec.google.ECMarker)
            {
               markersToHide.marker.setMap(null);
            }
            else
            {
               for (i=0; i<markersToHide.length; i++)
               {
                  markersToHide[i].marker.setMap(null);
               }
            }
            ec.google.params.infoWindow.close();
         };
      }

   };
   ec.google = _google;
})(jQuery);


