/*Weather data gathered from http://www.geonames.org weather service. 
Weather widget based on a script from http://www.myphpetc.com/2009/11/jquery-weather-widget.html*/
$("document").ready(function() {
		$.cookie('loc_latitude', '37.59', {expires: 0});	
		$.cookie('loc_longitude', '23.44', {expires: 0}); 
		$.cookie('loc_country', 'Greece', {expires: 0}); 
		$.cookie('loc_region', '', {expires: 0}); 
		$.cookie('loc_city', 'Athens', {expires: 0}); 
		$.cookie('loc_country_code', 'GR', {expires: 0}); 
		getWeather();
});

function geoPlugin(data) { 
	$.cookie('loc_latitude', '37.59', {expires: 0});	
	$.cookie('loc_longitude', '23.44', {expires: 0}); 
	$.cookie('loc_country', 'Greece', {expires: 0}); 
	$.cookie('loc_region', '', {expires: 0}); 
	$.cookie('loc_city', 'Athens', {expires: 0}); 
	$.cookie('loc_country_code', 'GR', {expires: 0}); 
	getWeather();
}

function getWeather() {
	var latitude = $.cookie('loc_latitude');  
	var longitude = $.cookie('loc_longitude'); 
	
	var loc_conditions = $.cookie('loc_conditions');
	var loc_conditions_img = $.cookie('loc_conditions_img');
	var loc_temp = $.cookie('loc_temp');
	var loc_humidity = $.cookie('loc_humidity');
	
	if (loc_conditions && loc_conditions_img) {
		setConditions(loc_conditions, loc_conditions_img, loc_temp, loc_humidity);
	} else {
		var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + latitude + "&lng=" + longitude + "&callback=?";
		$.getJSON(url, function(data) {
			var clouds = data.weatherObservation.clouds;
			var weather = data.weatherObservation.weatherCondition;
			var temp = data.weatherObservation.temperature;
			var humidity = data.weatherObservation.humidity;
			
			var conditions_img = getConditions(clouds, weather);
			
			var conditions = '';
			if (weather == 'n/a') {
				if (clouds == 'n/a') {
					conditions = 'fine';
				} else {
					conditions = clouds;
				}
			} else {
				conditions = weather;
			}
			
			$.cookie('loc_conditions', conditions);	
			$.cookie('loc_conditions_img', conditions_img);	
			$.cookie('loc_temp', temp);	
			$.cookie('loc_humidity', humidity);	
			setConditions(conditions, conditions_img, temp, humidity);
		});
	}
}

function getConditions(clouds, weather) {
	if (weather == 'n/a') {
		switch (clouds) {
			case 'n/a':
				return '01_sunny.png';
			case 'clear sky':
				return '01_sunny.png';
			case 'few clouds':
				return '03_partly_sunny.png';
			case 'scattered clouds':
				return '04_intermittentclouds.png';
			case 'broken clouds':
				return '04_intermittentclouds.png';
			default:
				return '07_cloudy.png';
		}
	} else {
		weather = weather.replace('light ', '').replace('heavy ', '').replace(' in vicinity', '');
		switch(weather) {
			case 'drizzle':
				return '16_rain.png';
			case 'rain':
				return '16_rain.png';
			case 'snow':
				return '20_snow_png';
			case 'snow grains':
				return '22_ice.png';
			case 'ice crystals':
				return '22_ice.png';
			case 'ice pellets':
				return '22_ice.png';
			case 'hail':
				return '22_ice.png';
			case 'small hail':
				return '22_ice.png';
			case 'snow pellets':
				return '22_ice.png';
			case 'unknown precipitation':
				return '16_rain.png';
			case 'mist':
				return '08_dreary.png';
			case 'fog':
				return '09_fog.png';
			case 'smoke':
				return '07_cloudy.png';
			case 'volcanic ash':
				return '07_cloudy.png';
			case 'sand':
				return '07_cloudy.png';
			case 'haze':
				return '08_dreary.png';
			case 'spray':
				return '16_rain.png';
			case 'widespread dust':
				return '07_cloudy.png';
			case 'squall':
				return '17_flurries.png';
			case 'sandstorm':
				return '07_cloudy.png';
			case 'duststorm':
				return '07_cloudy.png';
			case 'well developed dust':
				return '07_cloudy.png';
			case 'sand whirls':
				return '07_cloudy.png';
			case 'funnel cloud':
				return '17_flurries.png';
			case 'tornado':
				return '26_windy.png';
			case 'waterspout':
				return '11_mostlycloudywithshowers.png';
			case 'showers':
				return '11_mostlycloudywithshowers.png';
			case 'thunderstorm':
				return '13_thunderstorms.png';
			default:
				if (weather.indexOf("rain")) {
					return '16_rain.png';
				} else if (weather.indexOf("snow")) {
					return '20_snow_png';
				} else if (weather.indexOf("thunder")) {
					return '13_thunderstorms.png';
				} else if (weather.indexOf("dust")) {
					return '07_cloudy.png';
				} else {
					return '01_sunny.png';
				}
		}
	}
}

function setConditions(conditions, conditions_img, temp, humidity) {
	var country = $.cookie('loc_country');
	var region = $.cookie('loc_region');
	var city = $.cookie('loc_city');
	var loc_country_code = $.cookie('loc_country_code');
	temp_f = ((parseInt(temp)*9)/5) + 32; 
	temp_type = "C";
	temp_type_f = "F";

	$("#weather_widget").append("<div id='weather_conditions'><span id='weather_temp'>" + temp + "&deg; " + temp_type + " / " + temp_f + "&deg; " + temp_type_f + " </span><img id='weather_img' src='/assets/images/weather_icons/" + conditions_img + "' /></div>");
}
