;
(function($) {
    
    $(document).ready(function() {
      
      if($('#map_canvas').length) {
          
      // ~ centras Lietuvos, kazkur apie kedainius 
        var latitude = '55.185768';
        var longitude = '23.633716';

        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()]);
            });
        }
        
        /* spausdiname zymeklius */
        $.each(markers, function () {

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

            var new_marker = new google.maps.Marker({
                position: coords,
                map: myMap,
				icon: image
            });

            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.setZoom(15);
                myMap.setCenter(new_marker.getPosition());
            });
        });
        
        // ------------- Pakeitimai ---------------------------------------------------
        $('#select-coord').change(function(){
            var Coord = $('option:selected', this).attr('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'+$('option:selected', this).val()).html();
            
            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.setZoom(15);
            myMap.setCenter(zoomingTo.getPosition());
        });
        // ----------------------------------------------------------------------------
        
      }


    });

})(jQuery);