var googlemaps_enableDebug = true;
var googlemaps_map = null;

// defaultit
var googlemaps_language = 'fi';
var googlemaps_markerList = [];
var googlemaps_enableFilterCookie = false;

function googlemaps_initialize(lat, long, zoom, hash) {
	debugPrint("googlemaps_initialize()", "googlemaps");
	var init = {"lat":lat, "lng":long, "zoom":zoom, "hash":hash};
	google.load("maps", "2", {"callback":common_createBoundedWrapper(init,_googlemaps_initialize), "language":googlemaps_language});
}

function _googlemaps_initialize() {
	var map = new google.maps.Map2(document.getElementById("map_canvas"));
	map.setCenter(new google.maps.LatLng(this.lat,this.lng), this.zoom);
	map.setUIToDefault();
	map.disableScrollWheelZoom();

	GEvent.addListener(map, "moveend", function() {
		var el_lat = document.getElementById("structureField_geo_latitude");
		var el_lng = document.getElementById("structureField_geo_longitude");
		var el_zoom = document.getElementById("structureField_geo_zoom");
		if(el_lat && el_lng && el_zoom) {
			var center = map.getCenter();
			var zoom = map.getZoom();
			el_lat.innerHTML = center.lat();
			el_lng.innerHTML = center.lng();
			el_zoom.innerHTML = zoom;
		}
	});

	if(googlemaps_markerList && googlemaps_markerList.length) {
		googlemaps_markerListToMap(map);
		var checkAll = document.getElementById("filter-groups-Kaikki");
//		checkAll.checked = true;
		googlemaps_filterChanged(checkAll);
	}
	googlemaps_map = map;
	if(this.hash) googlemaps_clickMarker(this.hash);
}

function googlemaps_markerListToMap(map) {
	debugPrint("googlemaps_markerListToMap()", "googlemaps");
	for(var i=0; i < googlemaps_markerList.length; i++) {
		var lat = Number(googlemaps_markerList[i].latitude);
		var lng = Number(googlemaps_markerList[i].longitude);
		var latLng = new GLatLng(lat, lng);

		var iconSrc = googlemaps_markerList[i].icon;
		googlemaps_markerList[i].icon = new GIcon();
		googlemaps_markerList[i].icon.image = "index.php?action=googlemaps-marker";
		if(! googlemaps_markerList[i].isSibling) googlemaps_markerList[i].icon.image += "&googlemaps-str="+googlemaps_markerList[i].number;
		googlemaps_markerList[i].icon.image += "&googlemaps-icon="+iconSrc;
		googlemaps_markerList[i].icon.shadow = "";
		googlemaps_markerList[i].icon.iconSize = new GSize(19, 32);
		googlemaps_markerList[i].icon.iconAnchor = new GPoint(10, 32);
		googlemaps_markerList[i].icon.infoWindowAnchor = new GPoint(10, 10);
		googlemaps_markerList[i].marker = new GMarker(latLng, {icon:googlemaps_markerList[i].icon});

		if(googlemaps_markerList[i].isSibling) {
			GEvent.addListener(googlemaps_markerList[i].marker, "click", common_createBoundedWrapper(googlemaps_markerList[i], googlemaps_sibling_marker_click));
		} else {
			GEvent.addListener(googlemaps_markerList[i].marker, "click", common_createBoundedWrapper(googlemaps_markerList[i], googlemaps_marker_click));
		}

		map.addOverlay(googlemaps_markerList[i].marker);

		debugPrint("googlemaps_markerListToMap(): marker added; href="+googlemaps_markerList[i].href, "googlemaps");
	}
}

function googlemaps_clickMarker(hash) {
	for(var i=0; i < googlemaps_markerList.length; i++) {
		if(googlemaps_markerList[i].hash == hash) {
			GEvent.trigger(googlemaps_markerList[i].marker, "click");
		}
	}
}

function googlemaps_marker_mouseOver() {
	debugPrint("googlemaps_marker_mouseOver "+this.id+" isSibling="+this.isSibling,"googlemaps");
	var naviItem = document.getElementById("navi_"+this.id);
	if(naviItem) naviItem.style.backgroundColor = "#eeeeee";
//	this.marker.openInfoWindowHtml("<span style='font-size: 7pt;'>"+this.name+"</span>", {maxWidth:120});
}

function googlemaps_marker_mouseOut() {
	debugPrint("googlemaps_marker_mouseOut "+this.id,"googlemaps");
	var naviItem = document.getElementById("navi_"+this.id);
	if(naviItem) naviItem.style.backgroundColor = "";
//	this.marker.closeInfoWindow();
}

function googlemaps_marker_click() {
	debugPrint("googlemaps_marker_click "+this.id,"googlemaps");
//	document.location.href = this.href;

	var html = "<span class='infoWindowTitle'>"+this.name+"</span><span class='infoWindowText'>"+this.infoHTML+"</span>";
	html += "<div><a class='infoWindowDetailsLink' href='"+this.href+"'>"+locale.googlemaps_details+"</a></div>"

	this.marker.openInfoWindowHtml(html, {maxWidth:120});
}

function googlemaps_sibling_marker_click() {
	debugPrint("googlemaps_sibling_marker_click "+this.id,"googlemaps");
	var href = "index.php?page="+this.ownerHash;
	href += "&googlemaps-lat=" + this.latitude + "&googlemaps-lng=" + this.longitude + "&googlemaps-zoom=" + googlemaps_map.getZoom();
	href += "&googlemaps-clickedHash=" + this.hash;
/*
	var groups = googlemaps_getFilterGroups();
	if(groups) for(var i in groups) {
		href += "&googlemaps-filter-" + i + "=1";
	}
*/
	document.location.href = href;
}

function googlemaps_getFilterGroups() {
	var groups = {};
	var el = document.getElementById("filter-groups");
	if(!el) return groups;

	var temp = el.getAttribute('value').split(',');
	for(var i in temp) {
		var grp = temp[i];
		var check = document.getElementById('filter-groups-'+grp);
		if(check && check.checked) groups[grp] = true;
	}

	return groups;
}

function googlemaps_filterMarkers(groups) {
	var temp = [];
	for(var test in groups) temp.push(test);
	debugPrint("googlemaps_filterMarkers "+temp.join(','),"googlemaps");

	if(googlemaps_markerList && googlemaps_markerList.length) {
		for(var i=0; i < googlemaps_markerList.length; i++) {
			var visible = false;
			if(googlemaps_markerList[i].groups) {
				for(var group in googlemaps_markerList[i].groups) {
					for(var test in groups) {
						if(test == group) {
							visible = true;
							break;
						}
					}
				}
			}
			if(visible) {
				googlemaps_markerList[i].marker.show();
			} else {
				googlemaps_markerList[i].marker.hide();
			}
		}
	}
}

function googlemaps_filterChanged(who) {
	var groups = {};
	var el = document.getElementById("filter-groups");
	if(!el) return;
//	debugPrint("----", "googlemaps");
//	for(var i in temp) groups[temp[i]] = false;
	var filterURL = '';
	var temp = el.getAttribute('value').split(',');
	for(var i in temp) {
		var grp = temp[i];
		var check = document.getElementById('filter-groups-'+grp);
//		debugPrint(check.id+" checked="+check.checked, "googlemaps");
		if(who && who != check && who.checked) {
			check.disabled = true;
		} else {
			check.disabled = false;
		}
		if(check.checked) {
			filterURL += "&googlemaps-filter-"+grp+"=1";
			var enableGroups = check.getAttribute('value').split(',');
			for(var j in enableGroups) {
				groups[enableGroups[j]] = true;
			}
		} else {
			filterURL += "&googlemaps-filter-"+grp+"=0";
		}
	}

	googlemaps_filterMarkers(groups);

	if(googlemaps_enableFilterCookie) {
		var saveURL = 'index.php?action=googlemaps&googlemaps-action=saveFilter'+filterURL;
		debugPrint(saveURL, 'googlemaps');
		sendXMLHttp('GET', saveURL, null, null);
	}

	return true;
}

