var gMap = {
	start: function() {
		if ($('cwc-map')) gMap.addCwcMap();
		if ($('carbonear-cwc-map')) gMap.addCarbonearCwcMap();
		if ($('regatta-cwc-map')) gMap.addRegattaCwcMap();
		if ($('duckworth-cwc-map')) gMap.addDuckworthCwcMap();
		if ($('placentia-cwc-map')) gMap.addPlacentiaCwcMap();
		if ($('hvgb-cwc-map')) gMap.addHvgbCwcMap();
		if ($('stephenville-cwc-map')) gMap.addStephenvilleCwcMap();
		if ($('springdale-cwc-map')) gMap.addSpringdaleCwcMap();
		if ($('lewisporte-cwc-map')) gMap.addLewisporteCwcMap();
		if ($('gander-cwc-map')) gMap.addGanderCwcMap();
		if ($('gfw-cwc-map')) gMap.addGFWCwcMap();
		if ($('cornerbrook-cwc-map')) gMap.addCornerBrookCwcMap();
	},
	addCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var app_root = "../";
			
			var map = new GMap2(document.getElementById('cwc-map'));
			map.setCenter(new GLatLng(52, -57), 5);
			map.addControl(new GLargeMapControl());
			
			gMap.addMarker(map, "80-84 Elizabeth Avenue, St. John's, Newfoundland and Labrador", "<b>St. John's & area Career Work Centre (Elizabeth Avenue)</b><br />Suite 210 Regatta Plaza<br />80-84 Elizabeth Avenue<br />St. John's, Newfoundland and Labrador<br />Telephone: (709) 729-7531<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "285 Duckworth Street, St. John's, Newfoundland and Labrador", "<b>St. John's & area Career Work Centre (Duckworth Street)</b><br />285 Duckworth Street<br />St. John's, Newfoundland and Labrador<br />Telephone: (709) 729-7505<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "17 Industrial Crescent, Carbonear, Newfoundland and Labrador", "<b>Carbonear & area Career Work Centre</b><br />17 Industrial Crescent<br />Carbonear, Newfoundland and Labrador<br />Telephone: (709) 945-3224<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "294 Memorial Drive, Clarenville, Newfoundland and Labrador", "<b>Clarenville & area Career Work Centre</b><br />White Dove Building<br />294 Memorial Drive<br />Clarenville, Newfoundland and Labrador<br />Telephone: (709) 466-0275<br />Telephone: (709) 466-0276<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "Airport Boulevard, Gander, Newfoundland and Labrador", "<b>Gander & area Career Work Centre</b><br />Fraser Mall<br />Airport Boulevard<br />Gander, Newfoundland and Labrador<br />Telephone: 1-888-632-4555<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "42 Hardy Avenue, Grand Falls-Windsor, Newfoundland and Labrador", "<b>Grand Falls-Windsor & area Career Work Centre</b><br />42 Hardy Avenue<br />Grand Falls-Windsor, Newfoundland and Labrador<br />Telephone: (709) 292-4331<br />Telephone: 1-888-632-4555<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "Dunville, Placentia, NL, Canada", "<b>Placentia & area Career Work Centre</b><br />The Business Centre<br />1116-1120 Main Street<br />Dunville, Newfoundland and Labrador<br />Telephone: (709) 227-1330<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "224 Main St, Lewisporte, Newfoundland and Labrador", "<b>Lewisporte & area Career Work Centre</b><br />224 Main St<br />Lewisporte, Newfoundland and Labrador<br />Telephone: 1-888-632-4555<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "3 Mall Street, Marystown, Newfoundland and Labrador", "<b>Marystown &amp; area Career Work Centre</b><br />3 Mall Street<br />Marystown, Newfoundland and Labrador<br />Telephone: (709) 279-5534<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "142 Little Bay Rd, Springdale, Newfoundland and Labrador", "<b>Springdale &amp; area Career Work Centre</b><br />142 Little Bay Rd.<br />Springdale, Newfoundland and Labrador<br />Telephone: (709) 673-2615", true, app_root);
            gMap.addMarker(map, "398 Goose Cove Rd, St. Anthony, Newfoundland and Labrador", "<b>St. Anthony &amp; area Career Work Centre</b><br />398 Goose Cove Rd.<br />St. Anthony, Newfoundland and Labrador<br />Telephone: (709) 454-3651<br />Fax: (709) 454-2334<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "163 Hamilton River Road, Happy Valley-Goose Bay, Newfoundland and Labrador", "<b>Happy Valley-Goose Bay &amp; area Career Work Centre</b><br />163 Hamilton River Road<br />Happy Valley-Goose Bay, Newfoundland and Labrador<br />Telephone: (709) 896-8846<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "29 Carolina Avenue, Stephenville, Newfoundland and Labrador", "Stephenville &amp; area Career Work Centre<br />29 Carolina Ave.<br />Stephenville, NL<br />Telephone: (709) 643-7954<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
            gMap.addMarker(map, "1-3 Union Street, Corner Brook, Newfoundland and Labrador", "<b>Corner Brook &amp; area Career Work Centre</b><br />1-3 Union Street, 2nd Floor<br />Corner Brook, Newfoundland and Labrador<br />Telephone: (709) 637-2601<br /><a href='Tour.aspx'>See what is offered at a Career Work Centre</a><br /><br />", true, app_root);
		}
	},
	addCarbonearCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('carbonear-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "17 Industrial Crescent, Carbonear, Newfoundland and Labrador", null, true, "../../", true, 13);
		}
	},
	addRegattaCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('regatta-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "80-84 Elizabeth Avenue, St. John's, Newfoundland and Labrador", null, true, "../../", true, 15);
		}
	},
	addDuckworthCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('duckworth-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "285 Duckworth Street, St. John's, Newfoundland and Labrador", null, true, "../../", true, 15);
		}
	},
	addPlacentiaCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('placentia-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "Dunville, Placentia, Newfoundland and Labrador", null, true, "../../", true, 12);
		}
	},
	addHvgbCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('hvgb-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "163 Hamilton River Road, Happy Valley-Goose Bay, Newfoundland and Labrador", null, true, "../../", true, 12);
		}
	},
	addStephenvilleCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('stephenville-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "29 Carolina Avenue, Stephenville, Newfoundland and Labrador", null, true, "../../", true, 13);
		}
	},
	addSpringdaleCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('springdale-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "142 Little Bay Rd, Springdale, Newfoundland and Labrador", null, true, "../../", true, 13);
		}
	},
	addLewisporteCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('lewisporte-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "224 Main St, Lewisporte, Newfoundland and Labrador", null, true, "../../", true, 12);
		}
	},
	addGanderCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('gander-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "Airport Boulevard, Gander, Newfoundland and Labrador", null, true, "../../", true, 13);
		}
	},
	addGFWCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('gfw-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "42 Hardy Avenue, Grand Falls-Windsor, Newfoundland and Labrador", null, true, "../../", true, 13);
		}
	},
	addCornerBrookCwcMap: function() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById('cornerbrook-cwc-map'));
			map.addControl(new GSmallMapControl());
			
            var marker = gMap.addMarker(map, "1-3 Union Street, Corner Brook, Newfoundland and Labrador", null, true, "../../", true, 15);
		}
	},
	addMarker: function(map, address, info, isOpened, app_root, setCenter, zoom) {
		var marker;
		if(isOpened) {
			var icon = new GIcon(G_DEFAULT_ICON, app_root+'images/gicon-blue2.png');
			icon.printImage = app_root+'images/gicon-blue2.png';
			icon.mozPrintImage = app_root+'images/gicon-blue2.png';
		}
		else {
			var icon = new GIcon(G_DEFAULT_ICON, app_root+'images/gicon-grey.png');
			icon.printImage = app_root+'images/gicon-grey.png';
			icon.mozPrintImage = app_root+'images/gicon-grey.png';
		}
		
		var geocoder = new GClientGeocoder();
		geocoder.getLocations(address, function(response) {
			if (response) {
				// if server is busy then resend request
				if (response.Status.code == 620) gMap.addMarker(map, address, info, isOpened, app_root);
				// if address is not found then resend request with community only.
				else if (response.Status.code == 602) {
					if (address.indexOf(',') != address.lastIndexOf(',')) gMap.addMarker(map, address.substring(address.indexOf(',')+1), info, isOpened, app_root);
				}
				// if response is OK then add the marker.
				else if (response.Status.code == 200) {
					var place = response.Placemark[0];
					var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					if (info) {
						marker = new GMarker(point, icon);
						marker.bindInfoWindowHtml(info);
					}
					else marker = new GMarker(point, icon, true);
					map.addOverlay(marker);
					if ((setCenter) && (zoom)) map.setCenter(point, zoom);
				}
			}
		});
	}
};

window.addEvent('domready', function(){
	gMap.start();
});
