﻿var TrackSegment=Class.create();
TrackSegment.prototype={
    m_properties : null,
    initialize : function(t_properties)
    {
        this.m_properties = t_properties;
    },
    
    previousItem : function(t_index)
    {
        return this._navigate(this.m_properties.previousItemIndex);
    },
    
    nextItem : function(t_index)
    {
        return this._navigate(this.m_properties.nextItemIndex);
    },
    
    _navigate : function(t_index)
    {
        if( t_index < 0 ) 
        {
            return null;
        }
        else
        {
            return g_friluftRectangleTreeController.getItem(t_index, this.m_properties.layerId);
        }
    },
    
    getHashCode : function()
    {
        return this.m_properties.hashCode;
    },
    
    getTrackCoordinates : function(t_tolerance)
    {
        //Traverse linked list to first item.
        var t_firstItem = null;
        var t_item = this;
        while(t_item != null)
        {
            t_firstItem = t_item;
            t_item = t_item.previousItem();
        }
        
        t_item = t_firstItem;
        //Collect coordinates by traversing to the last item.
        var t_coordinates = [];
        while(t_item!=null)
        {
            var t_itemCoords = t_item._getCoordinates();
            var i, t_length = t_itemCoords.length;
            for(i=0; i<t_length; i++)
            {
                t_coordinates.push(t_itemCoords[i]);
            }
            t_item = t_item.nextItem();
        }
        
        //filter the coordinates according to the current map viewport and size.
        if( $defined(t_tolerance) )
        {
            t_coordinates = Utilities.Karto.douglasPeuckerReduction(
                this._createCoordinateObjects(
                    t_coordinates), t_tolerance);
        }
        else
        {
            t_coordinates = this._createCoordinateObjects(t_coordinates);
        }

//        //offset the coordinates.
//        var i, t_length=t_coordinates.length;
//        var t_offset = this.getOffset();
//        for(i=0; i<t_length; i++)
//        {
//            t_coordinates[i].x += t_offset.x;
//            t_coordinates[i].y += t_offset.y;
//        }
        
        return t_coordinates;
    },
    
    getCoordinatesWithOffset : function()
    {
        return this.m_properties.coordinates;
    },
    
    _getCoordinates : function()
    {
        var t_coords = new Array(this.m_properties.coordinates.length);
        var i, t_length = this.m_properties.coordinates.length;
        for(i=0; i<t_length; i++)
        {
            t_coords[i] = this.m_properties.coordinates[i];
            if( i==0 || i % 2 == 0 )
            {
                t_coords[i] += this.m_properties.offsetX;
            }
            else
            {
                t_coords[i] += this.m_properties.offsetY;
            }
        }
        
        return t_coords;
    },
    
    _createCoordinateObjects : function(t_coordinates)
    {
        var t_retval = [];
        var i, t_length = t_coordinates.length;
        for( i=0; i<t_length-1; i+=2 )
        {
            t_retval.push(new Coordinate(t_coordinates[i], t_coordinates[i+1]));
        }
        return t_retval;
    },
    
    getOffset : function()
    {
        return {x : this.m_properties.offsetX, y : this.m_properties.offsetY};
    }
};

