var WeatherUnits;
var CurrentLocation;
var CurrentConditions;
var CurrentImages;
var CurrentForecasts;
var CurrentWatches;
var CurrentLinks;
var ErrorString="";
var DataComplete=false;

function Images(xmlNode) {
	this.radar=xmlNode.getElementsByTagName('radar').item(0).firstChild.data;
}

function Units(xmlNode) {
	this.tempUnit=xmlNode.getAttribute('temp');
	this.speedUnit=xmlNode.getAttribute('speed');
	this.distUnit=xmlNode.getAttribute('dist');
	this.presUnit=xmlNode.getAttribute('pres');
	this.precUnit=xmlNode.getAttribute('prec');
}

function Watches(xmlNode) {
	this.zone=xmlNode.getAttribute('zone');
	this.county=xmlNode.getAttribute('county');
	this.isactive=xmlNode.getAttribute('isactive');
	if (xmlNode.getElementsByTagName('url').item(0).firstChild!=null) {
		this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
	} else {
		this.url="";
	}
}

function Location(xmlNode) {
	this.city=xmlNode.getAttribute('city');
	this.state=xmlNode.getAttribute('state');
}

function Conditions(xmlNode) {
	this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
	tempNode=xmlNode.getElementsByTagName('wxc').item(0);
	this.temperature=tempNode.getAttribute('temp');
	this.realFeel=tempNode.getAttribute('rft');
	this.humidity=tempNode.getAttribute('hum');
	this.text=tempNode.getAttribute('text');
	this.icon=tempNode.getAttribute('icon');
	this.windGust=tempNode.getAttribute('wgus');
	this.windSpeed=tempNode.getAttribute('wspd');
	this.windDir=tempNode.getAttribute('wdir');
	this.visibility=tempNode.getAttribute('vis');
	this.precip=tempNode.getAttribute('prec');
	this.uvindex=tempNode.getAttribute('uvi');
	this.uvtext=tempNode.getAttribute('uvt');
	this.radarUrl=xmlNode.getElementsByTagName('radurl').item(0).firstChild.data;
	this.hourlyURL=xmlNode.getElementsByTagName('hourlyurl').item(0).firstChild.data;
}

function Links(xmlNode) {
	this.promo1Link=xmlNode.getElementsByTagName('promo1').item(0).getAttribute('url');
	this.promo1Name=xmlNode.getElementsByTagName('promo1').item(0).getAttribute('name');
	//this.homeLink=xmlNode.getElementsByTagName('home').item(0).getAttribute('url');
	this.homeLink="http://wwwa.accuweather.com";
}

function Forecasts(xmlNode) {
	tempNodes=xmlNode.getElementsByTagName('day');
	tempArray=new Array(tempNodes.length);
	for (count=0;count<tempNodes.length;count++) {
		tempArray[count]=new ForecastDay(tempNodes[count]);
	}
	this.forecastArray=tempArray;
}

function ForecastDay(xmlNode) {
	this.dayNumber=xmlNode.getAttribute('number');
	this.date=xmlNode.getAttribute('date');
	this.weekDay=xmlNode.getAttribute('wday');
	switch (this.weekDay) {
		case "Monday" : this.shortWeekDay="Mon";break;
		case "Tuesday" : this.shortWeekDay="Tue";break;
		case "Wednesday" : this.shortWeekDay="Wed";break;
		case "Thursday" : this.shortWeekDay="Thu";break;
		case "Friday" : this.shortWeekDay="Fri";break;
		case "Saturday" : this.shortWeekDay="Sat";break;
		case "Sunday" : this.shortWeekDay="Sun";break;
	}
	this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
	
	//Daytime forecast data
	this.daytimeTextShort=xmlNode.getElementsByTagName('./daytime/txtshort').item(0).firstChild.data;
	this.daytimeTextLong=xmlNode.getElementsByTagName('./daytime/txtlong').item(0).firstChild.data;
	this.daytimeIcon=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('icon');
	this.daytimeHigh=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('htmp');
	this.daytimeRealFeelHigh=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('rfh');
	this.daytimeWindSpeed=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wspd');
	this.daytimeWindDirection=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wdir');
	this.daytimeWindGust=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wgus');
	this.daytimeMaxUV=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('mxuv');
	this.daytimeRain=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('rain');
	this.daytimeSnow=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('snow');
	this.daytimePrecip=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('prec');
	
	//Nighttime forecast data
	this.nighttimeTextShort=xmlNode.getElementsByTagName('./nighttime/txtshort').item(0).firstChild.data;
	this.nighttimeTextLong=xmlNode.getElementsByTagName('./nighttime/txtlong').item(0).firstChild.data;
	this.nighttimeIcon=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('icon');
	this.nighttimeLow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('ltmp');
	this.nighttimeRealFeelLow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('rfl');
	this.nighttimeWindSpeed=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wspd');
	this.nighttimeWindDirection=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wdir');
	this.nighttimeWindGust=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wgus');
	this.nighttimeRain=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('rain');
	this.nighttimeSnow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('snow');
	this.nighttimePrecip=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('prec');
}

function loadData(units) {
	metric=1;
	if (units=="english")
		metric=0;

	try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.open("GET", "http://vwidget.accuweather.com/widget/vista4/weather_data_v2.asp?location=" + location + "&metric=" + metric, false);		
			req.send();
			if (req.status == 200) {
				parseData(req.responseXML.documentElement);
			} else {

			}
		}
	} catch (e) {

	}
}

function parseData(xmlData) {
	try {
		CurrentLocation=new Location(xmlData.getElementsByTagName("./local").item(0));
		WeatherUnits=new Units(xmlData.getElementsByTagName("./units").item(0));
		CurrentConditions=new Conditions(xmlData.getElementsByTagName("./currentconditions").item(0));
		CurrentImages=new Images(xmlData.getElementsByTagName("./images").item(0));
		CurrentForecasts=new Forecasts(xmlData.getElementsByTagName("./forecast").item(0));
		CurrentWatches=new Watches(xmlData.getElementsByTagName("./watchwarnareas").item(0));
		CurrentLinks=new Links(xmlData.getElementsByTagName("./links").item(0));
		DataComplete=true;
	} catch (e) {

	}
}