// *********************************************************************************************
// weather

// INIT

// weather script location
var str_weather_script = '/scripts/weather.aspx';

// weather page location
var str_weather_page = '/weather/index.html';

// form pointer
var obj_weather_form;

// request data object
var obj_weather_http;

// regions loaded flag
var bool_weather_regions = false;

// current region and city
var str_weather_region, str_weather_city;

// start once we find the form
var obj_weather_init = setInterval(function(){
	if(document.forms['form-weather'] != undefined){
		clearInterval(obj_weather_init);
		obj_weather_form = document.forms['form-weather'];
		
		// check saved
		if(get_cookie('np_weather')){
			var arr_location = get_cookie('np_weather').toString().split('/');
			weather_str_weather_region = arr_location[0];
			weather_str_weather_city = arr_location[1];
			weather_get_forecast(weather_str_weather_region, weather_str_weather_city);
		}else{
			
			// check for specified
			var str_query = document.location.search.replace('?', '');
			if((/rg=[^&]+/i.test(str_query)) && (/city=[^&]+/i.test(str_query))){
				
				weather_get_forecast(str_query.replace(/^.*?rg=([^&]+).*?$/i, '$1'), str_query.replace(/^.*?city=([^&]+).*?$/i, '$1'));
				
			// show region/city select boxes
			}else{
				obj_weather_form.rg.disabled = true;
				obj_weather_http = http_request('post', str_weather_script + '?item=regions', weather_show_regions);
			}
		}
	}
}, 100);

// *********************************************************************************************
// FN's

function weather_show_regions(){
	
	// abort unless loaded and okay
	if(typeof(obj_weather_http) == 'undefined' || obj_weather_http.readyState != 4 || obj_weather_http.status != 200){
		return;
	}
	
	bool_weather_regions = true;
	
	var arr_items = obj_weather_http.responseXML.firstChild.childNodes;
	var arr_regions = new Array();
	var obj_region = new Object();
	for(var x = 0; x < arr_items.length; x++){
		if(arr_items[x].nodeType != 1){
			continue;
		}
		obj_region = {};
		for(var y =0; y < arr_items[x].childNodes.length; y++){
			if(arr_items[x].childNodes[y].nodeType != 1){
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'region'){
				obj_region.id = arr_items[x].childNodes[y].firstChild.nodeValue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'name'){
				obj_region.caption = arr_items[x].childNodes[y].firstChild.nodeValue;
			}
		}
		arr_regions.push(obj_region);
	}
	
	// select box pointer
	var select_box = obj_weather_form.rg;
	
	// remove existing options
	remove_options(select_box);
	
	// default option
	var str_default = 'Please select...';
	
	// add options
	add_options(select_box, arr_regions, str_default);
	select_box.disabled = false;
}

function weather_get_cities(str_weather_region){
	obj_weather_form.city.disabled = true;
	obj_weather_http = http_request('post', str_weather_script + '?item=cities&rg=' + str_weather_region, weather_show_cities);
}

function weather_show_cities(){
	
	// abort unless loaded and okay
	if(obj_weather_http.readyState != 4 || obj_weather_http.status != 200){
		return;
	}
	
	var arr_items = obj_weather_http.responseXML.firstChild.childNodes;
	var arr_cities = new Array();
	for(var x = 0; x < arr_items.length; x++){
		if(arr_items[x].nodeType != 1){
			continue;
		}
		for(var y =0; y < arr_items[x].childNodes.length; y++){
			if(arr_items[x].childNodes[y].nodeType != 1){
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'city'){
				arr_cities.push({id:arr_items[x].childNodes[y].firstChild.nodeValue, caption:arr_items[x].childNodes[y].firstChild.nodeValue});
				break;
			}
		}
	}
	
	// select box pointer
	var select_box = obj_weather_form.city;
	
	// remove existing options
	remove_options(select_box);
	
	// default option
	var str_default = 'Please select...';
	
	// add options
	add_options(select_box, arr_cities, str_default);
	select_box.disabled = false;
}

function weather_get_forecast(str_region, str_city){
	
	str_weather_region = str_region;
	str_weather_city = str_city;
	
	// save loc'n in cookie
	set_cookie('np_weather', str_weather_region + '/' + str_weather_city, 90);
	
	obj_weather_http = http_request('post', str_weather_script + '?item=forecast&rg=' + str_weather_region + '&city=' + str_weather_city, weather_show_forecast);
}

function weather_show_forecast(){
	
	// abort unless loaded and okay
	if(typeof(obj_weather_http) == 'undefined' || obj_weather_http.readyState != 4 || obj_weather_http.status != 200){
		return;
	}
	
	var arr_items = obj_weather_http.responseXML.firstChild.childNodes;
	var str_temp = '?<span>&deg;</span>';
	var str_details = '';
	var str_wind = '';
	var str_bar = '';
	var str_value;
	for(var x = 0; x < arr_items.length; x++){
		if(arr_items[x].nodeType != 1){
			continue;
		}
		for(var y = 0; y < arr_items[x].childNodes.length; y++){
			if(arr_items[x].childNodes[y].nodeType != 1 || !arr_items[x].childNodes[y].hasChildNodes()){
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'temp'){
				str_value = arr_items[x].childNodes[y].firstChild.nodeValue;
				str_temp = str_value.length < 1 ? str_temp : str_value + '<span>&deg;</span>';
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'icon'){
				var str_icon = arr_items[x].childNodes[y].firstChild.nodeValue;
				//remove 'night' spec
				if(/\w+n/i.test(str_icon) && str_icon != 'unknown'){
					str_icon = str_icon.replace(/(\w+)n/i, '$1');
				}
				document.getElementById('weather-img').src = '/_assets/images/weather/' + str_icon + '.gif';
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'details'){
				var str_details = arr_items[x].childNodes[y].firstChild.nodeValue;
				str_details = str_details != 'Unknown' ? str_details : '';
				document.getElementById('weather-img').alt = str_details;
				str_details = str_details.length > 0 ? str_details + '<br />' : '';
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'wind'){
				str_wind = 'Wind: ' + arr_items[x].childNodes[y].getAttribute('direction') + ' at ' + arr_items[x].childNodes[y].firstChild.nodeValue + ' km/h, ';
				continue;
			}
			if(arr_items[x].childNodes[y].nodeName == 'humidity'){
				str_bar = 'Humidity: ' + arr_items[x].childNodes[y].firstChild.nodeValue + '%';
				continue;
			}
		}
	}
	
	document.getElementById('weather-temp').innerHTML = str_temp;
	document.getElementById('weather-text').innerHTML = '<strong>' +str_weather_city + '</strong><br />' + str_details +'<span>' + str_wind + str_bar + '</span>';
	document.getElementById('weather-forecast').getElementsByTagName('a')[0].href = '/weather/index.html?rg=' + str_weather_region + '&city=' + str_weather_city;
	
	// set visibilities
	obj_weather_form.style.display = 'none';
	document.getElementById('weather-forecast').style.display = 'block';
}

function weather_loc(){
	document.getElementById('weather-forecast').style.display = 'none';
	obj_weather_form.style.display = 'block';
	if(!bool_weather_regions){
		obj_weather_form.rg.disabled = true;
		obj_weather_http = http_request('post', str_weather_script + '?item=regions', weather_show_regions);
	}
	return false;
}

function weather_forecast(){
	
	document.location = str_weather_page + '?rg=' + obj_weather_form.rg.value + '&city=' + obj_weather_form.city.value;
	
	return false;
}

