//<![CDATA[

window.onload = init;
window.onunload = GUnload;

function init()
{
	GLoad();
}

// arrays to hold copies of the markers and html used by the sidebar
// because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var i = 0;

var map = null;
var geocoder = null;
		
// Icoana pt orase
var iconOras = new GIcon();
iconOras.image = "http://www.constantaterenuri.ro/images/icon1.png";
iconOras.shadow = "http://www.constantaterenuri.ro/images/icon1_shadow.png";
iconOras.iconSize = new GSize(28, 30);
iconOras.shadowSize = new GSize(42, 30);
iconOras.iconAnchor = new GPoint(14, 30);
iconOras.infoWindowAnchor = new GPoint(13, 5);

// Icoana pt terenuri
var iconTeren = new GIcon();
iconTeren.image = "http://www.constantaterenuri.ro/images/icon2.png";
iconTeren.shadow = "http://www.constantaterenuri.ro/images/icon2_shadow.png";
iconTeren.iconSize = new GSize(32, 32);
iconTeren.shadowSize = new GSize(42, 32);
iconTeren.iconAnchor = new GPoint(9, 30);
iconTeren.infoWindowAnchor = new GPoint(16, 5);


function GLoad() {

	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(44.177402744156524,28.634319305419922), 9);
		map.addControl(new GLargeMapControl());
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		// Read the data from xml.php
		var request = GXmlHttp.create();
		request.open("GET", "http://www.constantaterenuri.ro/oferte.php", true);
		request.onreadystatechange = function() 
		{
			if (request.readyState == 4) 
			{
				//alert(request.responseXML);
				var xmlDoc = request.responseXML;
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				
				for (var i = 0; i < markers.length; i++) 
				{
					// obtain the attribues of each marker
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var lng = parseFloat(markers[i].getAttribute("lng"));
					var point = new GLatLng(lat,lng);
					var html = markers[i].getAttribute("html");
					var label = markers[i].getAttribute("label");
					// create the marker
					var marker = createMarker(point,label,html);
					map.addOverlay(marker);
				}
			}
		}
		request.send(null);	
	}
	else 
	{
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

// A function to create the marker and set up the event window
function createMarker(point,name,html) {
	var marker = new GMarker(point, iconTeren);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	
	// save the info we need to use later for the sidebar
	gmarkers[i] = marker;
	htmls[i] = html;
	
	// add a line to the sidebar html
	i++;
	return marker;
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function showAddress(address) {
	if (geocoder) 
	{
		geocoder.getLatLng(
		address,
		function(point) 
		{
			if (!point) 
			{
				alert(address + " nu a fost gasita de Google!");
			} 
			else 
			{
				map.setCenter(point, 13);
				var marker = new GMarker(point, iconOras);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(address);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(address);
				});

			}
		}
		);
	}
}

//]]>
