$("document").ready(function() {
			geoPlugin({
  "geoplugin_city":"Mackay",
  "geoplugin_region":"Queensland",
  "geoplugin_areaCode":"0",
  "geoplugin_dmaCode":"0",
  "geoplugin_countryCode":"AU",
  "geoplugin_countryName":"Australia",
  "geoplugin_continentCode":"OC",
  "geoplugin_latitude":"-21.24",
  "geoplugin_longitude":"149.053",
  "geoplugin_regionCode":"04",
  "geoplugin_regionName":"Queensland",
  "geoplugin_currencyCode":"AUD",
  "geoplugin_currencySymbol":"&#36;",
  "geoplugin_currencyConverter":1.0939252613
});


});

function geoPlugin(data) {
	$.cookie('loc_latitude', data.geoplugin_latitude, {expires: 7});	
	$.cookie('loc_longitude', data.geoplugin_longitude, {expires: 7});
	$.cookie('loc_country', data.geoplugin_countryName, {expires: 7});
	$.cookie('loc_region', data.geoplugin_region, {expires: 7});
	$.cookie('loc_city', data.geoplugin_city, {expires: 7});
	$.cookie('loc_country_code', data.geoplugin_countryCode, {expires: 7});
	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 'sunnyh.png';
			case 'clear sky':
				return 'sunnyh.png';
			case 'few clouds':
				return 'partly_cloudy.png';
			case 'scattered clouds':
				return 'partly_cloudy.png';
			case 'broken clouds':
				return 'partly_cloudy.png';
			default:
				return 'cloudy.png';
		}
	} else {
		weather = weather.replace('light ', '').replace('heavy ', '').replace(' in vicinity', '');
		switch(weather) {
			case 'drizzle':
				return 'rainy.png';
			case 'rain':
				return 'rainy.png';
			case 'snow':
				return 'snow.gif';
			case 'snow grains':
				return 'sleet.gif';
			case 'ice crystals':
				return 'icy.gif';
			case 'ice pellets':
				return 'icy.gif';
			case 'hail':
				return 'sleet.gif';
			case 'small hail':
				return 'sleet.gif';
			case 'snow pellets':
				return 'sleet.gif';
			case 'unknown precipitation':
				return 'rainy.png';
			case 'mist':
				return 'mist.gif';
			case 'fog':
				return 'fog.gif';
			case 'smoke':
				return 'smoke.gif';
			case 'volcanic ash':
				return 'smoke.gif';
			case 'sand':
				return 'dust.png';
			case 'haze':
				return 'haze.png';
			case 'spray':
				return 'rainy.png';
			case 'widespread dust':
				return 'dust.png';
			case 'squall':
				return 'flurries.gif';
			case 'sandstorm':
				return 'dust.png';
			case 'duststorm':
				return 'dust.png';
			case 'well developed dust':
				return 'dust.png';
			case 'sand whirls':
				return 'dust.png';
			case 'funnel cloud':
				return 'flurries.gif';
			case 'tornado':
				return 'storm.gif';
			case 'waterspout':
				return 'storm.gif';
			case 'showers':
				return 'storm.gif';
			case 'thunderstorm':
				return 'thunder.png';
			default:
				if (weather.indexOf("rain")) {
					return 'rainy.png';
				} else if (weather.indexOf("snow")) {
					return 'snow.gif';
				} else if (weather.indexOf("thunder")) {
					return 'thunder.png';
				} else if (weather.indexOf("dust")) {
					return 'dust.png';
				} else {
					return 'sunnyh.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');
	if (loc_country_code == 'US') {
		temp = parseInt(temp) + 32;
		temp_type = "F";
	} else {
		temp_type = "C";
	}

	$("#weather_widget").append("<h3 id='weather_country'>" + city + "<span id='weather_city'> " + region + "</span></h3><img id='line' src='http://www.mackayairport.com/images/weathericons/line.png' /><img id='weather_img' src='http://www.mackayairport.com/images/weathericons/icons/" + conditions_img + "' />");
	$("#weather_widget").append("<div id='weather_conditions'><p id='weather_temp'>Temp: " + temp + "&deg; " + temp_type + "</p><p id='weather_hum'>Humidity: " + humidity + "%</p><p id='weather_cond'>" + conditions.substr(0, 1).toUpperCase() + conditions.substr(1) + "</p></div>");
}