(function($){

 	$.fn.weatherPlugin = function(options){
		var defaults = {  
		   url: "ajax/weather.php",
		   language: "pl",
		   cities: Array(
				"gdynia", "Gdynia",
				"10099","New York" // postal code
		   ),
		   showForecast: true,
		   showForecastString: 'Pokaż szczegółową pogodę',
		   hideForecastString: 'Ukryj',
		   hideCurrentConditions: true
		};	
	
		var options = $.extend(defaults, options); 
	
		// adds all needed html
		$(this).html('<h4 id="city"></h4><div id="current"><noscript><h4>JavaScript musi być włączony w Twojej przeglądarce.</h4></noscript></div><div id="forecast"></div><a id="show-forecast">'+options.showForecastString+'</a>');
	
		// adds loading bar, then getting forecast for the first of the selected cities
		$('#current').text("").append("<h4>Ładowanie...</h4>");
		getWeather(options.cities[0],options.cities[1],options.cities[2],options.language,options.url);
	
		

		// show hides forecast
		$("#show-forecast").click(function () {
			if(options.showForecast){
				if(options.hideCurrentConditions)
					$('#current').stop().hide();
				$('#forecast').stop().fadeIn();
				$("#show-forecast").html(options.hideForecastString+'');
				options.showForecast=false;
			}
			else{
				$('#forecast').stop().hide();
				$('#current').stop().fadeIn();
				$("#show-forecast").html(options.showForecastString+'');
				options.showForecast=true;
			}
		  }
		);
	
		// shows forecast if set in config
		if(options.showForecast){
			$('#show-forecast').css({"display": 'block'});
		}
	};
})(jQuery);

// functions which does the magic, gets data from google weather api
// weather - name, post code or coordinates of the city (check google-api documentation for more information)
// cityName - city name to be dosplayed
// lang - ISO language code (check google-api documentation for more information)

function getWeather(weather, cityName, weather2, language, url){
	$('#current').html("<h4>proszę czekać...</h4>");
	$('#city').html('Aktualna pogoda na Kaszubach:');

	// function for XML parsing
	var childData = function(selector, arg){
	    return selector.find(arg).attr('data');
	}
	$.ajax({
	    type: "GET",
	    url: url, // path to PHP file which gets XML from google-api
	    data: "weather=" + weather + "&hl=" + language,
	    success: function(data){
		
	        curConditions = $(data).find('current_conditions');
	        forecastInfo = $(data).find('forecast_information');
	        
			$('#current').text('');
	        $('#forecast').text('');
	        
	        // error when google-api doesn't return results
	        if (childData(curConditions, 'temp_c') == null) {
	            //$('#current').text("").append("<h4>Nie znaleziono regionu.</h4>");
				//$('#weather-plugin').remove();
				if(weather2=="") {
				$('#weather-plugin').remove();
				return;	
				}
				else {
				getWeather(weather2,cityName,weather,language,url);
	            return;	
				}
	        } 

	        // checking unit system, for converting °F to °C
	        conversion=false;
	        if (childData(forecastInfo, 'unit_system') == 'US'){
	            conversion=true;
	        }
	        
	        // displaying current conditons
			$('#current').append('\
				<img src="http://www.google.com'+childData(curConditions, 'icon')+'" alt="'+childData(curConditions, 'condition')+'" title="'+childData(curConditions, 'condition')+', '+childData(curConditions, 'wind_condition')+', '+childData(curConditions, 'humidity')+'" />\
				<span class="temp">'+childData(curConditions, 'temp_c')+'°C</span>');
		
			// displaying forecast conditons
			$(data).find('forecast_conditions').each(function(){
				low = childData($(this),'low');
				high = childData($(this),'high');
				if(conversion){
					low = Math.round(5/9 * (low-32));
					high = Math.round(5/9 * (high-32));
				}
				$('#forecast').append('\
				<div>\
					<span class="day">'+childData($(this), 'day_of_week')+'</span>\
					<img src="http://www.google.com'+childData($(this), 'icon')+'" alt="'+childData($(this), 'condition')+'" />\
					<span>'+high+"°C/"+low+'°C</span>\
				</div>');
	
			});
			
	    } // :success end 

	}); // ajax end 
	
} // function end 




