/// zakladni nastaveni
var myOptions = {
       mapTypeControl: true,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
      navigationControl: true,
      navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      scrollwheel: false 
     }




function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}

var markerArray = [];
 function openMarker(i){
   google.maps.event.trigger(markerArray[i],'click');
   /// self.scrollTo(0,10000);
   ScrollToElement(document.getElementById('map'));   
 };


    
/// icons
	var shadow = new google.maps.MarkerImage('images/ico/an_cil_s.png',
	new google.maps.Size(28, 20),
	new google.maps.Point(0,0),
	new google.maps.Point(12, 20));
		   
	var image = new google.maps.MarkerImage('images/ico/an_cil.png',
	new google.maps.Size(25, 20),
	new google.maps.Point(0,0),
	new google.maps.Point(12,20));


////////////////////////////////////////////////////////////////////////////
//// ukaz body, jejichž definice je v poli locations


function showMarkers(locations, map) {
	
	var delka = locations.length;
	var latlngbounds = new google.maps.LatLngBounds();
	var html = [];
	
	for (i=0; i < delka; i++) {
								if (locations[i][1]!="0" && locations[i][2]!="0") {

   							myLatLng = new google.maps.LatLng(locations[i][1],locations[i][2]);
   							latlngbounds.extend(myLatLng);
								
								html[i] = locations[i][3];
								html[i] = html[i].replace(/&lt;/g, "<");
								html[i] = html[i].replace(/&gt;/g, ">");
								
								
   							marker = new google.maps.Marker({
        					position: myLatLng,
        					map: map,
        					shadow: shadow,
        					icon: image,
        					title: locations[i][0]
									});

        					var infowindow = new google.maps.InfoWindow();

									google.maps.event.addListener(marker, 'click', (function(marker, i) {
									        return function() {
									          infowindow.setContent(html[i]);
									          infowindow.open(map, marker);
									        }
									      })(marker, i));
									
									
							
						}
				markerArray.push(marker);
				}					
				
				/// podle poctu polozek nastav stred
				if (delka > 1) {
				  map.fitBounds(latlngbounds);
				}
				else if (delka == 1) {
				  map.setCenter(latlngbounds.getCenter());
				  map.setZoom(8);
				}
				else {
					map.setCenter(new google.maps.LatLng (50.025, 15.203));
					map.setZoom(5);
					}
				
	
  }

////////////////////////////////////////////////////////////////////////////  
//// nový bod zapisuje souøadnice do poli s id lat a lng  

function NewMarker(latlng, name) {
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
        });
    
    document.getElementById('lat').value = latlng.lat();
  	document.getElementById('lng').value = latlng.lng();

		/// alert(document.getElementById('sekce').value);
      
    return marker;
}


//// 
function NewPlace() {
var marker = null;
	google.maps.event.addListener(map, 'click', function(event) {
	         if (marker) {
            marker.setMap(null);
            marker = null;
         }
	marker = NewMarker(event.latLng, "new");
  });

}

////////////////////////////////////////////////////////////////////////////  
//// geocode


function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        /// var marker = new google.maps.Marker({
            // map: map, 
            // position: results[0].geometry.location
        /// });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

