function MapTrail(new_item)
{
    var self = this;
    var points_array = [];
    var polyline = null;
    var list_item = null;
    var trail_id = null;
    var trail_name = null;
    var encoded_points = null;
    var encoded_levels = null;
    var num_levels = null;
    var zoom_factor = null;
    var trailhead = null;
    var selected = false;
    var map = null;
    var marker = null;
    var zoom_link = null;
    var mouse_off = false;
    self.done_before = false;

    var load_image = new Image();
    load_image.src = absoluteFilepath("/images/trailhead_marker_red_glow.png");
    load_image.src = absoluteFilepath("/images/trailhead_marker_glow.png");

    var selected_color = "#d80000";
    var unselected_color = "#0a6801";

    var selected_icon_image = absoluteFilepath("/images/trailhead_marker_red_glow.png");
    var unselected_icon_image = absoluteFilepath("/images/trailhead_marker_glow.png");

    function _init()
    {
        trail_id = new_item['trail_id'];
        trail_name = new_item['trail_name'];
        encoded_points = new_item['gmap_encoded_points'];
        encoded_levels = new_item['gmap_encoded_levels'];
        num_levels = new_item['gmap_num_levels'];
        zoom_factor = new_item['gmap_zoom_factor'];

        trailhead = new GLatLng(Number(new_item['trailhead_point']['latitude']), Number(new_item['trailhead_point']['longitude']));

        polyline = new GPolyline.fromEncoded({
            color: unselected_color,
            weight: 2,
            points: encoded_points,
            levels: encoded_levels,
            zoomFactor: zoom_factor,
            numLevels: num_levels,
            opacity:1
        });

        marker = new GMarker(trailhead, {
            'icon' : _getIcon(),
            'id' : trail_id
        });
    }

    self.getID = function()
    {
        return trail_id;
    }

    self.setOn = function()
    {
        _selectTrail();
    }

    self.setOff = function()
    {
        polyline = new GPolyline.fromEncoded({
            color: unselected_color,
            weight: 2,
            points: encoded_points,
            levels: encoded_levels,
            zoomFactor: zoom_factor,
            numLevels: num_levels,
            opacity:1
        });

        marker = new GMarker(trailhead, {
            'icon' : _getIcon(),
            'id' : trail_id
        });
    }

    self.addToMap = function(my_map)
    {
        map = my_map;

        map.addOverlay(marker);
        map.addOverlay(polyline);

        $('#mtgt_'+trail_id).tipsy({'gravity' : 's',
                                    'title' : function(){return trail_name}});

        list_item = $('#wizard_trail_choose .trail_list li .trail[trail_id="' + trail_id + '"]').parent();
        zoom_link = $('#wizard_trail_choose .trail_list .zoom[trail_id="' + trail_id + '"]');

        list_item.click(function(){
            _clickTrail();
        });

        list_item.mouseover(function(){
            $(this).css('cursor', 'pointer');
         });
        list_item.mouseout(function(){
            $(this).css('cursor', 'default');
         });

         zoom_link.mouseover(function(){
             _goToCloseup();
         });

         zoom_link.mouseout(function(){
             _goBack();
         });

         zoom_link.click(function(){
            mouse_off = true;
            _clickZoom();
         });

        GEvent.addListener(marker, 'click', function(){
            _clickTrail()
        });
    }

    function _getIcon()
    {
        var unselected_icon = new GIcon(G_DEFAULT_ICON);
        unselected_icon.iconSize = new GSize(15, 15);
        unselected_icon.image = unselected_icon_image;
        unselected_icon.iconAnchor = new GPoint(8, 16);
        unselected_icon.shadowSize = new GSize(0, 0);

        return unselected_icon;
    }

    function _clickTrail()
    {
        if(!mouse_off)
        {
            map.removeOverlay(polyline);

            if(selected)
            {
                _deselectTrail();
                selected = false;
            }
            else
            {
               _selectTrail();
               selected = true;
            }
        }
    }

    function _selectTrail()
    {
        list_item.attr('class', 'selected');
        map.removeOverlay(polyline);

        polyline = new GPolyline.fromEncoded({
            color: selected_color,
            weight: 2,
            points: encoded_points,
            levels: encoded_levels,
            zoomFactor: zoom_factor,
            numLevels: num_levels,
            opacity:1
        });
        
        map.addOverlay(polyline);
        marker.setImage(selected_icon_image);
    }

    function _deselectTrail()
    {
        list_item.attr('class', 'deselected');
        map.removeOverlay(polyline);

        polyline = new GPolyline.fromEncoded({
            color: unselected_color,
            weight: 2,
            points: encoded_points,
            levels: encoded_levels,
            zoomFactor: zoom_factor,
            numLevels: num_levels,
            opacity:1
        });

        map.addOverlay(polyline);
        marker.setImage(unselected_icon_image);
    }

    function _goToCloseup()
    {
        map.savePosition();
        map.setCenter(trailhead, 15);
    }

    function _clickZoom()
    {
        map.setCenter(trailhead, 15);
    }

    function _goBack()
    {
        if(!mouse_off)
        {
            map.returnToSavedPosition();
        }
        else
        {
            mouse_off = false;
        }
    }

    _init();
}

function MapTrails()
{
    var self = this;

    var trails_array = {};

    self.addTrail = function(map_trail)
    {
        trails_array[map_trail.getID().toString()] = map_trail;
    }

    self.each = function(callback)
    {
        $.each(trails_array, function(index, val){
           callback(index, val);
        });
    }

    self.getTrail = function(trail_id)
    {
        return trails_array[trail_id.toString()];
    }
}


