var se;
var nw;
var ride_colors = {};
var markersArray = [];

function load() {
    // look for ride_id in body tag
    // load or build ride object
    getRideFromDOM();

    if (! ride.stops)
        ride.stops = [];

    if (!GBrowserIsCompatible()) {
        alert("The browser you are using is not compatible with Google Maps API. Please use another browser.");
    } else {

        var markersArray = [];
        var options = {
            showOnLoad: false
        };

        map = new GMap2(document.getElementById("map"), {backgroundColor: '#000000', googleBarOptions: options});

        // start of add of terrain map type
        map.addMapType(G_PHYSICAL_MAP);
        var mapControl = new GHierarchicalMapTypeControl();

        // Set up map type menu relationships
        mapControl.clearRelationships();
        mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);

        // Add control after you've specified the relationships
        map.addControl(mapControl);
        // end of add of terrain map type

        // enable google bar for local search on the map
        map.enableGoogleBar();
        map.addControl(new GLargeMapControl());
        geocoder = new GClientGeocoder();
        directions = new GDirections();

        rides = searchAllRidesWithStops();
        if (rides <= 0) {
            // map centers at the world when no rides displayed
            map.setCenter(new GLatLng(-35.689, 139.691), 1);
        } else {
            extendBounds(rides);
            markersArray = displayRides(rides, markersArray);

            if (markersArray != null) {
                displayClusterMarkers(markersArray);
            }
        }
    }
}

function extendBounds(rides) {
    for (var index = 0; index < rides.length; index++) {
        if (! rides[index].polyline) continue;
        point = new GLatLng(rides[index].polyline.south, rides[index].polyline.west);
        bounds.extend(point);
        point = new GLatLng(rides[index].polyline.north, rides[index].polyline.east);
        bounds.extend(point);
    }
    map.setCenter(bounds.getCenter());
    map.setZoom(map.getBoundsZoomLevel(bounds));
}

function updateSearchResults() {
    if (map.getInfoWindow().isHidden()) {
        markersArray = [];
        bounds = map.getBounds();
        $('map_description').update('');
        map.clearOverlays();
        rides = [];
        rides = findRidesByArea(bounds);
        markersArray = displayRides(rides, markersArray);
        if (markersArray != null) {
            displayClusterMarkers(markersArray);
        }
    }
}

function getSearchResults() {
    if (map.getInfoWindow().isHidden()) {
        markersArray = [];
        bounds = map.getBounds();
        msg = 'There' + (rides.length > 1 ? ' are ' : ' is ') + rides.size() + ' ride' + (rides.length > 1 ? 's' : '') + ' on this map. Click on the marker to view the ride.';
        $('map_description').update(msg);
        markersArray = displayRides(rides, markersArray);
        if (markersArray != null) {
            displayClusterMarkers(markersArray);
        }
    }
}

function displayRides(rides, markersArray) {
    msg = 'There' + (rides.length > 1 ? ' are ' : ' is ') + rides.size() + ' ride' + (rides.length > 1 ? 's' : '') + ' on this map. Click on the marker to view the ride.';
    $('map_description').update(msg);
    markers = {};
    for (var index = 0; index < rides.length; index++) {
        markersArray = displayFirstMarker(rides[index], markersArray);
    }
    return markersArray;
}

function getRideColor(ride_id) {
    ride_colors[ride_id] = ride_colors[ride_id] || randomColor();
    return ride_colors[ride_id];
}

// get all rides without limit from database
function searchAllRidesWithStops() {
    rides = Ride.find('all', {
        with_stops: true,
        no_polyline_points: true
    });
    return rides;
}

function findRidesByArea(bounds) {
    rides = Ride.find('all', {
        south: bounds.getSouthWest().lat(),
        west: bounds.getSouthWest().lng(),
        north: bounds.getNorthEast().lat(),
        east: bounds.getNorthEast().lng(),
        difficulty: $('difficulty').value,
        road_types: $('road_types').value,
        with_stops: true,
        no_polyline_points: true
    });
    return rides;
}

function getRidesByBounds(northPoint, southPoint, eastPoint, westPoint) {
    rides = Ride.find('all', {
        south: southPoint,
        west: westPoint,
        north: northPoint,
        east: eastPoint,
        difficulty: $('difficulty').value,
        road_types: $('road_types').value,
        with_stops: true,
        no_polyline_points: true
    });
    return rides;
}

function displayFirstMarker(ride, markersArray) {
    var point = null;
    var marker = null;
    var thisIcon = new GIcon(baseIcon);

    stop = ride.stops[0];

    if (stop == null) return false;

    point = new GLatLng(stop.lat, stop.lng);
    thisIcon.image = getFirstMarkerIconURL();
    marker = new GMarker(point, {title:ride.title, icon: thisIcon});
    if (markers[stop.id]) {
        map.removeOverlay(markers[stop.id]);
    }
    markers['r' + ride.id] = marker;

    add_search_listeners(marker);
    markersArray.push(marker);
    return markersArray;
}

function getFirstMarkerIconURL() {
    return baseIconURL + "pause.png";
}

function add_search_listeners(marker) {
    GEvent.addListener(marker, "click", function() {
        for (stopIndex = 0; stopIndex < rides.length; stopIndex++) {
            if (markers['r' + rides[stopIndex].id] == this) {
                // display the info window
                var html = getSearchInfoWindowHTML(rides[stopIndex]);
                if (html != null) {
                    this.openInfoWindowHtml(html);
                }
            }
        }
    });
}

function getSearchInfoWindowHTML(ride) {
    var html = "<div id=\"info-window\" style=\"width:450px;\">";
    if(ride.polyline.static_maps_url != null) {
        html += "<div style=\"float:left; padding:10px;\"><img src=\"" + ride.polyline.static_maps_url + "\" width=\"150\" height=\"150\"/></div>";
    }
    html += "<div class=\"ride-info-window-header\">" + ride.title + " by " + ride.user_display_name + "</div><br/>";
    html += "<div class=\"ride-info-window-date-time\">Road Type: " + ride.road_types + "<br/>";
    html += "Difficulty: " + ride.difficulty + "<br/>";
    html += "Created on: " + formatDate(ride.created_milliseconds_at) + "<br/><br />";
    if (ride.stops != null) {
        var stops = ride.stops;
        var image = false;
        var video = false;
        var segments = false;

        html += "<div align=\"left\"><img src=\"" + getFirstMarkerIconURL() + "\" style=\"width:40px;\">";
        for (var index = 0; index < stops.length; index++) {
            if (stops[index].visual_type == "photo") {
                image = true;
            }
            if (stops[index].visual_type == "video") {
                video = true;
            }
            if (ride.segments.length >= 1) {
                segments = true;
            }
        }
        if (image) {
            html += "&nbsp;&nbsp;<img src=\"" + baseIconURL + "pause-photo.png\" style=\"width:40px;\">";
        }
        if (video) {
            html += "&nbsp;&nbsp;<img src=\"" + baseIconURL + "pause-video.png\" style=\"width:40px;\">";
        }
        if (segments) {
            html += "&nbsp;&nbsp;<img src=\"" + baseIconURL + "tlv.png\" style=\"width:40px;\">";
        }
        html += "</div><br />";
    }
    html += '<div class=\"ride-info-window-view-ride-link\"><a href="/rides/' + ride.id + '">View Ride</a></div></div></div>';
    return html;
}

function getRideFromDOM() {
    mode = $$('body')[0].getAttribute('mode');
    ride_id = $$('body')[0].getAttribute('ride_id');

	// get ride
    if (ride_id) {
        ride = Ride.find(ride_id);
        if (ride.stops) {
            current_stop = ride.stops[0];
            current_stop_pos = 0;
        }
    } else {
        ride = Ride.build();
    }
}

function clusterMarkerClickFunction(args) {
    cluster.defaultClickAction = function() {
        map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
        delete cluster.defaultClickAction;
    }
    var html = '<div id="clusterInfoWindow">' + args.clusteredMarkers.length + ' rides in this cluster:<br /><br />';
    if (args.clusteredMarkers.length <= 6) {
        listLimit = args.clusteredMarkers.length;
    }
    else {
        listLimit = 6;
    }
    for (i = 0; i < listLimit; i++) {
        html += '<a href="javascript:cluster.triggerClick(' + args.clusteredMarkers[i].index + ')">' + args.clusteredMarkers[i].getTitle() + '</a><br />';
    }
    if (args.clusteredMarkers.length > 6) {
        html += "and many more...<br />"
    }

    html += '<br /><a href="javascript:void(0)" onclick="cluster.defaultClickAction()">Zoom</a> in to show these locations</div>';
    args.clusterMarker.openInfoWindowHtml(html);
}

function displayClusterMarkers(markersArray) {
    var clusterIcon = new GIcon(baseIcon);
    clusterIcon.image = baseIconURL + "cluster2.png";
    cluster = new ClusterMarker(map, { markers:markersArray, clusterMarkerIcon:clusterIcon, clusterMarkerClick:clusterMarkerClickFunction });
    cluster.borderPadding = 512;
    cluster.clusterMarkerTitle = "This marker contains a cluster of %count rides. Click on the marker to view all rides.";
    cluster.refresh();
}


/*
	ClusterMarker Version 1.3.0

	A marker manager for the Google Maps API
	http://googlemapsapi.martinpearman.co.uk/clustermarker

	Copyright Martin Pearman 2008
	Last updated 5th March 2008

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('7 a(2c,6){2.3=2c;2.5=[];2.r=[];2.v=[];2.J=[];4(1r(6)==="1q"){6={}}2.16=(6.16)?6.16:2R;2.1g=(6.1g===h)?h:n;4(6.P){2.P=6.P}4(6.k){2.k=6.k}I{2.k=e 2Q();2.k.2P="2a://2O.29.28/27/2N.26";2.k.1M=e 25(24,23);2.k.1K=e D(9,2b);2.k.2M=e D(9,2b);2.k.2L="2a://2K.29.28/2J/2I/27/2H.26";2.k.2G=e 25(24,23)}2.18=(6.18)?6.18:"2F 2E 2D 21 2C 2B %1W 1C";4(6.E){2.E=6.E}2.Z=(6.Z)?6.Z:0;4(6.1C){2.22(6.1C)}A.1B(2.3,"2A",2,2.1O);A.1B(2.3,"2z",2,2.1D);A.1B(2.3,"2y",2,2.1P)}a.d.22=7(m){f i;4(!m[0]){f 1A=[];b(i 21 m){1A.t(m[i])}m=1A}b(i=m.8-1;i>=0;i--){m[i].q=h;m[i].g=h;m[i].o=h}2.5=2.5.2x(m)};a.d.1S=7(Q){7 $1X(20,1Z,1Y){1p e 2w(20,{2v:1Z,2u:1Y})}f 17=e 1j(),i,G,1z=[],R,1V=2;b(i=Q.8-1;i>=0;i--){R=2.5[Q[i]];R.2t=Q[i];17.12(R.p());1z.t(R)}G=$1X(17.1Q(),2.k,2.18.2s(/%1W/2r,Q.8));G.1U=17;2.J.t(A.2q(G,"1E",7(){1V.P({1x:G,2p:1z})}));1p G};a.d.P=7(1y){2.3.T(1y.1x.p(),2.3.1R(1y.1x.1U))};a.d.1J=7(){f F=2.16,u=2.3.U(),O=2.3.1f().1N(),15,1w,1u,14,1v,1t,w=2.3.2o(),i,c,13=[],1s;4(F){15=O.1m(w.2n(),u);1w=e D(15.x-F,15.y+F);1u=O.W(1w,u);14=O.1m(w.2m(),u);1v=e D(14.x+F,14.y-F);1t=O.W(1v,u);w.12(1u);w.12(1t)}2.K=h;4(1r(2.r[u])==="1q"){2.r[u]=[];2.K=n;b(i=2.5.8-1;i>=0;i--){c=2.5[i];c.g=w.1T(c.p())?n:h;c.o=c.g;4(c.g){13.t(i)}}}I{b(i=2.5.8-1;i>=0;i--){c=2.5[i];1s=c.g;c.g=w.1T(c.p())?n:h;c.o=c.g;4(!2.K&&1s!==c.g){2.K=n}4(c.g&&1r(2.r[u][i])==="1q"){13.t(i)}}}1p 13};a.d.1G=7(){f s,i,j,1o=2.3.U();b(i=2.5.8-1;i>0;i--){4(2.5[i].o){s=[];b(j=i-1;j>=0;j--){4(2.5[j].o&&2.r[1o][i].2l(2.r[1o][j])){s.t(j)}}4(s.8!==0){s.t(i);b(j=s.8-1;j>=0;j--){2.5[s[j]].o=h}2.v.t(2.1S(s))}}}};a.d.2k=7(){f 1n=2.5,11=e 1j(),i;b(i=1n.8-1;i>=0;i--){11.12(1n[i].p())}f 10=2.3.1R(11);4(2.E&&10>2.E){10=2.E}2.3.T(11.1Q(),10);2.H()};a.d.1P=7(){2.H(n)};a.d.1O=7(){4(!2.19){2.H()}I{2.19=h}};a.d.1H=7(V){f X=2.3.1f().1N(),L=2.3.U(),i,N,Y,C,B,1l,1k,1i,1h,M=2.Z;b(i=V.8-1;i>=0;i--){N=2.5[V[i]];Y=N.1L().1M;C=X.1m(N.p(),L);B=N.1L().1K;1l=e D(C.x-B.x-M,C.y-B.y+Y.2j+M);1k=e D(C.x-B.x+Y.2i+M,C.y-B.y-M);1i=X.W(1l,L);1h=X.W(1k,L);2.r[L][V[i]]=e 1j(1i,1h)}};a.d.H=7(1I){f i,l,1e=2.1J();4(2.K||1I){2.1b();4(2.1g&&2.3.U()<2.3.1f().2h()){4(1e.8>0){2.1H(1e)}2.1G()}b(i=2.v.8-1;i>=0;i--){2.3.1F(2.v[i])}b(i=2.5.8-1;i>=0;i--){l=2.5[i];4(!l.q&&l.o){2.3.1F(l);l.q=n}4(l.q&&!l.o){2.3.1d(l);l.q=h}}}};a.d.1b=7(){b(f i=2.v.8-1;i>=0;i--){2.3.1d(2.v[i])}b(i=2.J.8-1;i>=0;i--){A.2g(2.J[i])}2.v=[];2.J=[]};a.d.2f=7(){b(f i=2.5.8-1;i>=0;i--){4(2.5[i].q){2.3.1d(2.5[i])}1c 2.5[i].q;1c 2.5[i].g;1c 2.5[i].o}2.1b();2.5=[];2.r=[]};a.d.1a=7(S){f z=2.5[S];4(z.q){A.2e(z,"1E")}I{4(z.g){2.3.T(z.p());2.3.2d();2.1a(S)}I{2.3.T(z.p());2.1a(S)}}};a.d.1D=7(){2.19=n;2.H(n)};',62,178,'||this|_map|if|_mapMarkers|_2|function|length||ClusterMarker|for|_1c|prototype|new|var|_isActive|false|||clusterMarkerIcon|_36|_3|true|_makeVisible|getLatLng|_isVisible|_iconBounds|_1f|push|_12|_clusterMarkers|_1a|||_3b|GEvent|_2e|_2d|GPoint|fitMapMaxZoom|_11|_c|refresh|else|_eventListeners|_activeMarkersChanged|_29|_33|_2b|_13|clusterMarkerClick|_6|_e|_3a|setCenter|getZoom|_27|fromPixelToLatLng|_28|_2c|intersectPadding|_26|_24|extend|_1d|_17|_14|borderPadding|_a|clusterMarkerTitle|_cancelMoveEnd|triggerClick|_removeClusterMarkers|delete|removeOverlay|_37|getCurrentMapType|clusteringEnabled|_32|_31|GLatLngBounds|_30|_2f|fromLatLngToPixel|_23|_22|return|undefined|typeof|_1e|_19|_16|_18|_15|clusterMarker|_10|_d|_5|bind|markers|_zoomEnd|click|addOverlay|_filterIntersectingMapMarkers|_preCacheIconBounds|_34|_filterActiveMapMarkers|iconAnchor|getIcon|iconSize|getProjection|_moveEnd|_mapTypeChanged|getCenter|getBoundsZoomLevel|_clusterMarker|containsLatLng|clusterGroupBounds|_f|count|newClusterMarker|_9|_8|_7|in|addMarkers|34|39|GSize|png|mapfiles|com|google|http|31|_1|zoomIn|trigger|removeMarkers|removeListener|getMaximumResolution|width|height|fitMapToMarkers|intersects|getNorthEast|getSouthWest|getBounds|clusteredMarkers|addListener|gi|replace|index|title|icon|GMarker|concat|maptypechanged|zoomend|moveend|see|and|zoom|to|Click|shadowSize|arrowshadow|en_us|intl|www|shadow|infoWindowAnchor|arrow|maps|image|GIcon|256'.split('|'),0,{}))
