var styles = [
{
    stylers: [
      { hue: "#f1efe9" },
      { saturation: 0 }
    ]
}, {
    featureType: "road",
    elementType: "geometry",
    stylers: [
      { lightness: 100 },
      { visibility: "simplified" }
    ]
}, {
    featureType: "road",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
}, {
    featureType: "water",
    elementType: "geometry",
    stylers: [
        { color: "#a3bdc2" }
    ]
}, {
    featureType: "landscape.natural.terrain",
    stylers: [
      { color: "#d2e4c8" }
    ]
}, {
    featureType: "landscape.natural.landcover",
    stylers: [
      { color: "#d2e4c8" }
    ]
}, {
    featureType: "poi.park",
    stylers: [
      { color: "#d2e4c8" }
    ]
}

];
var bounds = null;
var neighborhoods = [];
var markers = [];
var map;
var iconBase = $('#map').data('marker');
var occupationAreaText = $('#map').data('occupatedareatext');
var hideOccupatedArea = $('#map').data('hideoccupatedarea');
var openedInfo;

function initMap() {
    if ($('#map').length < 1) return;

    var latitude = parseFloat($('#map').data('lat'));
    var longitude = parseFloat($('#map').data('lng'));

    var myLatLng = { lat: latitude, lng: longitude };

    map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: latitude, lng: longitude },
        zoom: 10,
        styles: styles
    });

    for (var i = 0; i < neighborhoods.length; i++) {
        var markerLoc = { lat: neighborhoods[i].lat, lng: neighborhoods[i].lng };
        
        var contentString = createContentString(neighborhoods[i].name, neighborhoods[i].address, neighborhoods[i].tel, neighborhoods[i].fax);
        var marker = new google.maps.Marker({
            position: markerLoc,
            map: map,
            icon: iconBase,
            animation: google.maps.Animation.DROP
        });
        markers.push(marker);
        var infowindow = new google.maps.InfoWindow();
        google.maps.event.addListener(marker, 'click', (function (marker, contentString, infowindow) {
            return function () {
                if (openedInfo) {
                    openedInfo.close();
                }
                infowindow.setContent(contentString);
                infowindow.open(map, marker);
                openedInfo = infowindow;
            };
        })(marker, contentString, infowindow));
    }

    if (JSON.parse(hideOccupatedArea.toLowerCase())) {
        OccupiedMap(map, occupationAreaText);
    }
}

function OccupiedMap(map, occupationAreaText) {

    var points =
        [
            new google.maps.LatLng(35.18377, 32.701149),
            new google.maps.LatLng(35.148126, 32.704754),
            new google.maps.LatLng(35.125665, 32.753506),
            new google.maps.LatLng(35.118083, 32.789726),
            new google.maps.LatLng(35.083534, 32.818909),
            new google.maps.LatLng(35.081427, 32.835217),
            new google.maps.LatLng(35.081848, 32.868347),
            new google.maps.LatLng(35.101934, 32.871265),
            new google.maps.LatLng(35.109939, 32.896328),
            new google.maps.LatLng(35.101653, 32.915726),
            new google.maps.LatLng(35.103338, 32.950058),
            new google.maps.LatLng(35.143634, 33.014088),
            new google.maps.LatLng(35.169318, 33.065414),
            new google.maps.LatLng(35.155565, 33.101635),
            new google.maps.LatLng(35.156828, 33.112965),
            new google.maps.LatLng(35.196537, 33.163776),
            new google.maps.LatLng(35.177737, 33.235016),
            new google.maps.LatLng(35.166231, 33.267803),
            new google.maps.LatLng(35.167213, 33.309517),
            new google.maps.LatLng(35.17465, 33.319302),
            new google.maps.LatLng(35.181806, 33.322906),
            new google.maps.LatLng(35.182227, 33.336983),
            new google.maps.LatLng(35.181385, 33.347454),
            new google.maps.LatLng(35.180473, 33.350759),
            new google.maps.LatLng(35.177877, 33.351231),
            new google.maps.LatLng(35.177597, 33.357453),
            new google.maps.LatLng(35.174124, 33.35814),
            new google.maps.LatLng(35.174054, 33.364363),
            new google.maps.LatLng(35.176615, 33.370886),
            new google.maps.LatLng(35.178474, 33.373332),
            new google.maps.LatLng(35.179782, 33.371966),
            new google.maps.LatLng(35.182490, 33.372162),
            new google.maps.LatLng(35.182490, 33.372162),
            new google.maps.LatLng(35.186950, 33.372120),
            new google.maps.LatLng(35.187775, 33.373014),
            new google.maps.LatLng(35.190546, 33.373883),
            new google.maps.LatLng(35.191136, 33.377066),
            new google.maps.LatLng(35.194082, 33.378096),
            new google.maps.LatLng(35.20071, 33.387151),
            new google.maps.LatLng(35.199903, 33.405261),
            new google.maps.LatLng(35.197203, 33.408093),
            new google.maps.LatLng(35.191486, 33.406076),
            new google.maps.LatLng(35.172791, 33.419294),
            new google.maps.LatLng(35.167003, 33.415861),
            new google.maps.LatLng(35.161319, 33.414402),
            new google.maps.LatLng(35.160337, 33.419123),
            new google.maps.LatLng(35.158863, 33.421612),
            new google.maps.LatLng(35.156688, 33.426504),
            new google.maps.LatLng(35.149389, 33.428993),
            new google.maps.LatLng(35.144407, 33.430109),
            new google.maps.LatLng(35.139844, 33.435602),
            new google.maps.LatLng(35.135352, 33.43749),
            new google.maps.LatLng(35.13107, 33.443069),
            new google.maps.LatLng(35.126227, 33.442554),
            new google.maps.LatLng(35.124542, 33.443413),
            new google.maps.LatLng(35.125314, 33.446159),
            new google.maps.LatLng(35.118715, 33.454571),
            new google.maps.LatLng(35.100108, 33.465815),
            new google.maps.LatLng(35.098704, 33.467703),
            new google.maps.LatLng(35.096738, 33.467016),
            new google.maps.LatLng(35.077002, 33.475599),
            new google.maps.LatLng(35.062109, 33.479548),
            new google.maps.LatLng(35.051922, 33.47826),
            new google.maps.LatLng(35.040468, 33.472853),
            new google.maps.LatLng(35.036813, 33.471394),
            new google.maps.LatLng(35.030769, 33.464527),
            new google.maps.LatLng(35.024163, 33.457232),
            new google.maps.LatLng(35.022898, 33.451309),
            new google.maps.LatLng(35.019945, 33.45294),
            new google.maps.LatLng(35.015798, 33.451481),
            new google.maps.LatLng(35.007081, 33.448563),
            new google.maps.LatLng(35.003636, 33.452339),
            new google.maps.LatLng(35.003285, 33.459806),
            new google.maps.LatLng(35.005886, 33.464355),
            new google.maps.LatLng(35.006237, 33.475342),
            new google.maps.LatLng(35.01397, 33.476543),
            new google.maps.LatLng(35.045879, 33.486671),
            new google.maps.LatLng(35.055997, 33.484955),
            new google.maps.LatLng(35.05965, 33.484612),
            new google.maps.LatLng(35.060774, 33.487015),
            new google.maps.LatLng(35.065411, 33.489418),
            new google.maps.LatLng(35.068783, 33.496284),
            new google.maps.LatLng(35.068643, 33.506069),
            new google.maps.LatLng(35.072436, 33.519802),
            new google.maps.LatLng(35.079179, 33.527184),
            new google.maps.LatLng(35.080022, 33.5392),
            new google.maps.LatLng(35.072998, 33.543835),
            new google.maps.LatLng(35.060774, 33.566322),
            new google.maps.LatLng(35.049111, 33.566837),
            new google.maps.LatLng(35.044895, 33.577137),
            new google.maps.LatLng(35.038008, 33.583488),
            new google.maps.LatLng(35.037587, 33.591557),
            new google.maps.LatLng(35.047987, 33.600998),
            new google.maps.LatLng(35.050797, 33.611813),
            new google.maps.LatLng(35.047565, 33.622971),
            new google.maps.LatLng(35.037727, 33.637562),
            new google.maps.LatLng(35.037024, 33.64254),
            new google.maps.LatLng(35.039414, 33.652668),
            new google.maps.LatLng(35.037165, 33.660049),
            new google.maps.LatLng(35.035759, 33.660908),
            new google.maps.LatLng(35.035759, 33.662624),
            new google.maps.LatLng(35.039695, 33.680649),
            new google.maps.LatLng(35.047987, 33.674126),
            new google.maps.LatLng(35.062039, 33.676872),
            new google.maps.LatLng(35.066535, 33.692322),
            new google.maps.LatLng(35.076089, 33.704338),
            new google.maps.LatLng(35.070469, 33.715668),
            new google.maps.LatLng(35.040679, 33.704681),
            new google.maps.LatLng(35.038711, 33.710861),
            new google.maps.LatLng(35.041241, 33.711891),
            new google.maps.LatLng(35.055575, 33.737297),
            new google.maps.LatLng(35.040398, 33.763046),
            new google.maps.LatLng(35.04883, 33.774033),
            new google.maps.LatLng(35.046019, 33.781586),
            new google.maps.LatLng(35.048549, 33.790169),
            new google.maps.LatLng(35.073279, 33.819695),
            new google.maps.LatLng(35.067097, 33.830338),
            new google.maps.LatLng(35.061758, 33.840637),
            new google.maps.LatLng(35.082832, 33.871536),
            new google.maps.LatLng(35.069345, 33.891792),
            new google.maps.LatLng(35.07637, 33.900719),
            new google.maps.LatLng(35.067378, 33.923378),
            new google.maps.LatLng(35.072155, 33.96183),
            new google.maps.LatLng(35.068221, 33.978996),
            new google.maps.LatLng(35.069345, 33.997192),
            new google.maps.LatLng(35.113169, 33.955994),
            new google.maps.LatLng(35.158653, 33.918915),
            new google.maps.LatLng(35.176053, 33.909302),
            new google.maps.LatLng(35.222063, 33.903122),
            new google.maps.LatLng(35.282061, 33.925781),
            new google.maps.LatLng(35.32465, 33.973846),
            new google.maps.LatLng(35.32633, 33.997879),
            new google.maps.LatLng(35.321288, 34.008179),
            new google.maps.LatLng(35.32577, 34.044571),
            new google.maps.LatLng(35.328011, 34.064484),
            new google.maps.LatLng(35.342015, 34.07341),
            new google.maps.LatLng(35.368335, 34.080963),
            new google.maps.LatLng(35.417594, 34.136581),
            new google.maps.LatLng(35.464585, 34.234085),
            new google.maps.LatLng(35.489747, 34.295197),
            new google.maps.LatLng(35.541166, 34.378281),
            new google.maps.LatLng(35.557368, 34.410553),
            new google.maps.LatLng(35.602602, 34.438705),
            new google.maps.LatLng(35.633861, 34.520416),
            new google.maps.LatLng(35.643905, 34.562988),
            new google.maps.LatLng(35.695225, 34.585648),
            new google.maps.LatLng(35.687976, 34.544449),
            new google.maps.LatLng(35.634977, 34.382401),
            new google.maps.LatLng(35.601486, 34.330902),
            new google.maps.LatLng(35.576358, 34.284897),
            new google.maps.LatLng(35.562395, 34.182587),
            new google.maps.LatLng(35.559602, 34.1716),
            new google.maps.LatLng(35.506518, 34.124222),
            new google.maps.LatLng(35.480802, 34.050751),
            new google.maps.LatLng(35.428785, 33.899002),
            new google.maps.LatLng(35.415355, 33.831711),
            new google.maps.LatLng(35.411998, 33.76442),
            new google.maps.LatLng(35.366656, 33.669662),
            new google.maps.LatLng(35.339214, 33.500061),
            new google.maps.LatLng(35.344815, 33.296814),
            new google.maps.LatLng(35.347616, 33.248749),
            new google.maps.LatLng(35.359376, 33.194504),
            new google.maps.LatLng(35.369455, 33.10936),
            new google.maps.LatLng(35.356016, 33.093567),
            new google.maps.LatLng(35.367216, 33.030396),
            new google.maps.LatLng(35.368335, 32.994003),
            new google.maps.LatLng(35.404722, 32.921219),
            new google.maps.LatLng(35.349296, 32.927399),
            new google.maps.LatLng(35.347616, 32.931519),
            new google.maps.LatLng(35.302237, 32.939758),
            new google.maps.LatLng(35.232159, 32.923965),
            new google.maps.LatLng(35.159775, 32.86972),
            new google.maps.LatLng(35.142371, 32.829208),
            new google.maps.LatLng(35.160337, 32.783203),
            new google.maps.LatLng(35.179982, 32.737885),
            new google.maps.LatLng(35.184472, 32.706299),
            new google.maps.LatLng(35.18377, 32.701149)
        ];

    var occupied = new google.maps.Polygon({
        paths: points,
        strokeColor: "#eeeeee",
        strokeOpacity: 0.8,
        strokeWeight: 1,
        fillColor: "#f8f8f8",
        fillOpacity: 1,
        zIndex: 999
    });

    occupied.setMap(map);

    var marker = new google.maps.Marker({
        label: occupationAreaText,
        position: new google.maps.LatLng(35.25377, 33.40),
        map: map,
        icon: " ",
        fontSize: 10,
        align: 'center',
        zIndex: 1
    });
}

function drop() {
    clearMarkers();
    for (var i = 0; i < neighborhoods.length; i++) {
        addMarkerWithTimeout(neighborhoods[i], i * 200);
    }
}

function addMarkerWithTimeout(position, timeout) {
    window.setTimeout(function () {
        markers.push(new google.maps.Marker({
            position: position,
            map: map,
            icon: iconBase,
            animation: google.maps.Animation.DROP
        }));
    }, timeout);
}

function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
    }
    markers = [];
}

function createContentString(name, address, tel, fax)
{
    return "<div><span style='color: #005a73; font-weight: bold; font-size: 16px;'>" + name + "</span><br/><span>" + address + "</span><br/><br/><span>" + tel + "</span><br/><span>" + fax + "</span></div>";
}

$(document).ready(function () {
    $('.branchCategory').on('change', function () {
        var filter = getBranchFilterObject();
        filterBranchs(filter);
    });

    $('#submitButton').on('click', function (event) {
        event.preventDefault();
        var filter = getBranchFilterObject();
        filterBranchs(filter);
    });
    $('.branchLink').on('click', function () {
        var region = $(this).data('value');
        var selectedRegion = $('#selectedRegion').data('value');
        if (region !== selectedRegion) {
            var filter = {};
            filter.IsAtm = true;
            filter.IsBNA = true;
            filter.IsStore = true;
            filter.IsCoinMachine = true;

            filter.Region = region;
            filter.SearchString = "";
            filter.Country = $('#countryBranch').data('value');
            filter.Language = $('#languageBranch').data('value');
            filterBranchs(filter);

            $('#stores').prop('checked', true);
            $('#atm').prop('checked', true);
            $('#atm_bna').prop('checked', true);
            $('#coins').prop('checked', true);
        }
    });
    $('ul.branchList').on('click', 'li', function () {
        var $coordinates = $(this).find('.result-item');
        var latitude = $coordinates.data('lat');
        var longitude = $coordinates.data('long');
        var address = $coordinates.data('address');
        var name = $coordinates.data('name');
        var tel = $coordinates.data('tel');
        var fax = $coordinates.data('fax');
        var contentString = createContentString(name, address, tel, fax);

        if (openedInfo){
            openedInfo.close();
        }

        var infowindow = new google.maps.InfoWindow();
        infowindow.setContent(contentString);
        var marker = markers[$(this).index()];
        infowindow.open(map, marker);
        openedInfo = infowindow;

        map.panTo({ lat: latitude, lng: longitude });
        map.setZoom(17);
    });

    $('#storeSerach').keyup(function () {
        delay(inputStoreSearchFiltrating, 300);
    });

    var inputStoreSearchFiltrating = function () {
        if (/\S/.test(this.value) || this.value === "") {
            var filter = getBranchFilterObject();
            filterBranchs(filter);
        } else {
            var isStore = $('#stores').is(':checked');
            var isAtm = $('#atm').is(':checked');
            var isAtmBna = $('#atm_bna').is(':checked');
            var isCoinMachine = $('#coins').is(':checked');

            if (!isStore && !isAtm && !isAtmBna && !isCoinMachine) {
                clearMap();
            }
        }
    };
    var filterBranchs = function (filter) {
        $.get("/ajax/StoresPage/GetFilteredStores", filter, function (data) {
            showResults(data);
        });
    };

    var mapInitialization = function () {
        var selectedRegion = $('#selectedRegion').data('value');

        var filter = {};
        filter.IsAtm = true;
        filter.IsBNA = true;
        filter.IsStore = true;
        filter.IsCoinMachine = true;

        filter.Region = selectedRegion;
        filter.SearchString = "";
        filter.Country = $('#countryBranch').data('value');
        filter.Language = $('#languageBranch').data('value');
        filterBranchs(filter);

        $('#stores').prop('checked', true);
        $('#atm').prop('checked', true);
        $('#atm_bna').prop('checked', true);
        $('#coins').prop('checked', true);
    }();
    var showResults = function (data) {
        $resultText = $('#resultsText');
        $resultList = $('#resultsList');
        $resultList.empty();
        $resultText.empty();
        neighborhoods = [];
        markers = [];

        $resultList.append(data);

        var $dadataCoordinates = $('.result-item');
        $resultText.text($dadataCoordinates.length);

        $('#selectedRegion').data('value');
        $.each($dadataCoordinates, function (key, value) {
            var branchLocation = { lat: $(this).data('lat'), lng: $(this).data('long'), name: $(this).data('name'), address: $(this).data('address'), tel: $(this).data('tel'), fax: $(this).data('fax') };
            neighborhoods.push(branchLocation);
        });

        initMap();
    };

    var getBranchFilterObject = function () {
        var branchFilter = {};

        var isStore = $('#stores').is(':checked');
        var isAtm = $('#atm').is(':checked');
        var isAtmBna = $('#atm_bna').is(':checked');
        var isCoinMachine = $('#coins').is(':checked');
        var searchString = $('#storeSerach').val();
        var region = $('#selectedRegion').data('value');

        branchFilter.IsAtm = isAtm;
        branchFilter.IsBNA = isAtmBna;
        branchFilter.IsStore = isStore;
        branchFilter.IsCoinMachine = isCoinMachine;

        branchFilter.Region = region;
        branchFilter.SearchString = $.trim(searchString);
        branchFilter.Country = $('#countryBranch').data('value');
        branchFilter.Language = $('#languageBranch').data('value');

        return branchFilter;
    };

    var clearMap = function () {
        $resultText = $('#resultsText');
        $resultList = $('#resultsList');
        if ($resultText.text() > 0) {
            $resultText.empty();
            $resultList.empty();
            neighborhoods = [];

            $resultText.text(0);
            initMap();
        }
    };

    var delay = (function () {
        var timer = 0;
        return function (callback, ms) {
            clearTimeout(timer);
            timer = setTimeout(callback, ms);
        };
    })();
});