var uitschieters;
var blnResultsInit        = true;
arrSpan['Uitschieters'];
arrSpan['Gratis'];
arrSpan['GratisCv'];
arrSpan['GratisSww'];
arrSpan['Co'];
arrSpan['CoSite'];

// init
$(document).ready(function(){  
  $.post("http://"+domainName+"/core/remote/getResults.php",{data: ''},showDescResult);  
});

// showDescResult
function showDescResult(r){
  uitschieters = JSON.parse(r);
  initSpanCycle('Gratis',2);
  initSpanCycle('GratisSww',3);
  initSpanCycle('Uitschieters',1);
  initSpanCycle('GratisCv',0);  
  initSpanCycle('Co',3);
  coTop         = 0;
  coStartindex  = 0;
  $('#tbSelectSpanCoSite').children().each(function(index) {    
    if(uitschieters.co[$(this).attr('id')]['year']>coTop){
      coTop         = uitschieters.co[$(this).attr('id')]['year'];
      coStartindex  = index;      
    }
  });
  initSpanCycle('CoSite',coStartindex+1);

  setDetails('Uitschieters');
  setDetails('Gratis');
  setDetails('Co');

  blnResultsInit = false;
}

// initSpanCycle
function initSpanCycle(typeTb,start){
  var spanObj = $('#tbSelectSpan'+typeTb); 
  spanObj.cycle({
    fx: 'scrollHorz',
    speed:  'fast',
    timeout: 0,
    next: '#hpSpanNext'+typeTb,
    prev: '#hpSpanPrev'+typeTb,
    startingSlide: start,
    after: onAfter
  });
}

// onAfter
var gratisSite = Array();
var coSite;
function onAfter(curr, next, opts) {  
  switch($(curr).parent().attr('id')){
    case 'tbSelectSpanUitschieters':
      typeTb = 'Uitschieters';
      break;
    case 'tbSelectSpanGratis':
      typeTb = 'Gratis';
      break;
    case 'tbSelectSpanGratisCv':
      typeTb = 'GratisCv';
      gratisSite['cv'] = $(next).attr('id');
      break;
    case 'tbSelectSpanGratisSww':
      typeTb = 'GratisSww';
      gratisSite['sww'] = $(next).attr('id');
      break;
    case 'tbSelectSpanCo':
      typeTb = 'Co';
      break;
    case 'tbSelectSpanCoSite':
      typeTb = 'CoSite';
      coSite = $(next).attr('id');      
      break;
  }
  arrSpan[typeTb] = next.id; 
  showHideSpanPrevNext(typeTb);
  if(!blnResultsInit){
    setDetails(typeTb);
  }
}

// setDetails
function setDetails(typeTb){  
  switch(typeTb){
    // Uitschieters
    case 'Uitschieters':
      // temp
      $('#uitschieters_temp_max').html(uitschieters.temp.max[arrSpan[typeTb]]['val']+'°');
      $('#uitschieters_temp_max_site').html(uitschieters.temp.max[arrSpan[typeTb]]['site']);
      $('#uitschieters_temp_max_site').attr("href", "/p/energieproductie/t/"+lang+"/woning/"+uitschieters.temp.max[arrSpan[typeTb]]['siteId']);
      $('#uitschieters_temp_med').html(uitschieters.temp.med[arrSpan[typeTb]]['val']+'°');
      $('#uitschieters_temp_min').html(uitschieters.temp.min[arrSpan[typeTb]]['val']+'°');
      $('#uitschieters_temp_min_site').html(uitschieters.temp.min[arrSpan[typeTb]]['site']);
      $('#uitschieters_temp_min_site').attr("href", "/p/energieproductie/t/"+lang+"/woning/"+uitschieters.temp.min[arrSpan[typeTb]]['siteId']);
      // spf cv
      $('#uitschieter_spf_cv_site').html(uitschieters.spf.cv[arrSpan[typeTb]]['site']);
      $('#uitschieter_spf_cv_site').attr("href", "/p/energieproductie/t/"+lang+"/woning/"+uitschieters.spf.cv[arrSpan[typeTb]]['siteId']);
      arrSpfCv = String(uitschieters.spf.cv[arrSpan[typeTb]]['val']).split('.');
      $('#uitschieter_spf_cv_unit').html('');
      for(i=0;i<parseInt(arrSpfCv[0]);i++){
         $('#uitschieter_spf_cv_unit').append('<div class="unit"/>');
      }
      if(arrSpfCv.length>1){
        $('#uitschieter_spf_cv_unit').append('<div class="unit" style="width:'+arrSpfCv[1]+'px;"/>');
      }
      $('#uitschieter_spf_cv_txt').html(uitschieters.spf.cv[arrSpan[typeTb]]['val']);
      // spf sww
      $('#uitschieter_spf_sww_site').html(uitschieters.spf.sww[arrSpan[typeTb]]['site']);
      $('#uitschieter_spf_sww_site').attr("href", "/p/energieproductie/t/"+lang+"/woning/"+uitschieters.spf.sww[arrSpan[typeTb]]['siteId']);
      arrSpfSww = String(uitschieters.spf.sww[arrSpan[typeTb]]['val']).split('.');
      $('#uitschieter_spf_sww_unit').html('');
      for(i=0;i<parseInt(arrSpfSww[0]);i++){
         $('#uitschieter_spf_sww_unit').append('<div class="unit"/>');
      }
      if(arrSpfSww.length>1){
        $('#uitschieter_spf_sww_unit').append('<div class="unit" style="width:'+arrSpfSww[1]+'px;"/>');
      }
      $('#uitschieter_spf_sww_txt').html(uitschieters.spf.sww[arrSpan[typeTb]]['val']);
      break;
    // Gratis
    case 'Gratis':
      setDetailsGratis('cv');
      setDetailsGratis('sww');
      break;
    case 'GratisCv':
      setDetailsGratis('cv');
      break;
    case 'GratisSww':
      setDetailsGratis('sww');
      break;
    // Co2 besparing
    case 'Co':
      setDetailsCo();
      break;
    case 'CoSite':
      setDetailsCo();
      break;
    
  }
 }

function setDetailsCo(){
  coVerschil  = Math.round(uitschieters.co[coSite][arrSpan['Co']]);
  coKm        = Math.round(coVerschil/99*1000);
  blnShowCo = (coVerschil>0 && coKm >0)?true:false;
  if(String(coVerschil).length>3){
    coVerschil = String(coVerschil).substring(0,String(coVerschil).length-3)+'.'+String(coVerschil).substring(String(coVerschil).length-3);
  }
  if(String(coKm).length>3){
    coKm = String(coKm).substring(0,String(coKm).length-3)+'.'+String(coKm).substring(String(coKm).length-3);
  }
  if(blnShowCo){
    $("#coGr").html(coVerschil);
    $("#coKm").html(coKm);
  }else{
    $("#coGr").html('');
    $("#coKm").html('');
  }  
}


// setDetailsGratis
function setDetailsGratis(gratisType){
  arrGraphDetails = uitschieters.gratis[gratisSite[gratisType]][arrSpan['Gratis']][gratisType];
  
  var data = [];
  if(gratisType == 'cv'){
    data[0] = {label: 'el', data:parseInt(arrGraphDetails['el']), color: '#0077B2'};
    data[1] = {label: 'energy_cv', data:parseInt(arrGraphDetails['energy_cv']), color: '#90CE13'};
    data[2] = {label: 'kwh_bu', data:parseInt(arrGraphDetails['kwh_bu']), color: '#66CCFF'};
  }
  if(gratisType == 'sww'){
    data[0] = {label: 'el', data:parseInt(arrGraphDetails['el']), color: '#0077B2'};
    data[1] = {label: 'energy_sun', data:parseInt(arrGraphDetails['energy_sun']), color: '#FFB300'};
    data[2] = {label: 'energy_sww', data:parseInt(arrGraphDetails['energy_sww']), color: '#90CE13'};
    data[3] = {label: 'kwh_sww_sww', data:parseInt(arrGraphDetails['kwh_sww_sww']), color: '#66CCFF'};
  }
  
  if(parseInt(arrGraphDetails['energy_cv'])== 0 && parseInt(arrGraphDetails['kwh_bu'])== 0){
    data = []
  }
  
  $.plot($("#graph"+gratisType), data,
  {
    series: {
      pie: {
        show: true,
        radius: 1,
        label: {
          show: true,
          radius: 2/3,
          formatter: function(label, series){
            return '<div class="piedetail">'+Math.round(series.percent)+'%</div>';
          },
          threshold: 0.1
        },
        stroke: {
          color: '#000000'
        }
      }
    },
    legend: {
        show: false
    }
  });
  
}

// showHideSpanPrevNext
function showHideSpanPrevNext(typeTb){
  spanType = arrSpan[typeTb];
  if(spanType == 'year'){
    $('#hpSpanNext'+typeTb).attr('class','NoScreenDisplay');
    $('#hpSpanNextDummy'+typeTb).attr('class','hpCol');
  }else{
    $('#hpSpanNext'+typeTb).attr('class','hpCol');
    $('#hpSpanNextDummy'+typeTb).attr('class','NoScreenDisplay');
  }
  if(spanType == 'day'){
    $('#hpSpanPrev'+typeTb).attr('class','NoScreenDisplay');
    $('#hpSpanPrevDummy'+typeTb).attr('class','hpCol');

    $('#hpDatePrev'+typeTb).attr('class','NoScreenDisplay');
    $('#hpDatePrevDummy'+typeTb).attr('class','hpCol');
    $('#hpDateNext'+typeTb).attr('class','NoScreenDisplay');
    $('#hpDateNextDummy'+typeTb).attr('class','hpCol');
  }else{
    $('#hpSpanPrev'+typeTb).attr('class','hpCol');
    $('#hpSpanPrevDummy'+typeTb).attr('class','NoScreenDisplay');

    $('#hpDatePrev'+typeTb).attr('class','hpCol');
    $('#hpDatePrevDummy'+typeTb).attr('class','NoScreenDisplay');
    $('#hpDateNext'+typeTb).attr('class','hpCol');
    $('#hpDateNextDummy'+typeTb).attr('class','NoScreenDisplay');
  }
}
