/*
 * Copyright (C) 2009 WorkSmart Labs, Inc.
 */

/**
 * Abstracts away operations with the TrackChart that displays
 * elevation data (and soon other data) about the track.
 *
 * @author Artem Petakov <artem@worksmartlabs.com>
 */

/**
 * Creates a new TrackChart object.
 * @param container DOMElement container where to put the chart. 
 */
function TrackChart(container, background, mode) {
    var NO_DATA_AVAILABLE_MESSAGE = "Sorry, no elevation data is available for this track.";   
    this.container_ = container;
    this.background_ = background;
    this.chartId_ = container.id + "id";
    if (mode == TracksPage.TracksPageType.normal || 
        mode == TracksPage.TracksPageType.shared){
    	this.width_ = "789";
    } else {
    	this.width_ = "695";
    }
    this.fusionChart_ = new FusionCharts(
            "fusion_charts/FCF_Line.swf?ChartNoDataText=" + NO_DATA_AVAILABLE_MESSAGE, 
            this.chartId_, this.width_, "140");
    this.fusionChart_.setDataXML(this.getChartXml_(null));
    this.fusionChart_.render(this.container_.id);
} 

/**
 * Displays the altitude data that was passed in on the graph. 
 */
TrackChart.prototype.showData = function(elevationArray) {
	var strXML = this.getChartXml_(elevationArray);
	// Execute this code right after the rest of this event completes to give
	// the chart time to be created. Otherwise, we may try to load data into a
	// chart that
	// does not exist yet.
	var me = this;
	setTimeout(function() {
		updateChartXML(me.chartId_, strXML);
		displayBlock(me.background_);
		me.container_.style.visibility = 'visible';
		me.container_.style.height = '150px';
	}, 10);
}

// TODO(artem): Figure out why display:none does not work as well
// as visibility:hidden to hide the track (why it causes the track
// not to be drawn sometimes).
TrackChart.prototype.hide = function() {
    this.container_.style.visibility = 'hidden';
    this.container_.style.height = '0px';
    displayNone(this.background_);
}

/**
 * Get the XML needed to draw the ChartFusion graph.
 * @param trackElevationArray double[] array of elevations to use. If null, it means there is
 *        no elevation data available, so generate a blank chart.
 */
TrackChart.prototype.getChartXml_ = function(trackElevationArray) {
    // TODO(artem): Intelligently compute the yAxisMinValue and yAxisMaxValue,
    // since ChartFusion always seems to want to include 0 on the axis.
    // TODO(artem): Compute and display the time when the point was gathered -- right now
    // this will not work correctly if there are breaks in GPS data. The plan is to add
    // semi-transparent linearly interpolated points, and also set the time
    // for all points.
    var strXML = 
        "<graph xAxisName='Time' " +
        "yAxisMinValue='15000' yAxisName='Elevation (m)' decimalPrecision='1' formatNumberScale='0' " + 
        "numberPrefix='' showNames='0' showValues='0' showAlternateHGridColor='0' " + 
        "divlinecolor='CCCCCC' lineColor='1C73A4' canvasBgColor='F6F6F4' " +
        "canvasBorderColor='333333' animation='0' " +
        "showAnchors='0' showShadow='0' bgColor='F6F6F4' canvasBorderThickness='0' " +
        "zeroPlaneAlpha='0' baseFontColor='333333' canvasBorderColor='333333' >";
    if (trackElevationArray) {
        var MAX_NUMBER_OF_POINTS_TO_DISPLAY = 700;
        var stepSize = 1;
        if (trackElevationArray.length > MAX_NUMBER_OF_POINTS_TO_DISPLAY) {
            stepSize = Math.floor(trackElevationArray.length / MAX_NUMBER_OF_POINTS_TO_DISPLAY);
        }
        for (var i = 0; i < trackElevationArray.length; i+=stepSize) {
            currentPoint = Math.floor(i);
            strXML += "<set name='" + currentPoint + "' value='" +
                trackElevationArray[currentPoint] + "'/>";
        }
    }
    strXML += "</graph>";
    return strXML; 
}