function dashboardToggleLock ()

{
	if ($("#dashboardLockButton").attr("class") == "locked")
		$("#dashboardLockButton").attr("class", "");
	else
		$("#dashboardLockButton").attr("class", "locked");
}

function updateButton (type, timeSpan)

{
	if (timeSpan == "day") {
		$("#"+type+"DayButton").attr ("state", "active");
		$("#"+type+"MonthButton").attr ("state", "");
		$("#"+type+"YearButton").attr ("state", "");
		$("#"+type+"TotalButton").attr ("state", "");
	}
	if (timeSpan == "month") {
		$("#"+type+"DayButton").attr ("state", "");
		$("#"+type+"MonthButton").attr ("state", "active");
		$("#"+type+"YearButton").attr ("state", "");
		$("#"+type+"TotalButton").attr ("state", "");
	}
	if (timeSpan == "year") {
		$("#"+type+"DayButton").attr ("state", "");
		$("#"+type+"MonthButton").attr ("state", "");
		$("#"+type+"YearButton").attr ("state", "active");
		$("#"+type+"TotalButton").attr ("state", "");
	}
	if (timeSpan == "total") {
		$("#"+type+"DayButton").attr ("state", "");
		$("#"+type+"MonthButton").attr ("state", "");
		$("#"+type+"YearButton").attr ("state", "");
		$("#"+type+"TotalButton").attr ("state", "active");
	}
}

function updateData (type, timeSpan, pageName, callDomain, macAddr)

{
	macAddr = typeof(macAddr) != 'undefined' ? macAddr : "00127B40C823";

	units = "kwh";
	
	if (type == "kwh") {
		if ($("#btuButton").attr("state") == "active")
			units = "btu";
	}
		
	if (callDomain == "") {
		callurl = "mysolar_dashboard_update.php";
	} else {
		callurl = "dashboard_proxy.php?url=" + escape('http://www.wiredsolar.net/mysolar_dashboard_update.php?mac=' + macAddr);
	}
	
	$.ajax({
	   type: "POST",
	   url: callurl,
	   data: { spanType: type, spanTime: timeSpan, pageName: pageName, graphUnits: units },
	   success: function(data)
			{ 
				items = data.split ("~");
				for (i=0; i<items.length; i++) {
					idAndValue = items[i].split ("`");
					divId = idAndValue[0];
					divContent = idAndValue[1];
				
					var visits = divContent;
					if (divId == "dashboardPanelGraph") {
						var visits = eval('(' + divContent + ')');
						var dispUnits;
						
						if (units == "btu")
							dispUnits = "Btu";
						else
							dispUnits = "Kwh";
							
						tickInterval = 1;
						
						switch (timeSpan) {
							case "day":
								timeFormat = "%h%p";
								minTick = "hour";
								tickInterval = 2;
								break;
							case "month":
								timeFormat = "%d";
								minTick = "day";
								tickInterval = 2;
								break;
							case "year":
								timeFormat = "%b";
								minTick = "month";
								break;
							default:
								timeFormat = "%b";
								minTick = "year";
								break;
						}
													
						$.plot($("#flot"), [ { label: dispUnits, data: visits } ], 
										{  grid: { 
											autoHighlight: true,
											hoverable: true,
											clickable: true 
											},
											xaxis: {
											mode: 'time',
											timeformat: timeFormat,
											tickSize: [tickInterval, minTick]
										  }  
										});
										
						var previousPoint = null;
						$("#flot").bind("plothover", function (event, pos, item) {
							$("#x").text(pos.x.toFixed(2));
							$("#y").text(pos.y.toFixed(2));
							
						   if (item) {
						       if (previousPoint != item.datapoint) {
						           previousPoint = item.datapoint;
						           $("#tooltip").remove();
						           var y = item.datapoint[1].toFixed(1);
						           showTooltip(item.pageX, item.pageY, y);
						       }
						   }
						   else {
						       $("#tooltip").remove();
						       previousPoint = null;            
						   }
						 });		
						$("#flot").bind("plotclick", function (event, pos, item) {
							$("#x").text(pos.x.toFixed(2));
							$("#y").text(pos.y.toFixed(2));
							
						   if (item) {
						       if (previousPoint != item.datapoint) {
						           previousPoint = item.datapoint;
						           $("#tooltip").remove();
						           var y = item.datapoint[1].toFixed(1);
						           showTooltip(item.pageX, item.pageY, y);
						       }
						   }
						   else {
						       $("#tooltip").remove();
						       previousPoint = null;            
						   }
						 });		
										
						 // show the tooltip
					    function showTooltip(x, y, contents) {
					        $('<div id="tooltip">' + contents + '</div>').css( {
					            position: 'absolute',
					            display: 'none',
					            top: y - 35,
					            left: x + 5,
					            border: '1px solid #fdd',
					            padding: '2px',
					            'background-color': '#fee',
					            opacity: 0.80
					        }).appendTo("body").fadeIn(200);
					    }
										
					} else {
						$("#"+divId).html (divContent);
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) 
			{
			}
		});
}

function dashboardUpdateTimeSpan (type, timeSpan, pageName, callDomain, macAddr)

{
	macAddr = typeof(macAddr) != 'undefined' ? macAddr : "00127B40C823";
	
	updateButton (type, timeSpan);	
	updateData (type, timeSpan, pageName, callDomain, macAddr);	

	if ($("#dashboardLockButton").attr("class") == "locked") {
		updateButton ("kwh", timeSpan);	
		updateButton ("co2", timeSpan);	
		updateButton ("trees", timeSpan);	
		updateButton ("saved", timeSpan);	
		updateButton ("bulbs", timeSpan);	
		updateButton ("miles", timeSpan);	
		updateButton ("homes", timeSpan);	
	
		updateData ("kwh", timeSpan, pageName, callDomain, macAddr);	
		updateData ("co2", timeSpan, pageName, callDomain, macAddr);	
		updateData ("trees", timeSpan, pageName, callDomain, macAddr);	
		updateData ("saved", timeSpan, pageName, callDomain, macAddr);	
		updateData ("bulbs", timeSpan, pageName, callDomain, macAddr);	
		updateData ("miles", timeSpan, pageName, callDomain, macAddr);	
		updateData ("homes", timeSpan, pageName, callDomain);	
	}
}

function dashboardUpdateGraphType (type, pageName, callDomain, macAddr)

{
	macAddr = typeof(macAddr) != 'undefined' ? macAddr : "00127B40C823";
	
	if (type == "btu") {
		$("#btuButton").attr ("state", "active");
		$("#kwhButton").attr ("state", "");
		$("#dashboardPanelImage").html ("<img src='http://www.wiredsolar.net/images/dashboard/btuflame.png'>");
		
	}
	if (type == "kwh") {
		$("#btuButton").attr ("state", "");
		$("#kwhButton").attr ("state", "active");
		$("#dashboardPanelImage").html ("<img src='http://www.wiredsolar.net/images/dashboard/powermeter.png'>");
	}
	
	var timeSpan = "";
	
	if ($("#kwhDayButton").attr("state") == "active")
		timeSpan = "day";
	if ($("#kwhMonthButton").attr("state") == "active")
		timeSpan = "month";
	if ($("#kwhYearButton").attr("state") == "active")
		timeSpan = "year";
	if ($("#kwhTotalButton").attr("state") == "active")
		timeSpan = "total";
	
	updateData ("kwh", timeSpan, pageName, callDomain, macAddr);	
}
