﻿var SearchOrganization = {};

SearchOrganization.mapObject = null;
SearchOrganization.coordinates = null;

SearchOrganization.getAndShowCoordinatesForCasenumbers = function (casenumbers) {
    if (casenumbers != null && casenumbers != "") {
        var url = '../../OverzichtMap.aspx?casenumbers=' + casenumbers;
        $.get(url, function (result) {
            SearchOrganization.showMapWithCoordinates(result.coordinates);
        });
    }
};

// also used by Contact page
SearchOrganization.showMapWithCoordinates = function (coordinates) {
    this.coordinates = coordinates;
    this.minLat = 0;
    this.maxLat = 0;
    this.minLong = 0;
    this.maxLong = 0;
    this._initializeMapObject();
    var coordinate;
    this.markers = [];
    this.infoWindows = [];
    for (var i = 0; i < coordinates.length; i++) {
        coordinate = coordinates[i];
        if (coordinate.lat != 0 && coordinate.long != 0) {
            this._rememberMinAndMax(coordinate);
            this._addMarker(coordinate);
        }
    }
    this._setMapBounds();
};

SearchOrganization._initializeMapObject = function () {
    var options = {
        zoom: 4,
        center: new google.maps.LatLng(52.003646, 4.34885),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.mapObject = new google.maps.Map(document.getElementById("searchMapCanvas"), options);
};

SearchOrganization._rememberMinAndMax = function (coordinate) {
    if (this.minLat == 0) {
        this.minLat = coordinate.lat;
        this.maxLat = coordinate.lat;
        this.minLong = coordinate.long;
        this.maxLong = coordinate.long;
    }
    this.minLat = Math.min(this.minLat, coordinate.lat);
    this.maxLat = Math.max(this.maxLat, coordinate.lat);
    this.minLong = Math.min(this.minLong, coordinate.long);
    this.maxLong = Math.max(this.maxLong, coordinate.long);
};

SearchOrganization._addMarker = function (coordinate) {
    var point = new google.maps.LatLng(coordinate.lat, coordinate.long);
    var marker = new google.maps.Marker({
        map: this.mapObject,
        position: point,
        title: coordinate.title,
        icon: '/Images/green-dot.png'
    });
    var titleLink = (coordinate.detailsUrl != null && coordinate.detailsUrl.length > 0)
        ? "<strong><a href=\"" + coordinate.detailsUrl + "\">" + coordinate.title + "</a></strong>\n"
        : "<strong>" + coordinate.title + "</strong>\n";
    var infoWindow = new google.maps.InfoWindow({
        content: "<div class=\"infoWindow\">" + titleLink + "<p>" + coordinate.address + "</p>\n</div>"
    });
    this.infoWindows.push(infoWindow);
    google.maps.event.addListener(marker, 'click', function () {
        SearchOrganization._closeAllInfoWindows();
        infoWindow.open(SearchOrganization.mapObject, marker);
    });
    this.markers.push(marker);
};

SearchOrganization._closeAllInfoWindows = function () {
    for (var i = 0; i < this.infoWindows.length; i++) {
        this.infoWindows[i].close();
    }
};

SearchOrganization._setMapBounds = function () {
    this._expandMinMaxLatForOnlyOne();
    var southWest = new google.maps.LatLng(this.minLat, this.minLong);
    var northEast = new google.maps.LatLng(this.maxLat, this.maxLong);
    var bounds = new google.maps.LatLngBounds(southWest, northEast);
    this.mapObject.fitBounds(bounds);
};

// expand box if just one point
SearchOrganization._expandMinMaxLatForOnlyOne = function () {
    if (this.minLat == this.maxLat) {
        this.minLat = this.minLat - 0.03;
        this.maxLat = this.maxLat + 0.03;
    }
    if (this.minLong == this.maxLong) {
        this.minLong = this.minLong - 0.03;
        this.maxLong = this.maxLong + 0.03;
    }
};

SearchOrganization.showDienstenSelect = function () {
    $('#dienstenSelect').show();
    $('#noDienstenSelect').hide();
};

SearchOrganization.hideDienstenSelect = function () {
    $('#dienstenSelect').hide();
    $('#noDienstenSelect').show();
};

SearchOrganization.checkAlleDiensten = function (isChecked) {
    $('.dienstCheckbox').attr('checked', isChecked);
};

SearchOrganization.checkDienst = function (isChecked) {
    if (!isChecked && $('.allCheckbox').attr('checked')) {
        $('.allCheckbox').attr('checked', false);
    } else if (!$('.allCheckbox').attr('checked') && this._areAllCheckboxesChecked()) {
        $('.allCheckbox').attr('checked', true);
    }
};

SearchOrganization._areAllCheckboxesChecked = function () {
    return $('.dienstCheckbox:checked').length == 12;
};

