//<![CDATA[
if (GBrowserIsCompatible()) {
// Used to create a default Map icon
function defaultIcon(img, imgShad, iSize, sSize, iAnchor, wAnchor, sWAnchor) {
	var icon=new GIcon();
	icon.iconSize=eval(iSize);
	icon.shadowSize=eval(sSize);
	icon.iconAnchor=eval(iAnchor);
	icon.infoWindowAnchor=eval(wAnchor);
	icon.infoShadowAnchor=eval(sWAnchor);
	icon.image=img;
	icon.shadow=imgShad;
	return icon;
}
function show(type) {
	var url = "getpoints.asp";			
	var pars = "";						
	pars += "type=" + type;
	pars += "&d=" + escape(settings.distance);
	pars += "&src=" + escape(loc.y + "," + loc.x);
	var loadMarkers = new Ajax.Request(url, {				
		requestHeaders: ['Content-type', 'text/xml'],
		method: 'get',
		parameters:pars,
		onComplete:function(or) {
			var data = or.responseXML;
			var points = data.getElementsByTagName("point");				
			var marker = data.getElementsByTagName("marker")[0];
			// get the icon dimensions from the xml
			var icon = new GIcon;
			icon.iconSize = eval(marker.getAttribute("iconsize"));
			icon.shadowSize = eval(marker.getAttribute("shadowsize"));
			icon.iconAnchor = eval(marker.getAttribute("iconanchor"));
			icon.infoWindowAnchor = eval(marker.getAttribute("infowindowanchor"));
			icon.infoShadowAnchor = eval(marker.getAttribute("infoshadowanchor"));
			icon.shadow = marker.getAttribute("shadow");
			icon.image= marker.getAttribute("image");
			var ul = document.createElement("ul");
			ul.setAttribute("id", "list-type-" + type);
			ul.setAttribute("class", "type-list");
			ul.className = "type-list";
			// build the markers, its html contents, and the list view html based on the location points retrieved
			for(var i =0; i < points.length; i++) {
				// adds this point to the unordered list 						
				var listElement = document.createElement("li");
				var address = "";
				var locationName = "";	
				var phoneNumber = "";
				// the inner contents of the list view
				var lihtml = "";
				lihtml += "<h5 class=\"type-list-name\">" + "<a href=\"#\" onclick=\"focusLocation(" + markers.length +")\">" + new String(points[i].getAttribute("name")) + "</a>" + "</h5>" ;
				lihtml += "<p>" + new String(points[i].firstChild.nodeValue).replace(/, Canada/, "") + "</p>";
				lihtml += "<p>" + new String(points[i].getAttribute("postal")) + "</p>";
				// the inner contents of the bubbles
				var html = "";
				html += "<div class=\"bubble\">";
				var logo = points[i].getElementsByTagName("image")[0];	
				if(logo.getAttribute("src")) {
					html += "<img src=\"" + logo.getAttribute("src") + "\" />";
				}
				locationName = new String(points[i].getAttribute("name"));
				html += "<h4 class=\"marker-name\">" + locationName + "</h4>";				
				html += "<h5 class=\"marker-header\">Address:</h5>";
				address = new String(points[i].firstChild.nodeValue);
				html += "<span class=\"marker-address\">" + address + "</span>";
				html += "<span class=\"marker-postal\">" + new String(points[i].getAttribute("postal")) + "</span>";
				if(points[i].getAttribute("phone") != "") {
					phoneNumber = new String(points[i].getAttribute("phone"));
					lihtml += "<p>" + "Phone: " + phoneNumber + "</p>";
					html += "<h5 class=\"marker-header\">Phone:</h5>";
					html += "<span class=\"marker-phone\">" + new String(points[i].getAttribute("phone")) + "</span>";
				}
				if(points[i].getAttribute("fax") != "") {
					lihtml += "<p>" + "Fax: " + new String(points[i].getAttribute("fax")) + "</p>";
					html += "<h5 class=\"marker-header\">Fax:</h5>";
					html += "<span class=\"marker-fax\">" + new String(points[i].getAttribute("fax")) + "</span>";
				}
				var notes = points[i].getElementsByTagName("notes");
				if(notes.length > 0) {
					if(notes[0].firstChild) {
						html += "<span class=\"marker-other\">";
						html += "<h5 class=\"marker-header\">Notes</h5>";							
						html += notes[0].firstChild.nodeValue;
						html += "</span>";								
						lihtml += "<p>Notes: ";
						lihtml += notes[0].firstChild.nodeValue;
						lihtml += "</p>";																
					}
				}

				var url= points[i].getElementsByTagName("url");
				if(url.length> 0){
					if(url[0].firstChild){
						if(url[0].firstChild.nodeValue != "null"){
							var linkURL=url[0].firstChild.nodeValue;
							if(linkURL.match(/https?:\/\//)==null){
								linkURL= "http://" + linkURL;
							}
							html += "<h5 class=\"marker-header\">Web</h5>";
							html += "<span><a href=\"" + linkURL + "\" title=\"External: Location URL\">" + linkURL + "</a></span>";
							lihtml += "<p><a href=\"" + linkURL + "\" title=\"External: Location URL\">";
							lihtml += linkURL;
							lihtml += "</a></p>";
						}
					}

				}

				var hoursOperation = points[i].getElementsByTagName("operation");
				if(hoursOperation.length > 0) {
					if(hoursOperation[0].firstChild) {
						html += "<h5 class=\"marker-header\">";
						html += "Operational Hours:";
						html += "</h5>";
						html += "<span class\"marker-other\">";
						html += hoursOperation[0].firstChild.nodeValue;
						html += "</span>";
						lihtml += "<p>Operational Hours: ";
						lihtml += hoursOperation[0].firstChild.nodeValue;
						lihtml += "</p>";
					}
				}
				address = address.replace(/<br\/>/, "");
				// Google maps gives error when having () and passing it as extra information
				locationName = locationName.replace(/\(/g, "[");
				locationName = locationName.replace(/\)/g, "]");
				var linkGetDirections = "<a href=\"http://maps.google.com/maps?saddr=" + loc.y + "%2C" + loc.x + "(" + escape(loc.location) + ")" + "&daddr=" + points[i].getAttribute("latt") + "%2C" + points[i].getAttribute("long") + "(" + escape(locationName + ((phoneNumber != "") ? " - " + phoneNumber : "") + " - " + address ) + ")" + "\">" + "Get directions to this location." + "</a>";
				html += "<div class=\"getdirections\">";
				html += "<span class=\"getdirections-span\">";
				html += linkGetDirections;
				html += "</span>";
				html += "</div>";
				html += "</div>";
				lihtml +="<p>";
				lihtml += linkGetDirections
				lihtml +="</p>";
				if(((i % 5)==0)&&(i!=0)){
					lihtml += "<div class=\"backtop\">";
					lihtml += "<a href=\"#\" title=\"back to top\">back to top</a>";
					lihtml += "</div>";
				}					
				var m = createMarker(new GLatLng(points[i].getAttribute("latt"), points[i].getAttribute("long")), html, icon);
				markers.push(m);
				map.addOverlay(m);
				lihtml = "<div class=\"locationText\">" + lihtml;
				lihtml+= "</div>"
				listElement.innerHTML = lihtml;
				ul.appendChild(listElement);
			}
			var hdr = document.createElement("h5");
			hdr.setAttribute("class", "type-header");
			hdr.className = "type-header";
			if(points.length > 0) {
				switch(type) {
				case 1:hdr.innerHTML="Licensing Offices";break;
				case 3:hdr.innerHTML="Claim Centres";break;
				case 4:hdr.innerHTML="Broker Offices";break;
				}
			}
			else {
				alert("Unable to find any locations around the address entered. \nClick on 'Start a new search' to attempt another search. \n\n Tips: \n * Try another starting address. \n * Try expanding your search radius.");
			}
			$('map-list').appendChild(hdr);
			$('map-list').appendChild(ul);				
			points = null;
			marker = null;
			if($('loader')){$('loader').style.display="none";}
		}
	});
}

// loads the elements of the map		 
function load() {

	// load map
	map = new GMap2($('map'));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());	
	map.addControl(new GScaleControl());
	map.enableContinuousZoom();

	// if address coordinates are not in query string
	if((loc.x=="")&&(loc.y=="")){
		var address=loc.location+" Canada";
		var geocoder=new GClientGeocoder();			
		geocoder.getLatLng(address, function(point) {	
			if(point){		
				
				loc.x=point.x;
				loc.y=point.y;	

				map.setCenter(new GLatLng(loc.y,loc.x),parseInt(settings.zLevel));
				var icon = defaultIcon(srcMarker.img, srcMarker.shadow, srcMarker.size, srcMarker.shadSize, srcMarker.iAnchor, srcMarker.winAnchor, srcMarker.shadWinAnchor);
				addSourceMarker(point, "<div id=\"bubble-source\">" + "<h4 class=\"marker-name\">" + "Your Location" + "</h4>" + "<h5 class=\"marker-header\">Address:</h5>" + "<span class=\"marker-address\">" + loc.location + "</span>" + "</div>", icon);
				if(settings.licensing=="true" || settings.claims=="true" || settings.brokers=="true"){
					if(settings.licensing=="true"){show(1);}		
					if(settings.claims=="true"){show(3);}
					if(settings.brokers=="true"){show(4);}
				}else {
					alert("The type of location to search for was not set.\n\n Head back to the search page and select one of the location types to search.");
					if($('loader')){$('loader').style.display="none";}
				}
				if(settings.defaultview=="list"){changeView('list');}
			}
			else {

				alert("Unable to find the address you entered. \n\n Try the following: \n\n * Remember to include your CITY in the address \n * Check your spelling \n * For numbered streets enter '49' instead of '49th' \n * Try entering your city only \n\n Click 'BACK' on your browser and re-enter a new address.");
				map.setCenter(new GLatLng(49.25437,-123.084672), parseInt(settings.zLevel));
				if($('loader')){$('loader').style.display="none";}
			}			
		});
	}
	else {
		map.setCenter(new GLatLng(loc.y,loc.x),parseInt(settings.zLevel));
		var icon=defaultIcon(srcMarker.img, srcMarker.shadow, srcMarker.size, srcMarker.shadSize, srcMarker.iAnchor, srcMarker.winAnchor, srcMarker.shadWinAnchor);
		addSourceMarker(new GLatLng(loc.y, loc.x), "<div id=\"bubble-source\">" + "<h4 class=\"marker-name\">" + "My Location" + "</h4>" + "<h5 class=\"marker-header\">Address:</h5>" + "<span class=\"marker-address\">" + loc.location + "</span>" + "</div>", icon);
		if(settings.licensing=="true"){show(1);}		
		if(settings.claims=="true"){show(3);}
		if(settings.brokers=="true"){show(4);}
		if(settings.defaultview=="list"){changeView('list');}
	}	
}
Event.observe(window,'load',load);Event.observe(window,'unload',unload);
}
//]]>