function getWeather(latitude,longitude) {    
          var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + latitude + "&lng=" + longitude + "&callback=?&username=canuto";
           jQuery.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 image_url_css = "url(wp-content/themes/infocus/images/weather/"+conditions_img+")";
			jQuery("#weathericon").css("background-image",image_url_css);
			jQuery("#temperature").html(temp);
     });
}

function getConditions(clouds, weather) {
     if (weather == 'n/a') {
          switch (clouds) {
               case 'n/a':
                    return 'sunny.png';
               case 'clear sky':
                    return 'sunny.png';
               case 'few clouds':
                    return 'partly_cloudy.png';
               case 'scattered clouds':
                    return 'scattered_clouds.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 'drizzle.png';
               case 'rain':
                    return 'rain.png';
               case 'snow':
                    return 'snow.png';
               case 'snow grains':
                    return 'sleet.png';
               case 'ice crystals':
                    return 'snow.png';
               case 'ice pellets':
                    return 'snow.png';
               case 'hail':
                    return 'sleet.png';
               case 'small hail':
                    return 'sleet.png';
               case 'snow pellets':
                    return 'sleet.png';
               case 'unknown precipitation':
                    return 'rain.png';
               case 'mist':
                    return 'mist.png';
               case 'fog':
                    return 'mist.png';
               case 'smoke':
                    return 'mist.png';
               case 'volcanic ash':
                    return 'mist.png';
               case 'sand':
                    return 'mist.png';
               case 'haze':
                    return 'mist.png';
               case 'spray':
                    return 'rain.png';
               case 'widespread dust':
                    return 'mist.png';
               case 'squall':
                    return 'flurries.png';
               case 'sandstorm':
                    return 'mist.png';
               case 'duststorm':
                    return 'mist.png';
               case 'well developed dust':
                    return 'mist.png';
               case 'sand whirls':
                    return 'mist.png';
               case 'funnel cloud':
                    return 'flurries.png';
               case 'tornado':
                    return 'storm.png';
               case 'waterspout':
                    return 'storm.png';
               case 'showers':
                    return 'rain.png';
               case 'thunderstorm':
                    return 'thunderstorm.png';
               default:
                    if (weather.indexOf("rain")) {
                         return 'rain.png';
                    } else if (weather.indexOf("snow")) {
                         return 'snow.png';
                    } else if (weather.indexOf("thunder")) {
                         return 'thunderstorm.png';
                    } else if (weather.indexOf("dust")) {
                         return 'dust.png';
                    } else {
                         return 'sunny.png';
                    }
          }
     }
} 

jQuery(document).ready(function() {
    // fanagolf location
	var latitude = 60.28928;
	var longitude = 5.30965;
//	this function updates #weathericon and #temperature
getWeather(latitude,longitude);	
	jQuery(".blog_module .entry p").hide();
	jQuery(".blog_module > .entry > p:first-child").show();
	if (jQuery.browser.msie)
	{
          jQuery(".blog_module .entry").each(function(){
	  jQuery(this).html(jQuery(this).html().replace("<BR>","XXX"));
	  jQuery(this).html(jQuery(this).text().replace("XXX","<BR/>"));	
	  jQuery(".button_link").css("margin-top","15px");
	});
	}
	jQuery(".blog_module .entry").jTruncate({length: 200});
	 
});
