(function ($) {
    $.fn.extend({
        jOtimer: function (options) {
            var defaults = {
                start: 0,
                hours_label: 'Hours',
                minutes_label: 'Mins',
                seconds_label: 'Secs'
            }
            var options = $.extend(defaults, options);
            return this.each(function () {
                var o = options;
                var obj = $(this);
                var timer = {};
                obj.attr('id', 'o_timer');
                var timer_headers = $('<div id="o_timer_headers"></div>');
                obj.append(timer_headers);
                var hours_label = $('<span>' + o.hours_label + '</span>');
                var minutes_label = $('<span>' + o.minutes_label + '</span>');
                var seconds_label = $('<span>' + o.seconds_label + '</span>');
                timer_headers.append(hours_label);
                timer_headers.append(minutes_label);
                timer_headers.append(seconds_label);
                var timer_panel = $('<div id="o_timer_panel"></div>');
                obj.append(timer_panel);
                var timer_shadow = $('<div id="o_timer_shadow"></div>');
                var timer_hours = $('<div id="o_timer_hours"></div>');
                var timer_minutes = $('<div id="o_timer_minutes"></div>');
                var timer_seconds = $('<div id="o_timer_seconds"></div>');
                timer_panel.append(timer_shadow);
                timer_panel.append(timer_hours);
                timer_panel.append(timer_minutes);
                timer_panel.append(timer_seconds);
                timer.startFromSeconds = o.start;
                timer.step = 34.025;
                timer.reset = 43;
                timer.hours = parseInt(timer.startFromSeconds / 3600);
                timer.minutes = parseInt((timer.startFromSeconds % 3600) / 60);
                timer.seconds = timer.startFromSeconds - (timer.hours * 3600) - (timer.minutes * 60);
                var o_t = setInterval(o_nextSecond, 1000);

                function o_nextSecond() {
                    timer.seconds++;
                    if (timer.seconds > 59) {
                        timer.seconds = 0;
                        if ($('#o_timer_panel').is(':visible')) $('#o_timer_seconds').css('top', timer.reset);
                        o_nextMinute();
                        return;
                    }
                    if ($('#o_timer_panel').is(':visible')) {
                        var top = timer.reset - (timer.step * timer.seconds);
                        $('#o_timer_seconds').css('top', top);
                        var top = timer.reset - (timer.step * timer.minutes);
                        if(timer.minutes > 59){
                            timer.minutes = 0;
                            top = timer.reset;
                        }
                        $('#o_timer_minutes').css('top', top);
                        var top = timer.reset - (timer.step * timer.hours);
                        if(timer.hours > 23){
                            timer.hours = 0;
                            top = timer.reset;
                        }
                        $('#o_timer_hours').css('top', top);
                    }
                }

                function o_nextMinute() {
                    timer.minutes++;
                    if (timer.minutes == 60) {
                        timer.minutes = 0;
                        if ($('#o_timer_panel').is(':visible')) $('#o_timer_minutes').css('top', timer.reset);
                        o_nextHour();
                        return;
                    }
                    if ($('#o_timer_panel').is(':visible')) {
                        var top = timer.reset - (timer.step * timer.minutes);
                        $('#o_timer_minutes').css('top', top);
                    }
                }

                function o_nextHour() {
                    timer.hours++;
                    if (timer.hours == 24) {
                        timer.hours = 0;
                        if ($('#o_timer_panel').is(':visible')) $('#o_timer_hours').css('top', timer.reset);
                        return;
                    }
                    if ($('#timer_panel').is(':visible')) {
                        var top = timer.reset - (timer.step * timer.hours);
                        $('#o_timer_hours').css('top', top);
                    }
                }
            });
        }
    });
})(jQuery);


