var gmarkers = [];
var htmls = [];
// global "map" variable
var map;
var mgr;
var icons = {};
var i = 0;
var currentPoint;
var description = 'Area under development.<br/>';
	description += 'Franchise opportunities are available.<br/>';
	description += ' For information click <a href="/franchise.realtyworld">here</a>';
// This function zooms in or out
// its not necessary to check for out of range zoom numbers, because the API checks
function zoomMap(z){map.setZoom(map.getZoom() + z);}
// A function to create the marker and set up the event window
function createMarker(point,name,html,ico)
{
	// create custom icon
	var icon = new GIcon();
	if (ico != "")
	{
		icon.image = "/themes/realtyworld/images/mapicons/" + ico + ".gif";
	}else
	{
		icon.image = "/themes/realtyworld/images/spacer.gif";
	}
	icon.iconSize = new GSize(14, 12);
	icon.iconAnchor = new GPoint(14, 12);
	icon.infoWindowAnchor = new GPoint(5, 1);
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function()
	{
		map.setCenter(marker.getPoint());
		marker.openInfoWindowHtml(html);
	});
	gmarkers[i] = marker;
	htmls[i] = html;
	// ======= Add the entry to the select box =====
	// ==========================================================        
	i++;
	return marker;
}
// ======= This function handles selections from the select box ====
// === If the dummy entry is selected, the info window is closed ==
function handleSelected(typeObj,countries,counties)
{
	var countryName = countries.options[countries.selectedIndex].text;
	var countryIndex = countries.options[countries.selectedIndex].value;
	var countyName = counties.options[counties.selectedIndex].text;
	var countyIndex = counties.options[counties.selectedIndex].value;
	var searchText = ''; var officeNum = 0;
	if(typeObj == 'country' && countryIndex )
	{
		searchText = countryName;
		officeNum = parseInt(countries.options[countries.selectedIndex].title);
	}else if(typeObj == 'county' && countyIndex && countryIndex )
	{
		searchText = countyName+','+countryName;
		officeNum = parseInt(counties.options[counties.selectedIndex].title);
	}
	
	if( searchText )
	{
		showAddress(searchText, officeNum);
	}
}

function showAddress(address, d)
{
	if (geocoder)
	{
		geocoder.getLatLng
		(
			address,
			function(point)
			{
				if(point)
				{
					map.setCenter(point);
					currentPoint = point;
					if(!d)
					{
						map.openInfoWindowHtml(point,description);
						var infoV = map.getInfoWindow();
					}
				}else alert(address + " not found");
			}
		);
	}
}

function loadMaps()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map2"));
		map.addControl(new GSmallZoomControl());
		map.setCenter(new GLatLng(20.0, 0.0), 1);
		map.enableDoubleClickZoom();
		geocoder = new GClientGeocoder();
		GDownloadUrl("location.xml", function (doc)
		{
			var xmlDoc = GXml.parse(doc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++)
			{
				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");
				var ico = markers[i].getAttribute("ico");
				// create the marker
				var marker = createMarker(point,label,html,ico);
				map.addOverlay(marker);
			}
			// ===== final part of the select box =====
			// Create The Select Box.
		});
		
		// states outline
		//for(var i in stateBorders)
		//{
		//	var polyCoords = stateBorders[i];
		//	var poly = new GPolygon(polyCoords['coords'], "#000000",1,1, polyCoords['color']);
		//	map.addOverlay(poly);
		//}
		// states outline
		
		//Populate the markers
		setupOfficeMarkers();
		//window.setTimeout(setupOfficeMarkers, 0);
	}else alert("Sorry, the Google Maps API is not compatible with this browser");
}

function getIcon(images)
{
	var icon = null;
	if (images)
	{
		if (icons[images[0]])
		{
			icon = icons[images[0]];
		}else
		{
			icon = new GIcon();
			icon.image = "/themes/realtyworld/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 = "/themes/realtyworld/images/" + images[1] + ".png";
			size = iconData[images[1]];
			icon.shadowSize = new GSize(size.width, size.height);
			icons[images[0]] = icon;
		}
	}
	return icon;
}

function createBrokerMarker(point,name,icon,address)
{
	var marker = new GMarker(point, {title:name, icon:icon });
	GEvent.addListener(marker, "click", function()
	{
		map.setCenter(marker.getPoint(),15);
		marker.openInfoWindowHtml("<b>"+name+"</b><br>"+address+" [<a onclick=\"zoomMap(-3)\">-</a>] [<a onclick=\"zoomMap(3)\">+</a>]");
	});
	return marker;
}

function goto(uri){window.location=uri}

function setupOfficeMarkers()
{
	var mgr = new GMarkerManager(map)
	for (var i in officeLayer)
	{
		var layer = officeLayer[i];
		var markers = [];
		for (var j in layer["places"])
		{
			var place = layer["places"][j];
			var icon = new GIcon();
			icon.image = "/themes/realtyworld/images/icon.png";
			icon.iconSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(30, 30);
			icon.infoWindowAnchor = new GPoint(5, 1);
			var posn = new GLatLng(place["posn"][0], place["posn"][1]);
			var marker = createBrokerMarker(posn, place["name"], icon, place["address"]);
			markers.push(marker);
		}
		mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
	}
	mgr.refresh();
	
	// change country to USA
	$('#country3').val(1);
	$('#country3').trigger('change');
	// change country to USA
}


//ADDED FROM HOMELIFE.CA SOURCE 03-24-09
function goToProvinceWebsiteIfNeeded(province)
{
	var direction = "";
	if(province == "Nevada Northern")
	{
		direction = "http://www.realtyworldnca.com/";
	}else if(province == "California Northern")
	{
		direction = "http://www.realtyworldnca.com/";
	}
	if( direction != "" ) window.location=direction;
}


//$('#country3').trigger('change',['1']);