$(function(){
	var current_id = $("select#select_categories").val();
	if(current_id)
	{
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: base_url + "api/get_typologies",
		   data: "category_id="+current_id+"",
		   success: function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
				$("select#select_typologies").html(options);
				$('select#select_typologies option:first').attr('selected', 'selected');
		   }
		});
	}
	
	
	$("select#select_categories").change(function(){
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: base_url + "api/get_typologies",
		   data: "category_id="+$(this).val()+"",
		   success: function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
				$("select#select_typologies").html(options);
				$('select#select_typologies option:first').attr('selected', 'selected');
		   }
		});
	})

	$("select#form_city").change(function(){
		$('select#select_subtypologies option:first').attr('selected', 'selected');
	})
});

//<![CDATA[

function load() { 
	if (GBrowserIsCompatible()) { 
	  var mapOptions = {
		googleBarOptions : {
		  style : "new"
		}
	  }
	var map = new GMap2(document.getElementById("map"), mapOptions); 
	var geocoder = new GClientGeocoder(); 
	map.setUIToDefault();
	map.enableGoogleBar();
	showcentermap(map, geocoder, my_latitude, my_longitude); 
	} 
}

function showcentermap(map, geocoder, my_latitude, my_longitude) { 
var oLatLng = new GLatLng(my_longitude, my_latitude);
map.setCenter(oLatLng, my_zoom);
var marker = new GMarker(oLatLng); 
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("<table width='315' height='100'><tr><td><img style='border:2px solid #ccc;' src='"+base_url+js_thumb+"'/></td></tr><tr><td style='font-weight:bold;'>"+js_entry_title+"</td></tr></table><br /><a target='_blank' href='http://maps.google.com/maps?q="+js_entry_address+"'>"+js_entry_address+"<br />"+js_entry_zip+" - "+js_entry_city+"</a>");
});
map.addOverlay(marker); 

}

//]]> 

var gvaMiniMaps = new Array();
var gvaMiniMapsTI = new Array();
var gvaMiniMapsLL = new Array();
var label_title;
var tab_2_title;
/*---------------------------------------------------------*
*
*----------------------------------------------------------*/
function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined") ? false : true;
}

function googleMap(nPos, nLat, nLon, nAcc, myZoom)
{
	if(label_title != undefined)
	{
		var tab_contatti = '<div id="bubble"><h2>'+label_title+'</h2><img src="'+logo_project+'" /><div class="rightcontent"><h3>'+label_phone+'</h3><p>'+value_phone+'</p><h3>'+label_fax+'</h3><p>'+value_fax+'</p><h3>'+label_email+'</h3><p><a href="mailto:'+value_email+'">'+value_email+'</a></p><h3>'+label_address+'</h3><p>'+value_address+'</p></div></div>';
	}
	else
	{
		//var image = (js_first_photo == "") ? '' : '<img src="'+js_first_photo+'" alt="'+js_entry_title+'" />';
		var tab_contatti = '<div id="little_bubble"><h2>'+js_entry_title+'</h2><h3>'+js_label_rif+js_entry_cod+'</h3><p><strong>'+js_entry_city+'</strong></p><p>'+js_entry_zip+' '+js_entry_address+' '+js_entry_district+' '+js_entry_region+'</p><p>'+js_entry_price+'</p></div>';
		var tab_2_title  = '';
	}
	
	var oGMap = new GMap2(document.getElementById("google_map_" + nPos));
	if (!oGMap)
		return(false);

	gvaMiniMaps[nPos] = oGMap;
	gvaMiniMapsLL[nPos] = new Array(nLat, nLon);

	oGMap.addControl(new GSmallMapControl());
	oGMap.addControl(new GMapTypeControl());

	var oLatLng = new GLatLng(nLat, nLon);
	gvaMiniMapsLL[nPos] = oLatLng;

	// Creiamo le varie tabs, possono anche contenere codice HTML!
	var schede = [
	new GInfoWindowTab(tab_2_title, tab_contatti)
	];				
	
	var marker = new GMarker(oLatLng);
	
	GEvent.addListener(marker, "click", apri_schede);
	
	var nZoom = myZoom;

	nZoom = myZoom;
	
	
	oGMap.setCenter(oLatLng, nZoom);
	oGMap.savePosition();
	oGMap.addOverlay(marker);

	function apri_schede() {
		marker.openInfoWindowTabsHtml(schede);
	}

}

// apre il popup centrato nella finestra principale
var profiles =
{
	windowCenter:
	{
		height:500,
		width:610,
		center:1
	}
	
};

$(function()
{
	
	$(".popupwindow").popupwindow(profiles);
	return false;
});

function changeInputTextColor (oObj, sText, bOnFocus, sColor)
{
	if (!oObj)
		return(false);



	sbHPOnInputFocus(oObj, bOnFocus);

	if(oObj.value=='' || oObj.value==sText)
	{
		if (bOnFocus)
			oObj.value="";
		else	oObj.value=sText;
	}
	else	sColor = oObj.style.color;

	oObj.style.color=sColor;


	return (true);
}

/*---------------------------------------------------------*
*
*----------------------------------------------------------*/
function sbHPOnInputFocus(oInput, bOnFocus) {
	if (!oInput)
		return(false);

	if (bOnFocus)
		oInput.style.backgroundColor = '#fff';
	else	oInput.style.backgroundColor = '';
}

