﻿$(document).ready(function() {
	//mapInit();
	$(":checkbox:not(.layer-group)").click(layerClick);
});

/*
function mapInit() {
	map = new GMap2($("#map_canvas").get(0));
	map.setCenter(new GLatLng(33.98364000000001, -117.89930500000001), 10);
	
	var mapTypePos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10));
	var mapControlPos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 50));

	map.addControl(new GLargeMapControl(), mapControlPos);
	map.addControl(new GMapTypeControl(), mapTypePos);
	map.addControl(new GScaleControl());
	
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	
	cgm = new CRGGoogleMap(map);
	cgm.onLayersLoadStart = function() { $("#loading").show(); };
	cgm.onLayersLoadComplete = function() { $("#loading").hide(); };
	cgm.onZoomEnd = function(o, n) { setupLayers(); };
	cgm.initialize();

	addLayers();

	setupLayers();
}
*/
function addLayers() {
	var micon = new GIcon(G_DEFAULT_ICON);
	micon.image = servicePath + '/images/map/ico_metro.png';
	micon.iconSize = new GSize(13, 13);
	micon.iconAnchor = new GPoint(0, 0);
	micon.shadowSize = new GSize(0, 0);
	micon.infoWindowAnchor = new GPoint(25, 7);

	var cicon = new GIcon(G_DEFAULT_ICON);
	cicon.image = servicePath + '/images/map/marker_cluster.png';
	cicon.iconSize = new GSize(53, 52);
	cicon.iconAnchor = new GPoint(0, 0);
	cicon.shadowSize = new GSize(0, 0);
	cicon.infoWindowAnchor = new GPoint(25, 7);

	ml = new MapLayer("Events", servicePath + "/MapService.svc/GetEventsByBounds", {}, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8 });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/events_icon_sm.png') };
	ml.onClick = evMarkerClick;
	cgm.addLayer(ml);

	var jdata = { 'query': 'Architecture & Public Art' };
	ml = new MapLayer("Architecture & Public Art", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/architecture_icon.png') };
	ml.onClick = orgMarkerClick;
	cgm.addLayer(ml);

	var jdata = { 'query': 'Visual Arts' };
	ml = new MapLayer("Visual Arts", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/visualart_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Museums' };
	ml = new MapLayer("Museums", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/museum_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Live Theater' };
	ml = new MapLayer("Live Theater", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/theatre_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'World Music' };
	ml = new MapLayer("World Music", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/worldmusic_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Opera' };
	ml = new MapLayer("Opera", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/opera_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Symphony' };
	ml = new MapLayer("Symphony", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/symphony_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Dance' };
	ml = new MapLayer("Dance", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/dance_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Film' };
	ml = new MapLayer("Film", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/film_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Band & Ensembles' };
	ml = new MapLayer("Band & Ensembles", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/band_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Farmers Market' };
	ml = new MapLayer("Farmers Market", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/farmers_icon.png') };
	ml.onClick = orgMarkerClick;
	cgm.addLayer(ml);

	var jdata = { 'query': 'Festival' };
	ml = new MapLayer("Festival", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/festival_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Parks & Gardens' };
	ml = new MapLayer("Parks & Gardens", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/park_icon.png') };
	cgm.addLayer(ml);

	var jdata = { 'query': 'Multidisciplinary' };
	ml = new MapLayer("Multidisciplinary", servicePath + "/MapService.svc/GetOrgsByBounds", jdata, { visible: false, center: false, filterMb: true, cluster: true, maxClusterZoom: 12, minZoom: 8, group: 'dest' });
	ml.ClusterMarkerOptions = { icon: cicon, clickable: true, labelOffset: new GSize(22, 19) };
	ml.onMaxResults = function() { $("#tooManyResults").show(); };
	ml.onLayerLoadComplete = function() { $("#tooManyResults").hide(); };
	ml.onClick = orgMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/multi_icon.png') };
	cgm.addLayer(ml);

	jdata = { 'query': 'airports' };
	ml = new MapLayer("Airports", servicePath + "/MapService.svc/GetMetroByQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, autoLabel: false });
	ml.onClick = airportMarkerClick;
	ml.MarkerOptions = { icon: createicon(servicePath + '/images/map/airport_icon.png') };
	cgm.addLayer(ml);

	if (typeof (metrovisible) == "undefined") {
		metrovisible = true;
	}

	jdata = { 'query': '' };
	ml = new MapLayer("Metro Points", servicePath + "/MapService.svc/GetMetroByQuery", jdata, { visible: metrovisible, center: false, filterMb: false, cluster: false, autoLabel: false, group: 'Metro' });
	ml.onClick = metroMarkerClick;
	ml.MarkerOptions = { icon: micon };
	cgm.addLayer(ml);

	tl = new TileLayer("Metro Lines", "http://mapserver.civicresource.com/xlamapservice/11111/XLA/_vemap/", { opacity: 1.0, visible: metrovisible, group: 'Metro' });
	cgm.addLayer(tl);	
}
function createicon(url) {
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = url;
	icon.iconSize = new GSize(19, 20);
	icon.iconAnchor = new GPoint(5, 5);
	icon.shadowSize = new GSize(0, 0);
	icon.infoWindowAnchor = new GPoint(25, 7);
	return icon;
}
function airportMarkerClickOpen(mo) {
	$.ajax({
		type: "GET",
		url: servicePath + "/MapService.svc/GetAirportInfoWindow",
		data: { 'id': mo.Id },
		dataType: "json",
		//processData: false,
		//contentType: "application/json; charset=utf-8",
		success: function(data) {
			mo.GOverlay.openInfoWindow(data.d, { maxWidth: infoWindowWidth });
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			for (var property in XMLHttpRequest) {
				window.alert(property + ": " + XMLHttpRequest[property]);
			}
		}
	});
}
function metroMarkerClickOpen(mo) {
	$.ajax({
		type: "GET",
		url: servicePath + "/MapService.svc/GetMetroInfoWindow",
		data: { 'id': mo.Id },
		dataType: "json",
		//processData: false,
		//contentType: "application/json; charset=utf-8",
		success: function(data) {
			mo.GOverlay.openInfoWindow(data.d, { maxWidth: infoWindowWidth });
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			for (var property in XMLHttpRequest) {
				window.alert(property + ": " + XMLHttpRequest[property]);
			}
		}
	});
}
function orgMarkerClickOpen(mo) {
	$.ajax({
		type: "GET",
		url: servicePath + "/MapService.svc/GetOrgInfoWindow",
		data: { 'id': mo.Id },
		dataType: "json",
		//processData: false,
		//contentType: "application/json; charset=utf-8",
		success: function(data) {
			mo.GOverlay.openInfoWindow(data.d, { maxWidth: infoWindowWidth });
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			for (var property in XMLHttpRequest) {
				window.alert(property + ": " + XMLHttpRequest[property]);
			}
		}
	});
}
function evMarkerClickOpen(mo) {
	$.ajax({
		type: "GET",
		url: servicePath + "/MapService.svc/GetEventInfoWindow",
		data: { 'id': mo.Id },
		dataType: "json",
		//processData: false,
		//contentType: "application/json; charset=utf-8",
		success: function(data) {
			mo.GOverlay.openInfoWindow(data.d, { maxWidth: infoWindowWidth });
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			for (var property in XMLHttpRequest) {
				window.alert(property + ": " + XMLHttpRequest[property]);
			}
		}
	});
}
function metroMarkerClick(o, ll, oll) {
	var crgmap = this;
	var mo = crgmap.findOverlay(o);
	metroMarkerClickOpen(mo);
}
function airportMarkerClick(o, ll, oll) {
	var crgmap = this;
	var mo = crgmap.findOverlay(o);
	airportMarkerClickOpen(mo);
}
function orgMarkerClick(o, ll, oll) {
	var crgmap = this;
	var mo = crgmap.findOverlay(o);
	orgMarkerClickOpen(mo);
}
function evMarkerClick(o, ll, oll) {
	var crgmap = this;
	var mo = crgmap.findOverlay(o);
	evMarkerClickOpen(mo);
}
function toggle(name) {
	cgm.toggleLayer(name);
}
function pan(id) {
	cgm.panToOverlayById(id);
}
function mapResize() {
	$("#map_canvas").height(800);
}
var photosVisible = false;
function togglePhotos() {
	if (photosVisible) {
		photosLayer.hide();
		photosVisible = false;
	}
	else {
		photosLayer.show();
		photosVisible = true;
	}
}
function layerClick() {
	if ($(this).attr("checked")) {
		cgm.showLayer($(this).attr("name"));
	} else {
		cgm.hideLayer($(this).attr("name"));
	}
}
function toggleGroup(chk, gname) {
	if ($(chk).attr("checked")) {
		cgm.showLayerGroup(gname);
		$(":checkbox." + gname).attr("checked", "true");
	} else {
		cgm.hideLayerGroup(gname);
		$(":checkbox." + gname).removeAttr("checked");
	}
}
function toggleGroupDiv(img, gdiv) {
	$("#" + gdiv).toggle();
}
function setupLayers() {
	enableLayerGroup("Public Transit", "Metro");
	//enableLayerGroup("Destinations", "dest");
	enableLayer("Architecture & Public Art");
	enableLayer("Visual Arts");
	enableLayer("Museums");
	enableLayer("Live Theater");
	enableLayer("World Music");
	enableLayer("Opera");
	enableLayer("Symphony");
	enableLayer("Dance");
	enableLayer("Film");
	enableLayer("Band & Ensembles");
	enableLayer("Farmers Market");
	enableLayer("Festival");
	enableLayer("Parks & Gardens");
	enableLayer("Multidisciplinary");
}
function enableLayer(name) {
	_enableLayer(name, cgm.isLayerEnabled(name));
}
function enableLayerGroup(name, gname) {
	_enableLayer(name, cgm.isLayerGroupEnabled(gname));
}
function _enableLayer(name, enable) {
	var chkbox = $("input[name='" + name + "']");

	if (!enable) {
		chkbox.attr("disabled", "disabled");
		chkbox.next().css("color", "Gray");
	}
	else {
		chkbox.removeAttr("disabled");
		chkbox.next().css("color", "Black");
	}
}
var layerControlVisible = true;
function toggleLayerControl() {
	if (layerControlVisible) {
		$("#layers").hide(); //, { direction: "right" });
		layerControlVisible = false;
		$("#layer-toggle img").attr("src", "images/map/sidebarArrowLeft.png");
		//alignLayerControl();
	}
	else {
		$("#layerControl").css("left", 0);
		$("#layers").show();
		layerControlVisible = true;
		$("#layer-toggle img").attr("src", "images/map/sidebarArrow.png");
		//alignLayerControl();
	}
}

var destV = true;
function toggleDest() {
	if (destV) {
		$("#dest-expand").text("-");
		$("#dest-group").hide();
		destV = false;
	}
	else {
		$("#dest-expand").text("+");
		$("#dest-group").show();
		destV = true;
	}
}