(function(){ var STATE_UNKNOWN = 0; var STATE_NONE = 1; var STATE_OFF = 2; var STATE_WAITING = 3; var STATE_ONCE = 4; var STATE_TRACK = 5; var STATE_DENIED = 6; var STATE_UPDATING = 7; var trackState = STATE_UNKNOWN; var map; var controlDiv; function drawButton(label){ if(!controlDiv){ controlDiv = document.createElement('div'); controlDiv.style.padding = '6px'; var controlUI = $('
'+(node.numberRoutes || 'No')+' route'+(node.numberRoutes == 1 ? '' : 's')+'
'; } if (node.styles){ var name = 'routes'; html += ''+node.subType+' has '; node.styles.sort(function(a,b){ return b.total - a.total; }); var styles = []; $.each(node.styles, function(i,v){ styles.push( v.total +' '+v.style.toLowerCase()); name = v.style == 'Boulder' ? 'problem' : 'route'; if (v.total > 1) name += 's'; }); var last = ''; if (styles.length > 1){ last = ' and '+styles.pop(); } html += styles.join(', '); html += last+' '+name+'
'; } if (node.unique){ html += ''+node.unique+'
'; } $('body') .poshytip('enable') .poshytip('update', html) .poshytip('show'); }); google.maps.event.addListener(label, "mouseout", function(){ $('body') .poshytip('disable') .poshytip('hide'); }); node.nodeLabel = label; } } var lastNearNodes = {}; var lastNearNodesKey = ''; function handleZoomChanged(){ var zoom = map.getZoom(); var type = map.getMapTypeId(); if (zoom > 14 && type == 'terrain'){ map.setMapTypeId('hybrid'); } saveState(); } var bboxPing = false; function saveState(latlng, zoom){ var center = map.getCenter(); var lat = center.lat(); var lng = center.lng(); var zoom = map.getZoom(); var hint = ''; var type = map.getMapTypeId(); if (history.replaceState){ history.replaceState({},'', document.location.pathname + '#' + lat.toFixed(6)+','+lng.toFixed(6)+','+zoom+','+hint+','+type); } else { location.hash = lat.toFixed(6)+','+lng.toFixed(6)+','+zoom+','+hint+','+type; } var center = map.getCenter(); var zoom = map.getZoom(); function makeNearNodesKey(mapto){ return mapto[0]; } var bounds = map.getBounds(); if (!bounds){ // Not loaded yet, wait and try again window.setTimeout(saveState, 100); return; } var NE = bounds.getNorthEast(); var SW = bounds.getSouthWest(); // Now find out where we are if (!bboxPing){ bboxPing = true; theCrag('/api/map/bbox?s='+SW.lng().toFixed(5)+','+NE.lat().toFixed(5)+','+NE.lng().toFixed(5)+','+SW.lat().toFixed(5) ).then(function(data){ bboxPing = false; var mapto = data.data.mapto; var newKey = makeNearNodesKey(mapto); if (newKey == lastNearNodesKey){ return; } // sort array by number then to string then compare to last one lastNearNodesKey = newKey; DAO.getNodes(mapto, function(nodes){ var node = nodes[0]; renderCrumbtrail(node); var newNearNodes = {}; $.each(nodes, function(index, node){ newNearNodes[node.id] = node; }); // remove previous shapes and labels $.each(lastNearNodes, function(index, oldNode){ // if (newNearNodes[oldNode.id]){ return; } if (oldNode.boundaryMarker){ oldNode.boundaryMarker.setMap(null); } if (oldNode.nodeLabel){ oldNode.nodeLabel.setMap(null); } $.each(oldNode.children, function(index, oldChild){ if (oldChild.boundaryMarker){ oldChild.boundaryMarker.setMap(null); } if (oldChild.nodeLabel){ oldChild.nodeLabel.setMap(null); } }); delete lastNearNodes[node.id]; }); $.each(nodes, function(index, node){ makeShape(node, '#777777'); if (node.children){ $.each(node.children, function(index, child){ makeShape(child, '#aaaaaa', true); }); } lastNearNodes[node.id] = node; }); }); }); } // if zoom >= 10 then load sub crag stuff var markers = []; var ids = []; if (zoom >= 10 && clusterer){ markers = clusterer.getVisibleMarkers(); // ignore markers that are loading, or have no children markers = $.grep(markers, function(m){ return !m.state; }); $.each(markers, function(i, m){ ids.push(m.nodeId[0]); }); // TODO add 100 id limit if (ids.length){ // load th data loadSecondLevelClusters(markers, ids); } $.each(markers, function(i, m){ m.state = 'loading'; }); } } function loadSecondLevelClusters(oldMarkers, ids){ // first remove any id's that have already been loaded ids = $.grep(ids, function(id){ marker = nodeid2marker[id]; if (marker && marker.state) return false; return true; }); if(ids.length == 0){ return; } theCrag('/api/map/summary/ids?expires=10&id='+ids.join(','), function(data){ // remove old marker if (oldMarkers){ $.each(oldMarkers, function(i, m){ if (m) m.state = 'remove'; }); clusterer.removeMarkers(oldMarkers); } // add back sub area markers and make status done so they don't get reloaded again var newMarkers = []; $.each(data.data, function(i,o){ var marks = makeMarkers(o.location, 'done'); newMarkers = newMarkers.concat( marks ); }); clusterer.addMarkers(newMarkers); }); } var hoveredCragId = null; var map; /* function createMarkerClickHandler(crag){ return function(e){ location.href = thecrag_index_url( crag[0] ); } } */ // TODO this function is shit remove it function createMarkerMouseOverHandler(crag){ return function(e){ function renderNode(node){ var html = '' +'Routes: ' + (node.numberRoutes || 0) +', Ascents: ' + (node.ascentCount || 0) +(node.unique ? '
'+node.unique : '') +'
Routes: ?, Ascents: ?
') .poshytip('show'); DAO.getNode(crag[0], function(node){ if (hoveredCragId === crag[0]){ //renderCrumbtrail(node); $('body') .poshytip('enable') .poshytip('show') .trigger('mousemove.poshytip'); var html = renderNode(node); window.setTimeout("$('body').poshytip('update', '"+html+"');", 0); } }); } } var crumbtrail_lastnode = null; function renderCrumbtrail(node){ var html = '', c, crumb, crumbs = [{ href: '/maps', icon: 'globe' }]; if (crumbtrail_lastnode && node.id == crumbtrail_lastnode.id){ return; } crumbtrail_lastnode = node; if (node.ancestors) { for (c=0; c < node.ancestors.length; c++){ crumb = node.ancestors[c]; if (crumb.id != '7546063'){ // World crumbs.push({ nid: crumb.id, href: thecrag_index_url(crumb), label: crumb.name, 'short': crumb['short'] }); } if (crumb.isTLC){ loadSecondLevelClusters([nodeid2marker[crumb.id]], [crumb.id]); } } } var here = thecrag_index_url(node); crumbs.push({ nid: node.id, href: here, label: node.name, 'short': node['short'] }); var map = $('#pt_maps > a').text(); crumbs.push({ href: here + '/map', label: map }); thecrag.crumbs.replace(crumbs); document.title = map+': '+node.name+' | theCrag'; var url = here + '/maps'; ga('send', 'event', { 'eventCategory': 'Maps', 'eventAction': document.title, 'eventLabel': url }); if (history.replaceState){ history.replaceState({},'',url+document.location.hash); } $('#pt_world a').attr('href', here); $('#pt_index a').attr('href', here); // Update link to index $('#pt_maps a').attr('href', url); // Update map link $('#pt_climbers a').attr('href', here+'/climbers/'); // Update top tab to climbers $('#pt_forum a').attr('href', here+'/forum'); // Update top tab link to chat forum $('#pt_photos a').attr('href', here+'/photos'); // Update top tab link to chat forum $('#pt_logbook a').attr('href', here+'/ascents'); // Update top tab link to logbook } function createMarkerMouseOutHandler(crag){ return function(e){ hoveredCragId = null; $('body').poshytip('disable').poshytip('hide'); } } var cragMarkerStyles = [{ url: staticBase + 'cids/images/cluster_17-1.1.0.png', height: 9, width: 9, anchor: [0, 0], textColor: '#000000', textSize: 7, maxRoutes: 0 },{ url: staticBase + 'cids/images/cluster_17-1.1.0.png', height: 13, width: 13, anchor: [0, 0], textColor: '#000000', textSize: 7, maxRoutes: 1 },{ url: staticBase + 'cids/images/cluster_21-1.1.0.png', height: 19, width: 19, anchor: [0, 0], textColor: '#000000', textSize: 8, maxRoutes: 10 },{ url: staticBase + 'cids/images/cluster_21-1.1.0.png', height: 21, width: 21, anchor: [0, 0], textColor: '#000000', textSize: 8, maxRoutes: 50 },{ url: staticBase + 'cids/images/cluster_27-1.1.0.png', height: 23, width: 23, anchor: [0, 0], textColor: '#000000', textSize: 8, maxRoutes: 100 },{ url: staticBase + 'cids/images/cluster_27-1.1.0.png', height: 25, width: 25, anchor: [0, 0], textColor: '#000000', textSize: 8, maxRoutes: 500 },{ url: staticBase + 'cids/images/cluster_27-1.1.0.png', height: 27, width: 27, anchor: [0, 0], textColor: '#000000', textSize: 9, maxRoutes: 1000 },{ url: staticBase + 'cids/images/cluster_31-1.1.0.png', height: 29, width: 29, anchor: [0, 0], textColor: '#000000', textSize: 9, maxRoutes: 2000 },{ url: staticBase + 'cids/images/cluster_31-1.1.0.png', height: 31, width: 31, anchor: [0, 0], textColor: '#000000', textSize: 12 }]; var maxCragMarkerStyles = cragMarkerStyles.length; function getIndexByRoutes(routes){ var index; for(index=0; index < maxCragMarkerStyles; index++){ if (routes < cragMarkerStyles[index].maxRoutes){ break; } } return index; } var clusterer; var nodeid2marker = {}; /* * takes nodes and creates markers for them * it also indexed them in nodeid2marker */ function makeMarkers(nodes, state){ var markers = []; var nodesC = nodes.length; for(var c=0; c