/**
 * Timeline
 *
 * Contains TimelineGroup and TimelineEvent
 *
 * (c)2006 Jimmy Moore
 */


/*******************************************************************************
 * TimelineGroup - This defines a collection of TimelineEvents.
 *
 ******************************************************************************/
function TimelineGroup() {
    this.events = new Array();
}

/**
 * Add a new event to this group.
 *
 */
TimelineGroup.prototype.addEvent = function(e) {
    this.events.push(e);
    this.sort();
}

TimelineGroup.prototype.sort = function() {
    this.events.sort(TimelineEvent.dateSorter);
}

/**
 * Show the group.
 *
 */
TimelineGroup.prototype.toString = function() {
    p = "";
    for (i=0;i<this.events.length;i++) {
        p = p + this.events[i] + "<br>";
    }
    return p;
}

/*******************************************************************************
 * TimelineEvent
 *
 * date         \ Set by constructor
 * shorttext    /
 *
 * color
 * bcolor
 * displayThresholdMin
 * displayThresholdMax
 *
 * endDate      // TODO for ranges.
 *
 ******************************************************************************/
function TimelineEvent(date, shorttext) {
    this.date = date;
    this.shorttext = shorttext;

    this.setColors("#d0ffd0", "#008000"); 
}

TimelineEvent.dateSorter = function(a, b) {
    return a.date.getTime() - b.date.getTime();
}

TimelineEvent.prototype.setColors = function(c, b) {
    this.color = c;
    this.bcolor = b;
}

TimelineEvent.prototype.shouldDisplay = function(startDate, endDate) {
    ra = endDate.getTime() - startDate.getTime();
    if (this.displayThresholdMin) {
        if (ra > this.displayThresholdMin) return false;
    }

    if (this.displayThresholdMax) {
        if (ra < this.displayThresholdMax) return false;
    }
    return true;
}

/**
 * Show the event as a string
 *
 */
TimelineEvent.prototype.toString = function() {
    return "[TimelineEvent] Date='" + this.date + "', shorttext=\"" + this.shorttext + "\"";
}

/**
 * This is used to place the event on the timeline.
 * Returns a number from 0-scale, or null if the event is not in the view.
 */
TimelineEvent.prototype.getOffset = function(startDate, endDate, scale) {
    return getTimeOffset(this.date, startDate, endDate, scale);
}


function getTimeOffset(date, startDate, endDate, scale) {
    if (date.getTime() < startDate.getTime()
     || date.getTime() > endDate.getTime()) return null;
    var timespan = endDate.getTime() - startDate.getTime();
    return Math.floor(((date.getTime() - startDate.getTime()) * scale) / timespan);
}

