/*
 * jDigiClock plugin 2.1
 *
 * http://www.radoslavdimov.com/jquery-plugins/jquery-plugin-digiclock/
 *
 * Copyright (c) 2009 Radoslav Dimov
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


(function($) {
    $.fn.extend({

        jdigiclock: function(options) {

            var defaults = {
                weatherImagesPath: '/pub/template/star-apart/img/weather/',
                lang: 'en',
                weatherLocationCode: 'EUR|PL|PL015|POZNAN',
                weatherMetric: 'C'
            };

            var regional = [];
            regional['pl'] = {
            		loading: 'Ładowanie...',
                    dayNames: ['Nie', 'Pon', 'Wto', 'Śro', 'Czw', 'Pią', 'Sob']
            }
            
            regional['en'] = {
            		loading: 'Loading...',
            		dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
            }
            
            regional['de'] = {
            		loading: '',
                    dayNames: ['Son', 'Mon', 'Die', 'Mit', 'Don', 'Fre', 'Sam']
            }
        	
            var options = $.extend(defaults, options);

            return this.each(function() {
                var $this = $(this);
                var o = options;
                $this.weatherImagesPath = o.weatherImagesPath;
                $this.lang = regional[o.lang] == undefined ? regional['en'] : regional[o.lang];
                $this.weatherLocationCode = o.weatherLocationCode;
                $this.weatherMetric = o.weatherMetric == 'C' ? 1 : 0;

                $this.html('<div id="forecast_container"></div>');
                $this.displayWeather($this);
            });
        }
    });

    $.fn.displayWeather = function(el) {
        $.fn.getWeather(el);
    }

    $.fn.getWeather = function(el) {
        el.find('#forecast_container').html('<p class="loading"><span>'+el.lang.loading+'</span></p>');
        var metric = el.weatherMetric == 1 ? 'C' : 'F';

        $.getJSON('/weather.php?location=' + el.weatherLocationCode + '&metric=' + el.weatherMetric, function(data) {
        	el.find('#forecast_container .loading').hide();

        	/*
            var curr_temp = '<p class="temp">' + data.curr_temp + '&deg;<span class="metric">' + metric + '</span></p>';  
            // forecast
            el.find('#forecast_container').append('<div id="forecast_current"></div>');
            var curr_for = curr_temp + '<p class="high_low">' + data.forecast[0].day_htemp + '&deg;&nbsp;/&nbsp;' + data.forecast[0].day_ltemp + '&deg;</p>';
            curr_for    += '<p class="text">' + data.forecast[0].day_text + '</p>';
            el.find('#forecast_current').css('background','url(' + el.weatherImagesPath + data.forecast[0].day_icon + '.png) 50% 0 no-repeat').append(curr_temp);
             */
        	
            el.find('#forecast_container').append('<ul id="forecast" class="clearfix"></ul>');
     
            //data.forecast.shift();
            
            for (var i in data.forecast) {
                var d_date = new Date(data.forecast[i].day_date);
                var day_name = el.lang.dayNames[d_date.getDay()];
                var forecast = '<li>';
                forecast    += '<p>' + day_name + '</p>';
                forecast    += '<img src="' + el.weatherImagesPath + data.forecast[i].day_icon + '.png" alt="' + data.forecast[i].day_text + '" title="' + data.forecast[i].day_text + '" />';
                forecast    += '<p>' + data.forecast[i].day_htemp + '&deg;&nbsp;/&nbsp;' + data.forecast[i].day_ltemp + '&deg;</p>';
                forecast    += '</li>';
                el.find('#forecast').append(forecast);
            }
        });
    }

})(jQuery);

