/*
 * 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 = {
                clockImagesPath: 'images/clock/',
                weatherImagesPath: '/wp-content/themes/hotelBarka/img/pogoda/',
                lang: 'pl',
                am_pm: false,
                weatherLocationCode: 'EUR|PL|PL006|KALWARIA',
                weatherMetric: 'C',
                weatherUpdate: 0,
                proxyType: 'php'
                
            };

            var regional = [];
            regional['en'] = {
                monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
            }
						regional['pl'] = {
                monthNames: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
                dayNames: ['Nie', 'Pon', 'Wto', 'Śro', 'Czw', 'Pią', 'Sob']
            }


            var options = $.extend(defaults, options);

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


                var html = '<div id="plugin_container">';
                html    += '<div id="digital_container">';
                html    += '<div id="weather"></div>';
                html    += '</div>';
                html    += '</div>';

                $this.html(html);

                //$this.displayClock($this);

                $this.displayWeather($this);               

                var panel_pos = ($('#plugin_container > div').length - 1) * 500;
                var next = function() {
                    $('#right_arrow').unbind('click', next);
                    $('#plugin_container > div').filter(function(i) {
                        $(this).animate({'left': (i * 500) - 500 + 'px'}, 400, function() {
                            if (i == 0) {
                                $(this).appendTo('#plugin_container').css({'left':panel_pos + 'px'});
                            }
                            $('#right_arrow').bind('click', next);
                        });                        
                    });
                };
                $('#right_arrow').bind('click', next);

                var prev = function() {
                    $('#left_arrow').unbind('click', prev);
                    $('#plugin_container > div:last').prependTo('#plugin_container').css({'left':'-500px'});
                    $('#plugin_container > div').filter(function(i) {
                        $(this).animate({'left': i * 500 + 'px'}, 400, function() {
                            $('#left_arrow').bind('click', prev);
                        });
                    });
                };
                $('#left_arrow').bind('click', prev);

            });
        }
    });
   

    $.fn.displayClock = function(el) {
        $.fn.getTime(el);
        setTimeout(function() {$.fn.displayClock(el)}, $.fn.delay());
    }

    $.fn.displayWeather = function(el) {
        $.fn.getWeather(el);
        if (el.weatherUpdate > 0) {
            setTimeout(function() {$.fn.displayWeather(el)}, (el.weatherUpdate * 60 * 1000));
        }
    }

    $.fn.delay = function() {
        var now = new Date();
        var delay = (60 - now.getSeconds()) * 1000;
        
        return delay;
    }

    $.fn.getTime = function(el) {
        var now = new Date();
        var old = new Date();
        old.setTime(now.getTime() - 60000);
        
        var now_hours, now_minutes, old_hours, old_minutes, timeOld = '';
        now_hours =  now.getHours();
        now_minutes = now.getMinutes();
        old_hours =  old.getHours();
        old_minutes = old.getMinutes();

        if (el.am_pm) {
            var am_pm = now_hours > 11 ? 'pm' : 'am';
            now_hours = ((now_hours > 12) ? now_hours - 12 : now_hours);
            old_hours = ((old_hours > 12) ? old_hours - 12 : old_hours);
        } 

        now_hours   = ((now_hours <  10) ? "0" : "") + now_hours;
        now_minutes = ((now_minutes <  10) ? "0" : "") + now_minutes;
        old_hours   = ((old_hours <  10) ? "0" : "") + old_hours;
        old_minutes = ((old_minutes <  10) ? "0" : "") + old_minutes;
        // date
        el.currDate = el.lang.dayNames[now.getDay()] + ',&nbsp;' + now.getDate() + '&nbsp;' + el.lang.monthNames[now.getMonth()];
        // time update
        el.timeUpdate = el.currDate + ',&nbsp;' + now_hours + ':' + now_minutes;

        var firstHourDigit = old_hours.substr(0,1);
        var secondHourDigit = old_hours.substr(1,1);
        var firstMinuteDigit = old_minutes.substr(0,1);
        var secondMinuteDigit = old_minutes.substr(1,1);
        
        timeOld += '<div id="hours"><div class="line"></div>';
        timeOld += '<div id="hours_bg"><img src="' + el.clockImagesPath + 'clockbg1.png" /></div>';
        timeOld += '<img src="' + el.clockImagesPath + firstHourDigit + '.png" id="fhd" class="first_digit" />';
        timeOld += '<img src="' + el.clockImagesPath + secondHourDigit + '.png" id="shd" class="second_digit" />';
        timeOld += '</div>';
        timeOld += '<div id="minutes"><div class="line"></div>';
        if (el.am_pm) {
            timeOld += '<div id="am_pm"><img src="' + el.clockImagesPath + am_pm + '.png" /></div>';
        }
        timeOld += '<div id="minutes_bg"><img src="' + el.clockImagesPath + 'clockbg1.png" /></div>';
        timeOld += '<img src="' + el.clockImagesPath + firstMinuteDigit + '.png" id="fmd" class="first_digit" />';
        timeOld += '<img src="' + el.clockImagesPath + secondMinuteDigit + '.png" id="smd" class="second_digit" />';
        timeOld += '</div>';

        el.find('#clock').html(timeOld);

        // set minutes
        if (secondMinuteDigit != '9') {
            firstMinuteDigit = firstMinuteDigit + '1';
        }

        if (old_minutes == '59') {
            firstMinuteDigit = '511';
        }

        setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-1.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
        },200);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},250);
        setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-2.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
        },400);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},450);
        setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-3.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
        },600);

        setTimeout(function() {
            $('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-1.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
        },200);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},250);
        setTimeout(function() {
            $('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-2.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
        },400);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},450);
        setTimeout(function() {
            $('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-3.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
        },600);

        setTimeout(function() {$('#fmd').attr('src', el.clockImagesPath + now_minutes.substr(0,1) + '.png')},800);
        setTimeout(function() {$('#smd').attr('src', el.clockImagesPath + now_minutes.substr(1,1) + '.png')},800);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg1.png')},850);

        // set hours
        if (now_minutes == '00') {
           
            if (el.am_pm) {
                if (now_hours == '00') {                   
                    firstHourDigit = firstHourDigit + '1';
                    now_hours = '12';
                } else if (now_hours == '01') {
                    firstHourDigit = '001';
                    secondHourDigit = '111';
                } else {
                    firstHourDigit = firstHourDigit + '1';
                }
            } else {
                if (now_hours != '10') {
                    firstHourDigit = firstHourDigit + '1';
                }

                if (now_hours == '20') {
                    firstHourDigit = '1';
                }

                if (now_hours == '00') {
                    firstHourDigit = firstHourDigit + '1';
                    secondHourDigit = secondHourDigit + '11';
                }
            }

            setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-1.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
            },200);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},250);
            setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-2.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
            },400);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},450);
            setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-3.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
            },600);

            setTimeout(function() {
            $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-1.png');
            $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
            },200);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},250);
            setTimeout(function() {
                $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-2.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
            },400);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},450);
            setTimeout(function() {
                $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-3.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
            },600);

            setTimeout(function() {$('#fhd').attr('src', el.clockImagesPath + now_hours.substr(0,1) + '.png')},800);
            setTimeout(function() {$('#shd').attr('src', el.clockImagesPath + now_hours.substr(1,1) + '.png')},800);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg1.png')},850);
        }
    }

    $.fn.getWeather = function(el) {

        //el.find('#weather').html('<p class="loading">Trwa odświeżanie</p>');
        //el.find('#forecast_container').html('<p class="loading">Trwa odświeżanie</p>');
        var metric = el.weatherMetric == 1 ? 'C' : 'F';
        var proxy = '';

        switch (el.proxyType) {
            case 'php':
                proxy = 'php/proxy.php';
            break;
            case 'asp':
                proxy = 'asp/WeatherProxy.aspx';
            break;
        }

        $.getJSON('/wp-includes/js/proxy/' + proxy + '?location=' + el.weatherLocationCode + '&lang=pl&metric=' + el.weatherMetric, function(data) {
            el.find('#weather .loading, #forecast_container .loading').hide();
						var pogoda = new Array();
						pogoda['A Flurry'] = 'Opady z podmuchami wiatru';
						pogoda['A Shower'] = 'Ulewa';
						pogoda['Blowing Snow'] = 'Zawieja śnieżna';
						pogoda['Chilly with clouds and sun'] = 'Chłodno z chmurami i słońcem';
						pogoda['Clear'] = 'Przejrzysto';
						pogoda['Clouds And Sun'] = 'Chmury i słońce';
						pogoda['Cloudy'] = 'Zachmurzenie';
						pogoda['Cloudy and warmer with a brief shower or two'] = 'Częściowe zachmurzenie i cieplej, przelotne opady';
						pogoda['Cooler with some sun'] = 'Zimniej, słonecznie';
						pogoda['Dense Fog'] = 'Gęsta mgła';
						pogoda['Drifting Snow'] = 'Zawieje śnieżne';
						pogoda['Drizzle'] = 'Mżawka';
						pogoda['Dust'] = 'Pył';
						pogoda['Fair'] = 'Pogodnie';
						pogoda['Fair and Windy'] = 'Pogodnie i wietrznie';
						pogoda['Fog'] = 'Mgła';
						pogoda['Foggy'] = 'Mgliście';
						pogoda['Freezing Rain'] = 'Marznący deszcz';
						pogoda['Haze'] = 'Mglisto';
						pogoda['Hazy Sunshine'] = 'Zamglone słońce';
						pogoda['Heavy Snow'] = 'Intensywne opady śniegu';
						pogoda['Heavy Snow Shower'] = 'Ciężki śnieg';
						pogoda['Hvy.snowshower'] = 'Ciężki śnieg';
						pogoda['Ice'] = 'Lód';
						pogoda['Ice Crystals'] = 'Kryształki lodu';
						pogoda['Light Fog'] = 'Lekka mgła';
						pogoda['Light Rain'] = 'Lekki deszcz';
						pogoda['Light Rain with Thunder'] = 'Lekki deszcz i burze';
						pogoda['Light Freezing Rain'] = 'Marznący deszcz';
						pogoda['Light Drizzle'] = 'Lekka mżawka';
						pogoda['Light Rain Shower'] = 'Lekki deszcz';
						pogoda['Light Rain Shower and Windy'] = 'Lekki deszcz i wietrznie';
						pogoda['Light Snow Grains'] = 'Ziarnisty śnieg';
						pogoda['Light Snow Shower'] = 'Lekki śnieg';
						pogoda['Light Snow'] = 'Lekki snieżek';
						pogoda['Lgt.Rainshower'] = 'Lekki deszczyk';
						pogoda['Lgt.Snowshower'] = 'Lekki śnieg';
						pogoda['Lgt.Snow/fog'] = 'Lekka mgła';
						pogoda['Mist'] = 'Mgła';
						pogoda['Mostly Clear'] = 'Pogodnie';
						pogoda['Mostly Cloudy and Windy'] = 'Częsciowe zachmurzenie i wiatr';
						pogoda['Mostly Cloudy'] = 'Zachmurzenie';
						pogoda['Mostly Sunny'] = 'Słonecznie przez większość dnia';
						pogoda['Mostly sunny and chilly'] = 'Słonecznie i rześko';
						pogoda['Overcast'] = 'Zachmurzenie';
						pogoda['Partial Fog'] = 'Częściowe mgła';
						pogoda['Partly Clear'] = 'Chwilami przejrzyście';
						pogoda['Partly Sunny'] = 'Cześciowo słonecznie';
						pogoda['Partly Cloudy'] = 'Cześciowe zachmurzenie';
						pogoda['Rain'] = 'Deszcz';
						pogoda['Rain Shower'] = 'Silny deszcz';
						pogoda['Rain and Snow'] = 'Deszcz i śnieg';
						pogoda['Rain and Drizzle'] = 'Deszcz i mżawka';
						pogoda['Scattered Showers'] = 'Przelotne opady';
						pogoda['Scattered T-Storms'] = 'Przelotne burze';
						pogoda['Shallow Fog'] = 'Płytkie mgły';
						pogoda['Showers'] = 'Ulewy';	
						pogoda['Showers in the Vicinity'] = 'Ulewy w okolicy';	
						pogoda['Sleet'] = 'Deszcz ze śniegiem';	
						pogoda['Smoke'] = 'Zadymienie';	
						pogoda['Snow'] = 'Śnieg';	
						pogoda['Snow Shower'] = 'Silne opady śniegu';	
						pogoda['Snowshower'] = 'Silne opady śniegu';	
						pogoda['Some Clouds'] = 'Cześciowe zachmurzenie';	
						pogoda['Sunny'] = 'Słonecznie';	
						pogoda['Thunder'] = 'Burze';	
						pogoda['Thunder/rain'] = 'Burze / deszcz';	
						pogoda['Thundershower'] = 'Burze / ulewy';	
						pogoda['Thunderstorm'] = 'Burze';	
						pogoda['T-Storm'] = 'Burze';
						pogoda['Widespread Dust'] = 'Zamieć pyłowa';		
						
            var curr_temp = data.curr_temp + '&deg;<span class="metric">' + metric + '</span>';

            el.find('#weather').css('background','url(' + el.weatherImagesPath + data.curr_icon + '.png) left top no-repeat');
            var weather = '<div id="local"><p class="city">Kalwaria Zebrzydowska</p><p>'+curr_temp+' '+ pogoda[data.curr_text] + '</p><p>Wiatr '+data.wiatr_sila+' km/h, kierunek: '+data.wiatr_kierunek+'</div>';
            el.find('#weather').html(weather);

        });
    }

})(jQuery);
