var map = null;
var geocoder = null;
var icons = {};
var allmarkers = [];

function init_reality() {
	
}

function init_map() {
	if (GBrowserIsCompatible()) {
		var pos = new GLatLng(48.7, 19.66);
		map = new GMap2(document.getElementById("map"));
		map.setCenter(pos, 7);
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.enableDoubleClickZoom();
//		map.setUIToDefault();
		geocoder = new GClientGeocoder();
	}
}
function show_address(address) {
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.checkResize();
				map.setCenter(point, 16);
//				var icon = getIcon(Array("", ""));
				var marker = new GMarker(point,{title:address}); //, icon:icon});
				map.addOverlay(marker);
				marker.openInfoWindowHtml(address);
			}
		});
	}
}
function getIcon(images) {
	var icon = null;
	if (images) {
		if (icons[images[0]]) {
			icon = icons[images[0]];
		} else {
			icon = new GIcon();
			icon.image = "images/" + images[0] + ".png";
			var size = iconData[images[0]];
			icon.iconSize = new GSize(size.width, size.height);
			icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
			icon.shadow = "images/" + images[1] + ".png";
			size = iconData[images[1]];
			icon.shadowSize = new GSize(size.width, size.height);
			icons[images[0]] = icon;
		}
	}
	return icon;
}


function actSelect(nm, x) {
	txt = x.html();
	val = x.attr("val");
	lnk = to_url(txt)+(nm == "kraj" ? "-kraj" : "")+"|"+val;
	$("#"+nm+" > .seldropdown > A.selected").removeClass("selected");
	x.addClass("selected");
	
	$("#"+nm+"_input").val(val);
	$("#"+nm+"_input").attr("text", txt);
	$("#"+nm+"_input").attr("link", lnk);
	$("#"+nm+"_sel").html(txt);
	
	return false;
}

function show_filter(nm) {
	if ($("#"+nm+" > .seldropdown").css("display") == "none") {
		$(".seldropdown").hide();
	}
	$("#"+nm+" > .seldropdown").toggle();
}
function select(nm, parent, id) {
	var x = $("#"+nm+" > .seldropdown > A").get(0);
	$("#"+nm+"_input").val("");
	$("#"+nm+"_sel").html(x.innerHTML);
	if (nm == "okres") {
		var x = $("#obec > .seldropdown > A").get(0);
		$("#obec_input").val("");
		$("#obec_sel").html(x.innerHTML);
		$("#obec > .seldropdown > A[okres]").hide();
	}
	
	$("#"+nm+" > .seldropdown > A["+parent+"]").hide();
	if (id != "") {
		$("#"+nm+" > .seldropdown > A["+parent+"="+id+"]").show();
	}
}

function submit_reality(frm, ignore_id) {
	url_t = furl_order;
	url_f = filter_order;
	id_param = "";
	
	if ($("#id_input").val() != "") {
		if (parseInt($("#id_input").val(), 10) > 0 && ignore_id != true) {
			redirect_to_detail(frm, parseInt($("#id_input").val(), 10));
			return false;
		}
		if (parseInt($("#id_input").val(), 10) > 0) {
			url_t = url_t.replace("<typ>", "");
			if ($("#okres_input").val() == $("#obec_input").val()) {
				url_t = url_t.replace("<okres>", "");
			}
		}
	}
	
	for (i=0; i<frm.elements.length; i++) {
		nm = frm.elements[i].name;
		if (nm == "id") {
			if ($("#id_input").val() != "") {
				id_param = "/ID-"+str_pad($("#"+nm+"_input").val(), 1, "0", "STR_PAD_LEFT");
			}
		} else if (typeof($("#"+nm+"_input").attr("link")) != "undefined" && $("#"+nm+"_input").attr("link") != "") {
			x = $("#"+nm+"_input").attr("link").split("|");
			url_t = url_t.replace("<"+nm+">", "/"+x[0]);
			url_f = url_f.replace("<"+nm+">", "-"+x[1]);
		} else if (typeof($("#"+nm+"_input").val()) != "undefined" && $("#"+nm+"_input").attr("type") == "checkbox") {
			if ($("#"+nm+"_input").attr("checked")) {
				url_t = url_t.replace("<"+nm+">", "/"+nm);
				url_f = url_f.replace("<"+nm+">", "-"+$("#"+nm+"_input").val().replace(" ", ""));
			}
		} else if (typeof($("#"+nm+"_input").val()) != "undefined" && $("#"+nm+"_input").val() != "") {
			if (parseInt($("#"+nm+"_input").val(), 10) > 0) {
				if (nm == "cena_do") {
					url_t = url_t.replace("<"+nm+">", "-do-"+$("#"+nm+"_input").val().replace(" ", ""));
				} else {
					url_t = url_t.replace("<"+nm+">", "/"+nm.replace("_", "-")+"-"+$("#"+nm+"_input").val().replace(" ", ""));
				}
				url_f = url_f.replace("<"+nm+">", "-"+$("#"+nm+"_input").val().replace(" ", ""));
			}
		}
	}
	url_t = url_t.replace(/<[a-z_]+>/g, '').substring(1);
	url_f = "/f-"+url_f.replace(/<[a-z_]+>/g, '-0').substring(1);
	if (id_param != "") {
		url_t = url_t.replace(/\//g, "-");
	}
	document.location.href = url_prefix + "/" + url_t + id_param + url_f;
	
	return false;
}

function redirect_to_detail(frm, id) {
	$.ajax({
		type: "POST",
		url: "inc/submit.php",
		data: "status=data&type=reality&id="+id,
		dataType: "json",
		success: function(res){
			switch (parseInt(res[0].type_sell)) {
				case 1: url_prefix = "/predaj"; break;
				case 2: url_prefix = "/prenajom"; break;
				case 3: url_prefix = "/podnajom"; break;
				case 4: url_prefix = "/drazby"; break;
			}
			if (res[0].loc_county > 0) { actSelect("kraj", $("#kraj > .seldropdown > A[val='"+res[0].loc_county+"']")); }
			if (res[0].loc_district > 0) { actSelect("okres", $("#okres > .seldropdown > A[val='"+res[0].loc_district+"']")); }
			if (res[0].loc_city > 0) { actSelect("obec", $("#obec > .seldropdown > A[val='"+res[0].loc_city+"']")); }
			if (res[0].property_type > 0) { actSelect("typ", $("#typ > .seldropdown > A[val='"+res[0].property_type+"']")); }
			if (res[0].property_cat > 0) { actSelect("kategoria", $("#kategoria > .seldropdown > A[val='"+res[0].property_cat+"']")); }
			if (res[0].property_state > 0) { actSelect("stav", $("#stav > .seldropdown > A[val='"+res[0].property_state+"']")); }
			submit_reality(frm, true);
		},
		beforeSend: function(){
//			$("#form_alert").remove();
//			html = ret.html();
//			ret.html("Odosielam formulár....");
		}
	});
	return false;	
}

