﻿(function(jQuery) {
jQuery.fn.pogoda = function(options) {

	var opts = jQuery.extend({}, jQuery.fn.pogoda.defaults, options);
	var pogodaBox = jQuery(this);
	var date = opts.date; 
	var days = opts.daysForward;
	var imgSize = opts.imgSize;
	
	if(jQuery.browser.msie && jQuery.browser.version <= 6) {
		loadForecastSelect();
	}
	else {
		loadForecastMenu();
	}

	function readCityForecast(id){
		jQuery.ajax({
			type: "GET",
			url: "/_pogoda/cityxml.aspx?days="+days+"&data="+date+"&id="+id,
			dataType: "xml",
			success: function(xml) {
				pogodaBox.find('div.day').remove();
				jQuery(xml).find('prognoza').each(function(index){														   
					var city = '<span class="pMiasto">'+ jQuery('miasto', this).text() +'</span>';
					var ico = '<img class="pIco" src="http://www.polskieradio.pl/_pogoda/_img/'+imgSize+'/'+ jQuery('stanpogody', this).text() +'.png" />';
					var temp = '<span class="pTemperatura">'+ jQuery('temperatura_max', this).text() +'&#186C</span>';
					var tempmin = '<span class="pTemperaturaMin">'+ jQuery('temperatura_min', this).text() +'&#186C</span>';
					var wrapperDiv = jQuery('<div id="day'+index+'" class="day"></div>').append(ico + '<div class="tempWrapper">' + temp + tempmin + '</div>');
					wrapperDiv.appendTo(pogodaBox); 
				}); 
				if(days>0){
					jQuery(pogodaBox).children('div:first').prepend('<p class="pDzien">DZIŚ</p>');
					jQuery(pogodaBox).children('div:last').prepend('<p class="pDzien">JUTRO</p>');
				}
			},
			error: function(){
				 jQuery(pogodaBox).html('<span class="ajaxError">.</span>');
			},
			complete: function(){
				jQuery("img").pngfix();
			}
		});
	}

	function loadForecastMenu(){
		pogodaBox.append('<div id="cityWeather"><span id="cityCurrent"></span><div id="cityList"><a id="arrTop" href=""></a><ul id="forecastSelect"></ul><a id="arrBottom" href=""></a></div></div>');
		jQuery.ajax({
			type: "GET",
			url: "/_pogoda/_xml/cities.xml",
			dataType: "xml",
			success: function(xml) {
				jQuery(xml).find('city').each(function(){
					var id_text = jQuery(this).attr('id')
					var name_text = jQuery(this).attr('name')
					jQuery('<li><a href="'+id_text+'" rel="'+id_text+'">'+name_text+'</a></li>').appendTo('ul#forecastSelect');
				});				
			},
			error: function(){
				jQuery(pogodaBox).html('<span class="ajaxError">.</span>');
			},
			complete: function(){
				jQuery('#forecastSelect li a').click(function(){
					var id = jQuery(this).attr('rel');
					jQuery("#cityCurrent").text(jQuery(this).text());
					readCityForecast(id);					
					return false;
				});
				readCityForecast(191);
				jQuery("#cityCurrent").text('Warszawa');
				//jQuery('#forecastSelect li a:first').trigger('click');				
				//readCityForecast(jQuery('#forecastSelect li a:first').attr('rel'));
			}
		});
	}

	function loadForecastSelect(){
		pogodaBox.append('<span id="forecastToday">Dziś:</span><select id="forecastSelect" name="forecastSelect"></select>');
		jQuery.ajax({
			type: "GET",
			url: "/_pogoda/_xml/cities.xml",
			dataType: "xml",
			success: function(xml) {
				jQuery(xml).find('city').each(function(){
						var id_text = jQuery(this).attr('id')
						var name_text = jQuery(this).attr('name')
						jQuery('<option value="'+id_text+'">'+name_text+'</option>').appendTo('select#forecastSelect');
					});
				},
				error: function(){
					 jQuery(pogodaBox).html('<span class="ajaxError">.</span>');
				},
				complete: function(){
					jQuery('#forecastSelect').change(function(){
						readCityForecast(jQuery('#forecastSelect option:selected').val());
						return false;
					});
					if(jQuery.browser.msie && jQuery.browser.version < 7){
						//readCityForecast(jQuery('#forecastSelect option:first').val());
						readCityForecast(191);
					}
					else{
						jQuery('#forecastSelect').val(191);
						readCityForecast(191);
					}
				}
			}
		);
	}

};

function debug(jQueryobj) {
	if (window.console && window.console.log)
		window.console.log('debug: ' + jQueryobj);
	};
	jQuery.fn.pogoda.defaults = {
		zIndex: 10,
		date: '20080530',
		daysForward: 0, 
		imgSize: 42
	};
})(jQuery);