var host = '/';

var weather_widget = function(id, asset_host) {
	if(asset_host) host = asset_host + '/';

	feed = '/cross-site-request?url=http%3A%2F%2Ffeeds.bbc.co.uk%2Fweather%2Ffeeds%2Frss%2F5day%2Fid%2F2823.xml';

	if(!window.ie6) {
		new Ajax(
			feed,
			{
				method: 'get',
				onComplete: function() {
					process_weather(id, this.transport.responseXML);
				}
			}
		).request();
	}
}

var weather_item = function(icon, alt, day, temp_day, temp_night, time_day, time_night) {
	return '<div class="weather_item wi_3"><span class="symbol"><img src="' + host + 'images/icons/24/' + icon + '" width="24" height="24" alt="' + alt + '" title="' + alt + '" class="png" /></span><span class="name">' + day + '</span><span class="temp day">' + temp_day + '</span><span class="temp night">' + temp_night + '</span><span class="time day">' + time_day + '</span><span class="temp night">' + time_night + '</span></div>';
}

var process_weather = function(id, xmi) {
	empty_weather(id);
	
	add_weather(id, '<h2><img src="' + host + 'images/icons/24/157.png" width="24" height="24" alt=" " class="png" /> Weather</h2>');
	
	items = xmi.getElementsByTagName('item');
	weather_content = '';

	for(i = 0; i < items.length; i++) {
		title = items[i].getElementsByTagName('title')[0].firstChild.nodeValue;
		descriptions = process_options(items[i].getElementsByTagName('description')[0].firstChild.nodeValue);
		
		col_title = weather_day(title.split(":")[0]);	
		col_icon = weather_icon(title.split(":")[1].split(',')[0])
		col_icon_alt = title.split(":")[1].split(',')[0];
		
		col_max_temp = descriptions['Max Temp'].split(' ')[0];
		col_min_temp = descriptions['Min Temp'].split(' ')[0];
		
		col_sunrise = descriptions['Sunrise'].split('GMT')[0];
		col_sunset = descriptions['Sunset'].split('GMT')[0];
		
		weather_content += weather_item(col_icon, col_icon_alt, col_title, col_max_temp, col_min_temp, col_sunrise, col_sunset);
	}
	
	add_weather(id, '<div class="weather_container clear">' + weather_content + '</div><div class="weather_source">Source: <a href="http://www.bbc.co.uk/weather/">BBC Weather</a></div>');
}

var empty_weather = function(id) {
	$(id).innerHTML = '';
}

var add_weather = function(id, content) {
	$(id).innerHTML += "\n" + content;
}

var weather_day = function(day) {
	switch(day.toLowerCase().replace(/^\W+/,'').replace(/\W+$/,'')) {
		case 'monday':
			return 'mon';
		break;
		case 'tuesday':
			return 'tues';
		break;
		case 'wednesday':
			return 'wed';
		break;
		case 'thursday':
			return 'thurs';
		break;
		case 'friday':
			return 'fri';
		break;
		case 'saturday':
			return 'sat';
		break;
		case 'sunday':
			return 'sun';
		break;
		default:
			return 'n/a';
	}
}

var weather_icon = function(type) {
	type = type.toLowerCase().replace(/^\W+/,'').replace(/\W+$/,'');
	switch(type) {
		case 'light showers':
		case 'light rain':
		case 'drizzle':
		case 'showers':
			return '184.png';
		break;
		case 'heavy rain':
		case 'heavy showers':
		case 'rain':
			return '202.png';
		break;
		case 'snow':
			return '186.png';
		case 'sunny':
			return '155.png';
		break;
		case 'foggy':
			return '201.png';
		break;
		default:
			return '157.png';
	}
}

var process_options = function(text) {
	arr = new Array();
	opts = text.split(",");
	for(var i = 0; i < opts.length; i++) {
		optColon = opts[i].indexOf(":");
		
		if(optColon > -1) {
			k = opts[i].substring(0, optColon).replace(/^\W+/,'').replace(/\W+$/,'');
			v = opts[i].substring(optColon, opts[i].length).replace(/^\W+/,'').replace(/\W+$/,'');
			arr[k] = v;
		}
	}
	return arr;
}
