/**------------------------------------------------------------------------
 * Set of function and classes which deal with bond yield curves chart on
 * the bond overview page
 *
 * Dependencies:
 *  dojo.js, version 1.1 
 *  yield_curves_chart_utils.js
 *  yield_curves_data_utils.js
 *
 * Author: 
 *  Bartlomiej.Pawlowski@swx.com
 *
 * @version $Id: yield_curves_chart.js,v 1.1 2009/08/11 15:44:42 ssk Exp $
 *
 *-----------------------------------------------------------------------*/


/**
 * YieldCurvesSimpleChart class produces the yield chart on the bond overview page
 * 
 * @param yieldData (hash) like { CH: {"1D": 1.22, "1Y" : 3.2... }, US: { ... } }
 * @param xAxisLabels (array) array with all x axis labels
 * @param htmlUniqueKey (string) unique id that is appended to all yield curve specific ids
 * @param chartOpts (hash) with the following keys: 
 *          tooltip_yield_label, tooltip_term_label, default_country_code 
 */
function YieldCurvesSimpleChart(yieldData, xAxisLabels, htmlUniqueKey, chartOpts) {
 
    // ------------------------ constants 
    // static values that can be used as public values
    this.pubstatics = {

        GRAPH_COLOR     : "#306e92"    // color used for markers on graph on a simple chart
        
    }

    // ------------------------ private variables 

    var _objRef = this;

    // hash containig all yield curves data
    var _yieldData = yieldData;

    // x axis labels
    var _xAxisLabels = xAxisLabels;


    var _chartOpts = chartOpts;

    // current country code
    var _countryCode = _chartOpts.default_country_code;

    if ( !_countryCode )
        _countryCode = "CH";

    var _htmlUniqueKey = htmlUniqueKey;

    // the main surface on which the whole chart will be drawn
    var _mainSurface;

    // x coordinates of the x axis labels in pixels
    // { "2Y": 30 }
    var _xAxisLabelsCoords = {};

    // keeps all divs as around nodes for tooltips
    var _tooltipDivs = [];

    // description of the main grid, x, y, width and height
    var _gridDesc = {};

    var _dataUtils = new YieldDataUtils();

    var _chartUtils = new YieldCurvesChartUtils(htmlUniqueKey);

    // ------------------------ public methods 

    this.initChart = function() {
        var chartNode = dojo.byId("yieldCurvesChartDiv"+ _htmlUniqueKey);
        var layout = _chartUtils.pubstatics.SIMPLE_CHART_LAYOUT;
        
        _mainSurface = dojox.gfx.createSurface(chartNode, layout.chart_width, layout.chart_height);

        _gridDesc = { 
                x : layout.grid_x, y: layout.grid_y, 
                width: layout.chart_width - (layout.grid_x + layout.grid_margin_right), 
                height:layout.chart_height - (layout.grid_y + layout.grid_margin_bottom)  
        };

        _render();

    }

    /**
     * Change country code and redraw the chart for it 
     */
    this.changeCountryCode = function(newCountryCode) {
        _countryCode = newCountryCode;
        _reloadChart();
    }


    // ------------------------ private methods

    function _reloadChart() {
        dojo.byId("chartLoadingDiv"+ _htmlUniqueKey).style.display="";
        dojo.byId("yieldCurvesChartDiv"+ _htmlUniqueKey).style.display="none";

        _clear();
        _render();

        dojo.byId("chartLoadingDiv"+ _htmlUniqueKey).style.display="none";
        dojo.byId("yieldCurvesChartDiv"+ _htmlUniqueKey).style.display="";
    }

    function _render() {

        // make y labels
        var minMaxYield = _dataUtils.getMinMax(_yieldData[_countryCode].data);

        var ylabels = _chartUtils.makeYLabels(Math.floor(minMaxYield.min), Math.ceil(minMaxYield.max), false);

        // create grid
        var g = _chartUtils.createGrid(_mainSurface, _gridDesc, _xAxisLabels, ylabels,
                            {
                                gridColor       : _chartUtils.pubstatics.GRID_COLOR,
                                gridBgColor     : _chartUtils.pubstatics.GRID_BG_COLOR,
                                xAxisLabel      : _chartOpts.tooltip_term_label,
                                xAxisLabelStep  : 3
                            }); 

        var gridGroup = g.gridGroup; 
        _xAxisLabelsCoords = g.xCoords;

        // create graph
        var gr = _chartUtils.createGraph(_mainSurface, _gridDesc, minMaxYield, 
                                      _yieldData[_countryCode].data,
                                      _xAxisLabelsCoords,
                                      _createTooltipText,
                                      {
                                        graphColor : _objRef.pubstatics.GRAPH_COLOR,
                                        graphMarkerType : "circle"
                                      }
                                      );
        var graphGroup = gr.group;
        _tooltipDivs = gr.tooltipDivs;


        // set the date on the yield chart headline row
        dojo.byId("yieldChartDate"+ _htmlUniqueKey).innerHTML = 
            _yieldData[_countryCode].date.replace(/ \/ /," &nbsp;&nbsp;&nbsp;/\ &nbsp;&nbsp;&nbsp;");

    }

    /**
     * Clear/reset all some calculated data
     */
    function _clear() {
        _mainSurface.clear();

        // remove all tooltip divs
        var tooltipGroupNode = dojo.byId(_chartUtils.pubstatics.TOOLTIP_GROUP_ID + _htmlUniqueKey);
        
        while ( _tooltipDivs.length ) {
            var div = _tooltipDivs.shift();
            tooltipGroupNode.removeChild(div);
            div = null;
        }
        
    }

    /**
     * Create tooltip text
     * @param term (string) 
     * @param value (number)
     * @return tooltip text (string)
     */
    function _createTooltipText(term, value) {
        return _chartOpts.tooltip_term_label +': ' + term 
                + ', ' + _chartOpts.tooltip_yield_label + ': ' + value + '%'

    }
}



