/*!
 * jWeather - Google Weather info with jQuery
 *
 * Copyright (c) 2011 SR
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 */

/*
Sonne
    Partly Sunny    01d.png
    Mostly Sunny
    Clear           
Wolken
    Partly Cloudy   04.png
    Mostly Cloudy
    Cloudy
    Overcast
Regen
    Chance of Rain  09.png
    Drizzle
    Light rain
    Heavy Rain
    Rain Showers
    Showers
    Rain
Schnee
    Light snow      13.png
    Snow
    Sleet
    Snow Showers
    Sonstiges
    Fog
    Haze

var d = Date.parseDate("2005-10-05 12:13 am", "Y-m-d g:i a");
2011-06-07 07:50:00 +0000
 */
Date.prototype.setGoogleWeatherDate = function(dString){
    var regexp = /(\d\d\d\d)-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)\s\+(\d\d\d\d)/;

    if (dString.toString().match(new RegExp(regexp))) {
		var d = dString.match(new RegExp(regexp));
		var offset = 0;

		//this.setUTCDate(1);
		this.setFullYear(parseInt(d[1],10));
		this.setMonth(parseInt(d[2],10));
		this.setDate(parseInt(d[3],10));
		this.setHours(parseInt(d[4],10));
		this.setMinutes(parseInt(d[5],10));
		this.setSeconds(parseInt(d[6],10));
		/*if (d[12])
			this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
		else
			this.setUTCMilliseconds(0);
		if (d[13] != 'Z') {
			offset = (d[15] * 60) + parseInt(d[17],10);
			offset *= ((d[14] == '-') ? -1 : 1);
			this.setTime(this.getTime() - offset * 60 * 1000);
		}*/
    }
    else {
		this.setTime(Date.parse(dString));
    }
    return this;
};

(function($){
 	$.fn.extend({ 
 		weather: function(options) {
    		return this.each(function() {
                var self = $(this);
                $.getJSON("/include/weather.php?city=" + options.city, function(data) {
                    var dt = new Date();
                    dt.setGoogleWeatherDate(data.current.date);
                    
                    for(var d in data) {
                        if(d == "current")
                            continue;
                        
                        var day = '<div class="weather_day">' +
                            '<strong>' + data[d].day + ' ' + dt.getDate() + "." + dt.getMonth() + '</strong>' +
                            '<br />' +
                            '<img src="' + data[d].icon + '" alt="" title="' + data[d].condition + '" />' +
                            '<br />' +
                            data[d].high + ' &deg;C' +
                         '</div>';
                        
                        self.append(day);
                        
                        dt.setDate(dt.getDate() + 1);
                    }
                });
    		});
    	}
	});	
})(jQuery);
