﻿

function RiverMap()
{
    this.IdahoCenterPoint = new google.maps.LatLng(45.66012730272194, -114.17529296875); //intitial center point (centers idaho on the map)
	this.InitialZoomLevel = 6; //initial zoom level (shows all of idaho)
	this.RiverRuns = new Array(); //contains RiverRun instances
	this.map = new google.maps.Map2(document.getElementById("rivermap"));
    this.map.setCenter(this.IdahoCenterPoint, this.InitialZoomLevel);
    this.map.setMapType(G_PHYSICAL_MAP);
    
    var mapType = new GMapTypeControl();
    this.map.addMapType(G_PHYSICAL_MAP);
	this.map.addControl(new GLargeMapControl());
	this.map.addControl(mapType);
    var self = this;
    $.getJSON('/whitewater/assets/scripts/river-run-data.js', function(json)
    {
        self.RiverRuns = json.runs;
        self.DrawRuns();
    });
    
    GEvent.addListener(this.map, "click", function(ov, latlng) {
        if (console)
        {
            console.log(latlng.lat());
            console.log(latlng.lng());
        }
    });
    
    $('#rbAll').click(function(e)
    {
        self.ShowAll(self);
    });
    $('#rbPopular').click(function(e)
    {
        self.ShowPopular(self);
    });
    
    
}
$.extend(RiverMap.prototype, {
    DrawRuns: function()
    {
        if($('#rbPopular').attr('checked'))
        {
            for(var idx = 0; idx < this.RiverRuns.length; idx++)
            {
                if (this.RiverRuns[idx].featured == 'True' && 
                   parseFloat(this.RiverRuns[idx].latitude) < 0)
                {
                    this.DrawRun(this.RiverRuns[idx]);                        
                }
            }
        }
        else
        {
            for(var idx = 0; idx < this.RiverRuns.length; idx++)
            {
                if (parseFloat(this.RiverRuns[idx].latitude) < 0)
                {
                    this.DrawRun(this.RiverRuns[idx]);                        
                }
            }
        }
    },
    DrawRun: function(run)
    {
        run.isDrawn = true;
        if (run.latlng == undefined)
        {
            run.latlng = new google.maps.LatLng(run.longitude, run.latitude);
        }
        if (run.marker == undefined)
        {
            run.icon = new GIcon();
            run.icon.image = '/whitewater/assets/images/section-marker.png';
            run.icon.shadow = '/whitewater/assets/images/section-marker-shadow.png';
            run.icon.iconSide = new GSize(20, 34);
            run.icon.iconAnchor = new GPoint(10.0, 17.0);
            run.icon.infoWindowAnchor = new GPoint(10.0, 17.0);	
            run.marker = new GMarker(run.latlng, run.icon);
           
        }
        GEvent.bind(run.marker, 'click', run, this.RunClick);
        this.map.addOverlay(run.marker);
    },
    ShowAll: function(self)
    {
        for(var idx = 0; idx < self.RiverRuns.length; idx++)
        {
            if (parseFloat(self.RiverRuns[idx].latitude) < 0 &&
                !self.RiverRuns[idx].isDrawn)
            {
                self.DrawRun(self.RiverRuns[idx]);                      
            }
        }
    },
    ShowPopular: function(self)
    {    
        for(var idx = 0; idx < self.RiverRuns.length; idx++)
        {
            if (this.RiverRuns[idx].featured == 'False' &&
                self.RiverRuns[idx].isDrawn)
            {
                self.RiverRuns[idx].isDrawn = false;
                self.map.removeOverlay(self.RiverRuns[idx].marker);
            }
        }
    },
    RunClick: function()
    {
        this.marker.openInfoWindowHtml('<div class="runinfo">' +
            '<a href="/whitewater/' + this.url + '">' + this.runname +  ' <span>' + this.river + '</span</a>' +
            '<div><label>Difficulty:</label> ' + this.minClass + ' to ' + this.maxClass + '</div>' + 
	        '<div><label>Trip Length:</label> ' + this.tripLength + '</div>' + 
	        '<div><label>Season:</label> ' + this.season + '</div></div>');
    }
});
