/*
Settingsfile for JQuery plugin for rendering gauges, v. 1.0

* Initial create by Henkka, Jan 2010

*/

$(function () {
	// Settings start
	// Few notes:
	// - dots means how many values between the numbers in the gauge
	// - Offsets is for finetuning the placement of the values

	
	// Data file and messages
	var dataurl = '/clientraw.txt';
	var updates = 99999999;
	var timeout = 20; // In sec
	var errormsg = "Error with getting new data";
	var pausemsg = "Updates paused, refresh page to continue.\nP&auml;ivitys tauonnut, p&auml;ivit&auml;ä sivu jatkaaksesi.";
	
	// Temperature
	var showtemp = true;
	var tempgaugeMax = 40;
	var tempgaugeMin = -30;
	var tempnumbersoffset = 13;
	var	tempuom = '&deg;C'; // &deg;C & &deg;F supported
	var tempdots = 10;
	var tempdotsoffset = 13;
	var tempstartcolor = '92,172,238';
  var tempstopcolor = '255,100,100';
  var tempmiddlecolor = '255,255,255'; 
  
  // Windchill/humidx
  var showchill = false;
	var chillgaugeMax = 40;
	var chillgaugeMin = -40;
	var chillnumbersoffset = 13;
	var	chilluom = '&deg;C'; // &deg;C & &deg;F supported
	var chilldots = 10;
	var chilldotsoffset = 13;
	var chillstartcolor = '92,172,238';
  var chillstopcolor = '255,100,100';
  var chillmiddlecolor = '255,255,255';
  
  // Dewpoint
  var showdew = false;
	var dewgaugeMax = 30;
	var dewgaugeMin = -30;
	var dewnumbersoffset = 13;
	var	dewuom = '&deg;C'; // &deg;C & &deg;F supported
	var dewdots = 10;
	var dewdotsoffset = 13;
	var dewstartcolor = '92,172,238';
  var dewstopcolor = '255,100,100';
  var dewmiddlecolor = '255,255,255';
	
	// Humidity
	var showhum = true;
  var humgaugeMax = 100;
	var humgaugeMin = 0;
	var humnumbersoffset = 13;
	var	humuom = '%';
	var humdots = 20;
	var humdotsoffset = 13;
  var humstartcolor = '255,255,255';
  var humstopcolor = '0,205,0';
  var hummiddlecolor = false;
  
  // Pressure
  var showbaro = true;
  var barogaugeMax = 1050;
	var barogaugeMin = 960;
	var baronumbersoffset = 13;
	var	barouom = 'hPa';  // hpa, in, mb supported
	var barodots = 20;
	var barodotsoffset = 13;
  var barostartcolor = '190,190,190';
  var barostopcolor = '255,255,100';
  var baromiddlecolor = false;
  
  // Solar
  var showsolar = true;
  var solargaugeMax = 1000;
	var solargaugeMin = 0;
	var solarnumbersoffset = 13;
	var	solaruom = 'w/m&sup2;';
	var solardots = 200;
	var solardotsoffset = 13;
  var solarstartcolor = '221,202,165';
  var solarstopcolor = '255,165,0';
  var solarmiddlecolor = false;
  
  // UV
  var showuv = true;
  var uvgaugeMax = 10;
	var uvgaugeMin = 0;
	var uvnumbersoffset = 13;
	var	uvuom = 'uvi';
	var uvdots = 1;
	var uvdotsoffset = 13;
  var uvstartcolor = '0,205,0';
  var uvstopcolor = '255,0,0';
  var uvmiddlecolor = '255,255,100';
  
  // Gustwind
  var showwind = false;
  var windnumbersoffset = 6;
  var windpuom = 'm/s';  // m/s, kts, mph and kmh supported
  var winddotsoffset = 14;
  var winddirs = 'N,NE,E,SE,S,SW,W,NW';
  
  // Avg wind
  var showavgwind = true;
  var avgwindnumbersoffset = 6;
  var avgwindpuom = 'kmh';  // m/s, kts, mph and kmh supported
  var avgwinddotsoffset = 14;
  var avgwinddirs = 'N,NE,E,SE,S,SW,W,NW';
  
  // Daily rain
  var showrain = true;
  var raingaugeheight = 180;
  var rainuom = 'mm';  // mm & in supported
  
  // Snow
  var showsnow = false;
  var snowgaugeheight = 180;
  var snowuom = 'cm';
  var snowvalue = rawsnow;
  
// Few global settings also ;)
 var options = {
				gradient: { 
				colors:["#fafafa","#ddd","#ccc","#bbb"]
				},
				notusebgimage: false
	};
	
	// Block of used divs, comment out if not needed <-- IMPORTANT
	if(showhum) { var hum = $("#hum"); }
	if(showbaro) { var baro = $("#baro"); }
	if(showtemp) { var temp = $("#temp"); }
	if(showdew) { var dew = $("#dew"); }
	if(showchill) { var chill = $("#chill"); }
	if(showwind) { var wind = $("#wind"); }
	if(showsolar) { var solar = $("#solar"); }
	if(showuv) { var uv = $("#uv"); }
	if(showavgwind) { var avgwind = $("#avgwind"); }
	if(showrain) { var rain = $("#rain"); }
	if(showsnow) { var snow = $("#snow"); }
	var clock = $("#clock"); 
	 
	// END OF BASIC SETTINGS
	//***************************************
	// To change a value in a gauge, you need to go below to approx line 180 and
	// just change the clientraw-value for that gauge
	// To create a new gauge, just copy settings for one gauge,rename and tweak it. ;)
	//***************************************
	 
	// Dummy data as there are instanses what ask for it ;)
	// DON'T TOUCH!!!
    var data = ''; 
	 var alreadyFetched = {};
   var cr = '';
   var iteration = 0;
   var crcache = {};
   update();
   
   
   function trend_image(trend) {
	   if (trend == '1') {
		   return image_dir + 'rising.gif';
	   }  else if (trend == '-1') {
		   return image_dir + 'falling.gif';
	   }
	   return '';
   };
   

	   function wind_speed(value, uom) {
		if (uom == 'm/s') {
			return (value * 0.514444).toFixed(1);
		} else if (uom == 'kmh') {
			return (value * 1.85200).toFixed(1);
		} else if (uom == 'mph') {
			return (value * 1.1507794).toFixed(1);
		}
	};
   
   function update() {
   
            // Reset data
            alreadyFetched = {};
            
          function onDataReceived(series) {
            iteration++;
            
            //if (iteration == updates) {
            //alert(pausemsg);
            //return;
            //}
            
            // Disable some things as default
            options.snowvalue = false;
            options.windgauge = false;
            options.winddirs = false;
            options.raingauge = false;
            
          // split clientraw
          var cr = series.split(' ');
          
          // We need some some values from clientraw
          var crsolar = (cr[127] * 1).toFixed(0);
          var cruv = cr[79];
          var crhum = cr[5];
          var crbaro = cr[6];
          var crtemp = cr[4];
          var crchill = cr[44];
          var crhumid = cr[45];
          var crbarorate = cr[50];
          var crdew = cr[72];
          var crwspd = cr[1];
          var crwdir = cr[3];
          var cravg = cr[158];
          var cradir = cr[117];
          var crhumtrend = cr[144];
          var crrain = cr[7];
          var crrainrate = cr[10];
          
          
          // Example how to do a "normal" ajax-thing
          $("#dewpoint").html(crdew + dewuom);
          $("#feelslike").html(crchill + chilluom);
          $("#baro_rate").html(crbarorate + barouom + phuom);
          var trend_img = trend_image(crhumtrend);
          var trend = $("#hum_trend");
          trend.attr('src', trend_img);
          if (trend_img == "") {
        	  trend.hide();
          } else {     	 
        	  trend.show();
          }
          $("#rain_rate").html(crrainrate + ' '+ rainuom + pmuom);
          $("#wind_gust").html(wind_speed(crwspd, windpuom) + windpuom)
          
            // Plot gauges
            // Solar
            if((showsolar) && (crsolar != options.solarcache)) { 
            options.gaugeMax = solargaugeMax;
            options.gaugeMin = solargaugeMin;
            options.numbersoffset = solarnumbersoffset;
            options.uom = solaruom;
            options.dots = solardots;
            options.dotsoffset = solardotsoffset;
            options.crvalue = crsolar;
            options.solarcache = crsolar;
            options.startcolor = solarstartcolor;
            options.stopcolor = solarstopcolor;
            options.middlecolor = solarmiddlecolor;
            $.ga(solar, data, options); }
            
            // UV
            if((showuv) && (cruv != options.uvcache)) { 
            options.gaugeMax = uvgaugeMax;
            options.gaugeMin = uvgaugeMin;
            options.numbersoffset = uvnumbersoffset;
            options.uom = uvuom;
            options.dots = uvdots;
            options.dotsoffset = uvdotsoffset;
            options.crvalue = cruv;
            options.uvcache = cruv;
            options.startcolor = uvstartcolor;
            options.stopcolor = uvstopcolor;
            options.middlecolor = uvmiddlecolor;
            $.ga(uv, data, options); }
            
            // Humidity
            if((showhum) && (crhum != options.humcache)) {
            options.gaugeMax = humgaugeMax;
            options.gaugeMin = humgaugeMin;
            options.numbersoffset = humnumbersoffset;
            options.uom = humuom;
            options.dots = humdots;
            options.dotsoffset = humdotsoffset;
            options.crvalue = crhum;
            options.humcache = crhum;
            options.startcolor = humstartcolor;
            options.stopcolor = humstopcolor;
            options.middlecolor = hummiddlecolor;
            $.ga(hum, data, options); }
            
            // Pressure
            if((showbaro) && (crbaro != options.barocache)) {
            options.gaugeMax = barogaugeMax;
            options.gaugeMin = barogaugeMin;
            options.numbersoffset = baronumbersoffset;
            options.uom = barouom;
            options.dots = barodots;
            options.dotsoffset = barodotsoffset;
            options.crvalue = crbaro;
            options.barocache = crbaro;
            options.startcolor = barostartcolor;
            options.stopcolor = barostopcolor;
            options.middlecolor = baromiddlecolor;
            $.ga(baro, data, options); }
            
            // Temperature
            
            if((showtemp) && (crtemp != options.tempcache)) {
            options.gaugeMax = tempgaugeMax;
            options.gaugeMin = tempgaugeMin;
            options.numbersoffset = tempnumbersoffset;
            options.uom = tempuom;
            options.dots = tempdots;
            options.dotsoffset = tempdotsoffset;
            options.crvalue = crtemp;
            options.tempcache = crtemp;
            options.startcolor = tempstartcolor;
            options.stopcolor = tempstopcolor;
            options.middlecolor = tempmiddlecolor;
            $.ga(temp, data, options); }
            
            // Windchill/humidx
            if((showchill) && (crchill != options.chillcache)) {
            options.gaugeMax = chillgaugeMax;
            options.gaugeMin = chillgaugeMin;
            options.numbersoffset = chillnumbersoffset;
            options.uom = chilluom;
            options.dots = chilldots;
            options.dotsoffset = chilldotsoffset;
            if(crtemp < 10) {
            options.crvalue = crchill;
            } else {
            options.crvalue = crhumid;
            }
            options.chillcache = options.crvalue;
            options.startcolor = chillstartcolor;
            options.stopcolor = chillstopcolor;
            options.middlecolor = chillmiddlecolor;
            $.ga(chill, data, options); }
            
            // Dewpoint
            if((showdew) && (crdew != options.dewcache)) {
            options.gaugeMax = dewgaugeMax;
            options.gaugeMin = dewgaugeMin;
            options.numbersoffset = dewnumbersoffset;
            options.uom = dewuom;
            options.dots = dewdots;
            options.dotsoffset = dewdotsoffset;
            options.crvalue = crdew;
            options.dewcache = crdew;
            options.startcolor = dewstartcolor;
            options.stopcolor = dewstopcolor;
            options.middlecolor = dewmiddlecolor;
            $.ga(dew, data, options); }
            var wchange = false;
            
            // Gustwind
            if(crwspd != options.wscache) { wchange = true; }
            if(crwdir != options.dircache) { wchange = true; }
            if(showwind && wchange) {
            options.numbersoffset = windnumbersoffset;
            options.uom = windpuom;
            options.dotsoffset = winddotsoffset;
            options.crvalue = crwspd;
            options.wscache = crwspd;
            options.dircrvalue = crwdir;
            options.dircache = crwdir;
            options.windgauge = true;
            options.winddirs = winddirs;
            $.ga(wind, data, options); } 
            var achange = false;
            
            // Avg wind
            if(cravg != options.avgcache) { achange = true; }
            if(cradir != options.adircache) { achange = true; }
            if(showavgwind && achange) {
            options.numbersoffset = avgwindnumbersoffset;
            options.uom = avgwindpuom;
            options.dotsoffset = avgwinddotsoffset;
            options.crvalue = cravg;
            options.avgcache = cravg;
            options.dircrvalue = cradir;
            options.adircache = cradir;
            options.windgauge = true;
            options.winddirs = avgwinddirs;
            $.ga(avgwind, data, options); }
            
            // Rain
            if((showrain) && (crrain != options.raincache)) {
            options.raingauge = true;
            options.raingaugeheight = raingaugeheight;
            options.crvalue = crrain;
            options.raincache = crrain;
            options.uom = rainuom;
            $.ga(rain, data, options); }
            
            // Snow
            if((showsnow) && (rawsnow != options.snowcache)) {
            options.raingauge = true;
            options.raingaugeheight = snowgaugeheight;
            options.uom = snowuom;
            options.snowvalue = snowvalue;
            options.snowcache = snowvalue;
            $.ga(snow, data, options); }
         
          cr = '';
          cr = false;
          //alert(cr);
                  
          }      
            
          $.ajax({
            url: dataurl,
            method: 'GET',
            dataType: 'text',
            cache: false,
            success: onDataReceived,
            error: function(msg){
            alert(errormsg);
            }
            
          });
            
            if (iteration < updates) {
            setTimeout(update, timeout*1000);
            }
            
           
   };

});
