// Copyright (c) 2007 Legendum LLC
// This code may be used for free,
// provided this notice is included.
function Widget(element)
{
this.element = element;
this.months = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
this.name = '';
this.base = 'http://www.net-filter.com/';
this.link = null;
this.open = null;
this.date = null;
// Show the widget loading
this.loading = function()
{
this.display('');
}
// Display the widget
this.display = function(body)
{
var link = '';
if (this.link) link = ' ';
var open = '';
if (this.open) open = ' ';
var html = '
';
this.element.innerHTML = html;
}
// Get the forecast from an hour of day report
this.getForecast = function(report, secs)
{
var hours = [];
for (var i = 0; i < report.data.length; i++)
{
var hour = parseInt(report.data[i].field);
var hits = parseInt(report.data[i].value);
if (hour < 0 || hour > 23) continue;
hours[hour] = hits;
}
var total = 0;
var today = 0;
var hour = secs / 3600;
for (var h = 0; h < 24; h++)
{
total += hours[h];
if (h < Math.floor(hour)) today += hours[h];
else if (h == Math.floor(hour)) today += hours[h] * (hour-Math.floor(hour));
}
return total > 0 ? today / total : 0;
}
// Get arrow image HTML code to show the percentage change
this.getArrow = function(value_now, value_then, forecast)
{
if (!forecast) forecast = 1.0;
var percent = 0;
var value_forecast = Math.round(value_then * forecast);
if (value_forecast)
percent = parseInt((value_now/value_forecast - 1.0) * 100);
return '';
}
// Set the name
this.setName = function(name)
{
this.name = name;
}
// Get the title
this.getTitle = function()
{
var title = this.name;
title += this.date ? ' on ' + this.date : ' loading...';
return title;
}
// Set the base URL (must end in a slash)
this.setBase = function(base)
{
this.base = base;
}
// Get the base URL
this.getBase = function()
{
return this.base;
}
// Set the link-for-info URL
this.setLink = function(link)
{
this.link = link;
}
// Set the open-window URL
this.setOpen = function(open)
{
this.open = open;
}
// Format a YYYYMMDD date as "Mon DD, YYYY"
this.formatDate = function(date)
{
date = new String(date);
var year = parseInt(date.substr(0, 4));
var month = parseInt(date.substr(4, 2), 10);
var day = parseInt(date.substr(6, 2), 10);
return this.months[month] + ' ' + day + ', ' + year;
}
// Set the date as a formatted string
this.setDate = function(date)
{
this.date = this.formatDate(date);
}
};