/*<![CDATA[*/

if(typeof StocksGraph != 'function') { 

  function StocksGraph(now, containerID) {
    this.now = now;
    this.containerID = containerID;
    this.graphTitle = '';
    this.graphSubtitle = '';
    this.xTitle = '';
    this.xStep = 1;
    this.yMin = 0;
    this.yMax = 0;
    this.margin = [35, 0, 18, 40];
    this.height = 150;
    this.data = [];
    this.isSmall = false;

    this.render = function (period) {
      var xMin;
      var tInterval;

      if (period == 'week') {
        periodLength = 7*24*3600;
        tInterval = 24 * 3600;
      } else if (period == 'month') {
        periodLength = 30*24*3600;
        tInterval = 7 * 24 * 3600;
      } else if (period == 'half') {
        periodLength = 6*30*24*3600;
        tInterval = 30* 24 * 3600;
      } else {
        periodLength =  365*24*3600;
        tInterval = 30* 24 * 3600;
      }

      xMin = (this.now - periodLength) * 1000;

      yFactor = this.isSmall ? 2 : 4;

      yRealTickInterval = (this.yMax - this.yMin)/yFactor;
      yTickInterval = 5;
      yTickIntervals=[10,50,100,200,250,500,1000, 5000, 10000];


      for(i = 0; i < yTickIntervals.length; ++i) {
        if (yRealTickInterval > yTickIntervals[i]) {
          yTickInterval = yTickIntervals[i];
        }
      }

      // nastaveni max a min osy Y
      this.yMax = 0;
      this.yMin = 10000;
      for (i = 0; i < this.data.length; ++i) {
        if (this.data[i][0] > (xMin)) {
          if (this.data[i][1] > this.yMax) {
            this.yMax = this.data[i][1];
          }
          if (this.data[i][1] < this.yMin) {
            this.yMin = this.data[i][1];
          }

        }
      }


      chart = new Highcharts.Chart({
        chart: {
          renderTo: this.containerID,
          zoomType: 'x',
          margin: this.margin,
          height: this.height
        },
        colors: [
        '#89A54E',
        '#4572A7',
        '#AA4643',
        '#80699B',
        '#3D96AE',
        '#DB843D',
        '#92A8CD',
        '#A47D7C',
        '#B5CA92'
        ],
        credits: {
          enabled: false
        },
        title: {
          text: this.graphTitle,
          style: {
            fontSize: '12px',
            color: '#5E8601',
            fontWeight: 'bold'
          }
        },
        subtitle: {
          text: this.graphSubtitle
        },
        xAxis: {
          type: 'datetime',
          maxZoom: 24 * 3600 * 1000, // fourteen days
          title: {
            text: this.xTitle
          },
          dateTimeLabelFormats: {
            second: '%H:%M:%S',
            minute: '%H:%M',
            hour: '%H:%M',
            day: '%e.%m.',
            week: '%e.%m.',
            month: '%b %y',
            year: '%Y'
          },
          labels: {
            style: {
              fontSize: '10px'
            },
            step: this.xStep
          },
          min: (xMin - 8*3600), // * 1000,
          //            min: (<{'2010-06-01'|strtotime}> - 8*3600) * 1000, // odectu 8 hodin, at osa zacina trosku driv
          max: (this.now + 8*3600) * 1000, // prictu 8 hodin, at osa konci trosku dal
          //            min: (<{$firstDayTimestamp}> - 8*3600) * 1000, // odectu 8 hodin, at osa zacina trosku driv
          //            max: (<{$lastDayTimestamp}> + 8*3600) * 1000, // prictu 8 hodin, at osa konci trosku dal
          tickInterval: tInterval * 1000,

          minPadding: 0.2

        //showFirstLabel: true,
        //            startOnTick: true,
        //            endOnTick: true
        },
        yAxis: {
          title: {
            text: '' // this.yTitle
          },
          min: this.yMin - (0.05*(this.yMax-this.yMin)),
          max: this.yMax + (0.05*(this.yMax-this.yMin)),
          startOnTick: false,
          showFirstLabel: false,
          labels: {
            style: {
              fontSize: '10px'
            },
            step: 1,
            formatter: function() {
              return this.value; // clean, unformatted number for thousands
            }
          }
        ,  tickInterval: yTickInterval
        },
        tooltip: {
          formatter: function(unit) {
            return ''+
            Highcharts.dateFormat('%d.%m.%Y', this.x) + ': <strong>'+
            ''+ Highcharts.numberFormat(this.y, 2)+'</strong>';
          },
          borderColor: '#89A54E',
          crosshairs: true
        },
        legend: {
          enabled: false
        },
        plotOptions: {
          area: {
            fillColor: {
              linearGradient: [0, 0, 0, 150],
              stops: [
              [0, 'rgb(107, 150, 0)'],
              [1, 'rgba(126, 178, 0, 0.7)']
              ]
            },
            lineWidth: 1,
            lineColor: '#89A54E',
            marker: {
              fillColor: '#fff',
              lineColor: '#89A54E',
              lineWidth: 2,
              enabled: false,
              states: {
                hover: {
                  enabled: true,
                  radius: 5
                }
              }
            },
            shadow: false,
            states: {
              hover: {
                lineWidth: 1
              }
            }
          }
        },

        series: [{
          type: 'area',
          //						pointInterval: 30 * 24 * 3600 * 1000,
          //						pointStart: Date.UTC(<{$thisYear}>, <{$thisMonth}>, 01),
          data: this.data
        }]
      });
    }
  }

  Highcharts.setOptions({
    lang: {
      decimalPoint: ",",
      downloadPNG: "Stáhnout PNG obrázek",
      downloadJPEG: "Stáhnout JPEG obrázek",
      downloadPDF: "Stáhnout PDF dokument",
      downloadSVG: "Stáhnout SVG vektorový obrázek",
      exportButtonTitle: "Exportovat do rastrového nebo vektorového obrázku",
      loading: "Načítám...",
      months: ['leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen', 'září', 'říjen', 'listopad', 'prosinec'],
      printButtonTitle: "Vytisknout graf",
      resetZoom: "Výchozí",
      resetZoomTitle: "Zrušit úroveň přiblížení na 1:1",
      thousandsSep: " ",
      weekdays: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek']
    },
    toolbar: {
      itemStyle: {
        color: '#4572A7',
        cursor: 'pointer',
        fontSize: '11px'
      }
    }
  });

}

function switchGraph(active, tabsElement) {
  $(tabsElement).previous('div.tabs_wrp').select('ul li a').each(function (a, i){
    a = $(a);
    a.removeClassName('active');
    if (active == i) {
      a.addClassName('active');
    }
  });
  $(tabsElement).select('.tab').each(function(tab, i){
    tab = $(tab);
    tab.hide();
    if (active == i) {
      tab.show();
    }
  })
}

  /*]]>*/
