
	/* gmaps */

		var geocoder;
  		var map;
	
  		function initialize(x,y,z, typ) {

  			geocoder = new google.maps.Geocoder();	
    		var myLatlng = new google.maps.LatLng(x,y);
    		var myOptions = {
    			zoom: z,
    			disableDefaultUI: true,
  				navigationControl: true,
  				mapTypeControl: true,
  				scaleControl: false, 
  				navigationControlOptions: {
    				//style: google.maps.NavigationControlStyle.SMALL
  				},		
    			center: myLatlng,
    			mapTypeId: typ
    		}
    		map = new google.maps.Map(document.getElementById("mapa"), myOptions);
    		google.maps.event.addListener(map, 'dragend', function(event) {
				$.cookie('fruitmap_map', map.getCenter(), { expires: 365, path: '/' });
				$.cookie('fruitmap_maptype', map.getMapTypeId(), { expires: 365, path: '/' });				
  			});    		
			google.maps.event.addListener(map, 'zoom_changed', function(event) {
				$.cookie('fruitmap_zoom', map.getZoom(), { expires: 365, path: '/' });
				$.cookie('fruitmap_map', map.getCenter(), { expires: 365, path: '/' });
				$.cookie('fruitmap_maptype', map.getMapTypeId(), { expires: 365, path: '/' });				
  			});    		
    		
		}

		function newMarker(x,y,type,slovom) {

    		marker = new google.maps.Marker({
    			position: new google.maps.LatLng(x,y), 
    			icon:fruit[type],
    			title:slovom,
    			map: map
    		});
		}
		function newAdminMarker(x,y,type,slovom,id,user) {

			marker[id] = new google.maps.Marker({
				position: new google.maps.LatLng(x,y), 
				icon:fruit[type],
				title:slovom+' id:'+id+' user:'+user,
				map: map
			});
			google.maps.event.addListener(marker[id], 'click', function(e) {
				//if (e.ctrlKey) {
				if (isCtrl===true) { 
					if (confirm('Naozaj chcete zmazať tento strom ('+slovom+')?')) {
						$.post('index.php?ajax=fruit/remove',{id:id},function(response){
							if (response.status == 'Ok') {
								marker[id].setMap(null);	
							}
							else {
								alert(response.status);
							}
						},"json");
					}
				}
			});				
		}		
		

		function codeAddress() {
    	var address = document.getElementById("search").value;
    	$('.ac_results').hide();
    	if (geocoder) {
    	  geocoder.geocode( { 'address': address}, function(results, status) {
    	    if (status == google.maps.GeocoderStatus.OK) {
    	    	x = results[0].geometry.location.lat();
    	    	y = results[0].geometry.location.lng();
    	    	map.setCenter(new google.maps.LatLng(x,y));
    	    	map.setZoom(16);
    	    	$('#searchstring').html('<a href="http://fruitmap.sk?q='+address+'">http://fruitmap.sk?q='+address+'</a>');

				$.cookie('fruitmap_zoom', map.getZoom(), { expires: 365, path: '/' });
				$.cookie('fruitmap_map', map.getCenter(), { expires: 365, path: '/' });
				$.cookie('fruitmap_maptype', map.getMapTypeId(), { expires: 365, path: '/' });				
    	    	
    	    } else {
    	      //alert("Geocode was not successful for the following reason: " + status);
    	    }
    	  });
    	}
  		}
  	
  	/* autocomplete */
  	
		$(function(){
			
			$("#search").autocomplete('index.php', {
					width:290, multiple: false, matchContains: true, extraParams : { 
					ajax:'search/autocomplete'
				},
				formatItem: function(item) {
					return item[0];
				}
			}).result(function(event, item) 
			{
				codeAddress(item[0]);
				
				var q = item[0].replace('script');
				
				$('#search').val('');
			});

	        $("#search").bind("keypress", function(e) {

	             if (e.keyCode == 13) {
	                codeAddress();
	             	return false;
	            }

	         });
	         function setCluster()
	         {
	         	var ch = $('#setCluster input').attr('checked');
	         	if (ch == false) {
	         		$.cookie('fruitmap_clusterer', '0');
         		}
	         	else 
	         	{
	         		$.cookie('fruitmap_clusterer', '1');
	         	}
				window.location.reload();
	         }
	         $('#setCluster span').click(function(){
	         	var ch = $('#setCluster input').attr('checked');
	         	if (ch == false) {
	         		$('#setCluster input').attr('checked',true);
         		}
	         	else 
	         	{
	         		$('#setCluster input').attr('checked',false);
	         	}
	         	setCluster(ch);
	         })
	         $('#setCluster input').change(function(){
	         	setCluster($(this).attr('checked'));
	         })
	         
	         
			
		})

