var map;
var marker = new Array();
var prenajmy = new Array();
var podnajmy = new Array();
var privaty = new Array();
var image = 'img/map/prenajmy.png';
var imageH = 'img/map/prenajmy_hover.png';
var children;
var bounds;
var i_index;
var infowindow = new Array();
var ib = new Array();
var children=1;

function initialize() {
    var latlng = new google.maps.LatLng(48.1484, 17.1076);    
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  
    //nacitanie bodov pri inicializacii defaultne prenajmy
    placeMarker(prenajmy);       
  
    google.maps.event.addListener(map, 'click', function() {
    	if (infowindow[i_index]) {infowindow[i_index].close();}
    });
    
	//nacitanie novych bodov pri pohybe mapy
	google.maps.event.addListener(map, 'dragend', function() {
    	if (infowindow[i_index]) {infowindow[i_index].close();}
		center = map.getCenter();
    	swCorner = new google.maps.LatLng(center.lat().toFixed(4)-1,center.lng().toFixed(4)+1);
    	neCorner = new google.maps.LatLng(center.lat().toFixed(4)+1,center.lng().toFixed(4)-1);

    	bounds = map.getBounds();
    	southWest = bounds.getSouthWest();
		northEast = bounds.getNorthEast();
		
    	var swlnglat= southWest.lat().toFixed(4);
		var swlnglon= southWest.lng().toFixed(4);
		var nelnglat= northEast.lat().toFixed(4);
		var nelnglon= northEast.lng().toFixed(4);    	
		children= 1;
		i_index= undefined;		
		
		Zobraz_Inzeraty(swlnglat,swlnglon,nelnglat,nelnglon);
  });   
}
  
//vytvorenie bodov na mape s pola
function placeMarker(locations) {
	
	pocet= Math.round(locations.length/5);
	
	for (var i = 0; i < pocet; i++) {	
   		var myLatLng = new google.maps.LatLng(locations[(i*5)+1], locations[(i*5)+2]);    
   	
    	marker[i] = new google.maps.Marker({
        	position: myLatLng,
        	map: map,
        	icon: image,
        	title: locations[i*5]
    	});
    
    	zobrazBublinu(map,i,marker[i],locations[(i*5)+3],locations[(i*5)+4]); 
  	}
}

//informacne okno po kliknuti na ikonku na mape
function zobrazBublinu(map, index, marker, number, message) {
	okno_messeage= '<div id="mapWindow"><div id="next_prev"><a href="javascript: ukaz_inzerat('+number+',0);">Predchádzajúci</a> <a href="javascript: ukaz_inzerat('+number+',1);">Ďalší</a></div>'+message+'</div>';

	infowindow[index] = new google.maps.InfoWindow(
      { content: okno_messeage,
        size: new google.maps.Size(500,500)
      });

  google.maps.event.addListener(marker, 'mouseover', function() {
    marker.setIcon(imageH);
  });

  google.maps.event.addListener(marker, 'mouseout', function() {
    marker.setIcon(image);
  });   
  
  google.maps.event.addListener(marker, 'click', function() {
 	if (infowindow[i_index]!=undefined) {infowindow[i_index].close();}
  	infowindow[index].open(map,marker);
    children=1;
    i_index= index;
    hide_inzerat(number);
  });
}

function Zobraz_Inzeraty(swlat,swlon,nelat,nelon) {
	typ= $("#mapa_typ_inzeratu").val();
	
	for (var i = 0; i < marker.length; i++) {
	  marker[i].setMap(null);
	}
	
	infowindow.length= 0;
	marker.length= 0;
	//prenajmy.length= 0;
	
	m_typ= $("#mapa_typ_inzeratu").val();
	m_okres= $("#mapa_okres").val();
	m_lokalita= $("#mapa_lokalita").val();
	m_cena_od= $("#mapa_cena_od").val();
	m_cena_do= $("#mapa_cena_do").val();
	
	$.post("php/mapa_inzeraty.php", {swLat: swlat, swLon: swlon, neLat: nelat, neLon: nelon, typ: m_typ,okres: m_okres,lokalita: m_lokalita,cena_od: m_cena_od,cena_do: m_cena_do,vrat_pole: 1}, function(data){ 	
		if (data!="[ziadne]") {
	
			switch (typ) {
				case "0": podnajmy.length=0; 
					image = 'img/map/podnajmy.png'; 
					imageH = 'img/map/podnajmy_hover.png'; 
					podnajmy= data.split('*'); 
					placeMarker(podnajmy); 
					break;
				case "1": prenajmy.length=0; 
					image = 'img/map/prenajmy.png';
					imageH = 'img/map/prenajmy_hover.png';  
					prenajmy= data.split('*');
					placeMarker(prenajmy); break;
				case "2": privaty.length=0;  					
					image = 'img/map/hladat.png'; 
					imageH = 'img/map/hladat_hover.png'; 
					privaty= data.split('*'); 
					placeMarker(privaty); break;
			}
		}
	});
}

function nastav_stred_mapy() {
	$.post("php/stred_mapy.php", {okres: $("#mapa_okres option:selected").text()}, function(data){ 		
		if (data!="") {
			map.zoom= 14;
			
			for (var i = 0; i < marker.length; i++) {
	  			marker[i].setMap(null);
			}
			suradnice= data.split(',');
			suradnice[0]= Number(suradnice[0]);
			suradnice[1]= Number(suradnice[1]);			
			
			var stred = new google.maps.LatLng(suradnice[0],suradnice[1]);
  			
			map.setCenter(stred);
  			
    		bounds = map.getBounds();
    		southWest = bounds.getSouthWest();
			northEast = bounds.getNorthEast();
		
    		var swlnglat= southWest.lat().toFixed(4);
			var swlnglon= southWest.lng().toFixed(4);
			var nelnglat= northEast.lat().toFixed(4);
			var nelnglon= northEast.lng().toFixed(4); 
		
  			Zobraz_Inzeraty(swlnglat,swlnglon,nelnglat,nelnglon); 		
		}
	});
}

function hide_inzerat(inzerat_id) {	
	
	//inzerat= "#inzeraty_"+inzerat_id+" li:first";
	inzerat= "#inzeraty_"+inzerat_id+" li";
	if ($("#inzeraty_"+inzerat_id+" li:nth-child(2)").attr("title")==undefined) {$("#next_prev").hide();}
	$(inzerat).nextAll().hide();
	ukaz_inzerat(inzerat_id,1);
}

function ukaz_inzerat(inzerat_id,ktory) {
	dalsi= true;
	stary= children;
	switch (ktory) {
		case 1: children++; break;
		case 0: children--; break;
	}
	if (children<1) {children=1; dalsi= false;}
	
	if ($("#inzeraty_"+inzerat_id+" li:nth-child("+children+")").attr("title")==undefined) {dalsi= false; children--;}
	
	if (dalsi) {		
		inzerat= "#inzeraty_"+inzerat_id+" li:nth-child("+stary+")";
		$(inzerat).hide();
		
		inzerat= "#inzeraty_"+inzerat_id+" li:nth-child("+children+")";
		$(inzerat).fadeIn(2000);
	}
}

function map_lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post("php/rpc2.php", {queryString: ""+inputString+"",mesto: $("#mapa_okres").val()}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} 

function map_fill(thisid,thisName,thislat,thislon) {
	$('#map_ulica').val(thisName);
	$('#map_lat').val(thislat);
	$('#map_lon').val(thislon);
setTimeout("$('#suggestions').hide();", 200);
}

function nastav_ulicu() {

	var stred = new google.maps.LatLng($("#map_lat").val(),$("#map_lon").val());
  			
	map.zoom= 16;
	map.setCenter(stred);
  			
   	bounds = map.getBounds();
   	southWest = bounds.getSouthWest();
	northEast = bounds.getNorthEast();
		
    var swlnglat= southWest.lat().toFixed(4);
	var swlnglon= southWest.lng().toFixed(4);
	var nelnglat= northEast.lat().toFixed(4);
	var nelnglon= northEast.lng().toFixed(4); 
		
  	Zobraz_Inzeraty(swlnglat,swlnglon,nelnglat,nelnglon); 		
}
