// JavaScript Document

function createAccidentMarker(point, point_title, description, t_type, point_id, area, submittedby) {  
	var accidentIcon = new GIcon(G_DEFAULT_ICON);
	accidentIcon.image = 'images/map.car.png';
	accidentIcon.iconSize = new GSize(32, 32);
	accidentIcon.iconAnchor = new GPoint(32, 16);
	accidentIcon.infoWindowAnchor = new GPoint(2, 16);
	accidentIcon.shadow = 'images/icon_shadow.png';
	accidentIcon.shadowSize = new GSize(32, 32);

	var marker = new GMarker(point, {point_title: point_title, description: description, type: t_type, point_id: point_id, area: area, submittedby: submittedby, icon: accidentIcon });	
	GEvent.addListener(marker, 'click', function() {									 
		$("#accident_information").replaceWith('<div id="accident_information"></div>');
		if (description == "") {
			description = "No description available";	
		}
		$("#accident_information").slideDown();
		$("#accident_information").html('<strong>' + point_title  + '</strong><a href="javascript:void(0);" onClick="closeAccidentWindow();"><img id="close" src="images/closebutton.gif" title="Close this window" class="close"/></a><br/><br/>' + description + '<br/><br/>Reported By: ' + submittedby + '<br/>Report a Tip - <b>610-778-7900</b><br/>traffic@mcall.com </span>');		
	});		
	return marker;
}

function createConstructionMarker(point, point_title, description, t_type, point_id, area, submittedby) {  
	var constructionIcon = new GIcon(G_DEFAULT_ICON);
	constructionIcon.image = 'images/map.construction.png';
	constructionIcon.iconSize = new GSize(32, 32);
	constructionIcon.iconAnchor = new GPoint(32, 16);
	constructionIcon.infoWindowAnchor = new GPoint(2, 16);
	constructionIcon.shadow = 'images/icon_shadow.png';
	constructionIcon.shadowSize = new GSize(32, 32);

	var marker = new GMarker(point, {point_title: point_title, description: description, type: t_type, point_id: point_id, area: area, submittedby: submittedby, icon: constructionIcon });	
	GEvent.addListener(marker, 'click', function() {										 
		var folder = "";										 
		var imagetype = "";
		$("#construction_information").replaceWith('<div id="construction_information"></div>');
		if (description == "") {
			description = "No description available";	
		}
		$("#construction_information").slideDown();
		$("#construction_information").html('<strong>' + point_title  + '</strong><a href="javascript:void(0);" onClick="closeConstructionWindow();"><img id="close" src="images/closebutton.gif" title="Close this window" class="close"/></a><br/><br/>' + description + '<br/><br/>Reported By: ' + submittedby + '<br/>Report a Tip - <b>610-778-7900</b><br/>traffic@mcall.com </span>');		
	});		
	return marker;
}

function showAccident() {
	loadSubNavigation();
	if (GBrowserIsCompatible()) {
		var trafficInfo = new GTrafficOverlay();
		var map = new GMap2(document.getElementById("accident_map"));
		map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();
		map.removeMapType(G_HYBRID_MAP);
		map.addControl(new ExtMapTypeControl({showTraffic: true, showTrafficKey: true}));
		map.addControl(new GSmallMapControl());
		GDownloadUrl("./cache/accidents.xml", function(data) {
			if (data.length >= 1) {	
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
					var t_type = markers[i].getAttribute("type");
					var point_title = markers[i].getAttribute("title");
					var area = markers[i].getAttribute("area");
					var description = markers[i].getAttribute("description");
					var submittedby = markers[i].getAttribute("submittedby");
					var id = markers[i].getAttribute("id");
					var marker = createAccidentMarker(point, point_title, description, t_type, id, area, submittedby);
					map.addOverlay(marker);
					bounds.extend(point);
				}
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			}
			else {
				map.setCenter(new GLatLng(40.602229,-75.471537), 11);		
			}
		});
	}	
}

function showConstruction() {
	loadSubNavigation();
	if (GBrowserIsCompatible()) {
		var trafficInfo = new GTrafficOverlay();
		var map = new GMap2(document.getElementById("construction_map"));
		map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();
		map.removeMapType(G_HYBRID_MAP);
		map.addControl(new ExtMapTypeControl({showTraffic: true, showTrafficKey: true}));
		map.addControl(new GSmallMapControl());
		GDownloadUrl("./cache/construction.xml", function(data) {
			if (data.length >= 1) {		
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
					var t_type = markers[i].getAttribute("type");
					var point_title = markers[i].getAttribute("title");
					var area = markers[i].getAttribute("area");
					var description = markers[i].getAttribute("description");
					var submittedby = markers[i].getAttribute("submittedby");
					var id = markers[i].getAttribute("id");
					var marker = createConstructionMarker(point, point_title, description, t_type, id, area, submittedby);
					map.addOverlay(marker);
					bounds.extend(point);
				}
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			}
			else {
				map.setCenter(new GLatLng(40.602229,-75.471537), 11);		
			}
		});
	}	
}
