var unit_price, unit_date, unit_nav, yield_value, lang_date, tick_interval, guaranteed_price,
    currency         = currency || 'Ft/Pc',
    site_lang        = site_lang || 'hu',
    names            = [],
    chartColors      = [ '#d46a0a', '#a0865b', '#395e7a', '#d2d259', '#0eb9c4',
                         '#c9ae9c', '#c26ebb', '#84b54f', '#348f8c', '#fb9f9f',
                         '#853b81', '#ecec94', '#c03b57', '#875a52', '#b79e76',
                         '#969696', '#3f52a3', '#58a111', '#e79f43', '#c5c5c5'],
    chartColors_temp = [];

function linechartFunc(data){
  var chartSeries = [],
      chartSeriesPercent = [],
      i = 0,
      j = 0,
      act_color = '';

  chartNames_temp        = [];
  chartColors_temp       = [];
  chartColors_temp_yield = [];
  chartSeriesDataTable   = [];

  for (var fund in data){
    chartNames_temp.push({
      id: fund
    });
    
    chartSeries.push({
      name: $('#fmChbx-' + fund).next().html().split('<')[0],
      type: 'spline',
      data: []
    });

    chartSeriesPercent.push({
      name: $('#fmChbx-' + fund).next().html().split('<')[0],
      type: 'spline',
      data: []
    });

    chartSeriesDataTable.push({
      id:   fund,
      data: []
    })
    
    fund_temp = data[fund];
    
    
    var dataPrice;
    
    for (var fund_date in fund_temp){
      var d = fund_temp[fund_date].date;
      
      switch (site_lang){
        case 'en' :
          dataPrice = (Math.round(fund_temp[fund_date].price / fund_temp[0].price * 100 * 100) - 10000) / 100;
          d = d.split('/');
          chartSeriesPercent[j].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(dataPrice)]);
          chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(fund_temp[fund_date].price.replace(',','.'))]);
        break;
        case 'hu' :
          dataPrice = (Math.round(fund_temp[fund_date].price.replace(',','.') / fund_temp[0].price.replace(',','.') * 100 * 100) - 10000) / 100;
          d = d.split('. ');
          chartSeriesPercent[j].data.push([Date.UTC(d[0], d[1]-1, d[2]), parseFloat(dataPrice)]);
          chartSeries[i].data.push([Date.UTC(d[0], d[1]-1, d[2]), parseFloat(fund_temp[fund_date].price.replace(',','.'))]);
        break;
        case 'ro' :
          dataPrice = (Math.round(fund_temp[fund_date].price.replace(',','.') / fund_temp[0].price.replace(',','.') * 100 * 100) - 10000) / 100;
          d = d.split('/');
          chartSeriesPercent[j].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(dataPrice)]);
          chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(fund_temp[fund_date].price.replace(',','.'))]);
        break;
        case 'sk' :
          dataPrice = (Math.round(fund_temp[fund_date].price.replace(',','.') / fund_temp[0].price.replace(',','.') * 100 * 100) - 10000) / 100;
          d = d.split('.');
          chartSeriesPercent[j].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(dataPrice)]);
          chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(fund_temp[fund_date].price.replace(',','.'))]);
        break;
      }
      chartSeries[i].dashStyle = 'line';

      chartSeriesDataTable[j].data.push([ fund_temp[fund_date].date,
                                          fund_temp[fund_date].price,
                                          fund_temp[fund_date].nav
                                        ]);
    }

    act_color = chartColors[parseInt($('#' + chartNames_temp[i].id).parent()[0].className.replace('color_',''))];
    chartColors_temp.push(act_color);
    chartColors_temp_yield.push(act_color);

    i++;

    if (guaranteed[fund] != null) {
      chartNames_temp.push({
        id: fund
      });

      chartSeries.push({
        name: $('#fmChbx-' + fund).next().html().split('<')[0] + ' gp',
        type: 'spline',
        data: []
      });

      for (var fund_date in fund_temp){
        d = fund_temp[fund_date].date;
        var guaranteed_rate = guaranteed[fund].rate;

        switch (site_lang){
          case 'en' :
            d = d.split('/');
            chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(guaranteed_rate.replace(',','.'))]);
          break;
          case 'hu' :
            d = d.split('. ');
            chartSeries[i].data.push([Date.UTC(d[0], d[1]-1, d[2]), parseFloat(guaranteed_rate.replace(',','.'))]);
          break;
          case 'ro' :
            d = d.split('/');
            chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(guaranteed_rate.replace(',','.'))]);
          break;
          case 'sk' :
            d = d.split('.');
            chartSeries[i].data.push([Date.UTC(d[2], d[1]-1, d[0]), parseFloat(guaranteed_rate.replace(',','.'))]);
          break;
        }
        chartSeries[i].dashStyle = 'shortdot';
      }

      chartColors_temp.push(act_color);

      i++;
    }
    j++; /* for table */
  }

  var period = $('#period option:selected').val();
  
  switch (period){
    case  '1' : tick_interval =  7 * 3600 * 24000; break;
    case  '3' : tick_interval = 20 * 3600 * 24000; break;
    case  '6' : tick_interval = 30 * 3600 * 24000; break;
    case '12' : tick_interval = 91 * 3600 * 24000; break;
    case  '0' : tick_interval = null; break;
  }

  var unit_price_chart = new Highcharts.Chart({
    chart: {
      renderTo: 'unit_price_chart',
      width: 530,
      style: {
        font: '10px "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
      }
    },
    title: {
      text: ''
    },
    colors: chartColors_temp,
    plotOptions: {
      spline: {
        lineWidth: 2,
        marker: {
          enabled: false,
          states: {
            hover: {
              enabled: true,
              radius: 3
            }
          }
        }
      }
    },
    legend: {
      enabled: false
    },
    tooltip: {
      style: {
        font: '10px "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
      },
      formatter: function(){
        var d = new Date(this.x);
        switch(site_lang) {
          case 'en' :
          case 'ro' :
            lang_date = dates.twoDigit(d.getDate()) + '/' + dates.twoDigit(d.getMonth()+1) + '/' + d.getFullYear();
            break;
          case 'hu' :
            lang_date = d.getFullYear() + '. ' + dates.twoDigit(d.getMonth()+1) + '. ' + dates.twoDigit(d.getDate()) + '.';
            break;
          case 'sk' :
            lang_date = dates.twoDigit(d.getDate()) + '.' + dates.twoDigit(d.getMonth()+1) + '.' + d.getFullYear() + '.';
            break;
        }

        var series_name = this.series.name;
        var series_name_gp = series_name.split(' gp');
        series_name    = series_name.split(' ');

        if (series_name[series_name.length-1] == 'gp') { // guaranteed price
          return '<strong>' + series_name_gp[0] + '</strong><br/>' + unit_price + ':<strong> ' + this.y + '</strong>';
        }
        else {
          return '<strong>' + this.series.name + '</strong><br/>' + unit_date  + ':<strong> ' + lang_date + '</strong><br/>' + unit_price + ':<strong> ' + this.y + '</strong>';
        }
      }
    },
    xAxis: {
      tickInterval: tick_interval,
      lineColor: '#c0c0c0',
      labels: {
        y: 40,
        x: -15,
        rotation: -45,
        style: {
          align: 'right'
        },
        formatter: function(){
          var d = new Date(this.value);
          switch (site_lang){
            case 'en' :
            case 'ro' :
              lang_date = dates.twoDigit(d.getDate()) + '/' + dates.twoDigit(d.getMonth()+1) + '/' + d.getFullYear();
              break;
            case 'hu' :
              lang_date = d.getFullYear() + '. ' + dates.twoDigit(d.getMonth()+1) + '. ' + dates.twoDigit(d.getDate()) + '.';
              break;
            case 'sk' :
              lang_date = dates.twoDigit(d.getDate()) + '.' + dates.twoDigit(d.getMonth()+1) + '.' + d.getFullYear() + '.';
              break;
          }
          return lang_date;
        }
      }
    },
    yAxis: {
      title: {
        text: currency,
        margin: 20,
        style: {
          color: '#666666',
          font: '10px bold "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
        }
      }
    },
    credits: {
      enabled: false
    },
    series: chartSeries
  });
  
  
// yield
  var yield_chart = new Highcharts.Chart({
    chart: {
      renderTo: 'yield_chart',
      width: 530,
      style: {
        font: '10px "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
      }
    },
    title: {
      text: ''
    },
    colors: chartColors_temp_yield,
    plotOptions: {
      spline: {
        lineWidth: 2,
        marker: {
          enabled: false,
          states: {
            hover: {
              enabled: true,
              radius: 3
            }
          }
        }
      }
    },
    legend: {
      enabled: false
    },
    tooltip: {
      style: {
        font: '10px "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
      },
      formatter: function(){
        var d = new Date(this.x);
        switch(site_lang) {
          case 'en' :
          case 'ro' :
            lang_date = dates.twoDigit(d.getDate()) + '/' + dates.twoDigit(d.getMonth()+1) + '/' + d.getFullYear();
            break;
          case 'hu' :
            lang_date = d.getFullYear() + '. ' + dates.twoDigit(d.getMonth()+1) + '. ' + dates.twoDigit(d.getDate()) + '.';
            break;
          case 'sk' :
            lang_date = dates.twoDigit(d.getDate()) + '.' + dates.twoDigit(d.getMonth()+1) + '.' + d.getFullYear() + '.';
            break;
        }
        return '<strong>' + this.series.name + '</strong><br/>' + unit_date  + ':<strong> ' + lang_date + '</strong><br/>' + yield_value + ':<strong> ' + this.y + '%</strong>';
      }
    },
    xAxis: {
      tickInterval: tick_interval,
      lineColor: '#c0c0c0',
      labels: {
        y: 40,
        x: -15,
        rotation: -45,
        style: {
          align: 'right'
        },
        formatter: function(){
          var d = new Date(this.value);
          switch (site_lang){
            case 'en' :
            case 'ro' :
              lang_date = dates.twoDigit(d.getDate()) + '/' + dates.twoDigit(d.getMonth()+1) + '/' + d.getFullYear();
              break;
            case 'hu' :
              lang_date = d.getFullYear() + '. ' + dates.twoDigit(d.getMonth()+1) + '. ' + dates.twoDigit(d.getDate()) + '.';
              break;
            case 'sk' :
              lang_date = dates.twoDigit(d.getDate()) + '.' + dates.twoDigit(d.getMonth()+1) + '.' + d.getFullYear() + '.';
              break;
          }
          return lang_date;
        }
      }
    },
    yAxis: {
      title: {
        text: '%',
        margin: 20,
        style: {
          color: '#666666',
          font: '10px bold "Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica,sans-serif'
        }
      }
    },
    credits: {
      enabled: false
    },
    series: chartSeriesPercent
  });
}
