/*
 *   Depends:
 *	ui.core.js
 */

(function($) {

    TIME_MODE_NOUNS = { 
      'hourly' : 'Hour',
      'daily' : 'Day',
      'weekly' : 'Week',
      'monthly' : 'Month'
    }

    function KtSummaryTableHandler(element, options) {
        this.element = element;
        this.options = options;
        this.category = undefined;
        this.tab = undefined;
        this.page = undefined;
        this.table_id = undefined;
        this.root_url = undefined;
    }

    KtSummaryTableHandler.prototype = {

        init_impl: function() {
            this.category = this.options['category'];
            this.tab = this.options['tab'];
            this.page = this.options['page'];
            this.table_id = this.options['index'];
            this.root_url = this.options['root_url'];

            var summaryTableObj = this;

            $(document).bind('load_all_table_data', function(event, msg) {
                url = np_get_table_url(summaryTableObj, summaryTableObj.category, summaryTableObj.tab, summaryTableObj.page, summaryTableObj.table_id);
                
                // Blank out all of the cell data in the table
                // (All data cells have id's starting with "cell_")
                $(summaryTableObj.element).find('[id^=cell_]').text('---');
                summaryTableObj.update_time_strings();
                

                ktAjaxWrapper({
                    type: 'GET',
                    traditional: true,
                    caller: summaryTableObj.table_id,
                    url: url,
                    dataType: 'json',
                    success: function(data, textStatus) {
                        summaryTableObj.load_values_from_hash(data);
                    },
                    error: function() {
                    }
                });
            });

            $(this.element).parents(".k-box").find(".k-box-head-info").data("esc-title", escape(this.options['title']))

            var data_export_id = "data_export_" + this.table_id;
            $("#" + data_export_id).click(

            function() {
                var data_export_url = np_get_table_export_url(summaryTableObj, summaryTableObj.category, summaryTableObj.tab, summaryTableObj.page, summaryTableObj.table_id);
                document.location = data_export_url;
                return false;
            });

        },

        update_time_strings: function() {
          var time_mode = $('a.time_mode_button.active').data('time-mode');
          var time_adj = time_mode? time_mode.capitalize():"Daily";
          var time_noun = TIME_MODE_NOUNS[time_mode];

          this.element.find('time-string.adj').html(time_adj);
          this.element.find('time-string.noun').html(time_noun);
        },

        load_values_from_hash: function(hash) {
            for (h in hash) {
                var el = $('#' + h);
                el.empty();
                if (hash[h] != "undefined") {
                    el.append(hash[h]);
                }
            }
        }
    };

    function KtSummaryTable() {
        this.handler = null;

        this._init = function() {
            this.handler = new KtSummaryTableHandler(this.element, this.options);
            this.handler.init_impl();
        };
    }

    var kt_summary_table = new KtSummaryTable();
    $.widget("ui.ktSummaryTable", kt_summary_table);

})(jQuery);

