﻿(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;

	loadForecastSelect();

	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').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+'"></div>').append(ico + '<div class="tempWrapper">' + temp + tempmin + '</div>');
					wrapperDiv.insertBefore(pogodaBox.find('#forecastSelect')); 
				}); 
				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(){
		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#forecastMenu');
				});
			},
			error: function(){
				jQuery(pogodaBox).html('<span class="ajaxError">.</span>');
			},
			complete: function(){
				jQuery('#forecastMenu li a').click(function(){
					var id = jQuery(this).attr('rel')
					readCityForecast(id);
					return false;
				});
				jQuery(".nav").superfish();
				readCityForecast(jQuery('#forecastMenu li a:first').attr('rel'));
			}
		});
	}

	function loadForecastSelect(){
		pogodaBox.append('<span id="forecastToday">Today:</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);