/* The following JavaScript code is adapted from the Typo blog engine:
http://www.typosphere.org/trac/browser/trunk/public/javascripts/typo.js

Copyright (c) 2005 Tobias Luetke

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

function show_dates_as_local_time() {
    var spans = $$('.entryINFO h4');
    for (var i=0; i<spans.length; i++) {
        spans[i].innerHTML = get_local_time_for_date(spans[i].innerHTML);
    }
}

function get_local_time_for_date(time) {
    system_date = new Date(time);
    user_date = new Date();
    delta_minutes = Math.floor((user_date - system_date) / (60 * 1000));
    if (Math.abs(delta_minutes) <= (8*7*24*60)) { // eight weeks... I'm lazy to count days for longer than that
        distance = distance_of_time_in_words(delta_minutes);
        if (delta_minutes < 0) {
            return distance + '後';
        } else {
            return distance + '前';
        }
    } else {
        return 'on ' + system_date.toLocaleDateString();
    }
}

function distance_of_time_in_words(minutes) {
    if (minutes.isNaN) return '';
    minutes = Math.abs(minutes);
    if (minutes < 1) return ('1 分'); // ('less than a minute');
    if (minutes < 50) return (minutes + ' 分'); // (minutes + ' minute' + (minutes == 1 ? '' : 's'));
    if (minutes < 90) return ('約 1 時間');
    if (minutes < 1080) return (Math.round(minutes / 60) + ' 時間');
    if (minutes < 1440) return ('1 日');
    if (minutes < 2880) return ('約 1 日');
    else return (Math.round(minutes / 1440) + ' 日')
}
