// script.js for terkel.jp 3.2

(function ($) {

    $.fn.setGrid = function (options) {
        var opts = $.extend({}, $.fn.setGrid.defaults, options);
        return this.each(function () {
            var $this = $(this),
                $cell = $this.find(opts.cell).addClass(opts.cellClass),
                c = $this.data('column-count') || opts.columnCount,
                r = $cell.length % c,
                i;
            $cell.
                filter(':nth-child(' + c + 'n+1)').addClass(opts.firstInRowClass).end().
                filter(':nth-child(' + c + 'n)').addClass(opts.lastInRowClass);
            if (r === 0) {
                $cell.filter(':nth-child(n+' + ($cell.length - c + 1) + ')').addClass(opts.lastRowClass);
            } else {
                for (i = 0; i < r; i += 1) {
                    $cell.filter(':nth-child(' + ($cell.length - i) + ')').addClass(opts.lastRowClass + ' ' + opts.remainderClass + ' ' + opts.lastNthClassPrefix + (i + 1));
                }
            }
        });
    };

    $.fn.setGrid.defaults = {
        cell: '> *',
        cellClass: 'cell',
        columnCount: 0,
        firstInRowClass: 'first-in-row',
        lastInRowClass: 'last-in-row',
        lastRowClass: 'last-row',
        remainderClass: 'remainder',
        lastNthClassPrefix: 'last-'
    };

    $.fn.printLinkURLs = function () {
        return this.each(function () {
            var $this = $(this),
                html = [],
                urls = [];
            if (!$this.is(':has(a[href])')) {
                return;
            }
            html[html.length] = '<dl class="url-list">';
            $this.find('a[href]').each(function () {
                var url = this.href,
                    inArray = $.inArray(url, urls),
                    index;
                if (inArray < 0) {
                    urls[urls.length] = url;
                    index = urls.length;
                    html[html.length] = '<dt>' + index + '</dt><dd>' + url + '</dd>';
                } else {
                    index = inArray + 1;
                }
                $('<sup class="url-ref">[' + index + ']</sup>').insertAfter($(this)).hide();
            });
            html[html.length] = '</dl>';
            $(html.join('')).appendTo($this).hide();
        });
    };

    var supportsInputType = function (val) {
        var input = document.createElement('input');
        input.setAttribute('type', val);
        return input.type !== 'text';
    };

    var supportsInputAttribute = function (attr) {
        var input = document.createElement('input');
        return attr in input;
    };

    // DOM-ready functions
    $(function () {

        if (!window.Modernizr) {
            $('html.no-js').removeClass('no-js').addClass('js');
        }

        $('figure.quote').each(function () {
            var $this = $(this),
                $cite = $this.children('.cite').children('cite'),
                citeUrl = $this.children('blockquote').attr('cite'),
                pattern = /^https?:/i;
            if ($cite && pattern.test(citeUrl)) {
                $cite.wrap('<a href="' + citeUrl + '"/>');
            };
        });

        $('ins.block').each(function () {
            var $this = $(this),
                dt = $this.attr('datetime');
            if (dt) {
                $this.prepend('<span class="datetime">Updated <time itemprop="dateModified">' + dt + '</time></span>');
            }
        });

        $('.grid').setGrid();
        $('.cells.cols-2').setGrid({ columnCount: 2 });
        $('.cells.cols-3').setGrid({ columnCount: 3 });
        $('.cells.cols-4').setGrid({ columnCount: 4 });

        $('#comments-on-hatena-bookmark').loadHatenaBookmarks({
            loadingMessage: '<img src="/img/ico/loading.gif" alt="Loading&#8230;" />'
        });

        $('.twitter-statuses').loadTwitterStatuses({
            image: false,
            birdIconSrc: '/img/twitter/bird_16_gray.png',
            loadingMessage: '<img src="/img/ico/loading.gif" alt="Loading&#8230;" />'
        });

        $('.lastfm-profile').loadLastfmProfile({
            loadingMessage: '<img src="/img/ico/loading.gif" alt="Loading&#8230;" />'
        }).find('a').tipsy({
            gravity: 's',
            live: true
        });

        $('div.article-content').printLinkURLs();

        $('a:has(img:only-child)').addClass('has-img-only');

        // google-code-prettify
        // http://code.google.com/p/google-code-prettify/
        $('pre[class*="lang-"]').addClass('prettyprint');
        prettyPrint();

    });

    // Window-onload functions
    //$(window).load(function () {
    //});

})(jQuery);

