$(document).ready(function() { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; var alphabet_str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZaâà bcdeéèfghijklmnoôpqrstuvwxyz"; var lituanian_alphabet_str = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽaąbcčdeęėfghiįyjklmnoprsštuųūvzž"; var sort_alpha = function(alphabet, sort_order){ return function(a, b){ var pos = 0, min = Math.min(a.length, b.length); sort_order = sort_order || 1; while(a.charAt(pos) === b.charAt(pos) && pos < min){ pos++; } return alphabet.indexOf(a.charAt(pos)) > alphabet.indexOf(b.charAt(pos)) ? sort_order:-sort_order; }; }; var sort_alpha_array = function(alphabet, property, sort_order) { return function(a, b){ var pos = 0, min = Math.min(a[property].length, b[property].length); sort_order = sort_order || 1; while(a[property].charAt(pos) === b[property].charAt(pos) && pos < min){ pos++; } return alphabet.indexOf(a[property].charAt(pos)) > alphabet.indexOf(b[property].charAt(pos)) ? sort_order:-sort_order; }; }; var display_on_map = [], tmp = {}, regions = [], startsearch; cities.sort(sort_alpha_array(alphabet_str, "name")); $.each(cities, function(i, city){ display_on_map.push({ lat: city.lat, lng: city.lng, tag: city.region, data: city }); tmp[ city.region ] = true; $("#shop_choice").append(""); }); for(var r in tmp){ regions.push(r); } regions = regions.sort( sort_alpha(alphabet_str) ); for(var k in regions) { $("#region_choice").append(""); } $("#region_choice").change(function() { var current_choice = $(this).val(); reset_choice(); regen_shop(); display_on_map = []; $("#shop_choice option").each(function(){ current_option = $(this); $.each(cities, function() { if(current_option.val() != "" && current_option.val() != current_choice) { current_option.remove(); return false; } }); }); $.each(cities, function(i, city) { if(city.region == current_choice) { display_on_map.push({ lat: city.lat, lng: city.lng, tag: city.region, data: city }); } }); if(current_choice == "") { regen_shop(); } $('#gmap').gmap3({ action:'clear' }); display_map(); }); $("#shop_choice").change(function() { var id = $(this).find(':selected')[0].id; if(id == "") { reset_choice(); display_map(); } else { $.each(cities, function(i, city) { if(city.dpts == id) { $("#current_city").html("" + city.name + "

" + city.address.replace(/\n/g, "
" )); $("#id_collection_point").val(city.dpts); $('#gmap').gmap3({ action:'panTo', args:[ new google.maps.LatLng(city.lat, city.lng) ] }); $('#gmap').gmap3('get').setZoom(15); return false; } }); $(".submit-row").show(); $("#selected_city").show().css("margin", "0 auto"); $("#shop_selector").hide(); $("#gmap").hide(); } }); $(".search_unit").live("click", function() { var id = this.id; $("#" + id).attr("selected", "selected"); $.each(cities, function(i, city) { if(city.dpts == id) { $("#current_city").html("" + city.name + "

" + city.address.replace(/\n/g, "
" )); $("#id_collection_point").val(city.dpts); $('#gmap').gmap3({ action:'panTo', args:[ new google.maps.LatLng(city.lat, city.lng) ] }); $('#gmap').gmap3('get').setZoom(15); return false; } }); $(".submit-row").show(); $("#selected_city").show().css("margin", "0 auto"); $("#shop_selector").hide(); $("#gmap").hide(); }); var search_function = function() { var search_value = $("#search_field").val(); var n=0; $("#region_choice").val(""); reset_choice(); regen_shop(); display_on_map = []; $.each(cities, function(i, city) { if(city.region.toLowerCase().indexOf(search_value.toLowerCase()) != -1 || city.name.toLowerCase().indexOf(search_value.toLowerCase()) != -1) { n++; display_on_map.push({ lat: city.lat, lng: city.lng, tag: city.region, data: city }); } }); if(n==0) { $("#search_box").html(none_found); } else { if(n==1) { $("#search_box").html(n + " " + one_found); }else{ $("#search_box").html(n + " " + some_found); } $.each(display_on_map, function(i, city_found) { $("#search_box").append("
" + city_found.data.name + "
") }); $('#gmap').gmap3({ action:'clear' }); display_map(); } return false; } $("#search_button").click(function(){ search_function(); }); $("#search_field").keypress(function() { clearTimeout(startsearch); startsearch = setTimeout(function(){ search_function(); }, 1000); }); regen_shop = function() { shop_chooser = $("#shop_choice").find("option")[0]; $("#shop_choice").html(""); $("#shop_choice").append(shop_chooser); $.each(cities, function(i, city){ display_on_map.push({ lat: city.lat, lng: city.lng, tag: city.region, data: city }); $("#shop_choice").append(""); }); }; reset_choice = function() { $("#shop_choice").val("").check; $("#selected_city").hide(); $("#current_city").html(""); $("#search_box").html(""); $(".submit-row").hide(); $("#id_collection_point").val(""); }; display_map = function() { $('#gmap').gmap3( { action:'init', options:{ maxZoom: 15 } }, { action:'addMarkers', markers: display_on_map, callback:function(m) { var bounds=new google.maps.LatLngBounds(); for(var i=0;i" + data.name + "

" + data.address.replace(/\n/g, "
" )); } else { $(this).gmap3({action:'addinfowindow', anchor:marker, options:{content:"" + data.name + "

" + data.address.replace(/\n/g, "
" )}}); } }, mouseout: function(){ var infowindow = $(this).gmap3({action:'get', name:'infowindow'}); if (infowindow){ infowindow.close(); } }, click: function(marker, event, data){ $(".submit-row").show(); $("#selected_city").show().css("margin", "0 auto"); $("#gmap").hide(); $("#shop_selector").hide(); $("#current_city").html("" + data.name + "

" + data.address.replace(/\n/g, "
" )); $("#id_collection_point").val(data.dpts); $("#" + data.dpts).attr("selected", "selected"); } } } } ); }; display_map(); });