// @@require jms/lib/pager.js
// @@require jms/lib/pager.js.more.js
// @@require jms/lib/menutraffic.js
(function() {
var MONTHS = [
     "January" , "February" , "March"
    ,"April"   , "May"      , "June"
    ,"July"    , "August"   , "September"
    ,"October" , "November" , "December"
];

Pager.overrides.push(
    function(data, options, html, html_base_click)
    {
        var month_walk, html_first, i, month;

        if ((data.path[0] == 'date' || data.path[0] == 'wayback' || data.path[0] == 'futuredate') && data.path.length >= 3) {
            // path ~== ['date', 2000, 8, 7]

            if (options.master_links) {
                html_base_click =
                     ' onclick="Pager.more.clickMultiple(this); return Events.stop();" '
                    +' onmouseover="Pager.more.hoverMultiple(this)" '
                ;
            }

            month_walk = new Date(data.path[1], data.path[2]);

            html.push('<div class="c text">'+MONTHS[month_walk.getMonth()]+' '+month_walk.getFullYear()+'</div>');

            html.push('<div class="dates">');
            
            for (i = 0; i != 7; i++) {
                html.push('<span class="f day">' + 'SMTWTFS'.charAt(i) + '</span>');
            }
            i = 0;
            
            html_first = ' style="margin-left:' + Math.min(83, (14 * month_walk.getDay())) + '%" ';
            while (month_walk.getMonth() == data.path[2]) {
                if (data.children[i] && month_walk.getDate() == data.children[i].path[3]) {
                    html.push(
                         '<a menuri="'
                        +data.children[i].path.join('/')
                        +'" class="f" href=""'
                        +html_first
                        +html_base_click
                        +'>'
                        +data.children[i].title
                        +'</a>'
                    );
                    i++;
                }
                else {
                    html.push('<span class="f"' + html_first + '>' + month_walk.getDate() + '</span>');
                }
                month_walk.setDate(month_walk.getDate() + 1);
                html_first = '';
            }
            html.push('</div>');
            return html;
        }
        return false;
    }
);

function pager_date(ri, lower_limit, upper_limit) {
    var
         path
        ,data
        ,lower
        ,upper
        ,all
        ,title
        ,primary_unit
        ,menu_unit_index
        ,ti
        ,menu_value
        ,PATH_UNITS = [null, 'FullYear', 'Month', 'Date']
    ;
    
    lower_limit = lower_limit || new Date(2000, 8 - 1, 7); // da started
    upper_limit = upper_limit || new Date(); // now

    path = ri.split('/');
    data = [];

    primary_unit = PATH_UNITS[path.length - 1];
    menu_unit_index = path.length;
    
    if (path.length < 2) {
        lower = lower_limit;
        upper = upper_limit;
        all = 'All Time';
    }
    else {
        lower = new Date(path[1], path[2] || 0);
        upper = new Date(path[1], path[2] || 0);
        upper['set' + primary_unit](upper['get' + primary_unit]() + 1);
        if (lower_limit && (lower.valueOf() < lower_limit.valueOf())) {
            lower = lower_limit;
        }
        if (upper_limit && (upper.valueOf() > upper_limit.valueOf())) {
            upper = upper_limit;
        }
        title = path[1];
        if (primary_unit == 'Month') {
            title = MONTHS[path[2]] + ' ' + title;
        }
    }
    ti = 0;
    while (PATH_UNITS[(++ti) + menu_unit_index]) {
        lower['set' + PATH_UNITS[ti + menu_unit_index]]((ti + menu_unit_index) == 3 ? 1 : 0); // lower++
    }
    while (lower.valueOf() < upper.valueOf()) {
        menu_value = lower['get' + PATH_UNITS[menu_unit_index]]();
        data.push({
             title: menu_unit_index == 2 ? MONTHS[menu_value] : menu_value
            ,path: path.concat([menu_value])
            ,children: menu_unit_index == (path[0] == 'month' ? 2 : 3) ? null : []
        });
        lower['set' + PATH_UNITS[menu_unit_index]](lower['get' + PATH_UNITS[menu_unit_index]]() + 1); // lower++
    }
    if (path.length < 3 && path[0] == 'wayback') {
        all = '';
    }
    //return {all: all, title: title, path: path, children: data};
    MenuTraffic.got(true, ri, {all: all, title: title, path: path, children: data});
    return true;
}

MenuTraffic.overrides['date'] = MenuTraffic.overrides['month'] = MenuTraffic.overrides['wayback'] = function(ri) {
    return pager_date(ri);
}

MenuTraffic.overrides['futuredate'] = function(ri) {
    var now = new Date();
    var upper_limit = new Date(now.getFullYear() + 10, 12 - 1, 31); // 10 years ahead
    return pager_date(ri, false, upper_limit);
}
})();
