(function(jQuery) {
    jQuery.fn.timeSpan = function(o) {
        return this.each(function() {
            new jQuery.timeSpan(this, o);
        });
    };

    jQuery.timeSpan = function(e, o) {
        this.options = o || {};
        this.name = e.name;
        this.input = jQuery(e);
        this.holder = this.input.parent();
        this.input.hide();

        var div = this.input.parent();
        var self = this;
        self.init();

        if (!jQuery.timeSpans) jQuery.timeSpans = new Array();
        jQuery.timeSpans[this.name] = this;
        return self;
    };

    jQuery.timeSpan.initByClassName = function(className, o) {
        if ($(className).length > 0) $(className).timeSpan(o);
    };

    jQuery.timeSpan.fn = jQuery.timeSpan.prototype = {
        timeSpan: '1.0.0'
    };

    jQuery.timeSpan.fn.extend = jQuery.timeSpan.extend = jQuery.extend;

    jQuery.timeSpan.fn.extend({

        init: function() {
            var self = this;
            var div = self.holder;
            var val = self.input.val();
            var id = self.input.attr('id');

            self.timespan = new Timespan(val);

            var html = '<br/><nobr><div class="timespaneditor popupwrapper"><div class="popup">';
            html += '<input class="numeric hh" value="%hh%" /> '+TimespanResources.Hr+'&nbsp;';
            html += '<input class="numeric mm" value="%mm%" /> ' + TimespanResources.Min + '&nbsp;';
            html += '<a href="#" onclick="$(this).parents(\'.timespaneditor\').hide();return false;"><img src="img/accept.png" class="icon16"/></a></div></div></nobr>';
            html = html.replace(/%hh%/g, self.timespan.hours).replace(/%mm%/g, self.timespan.minutes);

            div.append('<a href="#" class="timespanlink" onclick="$(\'.timespaneditor\').hide();">' + self.timespan.toString() + '</a>');
            div.append(html);

            div.find('input').change(function() {
                var inp = $(this);
                if (inp.hasClass('hh'))
                    self.timespan.hours = parseInt(inp.val(), 10);
                if (inp.hasClass('mm'))
                    self.timespan.minutes = parseInt(inp.val(), 10);
                self.timespanChanged();
            });

            self.link = div.find('a:first');
            self.editor = div.find('.timespaneditor:first');
            self.link.click(function() { self.editor.show(); return false; });

            return self;
        },

        timespanChanged: function() {
            var self = this;
            self.input.val(self.timespan.toMinutes());
            self.link.html(self.timespan.toString());
        }

    });

})(jQuery);

function Timespan(val) {
    if (!val) val = 0;
    this.hours = Math.floor(val / 60);
    this.minutes = val - this.hours * 60;

    this.toString = function() {
        var str = '';
        if (this.hours > 0 )
            str += this.hours.decline(TimespanResources.HrDecline) + ' ';
        if (this.minutes > 0)
            str += this.minutes.decline(TimespanResources.MinDecline) + ' ';
        if (str.length == 0) return TimespanResources.Undefined;
        return str;
    }

    this.toMinutes = function() {
        return this.hours * 60 + this.minutes;
    }
}

$(function() {
    jQuery.timeSpan.initByClassName('.timespan');
});