/*
    Defines Segments which track a series of points that can be strung together to 
    indicate a route.
 
    Segments have:
       maximum Revenue Speed
       a list of segment sections
*/

function Segment(segmentSections, speed) {
    this.segmentSections = segmentSections;
    for(var i = 0; i < this.segmentSections.length; i++ ) {
        if ( this.segmentSections[i] instanceof Array ) {
            this.segmentSections[i] = new SegmentSection(this.segmentSections[i][0], this.segmentSections[i][1]);
        }
    }
    this.speed = speed;
}
Segment.prototype.getLine = function () {
    if ( !this.line ) {
        this.line = new GPolyline( this.getPoints(), this.getColor(), this.getWeight(), this.getOpacity() );
    }
    return this.line;
}

Segment.prototype.getPoints = function() {
    if ( !this.points ) {

        this.points = [];
        for(var i = 0 ; i < this.segmentSections.length; i++) {
            var segPoints = this.segmentSections[i].getPoints();
            this.points = this.points.concat(segPoints);
        }
     }
     return this.points;
}
Segment.prototype.getWeight = function () {
    return 4;
}

Segment.prototype.getOpacity = function() {
    return 0.5;
}
Segment.prototype.getColor = function() {
    return "#009900";
}
Segment.prototype.getSections = function() {
    return this.segmentSections;
}
/*
 index, segmentSection
 or just 
 segmentSection
*/
Segment.prototype.insertSection = function(a, b) {
    if ( b ) {
        var index = a;
        var segmentSection = b;
        if ( index == 0 ) {
            this.segmentSections = [ segmentSection ].concat(this.segmentSections);
        } else {
            this.segmentSections = this.segmentSections.slice(0, index).
                concat(segmentSection, this.segmentSections.slice(index));
        }
    } else {
        this.segmentSections.push(a);
        return;
    }
    this.uncache();
}

Segment.prototype.removeSection = function(index) {
    this.segmentSections.splice(index,index);
    this.uncache();
}

Segment.prototype.replaceSection = function(index, section) {
    this.segmentSections[index] = section;
    this.uncache();
}
Segment.prototype.uncache = function() {
    delete this.points;
    delete this.line;
}

/* SegmentSection definition */

/*
 a--> must be endLatitude or GPoint
 b--> must be endLongitude or undefined
 c--> must be radius or undefined
 
if radius is specified then this is an arc
 */
function SegmentSection(a, b, c) {
    if ( c ) {
        this.latitude = a;
        this.longitude = b;
        this.radius = c;
    } else if ( b) {
        this.latitude = a;
        this.longitude = b;
    } else {
        this.latitude = a.y;
        this.longitude = a.x;
    }
}
SegmentSection.prototype.getPoints = function () {
    if ( !this.points ) {
        this.points = [];
/*        if ( !this.radius ) { */
            this.points.push(new GPoint(this.longitude, this.latitude));
/*        } else {
        }*/
    }

    return this.points;
}

