
var box_locale = '<div class="box-locale" id="box-locale-ID" style="position:absolute;';
box_locale += 'top:-20px;left:150px;display:none;border-left:1px solid #333;';
box_locale += 'border-top:1px solid #333;border-right:1px solid #333;z-index:999;">';
box_locale += '<div id="box-locale-int-ID" style="background-color:#9e0b0e;color:#fff;';
box_locale += 'font-size:14px;font-weight:bold;padding:6px;cursor:pointer;"';
box_locale += ' onclick="$(\'#box-locale-ID\').hide(\'fast\');" title="Chiudi il box"></div>'
box_locale += '<div id="box-locale-content-ID" style="background-color:#f1f1f1;color:#000;';
box_locale += 'padding:2px 6px 4px 6px;font-size:12px;border-bottom:1px solid #333;"></div>';
box_locale += '</div>';

var map;
function find_on_map(coord_1, coord_2) {
	map.panTo(new GLatLng(coord_1, coord_2));
	map.setZoom(16);
	$("#span-starting-zoom").show();
	
	return false;
}

function back_to_starting_zoom(coord_1, coord_2) {
	map.closeInfoWindow();
	map.panTo(new GLatLng(coord_1, coord_2));
	map.setZoom(MAP_ZOOM);
	$("#span-starting-zoom").hide();

	return false;
}

function showInfo(marker,html,id) {
	marker.openInfoWindowHtml(html);
	$("#info-win-img-" + id).html('<img src="' + SITE_URL + 'img/common/ajax-loader1.gif" alt="" />').show();
	$.ajax({
		type: "POST",
		url: SITE_URL + "ajax/jajax.php",
		data: "m=get_locale_thumbs&id=" + id,
		success: function(result){
			$("#info-win-img-" + id).html(result);
			if(result.match(/^Errore:/i)) {
				$("#info-win-img-" + id).html( result.replace("Errore:","") ).show();
				return false;
			}

			$("#info-win-img-" + id).html(result).show();
		}
	});
}

function myMarker(point, html, name, id) {
	var icon = new GIcon();
	icon.image = "http://www.ristorantiitaliani.it/img/common/marker.png";
	icon.iconSize = new GSize(26, 40);
	icon.iconAnchor = new GPoint(15, 34);
	icon.infoWindowAnchor = new GPoint(10, 18);

	var marker_opts = new Object();
	marker_opts.icon = icon;
	marker_opts.title = name;
	var marker = new GMarker(point, marker_opts);
	
	//marker.tooltip = name;

	GEvent.addListener(marker, "click", function() {
		showInfo(marker,html,id);
	});
/*
	GEvent.addListener(marker, "mouseover", function() {
		showTooltip(marker);
	});
	GEvent.addListener(marker, "mouseout", function() {
		$("#gmap-tooltip").hide();
	});
*/
	return marker;
}

function showTooltip(marker) {
	$("#gmap-tooltip").html(marker.tooltip);
	var point = map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset = map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor = marker.getIcon().iconAnchor;
	var width = marker.getIcon().iconSize.width;
	var height = document.getElementById("gmap-tooltip").clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
	pos.apply(document.getElementById("gmap-tooltip"));
	$("#gmap-tooltip").show();alert(new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
}

function altri_rist_box_status() {
	var _altri_rist_box_status = $("#altri-rist-box").css("display");
	if(_altri_rist_box_status == 'none') {
		$("#open-close-altri-rist")
			.attr("src",SITE_URL + "img/common/plus.png")
			.attr("alt","Apri il box con gli altri ristoranti")
			.attr("title","Apri il box con gli altri ristoranti");
	} else {
		$("#open-close-altri-rist")
			.attr("src",SITE_URL + "img/common/minus.png")
			.attr("alt","Chiudi il box con gli altri ristoranti")
			.attr("title","Chiudi il box con gli altri ristoranti");
	}
}

function map_status() {
	var map_status = $("#search-google-map").css("display");
	var src = $("#open-close-map").attr("src");
	if(map_status == 'none') {
		$("#open-close-map")
			.attr("src",src.replace("minus","plus"))
			.attr("alt","Apri la mappa di Google")
			.attr("title","Apri la mappa di Google");
		$("#search-google-map-int").css("border-bottom","1px solid #a1a1a1");
	} else {
		$("#open-close-map")
			.attr("src",src.replace("plus","minus"))
			.attr("alt","Chiudi la mappa di Google")
			.attr("title","Chiudi la mappa di Google");
		$("#search-google-map-int").css("border-bottom","0px solid #a1a1a1");
	}
}

$(document).ready(
	function() {
		var altri_rist_img = $("#open-close-altri-rist").css("display");
		var google_map = $("#search-google-map").css("display");

		if(google_map) {
			showGoogleMap();
			map_status();
			if(adv == true) {
				$("#search-google-map").hide();
				map_status();
			}
			$("#open-close-map").click(
				function() {
					$("#search-google-map").toggle();
					map_status();
				}
			);
		}

		if(altri_rist_img) {
			altri_rist_box_status();
			$("#open-close-altri-rist").click(
				function() {
					$("#altri-rist-box").toggle();
					altri_rist_box_status();
				}
			);
		}

		$(".red1").click(
			function() {
				var _id = this.id.replace("a-","");
				var box_content = box_locale.replace(/ID/g,_id)
				$(".box-locale").each(
					function() {
						if(this.id != 'box-locale-' + _id) {
							$(this).hide();
						}
					}
				);
				var offset = $("#div-" + _id).offset();
				if( $("#box-locale-" + _id).html() == null ) {
                    $("body").append( box_content );
				}
				$("#box-locale-" + _id).css("top", (offset.top - 50) + "px" ).css("left", (offset.left + 150) + "px" );
				$("#box-locale-int-" + _id).html( $("#tipo-" + _id).html() + " " + $("#nl-" + _id).html() + " " + $("#nc-" + _id).html() );
				var locale_content =	$("#addr-" + _id).html() + " " +
										$("#num-" + _id).html() + "<br />" +
										$("#cap-" + _id).html() + " " +
										$("#city-" + _id).html() + " - " +
										$("#reg-" + _id).html();
				if($("#tel-" + _id).html().length > 0) {
					locale_content += "<br />" + $("#tel-" + _id).html();
				}
				locale_content += "<br /><br />Sei il titolare del locale e vuoi affiliarti ?";
				locale_content += '<div style="margin:3px 0px 2px 0px;text-align:center;">';
				locale_content += '<a class="red" href="' + SITE_URL + 'static/affiliati/index.html">';
				locale_content += "<strong>Clicca qui</strong></a></div>";
				$("#box-locale-content-" + _id).html( locale_content );
				$("#box-locale-" + _id).toggle("fast");
				
				return false;
			}
		);
	}
);