/** Used to store the GMarkers */
var gMarkers = new Array();

function initializeMarkers(id, lon, lat, price, size, street, city,query_id){
	//alert(id);
	//alert(lon);
	//alert(lat);
	//alert(price);
	//alert(size);
	//alert(price);
	//alert(size);
	
	//use customized CLicklodge icon on the map
	var clickIcon = new GIcon();
	clickIcon.iconAnchor = new GPoint(16, 16);
	clickIcon.infoWindowAnchor = new GPoint(16, 0);
	clickIcon.iconSize = new GSize(32, 32);
	clickIcon.image = "images/clicklodge_googlemaps.png";
	var markerOptions = {
		icon : clickIcon
	};

	// draw the marker
	var point = new GPoint(lon, lat);
	var html = "Price: "
			+ price
			+ "<br> Size: "
			+ size
			+ "<br> Street: "
			+ street
			+ "<br> <a href='' id='"
			+ id
			+ "' onclick='javascript: moreInfoEvent(event); return false'>More info</a><br><a href='http://maps.google.com/maps?saddr="
			+ street + ", " + city
			+ "&daddr=' target='_blank' >Get Directions</a>";
	var marker = createMarker(point, html, markerOptions, id,query_id);
	// map.addOverlay(marker);

	// var point = new GPoint(lon, lat);
	// var marker+id = createMarker(point, markerOptions);
	// alert(marker+id);
	// Price: " +price+ "<br> Size: " +size+ "<br> <a href='' id='"+id+"'
	// onclick='javascript: moreInfo(event); return false'>More info</a><br><a
	// href='http://maps.google.com/maps?saddr="+street+", "+city+"&daddr='
	// target='_blank' >Get Directions</a>
	// map.addOverlay(marker);
}

/** Create gMarkers and push them into the array */
function createMarker(point, html, markerOptions, id, query_id) {
	// name variables "dynamically" (marker+id) to prevent the info windows to
	// be opened in the same location
	window['marker' + id] = new GMarker(point, markerOptions); // {draggable:false}
	// alert(window['marker' + id]);
	// GEvent.addListener(window['marker' + id], "mouseover", function() {
	// window['marker' + id].openInfoWindowHtml(html);
	// });
	GEvent.addListener(window['marker' + id], "mouseover", function() {
		moreInfo(id);

	});
	GEvent
	.addListener(
			window['marker' + id],
			"click",
			function() {
				document.location.href = "index.php?option=com_search&Itemid=26&task=resultDetail&geo=yes"
+ "&qid="+query_id+"&id="+id+"&source=other";

			});
	gMarkers.push(window['marker' + id]);
	// return marker;
}

/** Adds gMarkers to the map */
function appendMarkers() {
	// alert(gMarkers.length);
	var zoomlevelcuonter = 0;
	var level = 0;
	var startingLevel=map.getZoom();
	var multip=2;
	for ( var i = 0; i < gMarkers.length; i++) {
		level = startingLevel + zoomlevelcuonter;
		mgr.addMarker(gMarkers[i], level, 17);
		var testlevel=(i+1) /(33*(Math.pow(multip,zoomlevelcuonter)*(zoomlevelcuonter+1)));
//		var testlevel=(i+1) /((zoomlevelcuonter * multip + 1) * 50);
//		var testlevel=(i+1) /((Mat.pow(multip,2))  * 50+50);
		if (testlevel== testlevel.toFixed(0)) {
			if (level < 17) {
				zoomlevelcuonter++;
			}
		}
	}

	/** mgr.addMarkers(gMarkers, 1); */
	mgr.refresh();

	// show how many markers are currently shown on the map
	//checkMarkerCount(mgr.getMarkerCount(map.getZoom()));
}

///** Adds gMarkers to the map */
//function appendMarkers() {
//	// alert(gMarkers.length);
//	var zoomlevelcuonter = 0;
//	var level = 0;
//	var startingLevel=map.getZoom();
//	for ( var i = 0; i < gMarkers.length; i++) {
//		level = startingLevel + zoomlevelcuonter;
//		mgr.addMarker(gMarkers[i], level, 17);
//		if ((i+1) % 50 == 0) {
//			if (level < 15) {
//				zoomlevelcuonter++;
//			}
//		}
//	}
//
//	/** mgr.addMarkers(gMarkers, 1); */
//	mgr.refresh();
//
//	// show how many markers are currently shown on the map
//	checkMarkerCount(mgr.getMarkerCount(map.getZoom()));
//}

// function appendMarkers() {
// // alert(gMarkers.length);
// var zoomlevelcuonter = 0;
// var level = 0;
// for ( var i = 0; i < gMarkers.length; i++) {
// if ((i % 50 ) == 0) {
// if (zoomlevelcuonter < 7) {
// zoomlevelcuonter++;
// }
// } else {
// if (zoomlevelcuonter == 1) {
// level = 0;
// } else {
// level = map.getZoom() + zoomlevelcuonter;
// }
// }
// mgr.addMarker(gMarkers[i], level, 17);
// }
//
// /** mgr.addMarkers(gMarkers, 1); */
// mgr.refresh();
//
// // show how many markers are currently shown on the map
// checkMarkerCount(mgr.getMarkerCount(map.getZoom()));
// }
/**
 * Updates the shown number of currently visible apartments on different
 * zoomlevels
 */
function checkMarkerCount(markerCount) {
	//document.getElementById("markers").innerHTML = "On this zoom level "
	//		+ map.getZoom() + ", " + markerCount + " apartments are shown";
	// alert(markerCount);
}

function moreInfoEvent(e) {
	var infoWindow = document.getElementById("message");
	var id;

	if (e.srcElement) { // IE
		// sourceId = window.event.srcElement.src="playActive.png";
	} else { // Mozilla
		// parse which address was selected
		id = e.target.id;
	}

	moreInfo(id);

}
function moreInfo(id) {
	// alert(id);
	// infoWindow.innerHTML = "DESCRIPTION HERE!";

	var ajaxRequest; // The variable that makes Ajax possible!
	var ajaxDisplay = document.getElementById('detailDiv');
	ajaxDisplay.innerHTML = "<br/><b>Lade Vorschau...</b><br>";
	

	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function() {
		if (ajaxRequest.readyState == 4) {
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			// alert(ajaxRequest.responseText);
		}
	}
	

	var queryString = "?id=" + id;
	ajaxRequest.open("GET", "components/com_search/getDetails.php"
			+ queryString, true);
	ajaxRequest.send(null);
}