var map;
var infoWin;

var zoomFactor = 2;
var numLevels = 18;

var alertLat = null;
var alertLng = null;

var carparkLat = null;
var carparkLng = null;

var webcamLat = null;
var webcamLng = null;

var mapTypes = new Array();
mapTypes['map'] = 0;
mapTypes['satellite'] = 1;
mapTypes['hybrid'] = 2;

var futureStationsIcon = null;
var parkingIcon = null;
var cameraIcon = null;

var t4dRoutes = {
    data: [],
    overlays: [],
    multilineOverlays: [],
    stopsOverlays: []
}

var t4dAlerts = {
		data: [],
		overlays: [],
		customIcons: []
}

var t4dParking = {
		data: [],
		overlays: []
}

var t4dWebcam = {
		data: [],
		overlays: [],
		customIcons: []
}

function load() {
	if ('projects' == page && Ext.fly('routeId')) {
		Ext.Ajax.request({
			url: ajaxUrl + '?p=ajax&ajaxAction=routeData&routeId=' + Ext.fly('routeId').getValue(),
			method: 'GET',
			success: loadSmallMap,
			failure: loadSmallMapFailure,
			params: {}
		});
	} else if ('projects' == page) {
		Ext.select('img.overviewListImage').setOpacity(0.7);
	} else if ('maps' == page) {
		Ext.Ajax.request({
			url: ajaxUrl + '?p=ajax&ajaxAction=maps',
			method: 'GET',
			success: loadMap,
			failure: loadMapFailure,
			params: {}
		});
	} else if ('alerts' == page && alertLat && alertLng) {
		map = new GMap(document.getElementById("gSmallMap"));
		
		centerPoint = new GLatLng(alertLat, alertLng);
		map.setCenter(centerPoint, 14);
		marker = new GMarker(centerPoint, {icon: customIcon});
		
		GEvent.addListener(marker, "click", function() {
			location.href = alertMap;
        });
		
		map.addOverlay(marker);
	} else if ('parking' == page && carparkLat && carparkLng) {
		map = new GMap(document.getElementById("gSmallMap"));
		
		centerPoint = new GLatLng(carparkLat, carparkLng);
		map.setCenter(centerPoint, 14);
		marker = new GMarker(centerPoint, {icon: customIcon});
		
		GEvent.addListener(marker, "click", function() {
			location.href = carparkMap;
		});
		
		map.addOverlay(marker);
	} else if ('camera' == page && webcamLat && webcamLng) {
		map = new GMap(document.getElementById("gSmallMap"));
		
		centerPoint = new GLatLng(webcamLat, webcamLng);
		map.setCenter(centerPoint, 14);
		marker = new GMarker(centerPoint, {icon: customIcon});
		
		GEvent.addListener(marker, "click", function() {
			location.href = webcamMap;
		});
		
		map.addOverlay(marker);
	} else if ('contact-us' == page) {
		$("#contactForm").validate({
			submitHandler: function(form) {
			    form.submit();
			},
			rules: {
				firstName: {
					required: true,
					minlength: 2
			    },
				surname: {
					required: true,
					minlength: 2
				},
			    email: {
			    	required: true,
			    	email: true
			    },
			    securityCode: {
			    	required: true
			    }
	  		}
		});
		refreshCaptcha();
	} else if ('your-feedback' == page) {
		$("#feedbackForm").validate({
			submitHandler: function(form) {
				form.submit();
			},
			rules: {
				firstName: {
					required: true,
					minlength: 2
				},
				surname: {
					required: true,
					minlength: 2
				},
				email: {
					required: true,
					email: true
				},
				securityCode: {
					required: true
				}
			}
		});
		refreshCaptcha();
	} else if ('home' == page) {
		var options = {
	  		newsList: "#ticker",
	 		startDelay: 10,
	 		placeHolder1: "  "
		}
		$().newsTicker(options);
		Ext.fly('ticker').dom.style.display = 'block';

		Ext.fly('home-about-us-image').setOpacity(0.7);
		Ext.fly('home-projects-image').setOpacity(0.7);
		Ext.fly('home-maps-image').setOpacity(0.7);
		Ext.fly('home-alerts-image').setOpacity(0.7);
	} else if ('getting-around' == page) {
		Ext.fly('getting-around-image1').setOpacity(0.7);
		Ext.fly('getting-around-image2').setOpacity(0.7);
		Ext.fly('getting-around-image3').setOpacity(0.7);
		Ext.fly('getting-around-image4').setOpacity(0.7);
	}
}

function loadMap(response, opts)
{
	var srv = Ext.decode(response.responseText);
	var counter, i, j, multilinePoints, multilinePoints;
	var tmpOverlays = [], tmpOverlayCounter = 0;
	
	map = new GMap(document.getElementById("googleMap"));
	
	infoWin = map.getInfoWindow();
	
	var gMapTypes = map.getMapTypes();
    var defaultMapType = mapTypes[srv.gMapType];
	
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(1*srv.gMapLat, 1*srv.gMapLon), 1*srv.gMapZoom);
	map.setMapType(gMapTypes[defaultMapType]);
	
	if (srv.parking) {
		t4dParking.data = srv.parking;
		
		parkingIcon = new GIcon();
		parkingIcon.image = srv.parkingIcon;
		parkingIcon.iconSize  = new GSize(srv.parkingIconW, srv.parkingIconH);
		parkingIcon.shadowSize  = new GSize(0, 0);
		parkingIcon.dragCrossSize  = new GSize(0, 0);
		parkingIcon.imageMap = [0,0, srv.parkingIconW,0, srv.parkingIconW,srv.parkingIconH, 0,srv.parkingIconH];
		parkingIcon.maxHeight = 3;
		parkingIcon.iconAnchor = new GPoint(0, srv.parkingIconH);
		parkingIcon.infoWindowAnchor = new GPoint(srv.parkingIconW / 1.3, srv.parkingIconH / 2.7);
		
		for (j in t4dParking.data.data) {
			if (!t4dParking.data.data[j].object_id) {
				continue;
			}
			t4dParking.overlays[j] = new GMarker(new GLatLng(t4dParking.data.data[j].lat, t4dParking.data.data[j].lng), {icon: parkingIcon});
			t4dParking.overlays[j].parkingId = j;
			GEvent.addListener(t4dParking.overlays[j], 'click', function(e, j) {
				openParkingInfoWin(this ,this.parkingId);
            });
			
			if ('' != t4dParking.data.checked) {
    			map.addOverlay(t4dParking.overlays[j]);
    			map.removeOverlay(t4dParking.overlays[j]);
    			map.addOverlay(t4dParking.overlays[j]);
    		}
		}
		Ext.DomHelper.append(Ext.fly('parkingListContainer'), {
			tag: 'div',
			id: 'parkingList',
			style: 'padding-right: 3px; text-align: left; margin-top: 5px; margin-right: 7px;'
		});
		Ext.fly('parkingList').insertHtml(
			'beforeEnd',
			"<input type='checkbox' " + t4dParking.data.checked + " id='parkingCheckbox' value='1'> " 
			+ "<a id='parkingTitle' href='javascript: void(0);'>" + t4dParking.data.parking_title + "</a>"
		);
		Ext.fly('parkingCheckbox').on('click', function(e, t) {
			var i, counter = 0;
			if (t.checked) {
				for (i in t4dParking.data.data) {
					if (t4dParking.data.data[i].object_id && t4dParking.data.data[i].object_id == i) {
						map.addOverlay(t4dParking.overlays[i]);
    	    		}
				}
				t4dParking.data.checked = 'checked';
			} else {
				for (i in t4dParking.data.data) {
					if (t4dParking.data.data[i].object_id && t4dParking.data.data[i].object_id == i) {
						map.removeOverlay(t4dParking.overlays[i]);
    	    		}
				}
				t4dParking.data.checked = '';
			}
			Ext.Ajax.request({
				url: ajaxUrl + '?p=ajax&ajaxAction=changeParkingDisplay',
				method: 'GET',
				params: {
					displayed: t.checked
				}
			});
		});
		Ext.fly('parkingTitle').on('click', function(e, t) {
			var t = Ext.get('parkingCheckbox');
			if (!t.dom.checked) {
				for (i in t4dParking.data.data) {
					if (t4dParking.data.data[i].object_id && t4dParking.data.data[i].object_id == i) {
						map.addOverlay(t4dParking.overlays[i]);
    	    		}
				}
				t4dParking.data.checked = 'checked';
				t.dom.checked = true;
				Ext.Ajax.request({
    				url: ajaxUrl + '?p=ajax&ajaxAction=changeParkingDisplay',
    				method: 'GET',
    				params: {
    					displayed: t.dom.checked
    				}
    			});
			}
			focusOnCarparks();
		});
		
		if (srv.focusOnCarpark) {
			focusOnCarpark(srv.focusOnCarpark);
		}
	}
	
	if (srv.alerts) {
		t4dAlerts.data = srv.alerts;
		for (i in t4dAlerts.data) {
			if (!t4dAlerts.data[i].alert_type_title) {
				continue;
			}
			for (j in t4dAlerts.data[i].data) {
				if (!t4dAlerts.data[i].data[j].object_id) {
					continue;
				}
				t4dAlerts.customIcons[j] = new GIcon();
				if (t4dAlerts.data[i].alertIcon) {
					t4dAlerts.customIcons[j].image = t4dAlerts.data[i].alertIcon;
					t4dAlerts.customIcons[j].iconSize  = new GSize(t4dAlerts.data[i].iconW, t4dAlerts.data[i].iconH);
					t4dAlerts.customIcons[j].shadowSize  = new GSize(0, 0);
					t4dAlerts.customIcons[j].dragCrossSize  = new GSize(0, 0);
					t4dAlerts.customIcons[j].imageMap = t4dAlerts.data[i].iconImageMap;
					t4dAlerts.customIcons[j].maxHeight = 3;
					t4dAlerts.customIcons[j].iconAnchor = new GPoint(0, t4dAlerts.data[i].iconH);
					t4dAlerts.customIcons[j].infoWindowAnchor = new GPoint(t4dAlerts.data[i].iconW / 1.3, t4dAlerts.data[i].iconH / 2.7);
				}
				t4dAlerts.overlays[j] = new GMarker(new GLatLng(t4dAlerts.data[i].data[j].lat, t4dAlerts.data[i].data[j].lng), {icon: t4dAlerts.customIcons[j]});
				t4dAlerts.overlays[j].alertId = j;
				GEvent.addListener(t4dAlerts.overlays[j], 'click', function(e, j) {
					openAlertInfoWin(this ,this.alertId);
				});
				
				if ('' != t4dAlerts.data[i].checked) {
					map.addOverlay(t4dAlerts.overlays[j]);
				}
			}
			Ext.DomHelper.append(Ext.fly('alertListContainer'), {
				tag: 'div',
				id: 'alert' + t4dAlerts.data[i].alert_type_id,
				style: 'padding-right: 3px; text-align: left; margin-top: 5px; margin-right: 7px;'
			});
			Ext.fly('alert' + t4dAlerts.data[i].alert_type_id).insertHtml(
					'beforeEnd',
					"<input type='checkbox' " + t4dAlerts.data[i].checked + " id='alertCheckbox" 
					+ t4dAlerts.data[i].alert_type_id + "' value='" + t4dAlerts.data[i].alert_type_id + "'> " 
					+ "<a id='alertTitle" + t4dAlerts.data[i].alert_type_id + "' href='javascript: void(0);'>" + t4dAlerts.data[i].alert_type_title + "</a>"
			);
			Ext.fly('alertCheckbox' + t4dAlerts.data[i].alert_type_id).on('click', function(e, t) {
				var i, counter = 0;
				if (t.checked) {
					for (i in t4dAlerts.data[t.value].data) {
						if (t4dAlerts.data[t.value].data[i].object_id && t4dAlerts.data[t.value].data[i].object_id == i) {
							map.addOverlay(t4dAlerts.overlays[i]);
						}
					}
					t4dAlerts.data[t.value].checked = 'checked';
				} else {
					for (i in t4dAlerts.data[t.value].data) {
						if (t4dAlerts.data[t.value].data[i].object_id && t4dAlerts.data[t.value].data[i].object_id == i) {
							map.removeOverlay(t4dAlerts.overlays[i]);
						}
					}
					t4dAlerts.data[t.value].checked = '';
				}
				Ext.Ajax.request({
					url: ajaxUrl + '?p=ajax&ajaxAction=changeAlertDisplay',
					method: 'GET',
					params: {
					objectId: t.value,
					displayed: t.checked
				}
				});
			});
			Ext.fly('alertTitle' + t4dAlerts.data[i].alert_type_id).on('click', function(e, t) {
				var objectId = t.id.replace('alertTitle', '');
				var t = Ext.get('alertCheckbox' + objectId);
				if (!t.dom.checked) {
					for (i in t4dAlerts.data[objectId].data) {
						if (t4dAlerts.data[objectId].data[i].object_id && t4dAlerts.data[objectId].data[i].object_id == i) {
							map.addOverlay(t4dAlerts.overlays[i]);
						}
					}
					t4dAlerts.data[objectId].checked = 'checked';
					t.dom.checked = true;
					Ext.Ajax.request({
						url: ajaxUrl + '?p=ajax&ajaxAction=changeAlertDisplay',
						method: 'GET',
						params: {
						objectId: objectId,
						displayed: t.dom.checked
					}
					});
				}
				focusOnAlerts(objectId);
			});
		}
		if (srv.focusOnAlert) {
			focusOnAlert(srv.focusOnAlert);
		}
	}
	
	//webcam
	if (srv.webcam) {
		t4dWebcam.data = srv.webcam;
		for (i in t4dWebcam.data) {
			if (!t4dWebcam.data[i].category_title) {
				continue;
			}
			for (j in t4dWebcam.data[i].data) {
				if (!t4dWebcam.data[i].data[j].object_id) {
					continue;
				}
				t4dWebcam.customIcons[j] = new GIcon();
				if (t4dWebcam.data[i].webcamIcon) {
					t4dWebcam.customIcons[j].image = t4dWebcam.data[i].webcamIcon;
					t4dWebcam.customIcons[j].iconSize  = new GSize(t4dWebcam.data[i].iconW, t4dWebcam.data[i].iconH);
					t4dWebcam.customIcons[j].shadowSize  = new GSize(0, 0);
					t4dWebcam.customIcons[j].dragCrossSize  = new GSize(0, 0);
					t4dWebcam.customIcons[j].imageMap = t4dWebcam.data[i].iconImageMap;
					t4dWebcam.customIcons[j].maxHeight = 3;
					t4dWebcam.customIcons[j].iconAnchor = new GPoint(0, t4dWebcam.data[i].iconH);
					t4dWebcam.customIcons[j].infoWindowAnchor = new GPoint(t4dWebcam.data[i].iconW / 1.3, t4dWebcam.data[i].iconH / 2.7);
				}
				t4dWebcam.overlays[j] = new GMarker(new GLatLng(t4dWebcam.data[i].data[j].lat, t4dWebcam.data[i].data[j].lng), {icon: t4dWebcam.customIcons[j]});
				t4dWebcam.overlays[j].webcamId   = j;
				GEvent.addListener(t4dWebcam.overlays[j], 'click', function(e, j) {
					openWebcamInfoWin(this ,this.webcamId);
				});
				
				if ('' != t4dWebcam.data[i].checked) {
					map.addOverlay(t4dWebcam.overlays[j]);
				}
			}
			Ext.DomHelper.append(Ext.fly('webcamListContainer'), {
				tag: 'div',
				id: 'webcam' + t4dWebcam.data[i].category_id,
				style: 'border-right: 4px solid ' + '#' + t4dWebcam.data[i].color + ';padding-right: 3px; text-align: left; margin-top: 5px; margin-right: 7px;'
			});

			Ext.fly('webcam' + t4dWebcam.data[i].category_id).insertHtml(
					'beforeEnd',
					"<input type='checkbox' " + t4dWebcam.data[i].checked + " id='webcamCheckbox" 
					+ t4dWebcam.data[i].category_id + "' value='" + t4dWebcam.data[i].category_id + "'> " 
					+ "<a id='webcamTitle" + t4dWebcam.data[i].category_id + "' href='javascript: void(0);'>" + t4dWebcam.data[i].category_title + "</a>"
			);
			Ext.fly('webcamCheckbox' + t4dWebcam.data[i].category_id).on('click', function(e, t) {
				var i, counter = 0;
				if (t.checked) {
					for (i in t4dWebcam.data[t.value].data) {
						if (t4dWebcam.data[t.value].data[i].object_id && t4dWebcam.data[t.value].data[i].object_id == i) {
							map.addOverlay(t4dWebcam.overlays[i]);
						}
					}
					t4dWebcam.data[t.value].checked = 'checked';
				} else {
					for (i in t4dWebcam.data[t.value].data) {
						if (t4dWebcam.data[t.value].data[i].object_id && t4dWebcam.data[t.value].data[i].object_id == i) {
							map.removeOverlay(t4dWebcam.overlays[i]);
						}
					}
					t4dWebcam.data[t.value].checked = '';
				}
				Ext.Ajax.request({
					url: ajaxUrl + '?p=ajax&ajaxAction=changeWebcamDisplay',
					method: 'GET',
					params: {
					objectId: t.value,
					displayed: t.checked
				}
				});
			});
			Ext.fly('webcamTitle' + t4dWebcam.data[i].category_id).on('click', function(e, t) {
				var objectId = t.id.replace('webcamTitle', '');
				var t = Ext.get('webcamCheckbox' + objectId);
				if (!t.dom.checked) {
					for (i in t4dWebcam.data[objectId].data) {
						if (t4dWebcam.data[objectId].data[i].object_id && t4dWebcam.data[objectId].data[i].object_id == i) {
							map.addOverlay(t4dWebcam.overlays[i]);
						}
					}
					t4dWebcam.data[objectId].checked = 'checked';
					t.dom.checked = true;
					Ext.Ajax.request({
						url: ajaxUrl + '?p=ajax&ajaxAction=changeWebcamDisplay',
						method: 'GET',
						params: {
						objectId: objectId,
						displayed: t.dom.checked
					}
					});
				}
				focusOnWebcams(objectId);
			});
		}
		if (srv.focusOnWebcam) {
			focusOnWebcam(srv.focusOnWebcam);
		}
	}
	//webcam
	
	if (srv.routes) {
		t4dRoutes.data = srv.routes;
		
		futureStationsIcon = new GIcon();
		futureStationsIcon.image = srv.stopIcon;
		futureStationsIcon.iconSize  = new GSize(srv.stopIconW, srv.stopIconH);
		futureStationsIcon.shadowSize  = new GSize(0, 0);
		futureStationsIcon.dragCrossSize  = new GSize(0, 0);
		futureStationsIcon.imageMap = [0,0, srv.stopIconW,0, srv.stopIconW,srv.stopIconH, 0,srv.stopIconH];
		futureStationsIcon.maxHeight = 3;
		futureStationsIcon.iconAnchor = new GPoint(0, srv.stopIconH);
		
		for (i in t4dRoutes.data) {
    		if (!t4dRoutes.data[i].object_id) {
    			continue;
    		}
    		if ('yes' == t4dRoutes.data[i].multiline) {
    			t4dRoutes.multilineOverlays[t4dRoutes.data[i].object_id] = [];
    			multilinePoints = Ext.decode(t4dRoutes.data[i].latlngs);
    			multilineLevels = Ext.decode(t4dRoutes.data[i].levels);
    			counter = 0;
    			for (j in multilinePoints) {
    				if (counter != j) {
    	    			continue;
    	    		}
    				//var latlngs = multilinePoints[j].replace(/\\\\/g, "\\");
    				var latlngs = multilinePoints[j];
    				t4dRoutes.multilineOverlays[t4dRoutes.data[i].object_id].push(new GPolyline.fromEncoded({
    	    			color: '#' + t4dRoutes.data[i].color, 
    	    			weight: 1 * t4dRoutes.data[i].weight, 
    	    			opacity: 1 * t4dRoutes.data[i].opacity, 
    	    			points: latlngs, 
    	    			levels: multilineLevels[j], 
    	    			zoomFactor: zoomFactor, 
    	    			numLevels: numLevels 
    	    		}));
    				if ('' != t4dRoutes.data[i].checked) {
    	    			//map.addOverlay(t4dRoutes.multilineOverlays[t4dRoutes.data[i].object_id][counter]);
    	    			tmpOverlays[tmpOverlayCounter] = t4dRoutes.multilineOverlays[t4dRoutes.data[i].object_id][counter];
    	    			tmpOverlayCounter++;
    	    		}
    				counter++;
    			}
    		} else {
	    		var latlngs = t4dRoutes.data[i].latlngs.replace(/\\\\/g, "\\");
	    		t4dRoutes.overlays[t4dRoutes.data[i].object_id] = new GPolyline.fromEncoded({
	    			color: '#' + t4dRoutes.data[i].color, 
	    			weight: 1 * t4dRoutes.data[i].weight, 
	    			opacity: 1 * t4dRoutes.data[i].opacity, 
	    			points: latlngs, 
	    			levels: t4dRoutes.data[i].levels, 
	    			zoomFactor: zoomFactor, 
	    			numLevels: numLevels 
	    		});
	    		if ('' != t4dRoutes.data[i].checked) {
	    			//map.addOverlay(t4dRoutes.overlays[t4dRoutes.data[i].object_id]);
	    			tmpOverlays[tmpOverlayCounter] = t4dRoutes.overlays[t4dRoutes.data[i].object_id];
	    			tmpOverlayCounter++;
	    		}
    		}
    		
    		if (t4dRoutes.data[i].type == 'Active') {
	    		Ext.DomHelper.append(Ext.fly('routeListContainer'), {
	    			tag: 'div',
	    			id: 'route' + t4dRoutes.data[i].object_id,
	    			style: 'border-right: 4px solid ' + '#' + t4dRoutes.data[i].color + '; padding-right: 3px; text-align: left; margin-top: 5px; margin-right: 7px;'
	    		});
    		} else {
    			Ext.DomHelper.append(Ext.fly('routeListContainer2'), {
	    			tag: 'div',
	    			id: 'route' + t4dRoutes.data[i].object_id,
	    			style: 'border-right: 4px solid ' + '#' + t4dRoutes.data[i].color + '; padding-right: 3px; text-align: left; margin-top: 5px; margin-right: 7px;'
	    		});
    		}
    		Ext.fly('route' + t4dRoutes.data[i].object_id).insertHtml(
    			'beforeEnd',
    			"<input type='checkbox' " + t4dRoutes.data[i].checked + " id='routeCheckbox" 
    			+ t4dRoutes.data[i].object_id + "' value='" + t4dRoutes.data[i].object_id + "'> " 
    			+ "<a id='routeTitle" + t4dRoutes.data[i].object_id + "' href='javascript: void(0);'>" + t4dRoutes.data[i].title + "</a>"
    		);
    		Ext.fly('routeCheckbox' + t4dRoutes.data[i].object_id).on('click', function(e, t) {
    			var i, counter = 0;
    			if (t.checked) {
    				if (t4dRoutes.data[t.value].multiline == 'yes') {
    					for (i in t4dRoutes.multilineOverlays[t.value]) {
    						if (counter != i) {
    	    	    			continue;
    	    	    		}
    						map.addOverlay(t4dRoutes.multilineOverlays[t.value][i]);
    						counter++;
    					}
    				} else {
    					map.addOverlay(t4dRoutes.overlays[t.value]);
    				}
    				t4dRoutes.data[t.value].checked = 'checked';
    				showHideStops(t.value, 'yes');
    			} else {
    				if (t4dRoutes.data[t.value].multiline == 'yes') {
    					for (i in t4dRoutes.multilineOverlays[t.value]) {
    						if (counter != i) {
    	    	    			continue;
    	    	    		}
    						map.removeOverlay(t4dRoutes.multilineOverlays[t.value][i]);
    						counter++;
    					}
    				} else {
    					map.removeOverlay(t4dRoutes.overlays[t.value]);
    				}
    				t4dRoutes.data[t.value].checked = '';
    				showHideStops(t.value, 'no');
    			}
    			Ext.Ajax.request({
    				url: ajaxUrl + '?p=ajax&ajaxAction=changeDisplay',
    				method: 'GET',
    				params: {
    					objectId: t.value,
    					displayed: t.checked
    				}
    			});
    		});
    		Ext.fly('routeTitle' + t4dRoutes.data[i].object_id).on('click', function(e, t) {
    			var objectId = t.id.replace('routeTitle', '');
    			var t = Ext.get('routeCheckbox' + objectId), counter = 0;
    			if (!t.dom.checked) {
    				if (t4dRoutes.data[objectId].multiline == 'yes') {
    					for (i in t4dRoutes.multilineOverlays[objectId]) {
    						if (counter != i) {
    	    	    			continue;
    	    	    		}
    						map.addOverlay(t4dRoutes.multilineOverlays[objectId][i]);
    						counter++;
    					}
    				} else {
    					map.addOverlay(t4dRoutes.overlays[objectId]);
    				}
    				t4dRoutes.data[t.dom.value].checked = 'checked';
    				t.dom.checked = true;
    				Ext.Ajax.request({
        				url: ajaxUrl + '?p=ajax&ajaxAction=changeDisplay',
        				method: 'GET',
        				params: {
        					objectId: objectId,
        					displayed: t.dom.checked
        				}
        			});
    				showHideStops(objectId, 'yes');
    			}
    			focusOnRoute(objectId);
    		});
    		if ('' != t4dRoutes.data[i].stops) {
    			t4dRoutes.stopsOverlays[i] = [];
    			for (j in t4dRoutes.data[i].stops) {
    				if (t4dRoutes.data[i].stops[j].lat) {
        				var marker = new GMarker(new GLatLng(1*t4dRoutes.data[i].stops[j].lat, 1*t4dRoutes.data[i].stops[j].lng), {icon: futureStationsIcon});
        				if ('' != t4dRoutes.data[i].checked) {
        					map.addOverlay(marker);
        				}
        				t4dRoutes.stopsOverlays[i].push(marker);
    				}
    			}
    		}
    	}
		
		if (tmpOverlayCounter > 0) {
			tmpOverlayCounter--;
			for (i = tmpOverlayCounter; i >= 0; i--) {
				map.addOverlay(tmpOverlays[i]);
			}
		}
		
		if (srv.focusOnRoute) {
			focusOnRoute(srv.focusOnRoute);
		}
	}
}

function openAlertInfoWin(marker, id) 
{
	infoWin.hide();
	var id = 'alertInfoWin' + id;
	var content = '<div class="infoWinDiv" style="width: 350px; height: ' + (Ext.fly(id).getHeight()-30) + 'px;">' + Ext.fly(id).dom.innerHTML + '</div>';
	marker.openInfoWindowHtml(content);
}

function openWebcamInfoWin(marker, id) 
{
	infoWin.hide();
	var id = 'webcamInfoWin' + id;
	var content = '<div class="infoWinDiv" style="width: 350px; height: ' + (Ext.fly(id).getHeight()-30) + 'px;">' + Ext.fly(id).dom.innerHTML + '</div>';
	marker.openInfoWindowHtml(content);
}

function openParkingInfoWin(marker, id) 
{
	infoWin.hide();
	var id = 'carparkInfoWin' + id;
	var content = '<div class="infoWinDiv" style="width: 350px; height: ' + (Ext.fly(id).getHeight()-30) + 'px;">' + Ext.fly(id).dom.innerHTML + '</div>';
	marker.openInfoWindowHtml(content);
}

function focusOnRoute(objectId)
{
	infoWin.hide();
	if ('yes' == t4dRoutes.data[objectId].multiline) {
		var boundsInfo = t4dRoutes.data[objectId].multiline_bounds.split('|');
		var bounds = new GLatLngBounds(new GLatLng(boundsInfo[0], boundsInfo[1]), new GLatLng(boundsInfo[2], boundsInfo[3]));

		map.setCenter(new GLatLng(bounds.getCenter().y, bounds.getCenter().x), map.getBoundsZoomLevel(bounds));
	} else {
		map.setCenter(new GLatLng(t4dRoutes.overlays[objectId].getBounds().getCenter().y, t4dRoutes.overlays[objectId].getBounds().getCenter().x), map.getBoundsZoomLevel(t4dRoutes.overlays[objectId].getBounds()));
	}
}

function focusOnAlerts(objectId)
{
	infoWin.hide();
	var bounds = new GLatLngBounds(new GLatLng(1*t4dAlerts.data[objectId].latMin, 1*t4dAlerts.data[objectId].lngMin), new GLatLng(1*t4dAlerts.data[objectId].latMax, 1*t4dAlerts.data[objectId].lngMax));
	var zoom = map.getBoundsZoomLevel(bounds);
	if (zoom > 15) {
		zoom = 15;
	}
	map.setCenter(new GLatLng(bounds.getCenter().y, bounds.getCenter().x), zoom);
}

function focusOnWebcams(objectId)
{
	infoWin.hide();
	var bounds = new GLatLngBounds(new GLatLng(1*t4dWebcam.data[objectId].latMin, 1*t4dWebcam.data[objectId].lngMin), new GLatLng(1*t4dWebcam.data[objectId].latMax, 1*t4dWebcam.data[objectId].lngMax));
	var zoom = map.getBoundsZoomLevel(bounds);
	if (zoom > 15) {
		zoom = 15;
	}
	map.setCenter(new GLatLng(bounds.getCenter().y, bounds.getCenter().x), zoom);
}

function focusOnCarparks()
{
	infoWin.hide();
	var bounds = new GLatLngBounds(new GLatLng(1*t4dParking.data.latMin, 1*t4dParking.data.lngMin), new GLatLng(1*t4dParking.data.latMax, 1*t4dParking.data.lngMax));
	var zoom = map.getBoundsZoomLevel(bounds);
	if (zoom > 15) {
		zoom = 15;
	}
	map.setCenter(new GLatLng(bounds.getCenter().y, bounds.getCenter().x), zoom);
}

function focusOnAlert(objectId)
{
	infoWin.hide();
	map.setCenter(t4dAlerts.overlays[objectId].getLatLng(), 15);
	openAlertInfoWin(t4dAlerts.overlays[objectId], objectId);
}

function focusOnWebcam(objectId)
{
	infoWin.hide();
	map.setCenter(t4dWebcam.overlays[objectId].getLatLng(), 15);
	openWebcamInfoWin(t4dWebcam.overlays[objectId], objectId);
}

function focusOnCarpark(objectId)
{
	infoWin.hide();
	map.setCenter(t4dParking.overlays[objectId].getLatLng(), 15);
	openParkingInfoWin(t4dParking.overlays[objectId], objectId);
}

function loadSmallMap(response, opts)
{
	var srv = Ext.decode(response.responseText);
	if (srv.latlngs) {
		map = new GMap(document.getElementById("gSmallMap"));
		
		//map.addControl(new GMapTypeControl(true));
		if ('yes' == srv.multiline) {
			var multilinePoints = Ext.decode(srv.latlngs);
			var multilineLevels = Ext.decode(srv.levels);
			var counter = 0, j, multilineOverlays = [];
			for (j in multilinePoints) {
				if (counter != j) {
	    			continue;
	    		}
				//var latlngs = multilinePoints[j].replace(/\\\\/g, "\\");
				var latlngs = multilinePoints[j];
				multilineOverlays.push(new GPolyline.fromEncoded({
	    			color: '#' + srv.color, 
	    			weight: 1 * srv.weight, 
	    			opacity: 1 * srv.opacity, 
	    			points: latlngs, 
	    			levels: multilineLevels[j], 
	    			zoomFactor: zoomFactor, 
	    			numLevels: numLevels 
	    		}));
	    		map.addOverlay(multilineOverlays[counter]);
				counter++;
			}
			
			boundsInfo = srv.multiline_bounds.split('|');
			var bounds = new GLatLngBounds(new GLatLng(boundsInfo[0], boundsInfo[1]), new GLatLng(boundsInfo[2], boundsInfo[3]));

			map.setCenter(new GLatLng(bounds.getCenter().y, bounds.getCenter().x), map.getBoundsZoomLevel(bounds));
		} else {
			var latlngs = srv.latlngs.replace(/\\\\/g, "\\");
			
			polyline = new GPolyline.fromEncoded({
				color: '#' + srv.color, 
				weight: 1 * srv.weight, 
				opacity: 1 * srv.opacity, 
				points: latlngs, 
				levels: srv.levels, 
				zoomFactor: zoomFactor, 
				numLevels: numLevels
			});
			
			map.addOverlay(polyline);
			map.setCenter(new GLatLng(polyline.getBounds().getCenter().y, polyline.getBounds().getCenter().x), map.getBoundsZoomLevel(polyline.getBounds()));
		}
	} else {
		loadSmallMapFailure();
	}
}

function showHideStops(objectId, yesNo) {
	var i, counter = 0;
	for (i in t4dRoutes.stopsOverlays[objectId]) {
		if (isNaN(i)) {
			continue;
		}
		if (i == counter) {
    		if ('yes' == yesNo) {
    			map.addOverlay(t4dRoutes.stopsOverlays[objectId][i]);
    		} else {
    			map.removeOverlay(t4dRoutes.stopsOverlays[objectId][i]);
    		}
		}
		counter++;
	}
}

function loadSmallMapFailure()
{
	Ext.fly('gSmallMapTitle').remove();
	Ext.fly('gSmallMap').remove();
}

function loadMapFailure()
{
	/*Ext.fly('gSmallMapTitle').remove();
	Ext.fly('gSmallMap').remove();*/
}

function resizeText(fontSize) {
	document.body.style.fontSize = fontSize + "em" ;
}

function refreshCaptcha() {
    var captchaImage = Ext.get('captcha_image');
    if(captchaImage) {
        Ext.Ajax.request({
			url: ajaxUrl + '?p=ajax&ajaxAction=refreshCaptcha&rnd=' + Math.random(),
			method: 'GET',
			success: function (response, opts) {
        		captchaImage.dom.src = response.responseText;
        	},
			failure: defaultFailure
		});
    }
}

function XAFtrim(input) {
    return input.replace(/^\s+|\s+$/g, '');
}

function t4dSearch()
{
	if ('' == XAFtrim(Ext.fly('t4d-keyword').dom.value)) {
	    return false;
	}
	return true;
}

function defaultFailure()
{
	alert('Server connection problems. Please try again.');
}