var llCop = new GLatLng(53.9153, -1.1411);


$(function() {
    if (typeof locations != "undefined"  &&  locations.length > 0) {
        var hols = new BagOnHols("#map");
        hols.init();
        hols.show();
    }
});


$(window).unload(function() {
  GUnload();
});


function BagOnHols(selector) {
    var elements = $(selector);
    if (elements.length > 0) {
        this.mapElement = elements[0];
    } else {
        this.mapElement = false;
    }
};


BagOnHols.prototype.init = function() {
    this.bounds = new GLatLngBounds();
    this.bounds.extend(llCop);
    for (var i = 0; i < locations.length; ++i ) {
        this.bounds.extend(locations[i].ll);
    }

    this.iconBag = new GIcon();
    this.iconBag.image = "/images/mm_sunflower.png";
    this.iconBag.iconSize = new GSize(30, 31);
    this.iconBag.iconAnchor = new GPoint(15, 15);
    this.iconBag.infoWindowAnchor = new GPoint(15, 1);

    this.iconCop = new GIcon();
    this.iconCop.image = "/images/mm_bag.png";
    this.iconCop.iconSize = new GSize(30, 40);
    this.iconCop.iconAnchor = new GPoint(14, 36);
    this.iconCop.infoWindowAnchor = new GPoint(11, 2);
};


BagOnHols.prototype.show = function() {
    if (this.mapElement) {
        if (!GBrowserIsCompatible()) {
            $(this.mapElement).text("Sorry, your browser is not compatible with Google Maps");
        } else {

            this.map = new GMap2(this.mapElement, {backgroundColor: "#104018"});
            this.zoom = this.map.getBoundsZoomLevel(this.bounds);
            this.map.setCenter(this.bounds.getCenter(), this.zoom);
            this.map.addControl(new GSmallMapControl());
            this.map.setMapType(G_NORMAL_MAP);
            this.map.addControl(new GMapTypeControl());

            this.mm = new MarkerManager(this.map);

            this.mm.addMarker(this.makeCopMarker(), 0);

            var options = {clickable: false, geodesic: false};
            for (var i = 0; i < locations.length; ++i ) {
                this.mm.addMarker(this.makeMarker(locations[i]), 0);
                this.map.addOverlay(new GPolyline([llCop, locations[i].ll],
                                                  "#104018", 2, 0.5, options));
            }
        }
    }
};


BagOnHols.prototype.makeCopMarker = function() {
    var options = {icon:      this.iconCop,
                   title:     "Copmanthorpe",
                   clickable: false,
                   zIndexProcess: function() { return 999; }
    };
    var marker = new GMarker(llCop, options);
    return marker;
};


BagOnHols.prototype.makeMarker = function(location) {
    var html = "";
    html += "<div class=\"popup\">";
    html += "  <img src=\"" + "images/bagonhols/" + location.image + "\" alt=\"Photo of bag in " + location.title + "\"/>";
    html += "  <h1>" + location.title + "</h1>";
    html += "  <span class=\"distance\">(" + Math.round(llCop.distanceFrom(location.ll) / 1609.344) + " miles)</span>";
    html += "  <p>" + location.text + "</p>";
    html += "</div>";
    
    var options = {icon:  this.iconBag,
                   title: location.title
    };
    var marker = new GMarker(location.ll, options);
    GEvent.addListener(marker, "click", function(ll) {
                           marker.openInfoWindowHtml(html);
                       });
    return marker;
};
