var baroText;

var ie = (document.all) ? true : false;



function hideClass(objClass){
//  This function will hide Elements by object Class
//  Works with IE and Mozilla based browsers

var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="none"
    }
  }
}

function showClass(objClass){
//  This function will show Elements by object Class
//  Works with IE and Mozilla based browsers
var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="block"
    }
  }
}
function activeLayer( whichLayer )
{
  
  if (whichLayer == 'home') {
  offLayer('records');
  
  onLayer('home');
  }
  else if (whichLayer == 'records') {
  onLayer('records');
  
  offLayer('home');
  }
   
}
function onLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  vis.display = 'block';
}
function offLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  vis.display = 'none';
}


function updateValues() {
//		document.getElementById('station_name').innerHTML=stationName;
		document.getElementById('time').innerHTML=time;
		document.getElementById('current').innerHTML=time + "<BR>" + actualOutTemp + "&#176;C / " + actualOutHumidity + " % / " + actualBaro + " hPa"; 
//		document.getElementById('out_temp').innerHTML=actualOutTemp + "&#176;C";
//		document.getElementById('wind').innerHTML=actualWind;
//		document.getElementById('out_temp_daily_min').innerHTML=dailyMinOutTemp + "&#176;C";
//		document.getElementById('out_temp_daily_max').innerHTML=dailyMaxOutTemp + "&#176;C";
//		document.getElementById('baro').innerHTML=actualBaro;
//		document.getElementById('out_humidity').innerHTML=actualOutHumidity;
		document.getElementById('sunrise').innerHTML=sunrise;
		document.getElementById('sunset').innerHTML=sunset;
		document.getElementById('dawn').innerHTML=dawn;
		document.getElementById('dusk').innerHTML=dusk;
		document.getElementById('ytdMaxOutTemp').innerHTML=ytdMaxOutTemp;
		document.getElementById('ytdMaxOutTempTimeStamp').innerHTML=ytdMaxOutTempTimeStamp;
		document.getElementById('ytdMinOutTemp').innerHTML=ytdMinOutTemp;
		document.getElementById('ytdMinOutTempTimeStamp').innerHTML=ytdMinOutTempTimeStamp;
		document.getElementById('atMaxOutTemp').innerHTML=atMaxOutTemp;
		document.getElementById('atMaxOutTempTimeStamp').innerHTML=atMaxOutTempTimeStamp;
		document.getElementById('atMinOutTemp').innerHTML=atMinOutTemp;
		document.getElementById('atMinOutTempTimeStamp').innerHTML=atMinOutTempTimeStamp;
		document.getElementById('mMaxOutTemp').innerHTML=mMaxOutTemp;
		document.getElementById('mMaxOutTempTimeStamp').innerHTML=mMaxOutTempTimeStamp;
		document.getElementById('mMinOutTemp').innerHTML=mMinOutTemp;
		document.getElementById('mMinOutTempTimeStamp').innerHTML=mMinOutTempTimeStamp;
//		document.getElementById('solarcurrent').innerHTML=solarWatts;







}



function drawGraph(graphType,dataSeries,yaxismin,yaxismax,xTickSize,yTickSize,graphName,popupText)
{
//reset arrays
d1 = [];
data=[];

// get Data series for graph
    	for (var counter = 0; counter < dataSeries.length; counter++)
	{
		d1.push( [ dataSeries[counter][0], dataSeries[counter][1] ]);
        	if(dataSeries[counter][1]!='-')
          	{
          	// Set min/max for graph, 
			yaxismin = Math.min(5*Math.floor(dataSeries[counter][1]/5),yaxismin);
			yaxismax = Math.max(5*Math.ceil(dataSeries[counter][1]/5),yaxismax);
		} 
	}

  

data = [{ data:d1,  color: "#993333" }];

$.plot($(graphName), data,{
	
    
    lines:  {      
      lineWidth: 2, color: "#993333"
      },
	  yaxis : {
	  minTickSize : yTickSize , min : yaxismin, max: yaxismax, labelWidth : 12
	  },

	  xaxis : { mode : "time"

	  },

	  grid : {
	  backgroundColor : "#FFFFFF",hoverable:true,autoHighlight:false,clickable:true
	  }
	  
  }
	
)
if(popupText != '')
{
$(graphName).bind("plothover", function(event,pos,item) { 
$("#tooltip").remove();

showTooltip(pos.pageX,pos.pageY,popupText);


 });

$(graphName).bind("mouseout", function(event,pos,item) { 
$("#tooltip").remove();
})
}

}
function drawBarGraph(graphType,dataSeries,yaxismin,yaxismax,xTickSize,yTickSize,graphName,popupText)
{
//reset arrays
d1 = [];
data=[];

// get Data series for graph
    	for (var counter = 0; counter < dataSeries.length; counter++)
	{
		d1.push( [ dataSeries[counter][0], dataSeries[counter][1] ]);
        	if(dataSeries[counter][1]!='-')
          	{
          	// Set min/max for graph, 
			yaxismin = Math.min(5*Math.floor(dataSeries[counter][1]/5),yaxismin);
			yaxismax = Math.max(5*Math.ceil(dataSeries[counter][1]/5),yaxismax);
		} 
	}
 

data = [{ data:d1,  color: "#993333" }];

$.plot($(graphName),
	data,
	{
	
    	bars:  {  show:true, align:"center", barWidth : 57600000  },
	xaxis : {mode:'time', autoscaleMargin: 0.01, ticks:[]  },
	yaxis : { tickSize : yTickSize },
	legend : {show:true} 
	}
)

if(popupText != '')
{
$(graphName).bind("plothover", function(event,pos,item) { 
$("#tooltip").remove();

showTooltip(pos.pageX,pos.pageY,popupText);


 });

$(graphName).bind("mouseout", function(event,pos,item) { 
$("#tooltip").remove();
})
}

}
function drawBarGraphNotTime(graphType,dataSeries,yaxismin,yaxismax,xTickSize,yTickSize,graphName,popupText)
{
//reset arrays
d1 = [];
data=[];

// get Data series for graph
    	for (var counter = 0; counter < dataSeries.length; counter++)
	{
		d1.push( [ dataSeries[counter][0], dataSeries[counter][1] ]);
        	if(dataSeries[counter][1]!='-')
          	{
          	// Set min/max for graph, 
			yaxismin = Math.min(5*Math.floor(dataSeries[counter][1]/5),yaxismin);
			yaxismax = Math.max(5*Math.ceil(dataSeries[counter][1]/5),yaxismax);
		} 
	}
 

data = [{ data:d1,  color: "#993333" }];

$.plot($(graphName),
	data,
	{
	
    	bars:  {  show:true, align:"center",barWidth:0.85 },
	xaxis : { autoscaleMargin: 0.01,ticks : [ [1,"J"],[2,"F"] ,[3,"M"],[4,"A"],[5,"M"],[6,"J"],[7,"J"],[8,"A"],[9,"S"],[10,"O"],[11,"N"],[12,"D"] ] , tickLength: 0},
	yaxis : { tickSize : yTickSize },
	legend : {show:true} 
	}
)

if(popupText != '')
{
$(graphName).bind("plothover", function(event,pos,item) { 
$("#tooltip").remove();

showTooltip(pos.pageX,pos.pageY,popupText);


 });

$(graphName).bind("mouseout", function(event,pos,item) { 
$("#tooltip").remove();
})
}

}

function drawDoubleGraph(graphType,dataSeries,yaxismin,yaxismax,dataSeries2, yaxismin2,yaxismax2,xTickSize,yTickSize,yTickSize2,xaxismin,xaxismax,graphName,popupText)
{

//reset arrays
d2 = [];
d1 = [];
data=[];
data2 = [];

// get Data series for graph
    	for (var counter = 0; counter < dataSeries.length; counter++)
	{
		d1.push( [ dataSeries[counter][0], dataSeries[counter][1] ]);
        	if(dataSeries[counter][1]!='-')
          	{
          	// Set min/max for graph, 
			yaxismin = Math.min(5*Math.floor(dataSeries[counter][1]/5),yaxismin);
			yaxismax = Math.max(5*Math.ceil(dataSeries[counter][1]/5),yaxismax);
		} 
		}
		for (var counter = 0; counter < dataSeries2.length; counter++)
	{
	
		d2.push( [ dataSeries2[counter][0], dataSeries2[counter][1] ]);
        	if(dataSeries2[counter][1]!='-')
          	{
          	// Set min/max for graph, 
			yaxismin2 = Math.min(5*Math.floor(dataSeries2[counter][1]/5),yaxismin2);
			yaxismax2 = Math.max(5*Math.ceil(dataSeries2[counter][1]/5),yaxismax2);
		} 
	}
  

//data = [{ data:d1,  color: "#993333" }];
//data2 = [{ data:d2,  color: "#3333BB", yaxis:2}];

$.plot($(graphName),
[
 { data:d1,  color: "#993333"},
 { data:d2,  color: "#3333BB", yaxis:2}],
	
    {
    
	  yaxis : {
	  minTickSize : yTickSize , min : yaxismin, max: yaxismax, labelWidth : 12
	  },
	  y2axis : { min :yaxismin2,max: yaxismax2
	  },

	  xaxis : {mode:'time' 

	  },

	  grid : {
	  backgroundColor : "#FFFFFF",hoverable:true,autoHighlight:false,clickable:true
	  }
	  
  }
	
)
if(popupText != '')
{
$(graphName).bind("plothover", function(event,pos,item) { 
$("#tooltip").remove();

showTooltip(pos.pageX,pos.pageY,popupText);


 });

$(graphName).bind("mouseout", function(event,pos,item) { 
$("#tooltip").remove();
})
}

}

function startDataUpdate() {

dataCheck();
if ( dataReady==1 ) {
	updateValues();
	drawThermo(dailyMaxOutTemp,dailyMinOutTemp,actualOutTemp,'#thermo');
	drawWind(actualWind,hourMaxWindGust,0,50,5,25,1,'Wind Speed<br>'+actualWind+' mph','#windGauge');
	//drawBaro(actualBaro,dayBaro[0][1],950,1050,10,20,1,'Pressure<br>'+actualBaro+' hPa','Pressure ' + baroText,'#baro');
	drawBaro(solarPercent,solarMaxPercent,0,100,10,20,1,'Solar Power<br>'+solarPercent+' % ', solarWatts + ' W Current<br>'+ solarMax + 'W Max<br>' + solarToday +  ' Wh Generated Today' ,'#solar');
//	drawGraph('24 Hour Temp',dayOutTemp,0,5,24,0.5,"#_24hourTemp",'Click Title to Change Timescale');
//	drawGraph('7 Day Temp',weekOutTemp,0,5,24,0.5,"#_7dayTemp",'Click Title to Change Timescale');
//	drawGraph('24 Hour Wind',dayOutWind,0,5,24,0.5,"#_24hourWind",'Click Title to Change Timescale');
//	drawGraph('7 Day Wind',weekOutWind,0,5,24,0.5,"#_7dayWind",'Click Title to Change Timescale');
	drawBarGraph('Solar History',solarDailyHistory,0,5,24,2,"#solarDaily",'Solar Generation History');
drawBarGraphNotTime('Solar Averages',solarMonthlyAverages,0,5,24,1,"#solarMonthlyAverage",'Daily Average Per Month');	
	drawDoubleGraph('Solar Today',solarTodayWatts,0,1700,solarTodayTotal,0,solarToday,24,1,1,solarSunriseTime,solarSunsetTime,"#_solarToday",'Today Solar');
//drawGraph('Solar Today',solarTodayTotal,0,0.01,24,0.5,0.5,solarSunriseTime,solarSunsetTime,"#_solarToday",'Today Solar');
	setTimeout('getWXData()',refreshTimeout); 
	dataReady=0;
	}
	else
	{
	 setTimeout('startDataUpdate()',200);
	}
}
