﻿function MarkerTracker(marker, map, opts) {
    this.map_ = map; this.marker_ = marker; this.enabled_ = true; this.arrowDisplayed_ = false; this.arrow_ = null; this.oldArrow_ = null; this.control_ = null; this.origCenter_ = marker.getLatLng(); opts = opts || {}; this.iconScale_ = MarkerTracker.DEFAULT_ICON_SCALE_; if (opts.iconScale != undefined) { this.iconScale_ = opts.iconScale; }
    this.padding_ = MarkerTracker.DEFAULT_EDGE_PADDING_; if (opts.padding != undefined) { this.padding_ = opts.padding; }
    this.color_ = MarkerTracker.DEFAULT_ARROW_COLOR_; if (opts.color != undefined) { this.color_ = opts.color; }
    this.weight_ = MarkerTracker.DEFAULT_ARROW_WEIGHT_; if (opts.weight != undefined) { this.weight_ = opts.weight; }
    this.length_ = MarkerTracker.DEFAULT_ARROW_LENGTH_; if (opts.length != undefined) { this.length_ = opts.length; }
    this.opacity_ = MarkerTracker.DEFAULT_ARROW_OPACITY_; if (opts.opacity != undefined) { this.opacity_ = opts.opacity; }
    this.updateEvent_ = MarkerTracker.DEFAULT_UPDATE_EVENT_; if (opts.updateEvent != undefined) { this.updateEvent_ = opts.updateEvent; }
    this.panEvent_ = MarkerTracker.DEFAULT_PAN_EVENT_; if (opts.panEvent != undefined) { this.panEvent_ = opts.panEvent; }
    this.quickPanEnabled_ = MarkerTracker.DEFAULT_QUICK_PAN_ENABLED_; if (opts.quickPanEnabled != undefined) { this.quickPanEnabled_ = opts.quickPanEnabled; }
    this.babyMarker_ = this.marker_; GEvent.bind(this.marker_, 'changed', this, this.updateArrow_); if (this.quickPanEnabled_) { GEvent.bind(this.babyMarker_, this.panEvent_, this, this.panToMarker_); }
    this.updateArrow_();
}; MarkerTracker.DEFAULT_EDGE_PADDING_ = 25; MarkerTracker.DEFAULT_ICON_SCALE_ = 0.6; MarkerTracker.DEFAULT_ARROW_COLOR_ = '#ff0000'; MarkerTracker.DEFAULT_ARROW_WEIGHT_ = 20; MarkerTracker.DEFAULT_ARROW_LENGTH_ = 20; MarkerTracker.DEFAULT_ARROW_OPACITY_ = 0.8; MarkerTracker.DEFAULT_UPDATE_EVENT_ = 'moveend'; MarkerTracker.DEFAULT_PAN_EVENT_ = 'click'; MarkerTracker.DEFAULT_QUICK_PAN_ENABLED_ = true; MarkerTracker.prototype.disable = function() { this.enabled_ = false; this.updateArrow_(); }; MarkerTracker.prototype.enable = function() { this.enabled_ = true; this.updateArrow_(); }; MarkerTracker.prototype.updateArrow_ = function() { if (!this.map_.getBounds().containsLatLng(this.origCenter_) && this.enabled_) { this.drawArrow_(); } else if (this.arrowDisplayed_) { this.hideArrow_(); this.babyMarker_.setLatLng(this.origCenter_); } }; MarkerTracker.prototype.drawArrow_ = function() {
    var bounds = this.map_.getBounds(); var SE = this.map_.fromLatLngToDivPixel(bounds.getSouthWest()); var NE = this.map_.fromLatLngToDivPixel(bounds.getNorthEast()); var minX = SE.x + this.padding_; var minY = NE.y + this.padding_; var maxX = NE.x - this.padding_; var maxY = SE.y - this.padding_; var center = this.map_.fromLatLngToDivPixel(this.map_.getCenter()); var loc = this.map_.fromLatLngToDivPixel(this.origCenter_); var m = (center.y - loc.y) / (center.x - loc.x); var b = (center.y - m * center.x); if (loc.x < maxX && loc.x > minX) { var x = loc.x; } else if (center.x > loc.x) { var x = minX; } else { var x = maxX; }
    var y = m * x + b; if (y > maxY) { y = maxY; x = (y - b) / m; } else if (y < minY) { y = minY; x = (y - b) / m; }
    var ang = Math.atan(-m); if (x > center.x) { ang = ang + Math.PI; }
    this.babyMarker_.setLatLng(this.map_.fromDivPixelToLatLng(this.getRotatedPoint_((-2) * 10, 0, ang, x, y))); if (!this.arrowDisplayed_) { this.arrowDisplayed_ = true; } 
}; MarkerTracker.prototype.hideArrow_ = function() { this.arrowDisplayed_ = false; }; MarkerTracker.prototype.panToMarker_ = function() { this.map_.panTo(this.origCenter_); }; MarkerTracker.prototype.getRotatedPoint_ = function(x, y, ang, xoffset, yoffset) { var newx = y * Math.sin(ang) - x * Math.cos(ang) + xoffset; var newy = x * Math.sin(ang) + y * Math.cos(ang) + yoffset; var rotatedPoint = new GPoint(newx, newy); return (rotatedPoint); }; function SPlaceIcon(count, name, color, shadowColor, centroid) {
    this.centroid_ = centroid; this.count_ = parseInt(count).format(0, ","); this.name_ = name; this.color_ = color; this.shadowColor_ = shadowColor; this.offset_ = { left: 20, top: 9 }
    this.dom_ = { number: null, shadowNumber: null, name: null, color: null, shadowColor: null}
}
try { SPlaceIcon.prototype = new GOverlay(); } catch (e) { }
SPlaceIcon.prototype.initialize = function(map) { var table = document.createElement("table"); var tbody = document.createElement("tbody"); var tr = document.createElement("tr"); var td = document.createElement("td"); td.className = "number"; this.dom_.color = td; var div = document.createElement("div"); var span = document.createElement("span"); span.className = "left"; this.dom_.shadowColor = span; var text = document.createTextNode(this.count_); this.dom_.numberShadow = text; span.appendChild(text); div.appendChild(span); span = document.createElement("span"); span.className = "right"; text = document.createTextNode(this.count_); this.dom_.number = text; span.appendChild(text); div.appendChild(span); td.appendChild(div); tr.appendChild(td); td = document.createElement("td"); td.className = "name"; text = document.createTextNode(this.name_); this.dom_.name = text; td.appendChild(text); tr.appendChild(td); tbody.appendChild(tr); table.appendChild(tbody); table.className = "placeIcon2"; table.style.position = "absolute"; this.dom_.color.style.backgroundColor = this.color_; this.dom_.shadowColor.style.color = this.shadowColor_; map.getPane(G_MAP_MARKER_PANE).appendChild(table); this.map_ = map; this.table_ = table; $j(this.table_).bind("mouseenter", { me: this }, function(event) { event.data.me.dom_.name.parentNode.style.textDecoration = "underline"; event.data.me.zIndex_ = event.data.me.table_.style.zIndex; event.data.me.table_.style.zIndex = 1000; GEvent.trigger(event.data.me, "mouseenter"); }); $j(this.table_).bind("mouseleave", { me: this }, function(event) { event.data.me.dom_.name.parentNode.style.textDecoration = "none"; event.data.me.table_.style.zIndex = event.data.me.zIndex_; GEvent.trigger(event.data.me, "mouseleave"); }); $j(this.table_).bind("click", { me: this }, function(event) { GEvent.trigger(event.data.me, "click"); }); }
SPlaceIcon.prototype.remove = function() {
    try { this.table_.parentNode.removeChild(this.table_); }
    catch (e) { } 
}
SPlaceIcon.prototype.copy = function() { return new SPlaceIcon(this.count_, this.name_, this.color_, this.shadowColor_, this.centroid_); }
SPlaceIcon.prototype.redraw = function(force) { if (!force) return; var c = this.map_.fromLatLngToDivPixel(this.centroid_); this.table_.style.left = (c.x - this.offset_.left) + "px"; this.table_.style.top = (c.y - this.offset_.top) + "px"; var z = GOverlay.getZIndex(this.centroid_.lat()); this.table_.style.zIndex = z; }
SPlaceIcon.prototype.show = function() { this.table_.style.visibility = 'hidden'; }
SPlaceIcon.prototype.hide = function() { this.table_.style.visibility = 'visible'; }
SPlaceIcon.prototype.edit = function(opt_number, opt_name, opt_color, opt_shadowcolor, opt_centroid) {
    if (opt_number != undefined) { this.dom_.number.nodeValue = opt_number; this.dom_.numberShadow.nodeValue = opt_number; }
    if (opt_name) this.dom_.name.nodeValue = opt_name; if (opt_color) {
        this.color_ = opt_color
        this.dom_.color.style.backgroundColor = this.color_;
    }
    if (opt_shadowcolor) {
        this.shadowColor_ = opt_shadowcolor
        this.dom_.shadowColor.style.color = this.shadowColor_;
    }
    if (opt_centroid) this.move(opt_centroid);
}
SPlaceIcon.prototype.move = function(centroid) { this.centroid_ = centroid; this.redraw(true); }
SPlaceIcon.prototype.getLatLng = function() { return this.centroid_; }
SPlaceIcon.prototype.setLatLng = function(point) { this.move(point); }
function SPolygon(map, points, levels, color, weight, fillOpacity) { this.map_ = map; this.points_ = points; this.levels_ = levels; this.color_ = color; this.weight_ = weight; this.fillOpacity_ = fillOpacity; }
SPolygon.prototype = { makePolygons: function() {
    var points = this.points_.split('$'); var levels = this.levels_.split('$'); var polylines = { under: [], over: [] }; for (var x = 0; x < points.length; x++) { polylines.under.push({ points: points[x], levels: levels[x], color: '#ffffff', weight: (this.weight_ + 2), opacity: 1, numLevels: 18, zoomFactor: 2 }); polylines.over.push({ points: points[x], levels: levels[x], color: this.color_, weight: this.weight_, opacity: 1, numLevels: 18, zoomFactor: 2 }); }
    return { under: new GPolygon.fromEncoded({ polylines: polylines.under, fill: true, color: "#000000", opacity: this.fillOpacity_, outline: true }), over: new GPolygon.fromEncoded({ polylines: polylines.over, fill: false, color: "#000000", opacity: 0, outline: true })}
}, add: function() { if (!this.polygons_) this.polygons_ = this.makePolygons(); if (!this.added_) { this.map_.addOverlay(this.polygons_.under); this.map_.addOverlay(this.polygons_.over); this.added_ = true; this.visible_ = true; } }, remove: function() { if (this.added_) { this.map_.removeOverlay(this.polygons_.under); this.map_.removeOverlay(this.polygons_.over); this.added_ = false; this.visible_ = false; } }, show: function() { if (!this.added_) this.add(); if (!this.visible_) { this.polygons_.under.show(); this.polygons_.over.show(); this.visible_ = true; } }, hide: function() { if (this.visible_) { this.polygons_.under.hide(); this.polygons_.over.hide(); this.visible_ = false; } }, fitAll: function(force) {
    var bounds = this.polygons_.under.getBounds(); if (!this.map_.getBounds().containsBounds(bounds) || force) {
        if (this.map_.getZoom() <= this.map_.getBoundsZoomLevel(bounds) && !force)
            this.map_.panTo(bounds.getCenter()); else
            this.map_.setCenter(bounds.getCenter(), this.map_.getBoundsZoomLevel(bounds));
    } 
}, isVisible: function() { if (!this.polygons_) this.polygons_ = this.makePolygons(); var bounds = this.polygons_.under.getBounds(); return this.map_.getBounds().intersects(bounds); } 
}
var SAreaColors = { orange: { color: '#f68d1e', shadow: '#7b460f' }, cyan: { color: '#00a3e1', shadow: '#005170' }, red: { color: '#ee3223', shadow: '#771911'} }
function SArea(areaData, map, colors) { this.areaData_ = areaData; this.map_ = map; this.colors_ = colors; this.polygon_ = new SPolygon(this.map_, this.areaData_.points, this.areaData_.levels, this.colors_.color, 3, 0.15); this.icon_ = new SPlaceIcon(this.areaData_.count, this.areaData_.name, this.colors_.color, this.colors_.shadow, new GLatLng(this.areaData_.lat, this.areaData_.lng)); this.map_.addOverlay(this.icon_); this.markerTracker_ = new MarkerTracker(this.icon_, this.map_, { color: "#121212", weight: 2, length: 0, iconScale: 0, quickPanEnabled: false }); GEvent.bind(this.icon_, "mouseenter", this, this.iconMouseover); GEvent.bind(this.icon_, "mouseleave", this, this.iconMouseout); GEvent.bind(this.icon_, "click", this, this.iconClick); }
SArea.prototype = { test: function() { alert('test'); }, iconMouseover: function() { this.polygon_.add(); }, iconMouseout: function() { this.polygon_.remove(); }, iconClick: function() { Sawbuck.Reporting.trackEvent('Area Relocation', 'Move between areas in map view', 'URL', window.location); Sawbuck.Page.goToPath(this.areaData_.path + this.areaData_.id); }, remove: function() { this.map_.removeOverlay(this.icon_); this.polygon_.remove(); this.markerTracker_.disable(); }, isVisible: function() { return this.polygon_.isVisible(); }, disableTracking: function() { return this.markerTracker_.disable(); }, enableTracking: function() { return this.markerTracker_.enable(); } }