////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  http://se7enth.com
//  Copyright (c) 2006 Seventh Sense Studio.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
var gadgetWidth;
var gadgetHeight;
var isVisible = true;
var gadgetTimeout;

////////////////////////////////////////////////////////////////////////////////
// GADGET FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

window.onload = function()
{
	// add event handlers
	System.Gadget.visibilityChanged = checkVisibility;
	System.Gadget.onUndock = checkPosition;
	System.Gadget.onDock = checkPosition;
	System.Machine.PowerStatus.powerLineStatusChanged = updateGadget;

	// initialize gadget
	updateGadget();
}

////////////////////////////////////////////////////////////////////////////////
// Battery object
////////////////////////////////////////////////////////////////////////////////
function Battery()
{
	var power = System.Machine.PowerStatus;
	this.batteryPercentRemaining = power.batteryPercentRemaining;
	this.isBatteryCharging = power.isBatteryCharging;
	this.isPowerLineConnected = power.isPowerLineConnected;
	// TODO: add batteryCapacityRemaining, batteryCapacityTotal, batteryStatus
	// (these properties either aren't implemented yet, or are not supported by my system :( )
	// DEVELOPERS: if you know that these properties work for you, contact the gadget author
}

////////////////////////////////////////////////////////////////////////////////
// update gadget
////////////////////////////////////////////////////////////////////////////////
function updateGadget()
{
	var battery = new Battery();

	batteryImage.src = battery.isPowerLineConnected ? "images/BatteryPlugged.png" : "images/BatteryUnplugged.png";

	percentRemaining.innerText = battery.batteryPercentRemaining + "%";
	remainingLabel.innerText = battery.isPowerLineConnected ? "available" : "remaining";

	chargingStatus.style.display = battery.isPowerLineConnected ? "block" : "none";

	powerLineConnected.innerText = (battery.batteryPercentRemaining == 100 && !battery.isBatteryCharging) ?
		"Fully charged" : "plugged in";

	charging.innerText = battery.isBatteryCharging ? "charging" :
		((battery.batteryPercentRemaining < 100) ? "not charging" : "");

	gadgetTimeout = setTimeout(updateGadget, 1000);
}

////////////////////////////////////////////////////////////////////////////////
// styles for gadget when UNDOCKED
////////////////////////////////////////////////////////////////////////////////
function applyUndockedStyles()
{
	gadgetWidth = "130px";
	gadgetHeight = "67px";

	// TODO: add undocked view in future version
}

////////////////////////////////////////////////////////////////////////////////
// styles for gadget when DOCKED
////////////////////////////////////////////////////////////////////////////////
function applyDockedStyles()
{
	gadgetWidth = "130px";
	gadgetHeight = "67px";

	background.src = "images/Docked.png";
}

////////////////////////////////////////////////////////////////////////////////
// determine if gadget is in sidebar - docked or on the desktop - undocked
////////////////////////////////////////////////////////////////////////////////
function checkPosition()
{
	if (System.Gadget.docked)
	{
		applyDockedStyles();
	}
	else
	{
		applyUndockedStyles();
	}

	with (document.body.style)
	{
		width = gadgetWidth;
		height = gadgetHeight;
	}

	background.style.width = gadgetWidth;
	background.style.height = gadgetHeight;
}

////////////////////////////////////////////////////////////////////////////////
// determine if gadget is visible
////////////////////////////////////////////////////////////////////////////////
function checkVisibility()
{
	isVisible = System.Gadget.visible;
	clearTimeout(gadgetTimeout);

	if (isVisible)
	{
		gadgetTimeout = setTimeout(updateGadget, 1000);
	}
}
