;
(function($) {

    $(document).ready(function() {

      if($('#map_canvas').length) {

      // ~ centras Lietuvos, kazkur apie kedainius
//        var latitude = '55.185768';
//        var longitude = '23.633716';
        var bounds = null;

        var myMapOptions = {
           // center: new google.maps.LatLng(latitude, longitude),
           // zoom: 7,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var myMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
        var image = '/mysite/img/pin.png';
        var markers = [];
        var info_window = '';
        // eval('$coords');
        if(coords) {
            $.each(coords, function() {
                markers.push([new google.maps.LatLng(this.RecLat,this.RecLong), $("#info-window"+this.RecID).html()]);
            });
        }
        printMarkers(markers);

        // by cities
        $('#city-list li').on('click', function(e) {
            e.preventDefault();
            var thisid = $(this).data('id');
            $('#city-list li').removeClass('act');
            $(this).addClass('act');
            if(thisid && thisid > 0) {

                var Coord = $(this).data('coord');
                if( Coord == 'undefined' ) return false;

                var matches = Coord.match(/([0-9.-]+).+?([0-9.-]+)/);
                var Lat  = parseFloat(matches[1]);
                var Long = parseFloat(matches[2]);
                var html = $('#info-window'+$(this).data('pointid')).html();

                var points = $(this).data('points');

                // more than one point
                if(points>1 && $(this).find('.hidden-coords').length>0) {
					markers = [];
                    var mmarkers = [];
                    $(this).find('.hidden-coords > span').each(function(){
                        var mcoords = $(this).data('coord');
                        var mmatches = mcoords.match(/([0-9.-]+).+?([0-9.-]+)/);
                        var mLat  = parseFloat(mmatches[1]);
                        var mLong = parseFloat(mmatches[2]);
                        var mhtml = $('#info-window'+$(this).data('id')).html();
                        mmarkers.push([new google.maps.LatLng(mLat,mLong), mhtml]);

                    });
                    printMarkers(mmarkers);

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

//                    info_window = new google.maps.InfoWindow({
//                        content: html
//                    });

                } else { // only one point

					markers = [];
                    bounds = new google.maps.LatLngBounds();
                    var zoomingTo = new google.maps.Marker({
                        position: new google.maps.LatLng(Lat, Long)
                        ,map: myMap
                        ,icon: image
                    });

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

                    info_window = new google.maps.InfoWindow({
                        content: html
                    });

                    info_window.open(myMap, zoomingTo);
					myMap.fitBounds(bounds);
                    myMap.setZoom(15);
                    myMap.setCenter(zoomingTo.getPosition());

                }

            } else {
                // rodom visa map
				markers = [];

                if(coords) {
                    $.each(coords, function() {
                        markers.push([new google.maps.LatLng(this.RecLat,this.RecLong), $("#info-window"+this.RecID).html()]);
                    });
                }
                printMarkers(markers);
                if(info_window){
                    info_window.close();
                }
            }

            return false;
        });

      }


        function printMarkers(markersAll) {

            bounds = new google.maps.LatLngBounds();
            /* spausdiname zymeklius */
            $.each(markersAll, function () {

                var coords2 = this[0];
                var html   = this[1];

                var new_marker = new google.maps.Marker({
                    position: coords2,
                    map: myMap,
                    icon: image
                });
                bounds.extend(new_marker.position);
				myMap.fitBounds(bounds);
                google.maps.event.addListener(new_marker, 'click', function() {

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

                    info_window = new google.maps.InfoWindow({
                        content: html
                    });

                    info_window.open(myMap, new_marker);
					myMap.setCenter(new_marker.getPosition());
                    myMap.fitBounds(bounds);
                    //myMap.setZoom(15);
                    if(myMap.getZoom() > 15) myMap.setZoom(15);
                    else myMap.setZoom(14);
                });
            });
            //myMap.fitBounds(bounds);
        }
    });


})(jQuery);
