////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  Copyright (c) 2006 Michel Borgman.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
var gadgetWidth;
var gadgetHeight;
var isVisible = true;
var gadgetTimeout;
var wireless = System.Network.Wireless;

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

window.onload = function()
{
	// add event handlers
	System.Gadget.visibilityChanged = checkVisibility;
	System.Gadget.onUndock = checkPosition;
	System.Gadget.onDock = checkPosition;
	System.Network.Wireless.connectionChanged = updateGadget;
	System.Network.Wireless.signalStrengthChanged = updateGadget;

	// initialize gadget
	updateGadget();
}

////////////////////////////////////////////////////////////////////////////////
// Signal object
////////////////////////////////////////////////////////////////////////////////
function Signal()
{
	this.address = wireless.address;
	this.ipv6Address = wireless.ipv6Address;
	this.primaryDNSAddress = wireless.primaryDNSAddress;
	this.secureConnection = wireless.secureConnection;
	this.signalStrength = wireless.signalStrength;
	this.ssid = wireless.ssid;
}

////////////////////////////////////////////////////////////////////////////////
// update gadget
////////////////////////////////////////////////////////////////////////////////
function updateGadget()
{
	var signal = new Signal();

	wirelessImage.src =  signal.secureConnection ? "images/" + signal.signalStrength + "s.png" : "images/" + signal.signalStrength + ".png";

	wirelessQuality.innerText = signal.signalStrength + "%";
	qualityLabel.innerText = (signal.signalStrength == 0 ) ?
	        "" : signal.secureConnection ? "Secure" : "Unsecure";

//	wirelessStatus.style.display = signal.secureConnection ? "block" : "none";

	wirelessConnected.innerText = (signal.signalStrength > 0 && signal.address != "") ?
		"Network:" : "Disconnected";

	connectedLabel.innerText = signal.ssid; // + "(" + signal.address + ")";

	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);
	}
}
