/* Copyright 2009 Palm, Inc.  All rights reserved. */

AccuWeatherUrl = "http://palmw.accu-weather.com/widget/palmw/weather-data.asp?slat=#{lat}&slon=#{lon}";
/*
Takes in a params object with {lat:latitude, lon:longitude}
Returns both the location and the weather for that location in the form 
{
	city:city,
	state:state,
	description:short description,
	high: high temp in fahrenheit,
	low: low temp in fahrenheit,
	temp: current temp in fahrenheit,
	icon: icon for image in the images folder
}

*/
function fetchWeatherData (callback, params) {
	function getTagValue(doc, tag){
		var el = doc.getElementsByTagName(tag).item(0);
		return el.firstChild.nodeValue;
	}
	function processWeatherData(callback, ajax){
		var xmldoc = ajax.responseXML;
		var ret = {};
		ret.temp = getTagValue(xmldoc, 'temperature');
		ret.high = getTagValue(xmldoc, 'hightemperature');
		ret.low = getTagValue(xmldoc, 'lowtemperature');
		ret.city = getTagValue(xmldoc, 'city');
		ret.state = getTagValue(xmldoc, 'state');
		ret.zip = getTagValue(xmldoc, 'state');
		ret.description = getTagValue(xmldoc, 'txtshort');
		ret.icon = icons[getTagValue(xmldoc, 'weathericon')];
		ret.url = xmldoc.getElementsByTagName("forecast").item(0).getElementsByTagName("url").item(0).firstChild.nodeValue;
		callback(ret);
	}

	var url = AccuWeatherUrl.interpolate(params);
	console.log("url for weather is \n\n" + url + "\n\n");
	new Ajax.Request(url, {
		method:'get',
		onSuccess: processWeatherData.curry(callback)
	});
	
	
	var icons = {
		'01':'Sunny.png',
		'02':'PartlyCloudy.png',
		'03':'PartlyCloudy.png',
		'04':'PartlyCloudy.png',
		'05':'Hazy.png',
		'06':'MostlyCloudy.png',
		'07':'Cloudy.png',
		'08':'Cloudy.png',
		'09':'Undefined.png', 
		'10':'Undefined.png', 		
		'11':'Foggy.png',
		'12':'showers.png',
		'13':'ScatteredShowers.png',
		'14':'ScatteredShowers.png',
		'15':'Thunderstorms.png',
		'16':'Thundershowers.png',
		'17':'Thundershowers.png',
		'18':'Showers.png',
		'19':'Snow.png',
		'20':'BlowingSnow.png',
		'21':'Snow.png',
		'22':'BlowingSnow.png',
		'23':'Snow.png',
		'24':'Mixed.png',
		'25':'Hail.png',
		'26':'Mixed.png',
		'27':'Undefined.png', 
		'28':'Undefined.png', 			
		'29':'Mixed.png',
		'30':'Sunny.png',
		'31':'Cold.png',
		'32':'Windy.png',
		'33':'Sunny.png',
		'34':'PartlyCloudy.png',
		'35':'MostlyCloudy.png',
		'36':'MostlyCloudy.png',
		'37':'Hazy.png',
		'38':'MostlyCloudy.png',
		'39':'ScatteredShowers.png',
		'40':'ScatteredShowers.png',
		'41':'IsolatedTstorms.png',
		'42':'Thundershowers.png',
		'43':'Snow.png',
		'44':'BlowingSnow.png'
	};
}

