﻿
var map;
var cgm;
var mbounds;
var jdata = {};
var gMapControl, gMapTypeControl;

$(document).ready(function() {
	mapInit();
	repos();
	$(":checkbox:not(.layer-group)").click(layerClick);
	$(window).resize(function() {
		repos();
	});

});
function repos() {
	var legendTop = $("#map_canvas").offset().top;
	var legendLeft = $("#map_canvas").offset().left + $("#map_canvas").width() - $("#layerControl").width();
	$("#layerControl").css("top", legendTop + "px").css("left", legendLeft + "px");
}
function layerClick() {
	toggle($(this).val());
}
function forPrint() {
	$("#layerControl").hide();
	map.removeControl(gMapControl);
	map.removeControl(gMapTypeControl);
}

var micon = new GIcon(G_DEFAULT_ICON);
micon.image = '/images/map/ico_dest03c.png';
micon.iconSize = new GSize(32, 32);
micon.iconAnchor = new GPoint(0, 0);
micon.shadowSize = new GSize(0, 0);
micon.infoWindowAnchor = new GPoint(25, 7);

var mhovericon = new GIcon(G_DEFAULT_ICON);
mhovericon.image = '/images/map/ico_dest03b.png';
mhovericon.iconSize = new GSize(53, 52);
mhovericon.iconAnchor = new GPoint(15, 15);
mhovericon.shadowSize = new GSize(0, 0);
mhovericon.infoWindowAnchor = new GPoint(25, 7);

function mapInit() {
	map = new GMap2($("#map_canvas").get(0));
	//map.setCenter(new GLatLng(43.01141864227728, -108.01904296875), 7);
	map.setCenter(new GLatLng(33.806755, -117.885006), 14);
	map.setMapType(G_SATELLITE_MAP);

	/*
	GEvent.addListener(map, "mousemove", function(latlng) {
		$("#mouselocation").text(latlng.lat() + ", " + latlng.lng());
	});
	*/

//	if (typeof (layerControlTop) != "undefined") {
//		$("#layerControl").css("top", layerControlTop);
//	}

	var mapTypePos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10));
	var mapControlPos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 50));

	gMapControl = new GLargeMapControl();
	gMapTypeControl = new GMapTypeControl();

	map.addControl(gMapControl, mapControlPos);
	map.addControl(gMapTypeControl, mapTypePos);

	cgm = new CRGGoogleMap(map, { maxInitZoom: 13 });
	cgm.onLayersLoadStart = function() { $("#loading").show(); };
	cgm.onLayersLoadComplete = function() { $("#loading").hide(); };
	cgm.onZoomEnd = function(o, n) { setupLayers(); };
	cgm.initialize();

	//jdata = { 'entryIds': ['1000'] };
	//var artic = new MapLayer("Artic", "/Webservices/MapService.svc/GetOverlayByEntryId", jdata, { visible: true, center: false, filterMb: false, cluster: false, autoLabel: false });
	//ml.MarkerOptions = { icon: searchIcon, labelOffset: new GSize(-1, -30) };
	//ml.MarkerOptions = lblMarkerOptions;
	//artic.onClick = markerClick;
	//cgm.addLayer(artic);

	//var jdata = { 'entryIds': ['1037'] };
	//var ml = new MapLayer("CA Highspeed Rail", "/Webservices/MapService.svc/GetOverlayByEntryId", jdata, { visible: true, center: false, filterMb: false, cluster: false });
	//var jdata = { 'query': 'filter=entryid=1037' };
	//var ml = new MapLayer("CA Highspeed Rail", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: true, center: false, filterMb: false, cluster: false });
	//ml.MarkerOptions = { icon: hsrIcon };
	//ml.onClick = markerClick;
	//cgm.addLayer(ml);

	/*
	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1000,1004,1005,1006)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("AC Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'Activity Centers' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1007,1008,1031)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("BL Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'Base Layer' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	cgm.addLayer(otherpoints);
	*/

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1033)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'layer3' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1037)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'layer9' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1035)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'layer8' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1180)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Amtrak", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'Amtrak' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1000)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: true, center: false, filterMb: false, cluster: false, group: 'Artic' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1041)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'Platinum Triangle' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1031)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'Anaheim Resort' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	//jdata = { 'query': "filter=publishedstatus=2 and entryid in (1004,1005,1006,1007,1008)", 'geotypes': ['Marker'] };
	jdata = { 'query': "filter=publishedstatus=2 and entryid in (1004,1006,1008)", 'geotypes': ['Marker'] };
	var otherpoints = new MapLayer("Future Points", "/Webservices/MapService.svc/GetOverlayByEntryQuery", jdata, { visible: false, center: false, filterMb: false, cluster: false, group: 'layer2' });
	otherpoints.MarkerOptions = { icon: micon };
	otherpoints.onClick = markerClick;
	otherpoints.onMarkerMouseOver = markermouseover;
	otherpoints.onMarkerMouseOut = markermouseout;
	cgm.addLayer(otherpoints);

	//Future
	var tl = new TileLayer("layer3", "http://articinfo.com/maptiles/layer3/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 11, group: 'layer3' });
	cgm.addLayer(tl);

	var tl = new TileLayer("layer8", "http://articinfo.com/maptiles/layer8/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 10, group: 'layer8' });
	cgm.addLayer(tl);

	var tl = new TileLayer("layer9", "http://articinfo.com/maptiles/layer9/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 9, group: 'layer9' });
	cgm.addLayer(tl);

	//Current
	var tl = new TileLayer("layer6", "http://articinfo.com/maptiles/layer6/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 8, group: 'layer6' });
	cgm.addLayer(tl);

	var tl = new TileLayer("Amtrak", "http://articinfo.com/maptiles/layer7/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 7, group: 'Amtrak' });
	cgm.addLayer(tl);

	//Layers
	var tl = new TileLayer("layer4", "http://articinfo.com/maptiles/layer4/", { opacity: 1.0, visible: true, urlExt: ".png", zPriority: 15, group: 'Artic' });
	cgm.addLayer(tl);
	
	var tl = new TileLayer("layer5", "http://articinfo.com/maptiles/layer5/", { opacity: 1.0, visible: true, urlExt: ".png", zPriority: 5, group: 'layer5' });
	cgm.addLayer(tl);
	
	tl = new TileLayer("Artic", "http://mapserver.civicresource.com/VEPreloader/00100000000/ARTIC/_vemap/", { opacity: 0.5, visible: true, zPriority: 4, group: 'Artic' });
	cgm.addLayer(tl);
	
	var tl = new TileLayer("Activity Centers", "http://articinfo.com/maptiles/layer2/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 3, group: 'layer2' });
	cgm.addLayer(tl);

	var tl = new TileLayer("Platinum Triangle", "http://articinfo.com/maptiles/layer1/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 2, group: 'Platinum Triangle' });
	cgm.addLayer(tl);

	var tl = new TileLayer("Anaheim Resort", "http://articinfo.com/maptiles/layer10/", { opacity: 1.0, visible: false, urlExt: ".png", zPriority: 2, group: 'Anaheim Resort' });
	cgm.addLayer(tl);

//	tl = new TileLayer("Study Area", "http://mapserver.civicresource.com/VEPreloader/010000000000/ARTIC/_vemap/", { opacity: 0.5, visible: true, zPriority: 0 });
//	cgm.addLayer(tl);

	var tl = new TileLayer("City Boundary", "http://mapserver.civicresource.com/VEPreloader/100000000000/ARTIC/_vemap/", { opacity: 0.5, visible: false, zPriority: 0 });
	cgm.addLayer(tl);

	
	/*

	tl = new TileLayer("Artic", "http://mapserver.civicresource.com/VEPreloader/00100000000/ARTIC/_vemap/", { opacity: 0.5, visible: true });
	cgm.addLayer(tl);

	tl = new TileLayer("Angel Stadium", "http://mapserver.civicresource.com/VEPreloader/00010000000/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);

	tl = new TileLayer("Convention Center", "http://mapserver.civicresource.com/VEPreloader/00001000000/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);

	tl = new TileLayer("Disneyland", "http://mapserver.civicresource.com/VEPreloader/00000100000/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);

	tl = new TileLayer("Garden Walk", "http://mapserver.civicresource.com/VEPreloader/00000010000/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);

	tl = new TileLayer("Honda Center", "http://mapserver.civicresource.com/VEPreloader/00000001000/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);

	tl = new TileLayer("Guideway", "http://mapserver.civicresource.com/VEPreloader/00000000100/ARTIC/_vemap/", { opacity: 0.5, visible: true });
	cgm.addLayer(tl);

	tl = new TileLayer("CA Highspeed Rail", "http://mapserver.civicresource.com/VEPreloader/00000000010/ARTIC/_vemap/", { opacity: 0.5, visible: false });
	cgm.addLayer(tl);
	
	tl = new TileLayer("Railroad", "http://mapserver.civicresource.com/VEPreloader/00000000001/ARTIC/_vemap/", { opacity: 0.5, visible: true });
	cgm.addLayer(tl);
	
	*/
	//setupLayers();
}
function markermouseout(crgmap, m, ll) {
	if (jQuery.browser.mozilla) {
		m.setImage('/images/map/ico_dest03c.png');
	}
}
function markermouseover(crgmap, m, ll) {
	if (jQuery.browser.mozilla) {
		m.setImage('/images/map/ico_dest03d.png');
	}
	else {
		var hover = null;
		var omarker = null;
		omarker = m;
		hover = new GMarker(m.getLatLng(), { icon: mhovericon });
		GEvent.addListener(hover, "click", function(o, ll, oll) {
			markerClick.call(cgm, omarker, ll, oll);
		});
		GEvent.addListener(hover, "mouseout", function(ll) {
			omarker.show();
			crgmap.GMap.removeOverlay(hover);
		});
		omarker.hide();
		crgmap.GMap.addOverlay(hover);
	}
}
function toggle(name) {
	cgm.toggleLayer(name);
}
function tileClick(o, ll, oll) {
	var crgmap = this;
	/*
	openInfoWindowHtml(latlng:GLatLng, html:String, opts?:GInfoWindowOptions) 
	*/

	if (typeof (ll) != "undefined") {
		$.ajax({
			type: "POST",
			contentType: "application/json; charset=utf-8",
			url: "/WebServices/MapWebService.asmx/GetTileInfoWindowHTML",
			data: "{'lat':" + ll.lat() + ",'lng':" + ll.lng() + "}",
			dataType: "text",
			success: function(data) { tileClickCallback(ll, cgm, data); },
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			}
		});
	}
	//var mo = crgmap.findOverlay(o);
	//markerClickOpenWindow(mo);
}
function tileClickCallback(ll, crgmap, data) {
	if (data != '') {
		crgmap.GMap.openInfoWindowHtml(ll, data, { maxWidth: 350 });
	}
}
function markerClick(o, ll, oll) {
	var crgmap = this;
	var mo = crgmap.findOverlay(o);
	markerClickOpenWindow(mo);
}
function markerClickOpenWindow(mo) {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/WebServices/MapWebService.asmx/GetInfoWindowHTML",
		data: "{'entryId':" + mo.Id + ",'parserType':" + 0 + "}",
		dataType: "text",
		success: function(data) { markerClickCallback(mo.GOverlay, cgm, data); },
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("request:" + XMLHttpRequest + ", status: " + textStatus + ", error: " + errorThrown);
			//						for (var property in XMLHttpRequest) {
			//							window.alert(property + ": " + XMLHttpRequest[property]);
			//						}
		}
	});
}
function openGeo(entryId) {
	var zoom = 13;
	var overlays = cgm.findOverlaysById(entryId);
	if (overlays.length > 0) {
		var mo = overlays.pop();
		if (cgm.GMap.getZoom() > zoom)
			zoom = cgm.GMap.getZoom();

		cgm.panToOverlay(mo, zoom);
		markerClickOpenWindow(mo);
	}
}
function markerClickCallback(marker, crgmap, data) {
	marker.openInfoWindow(data, { maxWidth: 400 });
}
function setupLayers() {
//	enableLayer("City Boundary");
//	enableLayer("Study Area");
//	enableLayer("Artic");
//	enableLayer("Angel Stadium");
//	enableLayer("Convention Center");
//	enableLayer("Disneyland");
//	enableLayer("Garden Walk");
//	enableLayer("Honda Center");
//	enableLayer("Guideway");
//	enableLayer("CA Highspeed Rail");
//	enableLayer("Railroad");
}
function enableLayer(name) {
	var chkbox = $("input[name='" + name + "']");
	if (!cgm.isLayerEnabled(name)) {
		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" });
		$("#layer-toggle").css("width", "150px");
		layerControlVisible = false;
	}
	else {
		$("#layers").show();
		$("#layer-toggle").css("width", "");
		layerControlVisible = true;
	}
}
function toggleGroup(chk, gname) {
	if ($(chk).attr("checked")) {
		cgm.showLayerGroup(gname);
		$(":checkbox." + gname).attr("checked", "true");
	} else {
		cgm.hideLayerGroup(gname);
		$(":checkbox." + gname).removeAttr("checked");
	}
}
