var location="16801";
var xmlData=null;
var units="english";

function init() {
	System.Gadget.settingsUI="Settings.html";
	System.Gadget.onSettingsClosed=settingsClosed;
	if (System.Gadget.Settings.read("Location")!='') {
		location=System.Gadget.Settings.read("Location");
	} else {
		System.Gadget.Settings.write("Location", location);
	}
	if (System.Gadget.Settings.read("units")!='') {
		units=System.Gadget.Settings.read("units");
	} else {
		System.Gadget.Settings.write("units", units);
	}
	initLayout();
	if (location!="") {
		retrieveWeather();
	}	
}

function retrieveWeather() {
	loadData(units);
	if (DataComplete) {
		temps.innerHTML=CurrentForecasts.forecastArray[0].daytimeHigh + "&deg; / " + CurrentForecasts.forecastArray[0].nighttimeLow + "&deg;";		
		tempcur.innerHTML=CurrentConditions.temperature + "&deg;";
		symbolImage.src="images/icons/lg/" + CurrentConditions.icon + ".png";
		symbolImage.alt=CurrentConditions.text;		
		if (CurrentLocation.city.length>20) {
			city.innerText=CurrentLocation.city.substr(0,19) + "...";
		} else {
			city.innerText=CurrentLocation.city;
		}
		if (CurrentWatches.isactive==1) {
			showWarning();
		} else {
			hideWarning();
		}
	} else {
		city.innerText="No data returned";
		temps.innerHTML="NA &deg; / NA &deg;";		
		tempcur.innerHTML="NA &deg;";
		symbolImage.src="images/icons/lg/01.png";
		hideWarning();		
	}
	self.setTimeout( "retrieveWeather()", 1000*60*30 );
}

function showExtendedFlyout() {
	if (System.Gadget.Flyout.show==false) {
		if (DataComplete) {
			System.Gadget.Flyout.file = "weatherExtended.html";
			System.Gadget.Flyout.show=true;
			System.Gadget.Flyout.onHide = blankFunction;
		}
	} else {
		System.Gadget.Flyout.show=false;
	}
}

function blankFunction() {}

function loadCityPage() {
	if (DataComplete) {
		var shell = new ActiveXObject("WScript.Shell");
		shell.Run(CurrentConditions.url);
	}
}

function loadADC() {
	var shell = new ActiveXObject("WScript.Shell");
	shell.Run(CurrentLinks.homeLink);
}

function loadWarning() {
	if (CurrentWatches.isactive==1) {
		var shell = new ActiveXObject("WScript.Shell");
		shell.Run(CurrentWatches.url);
	}
}

function initLayout() {
	with (document.body.style) {
		width = "130px";
		height = "52px";
	}
	background.style.width = "130px";
	background.style.height = "52px";
	background.src = "url(images/bg-docked.png)";
}

function showWarning() {	
	warning.style.display="block";	
}

function hideWarning() {
	warning.style.display="none";	
}

function settingsClosed() {
	units=System.Gadget.Settings.read("units");
	location=System.Gadget.Settings.read("Location");
	retrieveWeather();
}
