// JavaScript Document
   function getArgs(address,scope,zoom,real,orig){
	   var myargs=new Array();
	   myargs[0]=address;
	   myargs[1]=scope;
	   myargs[2]=zoom;
	   myargs[3]=real;
	   myargs[4]=orig;
	   return myargs;	
	}
			   
 	function SetMapCountry(id){
		
	   current_country=document.generic_search.s_drzava.options[(id-1)].value;
	   current_country_ln=document.generic_search.s_drzava.options[(id-1)].text;
   	   
	   current_region="";
	   current_region_ln="";
	   
	   
	   map.clearOverlays();	   	   
       my_buffer.push(getArgs(
							  current_country,
							  "country",
							  true,
							  current_country_ln,
							  current_country
							  )
					 );	   
	   
	   FillRegions(country_id=id);
	}
	
	function SetMapRegion(id){
		
	  clearRegionMarkers();
	  clearCityMarkers();
		
	  if(id > 0){
		  
	    current_region=document.generic_search.s_regija.options[(id-1)].value;
	    current_region_ln=document.generic_search.s_regija.options[(id-1)].text;
	
	    my_buffer.push(getArgs(
							   RealName(current_region),
							   "region",
							   true,
							   current_region_ln,
							   current_region
							   )
					   );
		
		FillCities(region_id=id);
	  } else {
		FillRegions(country_id);
		map.setCenter(country_index, country_zoom);
	  }
	  
	}
	
	function SetMapCity(id){
	
	  clearCityMarkers();	
		
	  if(id > 0){
	    current_city=document.generic_search.s_mjesto.options[(id)].value;
		showAddress(
					current_city, 
					"city", 
					true,
					"",
					current_city
				   );
	  } else {
		map.setCenter(region_index, region_zoom);
		FillCities(region_id);
	  }
		
		
	}
	
	function RealName(txt){
		txt=txt.replace(" i ", ",");
		txt=txt.replace("-", "");		
		return txt;
	}
	
	function FillRegions(id){
		var regionIndex=document.generic_search.s_regija.options.selectedIndex;
		if(regionIndex==0){
			
			var myreg=drz[id];
            for(n=0;n<myreg.length;n++){								  
			    my_buffer.push(getArgs(
									   RealName(myreg[n][1]),
									   "region",
									   false,
									   myreg[n][0],
									   myreg[n][1]
									   )
							   );
			}
			
		}
		
		
		startGeocode();
	}
	
	function FillCities(id){
		var cityIndex=document.generic_search.s_mjesto.options.selectedIndex;		
		if(cityIndex==0){
			var mycit=reg[id];
			for(n=0;n<mycit.length;n++){								
				var args=getArgs(
								 mycit[n][1],
								 "city",
								 false,
								 "",
								 mycit[n][1]
								 );
				
				my_buffer.push(args);
			}
	
		}
		
				
		startGeocode();
		
	}
	
	function startGeocode(){			
		if(my_buffer.length > 0){
			var mybuff=popBuffer();
			showAddress(
						mybuff[0],
						mybuff[1],
						mybuff[2],
						mybuff[3],
						mybuff[4]
					   );	
		} else return;
	}
	
	function popBuffer(){
		my_buffer.reverse();
		return my_buffer.pop();	
	}
	
	function clearRegionMarkers(){		
		for(n=0;n<region_markers.length;n++)
		map.removeOverlay(region_markers[n]);
		region_markers=[];
		my_buffer=[];
	}
	
	function clearCityMarkers(){		
		for(n=0;n<city_markers.length;n++)
		map.removeOverlay(city_markers[n]);
		city_markers=[];
		my_buffer=[];
	}		
		
	function showAddress(address,scope,dozoom,realname,origname) {      
	  
	  //alert(origname);
	  
	  geocoder.getLatLng(
      address+", "+current_country,
      
	  function(point) {
      if (!point) {
       
	   	//alert(address + " not found");
      
	  } else {        
	  
	  	var text;
		  	
		if(realname) text=realname;
		else text=address;			  
	  
        var marker = createInfoMarker(point,text,origname);
		
		if(scope=="region"){
			
			region_markers.push(marker);	
			if(dozoom) map.setCenter(point, region_zoom);
			region_index=point;
			
		} else if(scope=="city"){
			
			if(dozoom) map.setCenter(point, city_zoom);
			city_markers.push(marker);
			
		} else if(scope=="country"){
			
			if(dozoom) map.setCenter(point, country_zoom);
			country_index=point;
			
		} else { /* do nothing */ }			
		
        map.addOverlay(marker);			
		
		/* this is to reduce bandwith load on high number requests because google will drop the request */
		myFunc="startGeocode()";
		t=setTimeout(myFunc,100);
		
      }});
	  
    }
	
	function urlMe(text){
		return text.replace(/\s/g,"_");	
	}
	
    function createInfoMarker(point,text,origname) {
	  
	 
	  Icon = new GIcon(G_DEFAULT_ICON);	  
	  Icon.image=("/pipi.png");
	  Icon.shadow = false;
	  Icon.iconSize = new GSize(32,32);
	  mOptions = { icon:Icon };
			  
 	  var marker = new GMarker(point, mOptions);
   	 /* GEvent.addListener(marker, "click",
      function() {
		 
		 var href="";
		 
		 if(current_country_ln) href+=urlMe(current_country_ln)+"/";
 		 if(current_region_ln) href+=urlMe(current_region_ln)+"/";
		 
		 if(
			text!==current_country_ln && 
			text!==current_region_ln
			)
	     {
			href+=urlMe(text)+"/";			 
		 }
		 	
		 href+="1.html";
		  
		 var html ="<div class='infomap'>";
		 html+='<h3 id="mapinfo">'+text+'</h3>';
		 html+="found: <strong>" + last_item + "</strong> objects.";
		 html+="<br />";
		 html+="<a href='"+href+"'>Click to search</a>";
		 html+="</div>";
		 
         marker.openInfoWindowHtml(html);

      });*/

	  /*GEvent.addListener(marker, "mouseover",
      
	  function() {
		makeGetRequest("items.php?search="+origname,"NaN","NaN"); 
	  });*/ 
	  
	  return marker;

	}	
	
