/*
 * 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: '/_Files/socialize/clock/',
                weatherImagesPath: '/_Files/socialize/weather/',
                lang: 'en',
                am_pm: true,
                weatherLocationCode: 'NAM|US|NJ|BAY HEAD',
                weatherMetric: 'F',
                weatherUpdate: 0,
                proxyType: 'aspx'

            };

            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']
            }


            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    += '<p id="left_arrow"><img src="images/icon_left.png" /></p>';
                //html    += '<p id="right_arrow"><img src="images/icon_right.png" /></p>';
                //html += '<div id="digital_container">';
                //html += '<div id="clock"></div>';
                //html += '<div id="weather"></div>';
                //html += '</div>';
                html += '<div id="forecast_container"></div>';
                html += '</div>';

                $this.html(html);
                //$this.displayClock($this);
                $this.displayWeather($this);
            });
        }
    });


    $.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.getWeather = function (el) {

        //el.find('#weather').html('<p class="loading">Update Weather ...</p>');
        //el.find('#forecast_container').html('<p class="loading">Update Weather ...</p>');
        var metric = el.weatherMetric == 1 ? 'C' : 'F';
        var proxy = '';

        switch (el.proxyType) {
            case 'php':
                proxy = 'php/proxy.php';
                break;
            case 'aspx':
                proxy = 'Weather.aspx';
                break;
        }

        $.getJSON('/Proxy/' + proxy + '?location=' + el.weatherLocationCode + '&metric=' + el.weatherMetric, function (data) {

            //Load Complete - Hide Loading Panes
           // el.find('#weather .loading, #forecast_container .loading').hide();

            //Set Weather Image
            el.find('#weather').css('background', 'url(' + el.weatherImagesPath + data.curr_icon + '.png) 50% 100% no-repeat');

            // forecast
            el.find('#forecast_container').append('<div id="current"></div>');

            var curr_for = '';

            //ROW1 - City
            curr_for += '<div class="location">' + data.city + '</div>';

            //ROW2 - Time & Weather
            var now = new Date();
            var am_pm = 'error';
            var now_hours, now_minutes = '';

            //Time
            now_hours = now.getHours();
            now_minutes = now.getMinutes();

            //Time - AM/PM
            am_pm = now_hours > 11 ? 'PM' : 'AM';

            //Time - Hours
            now_hours = ((now_hours > 12) ? now_hours - 12 : now_hours);
            if (now_hours == 0) { now_hours = 12; }

            //Time - Minutes
            if (now_minutes < 10) { now_minutes = "0" + now_minutes; }

            curr_for += '<div class="timeweather">';
            curr_for += '<div class="col-time"><div class="label">Local Time</div>';
            curr_for += '<p class="time">' + now_hours + ':' + now_minutes + ' ' + am_pm + '</p>';
            curr_for += '</div>';
            curr_for += '<div class="col-weather"><div class="label">Local Temp</div>';
            curr_for += '<p class="temp">' + data.curr_temp + '&deg;<span class="metric">' + metric + '</span></p>';
            curr_for += '</div>';
            curr_for += '<div class="clear"></div>';
            curr_for += '</div>';

            //ROW3
            curr_for += '<div class="col-weatherimg"><img src=' + el.weatherImagesPath + data.forecast[0].day_icon + '.png' + ' width="60" /></div>';
            curr_for += '<div class="col-weatherdetails"><p class="weatherdetails">' + data.forecast[0].day_text + '</p></div>';
            curr_for += '<div class="clear"></div>';

            el.find('#current').append(curr_for);
            //el.find('#current').css('background', 'url(' + el.weatherImagesPath + data.forecast[0].day_icon + '.png) 50% 0 no-repeat').append(curr_for);

            el.find('#forecast_container').append('<ul id="forecast"></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);
            //            }

            //el.find('#forecast_container').append('<div id="update"><img src="images/refresh_01.png" alt="reload" title="reload" id="reload" />' + el.timeUpdate + '</div>');

            $('#reload').click(function () {
                el.find('#weather').html('');
                el.find('#forecast_container').html('');
                $.fn.getWeather(el);
            });
        });
    }

})(jQuery);
