﻿
var RBCommunityMap = Class.create({
    initialize: function(_mapID, _routeID)
    {
        this.mapID = _mapID;
        this.routeID = _routeID;
        this.communities = new Array();
        this.map = null;
        this.directions = null;
        this.bounds = new GLatLngBounds();
        Event.observe(window, "load", this.setup.bindAsEventListener(this));
    },
    setup: function()
    {
        this.map = new GMap2($(this.mapID));        this.map.addControl(new GMapTypeControl());        this.map.addControl(new GLargeMapControl());
        this.directions = new GDirections(this.map, $(this.routeID));        this.Draw();    },
    Draw: function()
    {        this.map.setCenter(this.bounds.getCenter());
        if (this.communities.length == 1)
        {
            this.map.setZoom(12);
        }
        else
        {
            this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds)-1);        }        for(var idx = 0; idx < this.communities.length; idx++)        {
            this.map.addOverlay(this.communities[idx].marker);        }
    },
    Redraw: function()
    {
        this.map.clearOverlays();
        this.Draw();
    },
    LoadDirections: function(from, lat, lng)
    {
        this.directions.load(from + " to " + lat + ", " + lng + "");
    },
    AddCommunity: function(_community)
    {
        this.communities.push(_community);
        this.bounds.extend(_community.point); 
        _community.directions = this.directions;
    }
});

var RBCommunity = Class.create({
    initialize: function(_name, _link, _lat, _lng, _logo)
    {
        this.name = _name;
        this.link = _link;
        this.lat = _lat;
        this.lng = _lng;
        this.logo = _logo;
        this.directions = null;
        this.point = new GLatLng(this.lat, this.lng);        this.marker = new GMarker(this.point);
	    GEvent.bind(this.marker, "click", this, this.onMarkerClick);
	    GEvent.bind(this.marker, "mouseover", this, this.onRollover);
	    GEvent.bind(this.marker, "mouseout", this, this.onRollout);
    },    onMarkerClick: function()    {        var infoHTML = '<div class="infowin" ><a href="' + this.link + '">';        if (this.logo != undefined)        {            infoHTML += '<img src="/assets/images/navlogos/' + this.logo + '" alt="' + this.name + '" />';        }        else        {            infoHTML += this.name;        }            infoHTML += '</a><br />';                    //infoHTML += 'Directions: to here:<br />' +
        //    '<input type="text" name="txtAddress" />' +
        //    '<input type="button" value="Go" onclick="communityMap.LoadDirections(this.previous().value, ' + this.lat + ', ' + this.lng +'); return false;" />'
        infoHTML += '</div>';                this.marker.openInfoWindowHtml(infoHTML, {maxWidth:170});     },    onRollover: function()    {        },    onRollout: function()    {        }
});

