(function($) {

	$.fn.weather = function(o){
		var settings = jQuery.extend({
			city				: '615702',					// Id of the city you want to show
			degree_type			: 'c',						// 'c' for degrees in Celsius, 'f' For degrees in Fahernheit
			show_min_temp		: true,						// true if you want to show, false if not
			show_max_temp		: true,						// true if you want to show, false if not
			show_city			: true,						// true if you want to show, false if not
			show_sunup			: true,						// true if you want to show, false if not
			show_sundown		: true,						// true if you want to show, false if not
			show_windspeed		: true,						// true if you want to show, false if not
			show_tomorrow		: true,						// true if you want to show, false if not
			min_temp_text		: 'Min',					// Text for minimum temperature
			max_temp_text		: 'Max',					// Text for maximum temperature
			sunup_text			: 'Zonsopgang',				// Text for sunrise
			sundown_text		: 'Zonsondergang',			// Text for sundown
			windspeed_text		: 'Windkracht',				// Text for windspeed
			tomorrow_text		: 'Het weer voor morgen'	// Text for tomorrow
		}, o);
	
		return $(this).each(function(){
			$.api.getResult(settings ,$(this));
		});
	};
	
	$.api = {
		getResult : function(s, el){
			var url = $.api.formatUrl(s,s.city);
			$.getJSON(url, function(data) {
			//if (console) console.log(data);
				$.api.buildWeather(s, data, el);
			});
			
		},
		formatUrl : function(s,id){
			var output = "http://weather.yahooapis.com/forecastjson?w=";
			output += id;
			switch(s.degree_type)
			{
				case "c" : output += "&u=c"; break;
				case "f" : output += "&u=f"; break;
			}
			output = escape(output);
			output = "/proxy.php?url="+output;
			return output;
		},
		buildWeather : function(s,json,el){
		
			//json = eval("("+json+")");
		
			var item = json.forecast;
			var cond = json.condition;
			var loc = json.location;
			var ast = json.astronomy;
			var wind = json.wind;
			
			var html = '<div class="weatherinfo">';
						if (s.show_city) html += '<h1>'+loc.city+'</h1>'
						html += '<div class="image">'+
							'<img src="'+cond.image+'" alt="" />';
						html += '<h5>'+$.api.getCondition(cond.code)+'</h5>'+
						'</div>'+
						'<ul class="weathertext">';
						if (s.show_min_temp)
						{
							html += '<li>'+
								'<label>'+s.min_temp_text+'</label>';
								if (s.degree_type == 'c') html += '<div>'+item[0].low_temperature+' &deg;C</div>';
								else html += '<div>'+item[0].low_temperature+' &deg;F</div>';
							html += '</li>';
						}
						if (s.show_max_temp)
						{
							html += '<li>'+
								'<label>'+s.max_temp_text+'</label>';
								if (s.degree_type == 'c') html += '<div>'+item[0].high_temperature+' &deg;C</div>';
								else html += '<div>'+item[0].high_temperature+' &deg;F</div>';
							html += '</li>';
						}
						if (s.show_sunup)
						{
							html += '<li>'+
								'<label>'+s.sunup_text+'</label>'+
								'<div>'+ast.sunrise+'</div>'+
							'</li>';
						}
						if (s.show_sundown)
						{
							html += '<li>'+
								'<label>'+s.sundown_text+'</label>'+
								'<div>'+ast.sunset+'</div>'+
							'</li>';
						}
						if (s.show_windspeed)
						{
							html += '<li>'+
								'<label>'+s.windspeed_text+'</label>'+
								'<div>'+wind.speed+'</div>'+
							'</li>';
						}
						html += '</ul>';
						if (s.show_tomorrow)
						{
							html += '<div class="tomorrow">';
								html += '<h3>'+s.tomorrow_text+'</h3>'+
										'<ul class="weathertext">';
								if (s.show_min_temp)
								{
									html += '<li>'+
										'<label>'+s.min_temp_text+'</label>'+
										'<div>'+item[1].low_temperature+'</div>'+
									'</li>';
								}
								if (s.show_max_temp)
								{
									html += '<li>'+
										'<label>'+s.max_temp_text+'</label>'+
										'<div>'+item[1].high_temperature+'</div>'+
									'</li>';
								}
								html += '</ul>';
							html += '</div>';
						}
					html +='</div>';
			el.innerHTML = '';
			el.append(html);
		},
		getCondition : function(nr){
			var output = "Niet beschikbaar";
			switch(nr){
				case "0" : output = "Tornado"; break;
				case "1" : output = "Tropische storm"; break;
				case "2" : output = "Orkaan"; break;
				case "3" : output = "Hevige onweersbuien"; break;
				case "4" : output = "Onweersbuien"; break;
				case "5" : output = "Regen en Sneeuw"; break;
				case "6" : output = "Regen en natte sneeuw"; break;
				case "7" : output = "Sneeuw en natte sneeuw"; break;
				case "8" : output = "Motregen"; break;
				case "9" : output = "Motregen"; break;
				case "10" : output = "Ijzel"; break;
				case "11" : output = "Regen"; break;
				case "12" : output = "Regen"; break;
				case "13" : output = "Sneeuwbuien"; break;
				case "14" : output = "Motsneeuwbuien"; break;
				case "15" : output = "Stuifsneeuw"; break;
				case "16" : output = "Sneeuw"; break;
				case "17" : output = "Hagel"; break;
				case "18" : output = "Sneeuw en regen"; break;
				case "19" : output = "Stof"; break;
				case "20" : output = "Mistig"; break;
				case "21" : output = "Nevel"; break;
				case "22" : output = "Rook"; break;
				case "23" : output = "heftige"; break;
				case "24" : output = "Winderig"; break;
				case "25" : output = "Koud"; break;
				case "26" : output = "Bewolkt"; break;
				case "27" : output = "Zwaar bewolkt"; break;
				case "28" : output = "Zwaar bewokt"; break;
				case "29" : output = "Licht bewolkt"; break;
				case "30" : output = "Licht bewolkt"; break;
				case "31" : output = "Helder"; break;
				case "32" : output = "Zonnig"; break;
				case "33" : output = "Heldere nacht"; break;
				case "34" : output = "Heldere dag"; break;
				case "35" : output = "Regen- en hagel buien"; break;
				case "36" : output = "Heet"; break;
				case "37" : output = "Onweersbuien"; break;
				case "38" : output = "Onweersbuien"; break;
				case "39" : output = "Onweersbuien"; break;
				case "40" : output = "Buien"; break;
				case "41" : output = "Hevige sneeuwval"; break;
				case "42" : output = "Licht bewolkt"; break;
				case "43" : output = "Hevige sneeuwbuien"; break;
				case "44" : output = "Licht bewolkt"; break;
				case "45" : output = "Onweersbuien"; break;
				case "46" : output = "Sneeuwbuien"; break;
				case "47" : output = "Onweersbuien"; break;
				case "3200" : output = "Niet beschikbaar"; break;
			}
			return output;		
		}
		
	};

})(jQuery);
