var Labs = {
    path: '/'
};
(function($) {
    Labs.isAgeAuthenticated = false;
    function showAgeAuthDialog(url, download) {
        var title = '年齢認証', 
            msg = 'ここから先のページでは、１８才以上の方向けのアダルトコンテンツを取り扱っております。<br/>１８才未満の方のアクセスは固くお断りいたします。<br/><br/>あなたは１８才以上ですか？';
        $.alerts.okButton = '&nbsp;はい&nbsp;';
        $.alerts.cancelButton = '&nbsp;いいえ&nbsp;';
        $.alerts.confirm(msg, title, function(r){
            if (r) {
                ageAuth(18);
                Labs.isAgeAuthenticated = true;
                if (download) trackPageview(url, '/_download');
                window.location.href = url;
            }
        });
    };
    
    function initAnchors(){
        try {
            $('#extra_content a').click(function(e){
                var $a = $(this);
                if ($a.hasClass('adult')) {
                    var url = $a.attr('href');
                    var download = $a.hasClass('download');
                    if (!Labs.isAgeAuthenticated) {
                        showAgeAuthDialog(url, download);
                        return false;
                    }
                    if (download) {
                        trackPageview(url, '/_download');
                    }
                } else if ($a.hasClass('download')) {
                    var url = $a.attr('href');
                    trackPageview(url, '/_download');
                }
            });
        } 
        catch (e) { /*alert(e);*/
        }
    };
    
    function ageAuth(value) {
        var name = 'ageauth_age';
        document.cookie = name + '=' + value + '; path=/';
    };
    
    function trackPageview(url, url_prefix) {
        try {
            // 引数 (任意のURL) はスラッシュ (/) で始める必要があるらしい
            if (!url_prefix) url_prefix = '';
            url = url.replace(/^https?:\/\/[^\/]+/, url_prefix);
            if (_gaq) {
                _gaq.push(['_trackPageview', url]);
            }
        } 
        catch (e) {
        }
    };
    
    $.fn.extend({
        blogPreview: function(opt) {
            var url = opt.url || opt;
            var $preview = this;
            var feed = new google.feeds.Feed(url);
            feed.load(function(result){
                $preview.empty();
                var f = result.feed || {};
                if (!f.entries) return;
                var $blog = $('<div/>').addClass('blog');
                if (!opt.notitle) {
                    var $header = $('<h2>').append($('<a/>').attr('href',f.link).html(f.title));
                    $blog.append($header);
                }
                function dateFormat() {
                    var a = [this.getFullYear(),this.getMonth()+1,this.getDate()];
                    for (var i=0; i<a.length; i++) {
                        if (a[i]<10) a[i] = '0' + a[i];
                    }
                    return a.join('/');
                }
                 
                $entries = $('<dl/>');
                for (var i = 0, len = f.entries.length; i < len; i++) {
                    var entry = f.entries[i];
                    $entries.append($('<dt/>').html(dateFormat.apply(new Date(entry.publishedDate),[])));
                    var $entry = $('<dd/>').append($('<a/>').attr('href',entry.link).html(entry.title));
                    $entry.append($('<p/>').addClass('entry_body').html(entry.contentSnippet));
                    $entries.append($entry);
                }
                $blog.append($entries);
                $preview.append($blog);
                
                /*
                for (var i = 0, len = f.entries.length; i < len; i++) {
                    var entry = f.entries[i];
                    var $entry = $('<div/>').addClass('entry');
                    var $title = $('<div/>').addClass('entry_title');
                    $title.append($('<a/>').attr('href',entry.link).html(entry.title));
                    var $body =  $('<div/>').addClass('entry_body').html(entry.contentSnippet+dateFormat.apply(new Date(entry.publishedDate),[]));
                    $entry.append($title).append($body);
                    $blog.append($entry);
                }
                $preview.append($blog);*/
            });
            return this;
        }
    });
        
    // onReady
    $(document).ready(function(){
        /*
        var $wrapper = $('#wrapper'), $canvas = $('#background_canvas');
        $(window).resize(function() {
            $canvas.attr({height:$wrapper.height(), width:$wrapper.width()});
        }).resize();
        */
        initAnchors();
        $('a.lightbox').lightBox({
            imageLoading:            Labs.path+'images/lightbox/lightbox-ico-loading.gif',      // (string) Path and the name of the loading icon
            imageBtnPrev:            Labs.path+'images/lightbox/lightbox-btn-prev.gif',         // (string) Path and the name of the prev button image
            imageBtnNext:            Labs.path+'images/lightbox/lightbox-btn-next.gif',         // (string) Path and the name of the next button image
            imageBtnClose:           Labs.path+'images/lightbox/lightbox-btn-close.gif',        // (string) Path and the name of the close btn
            imageBlank:              Labs.path+'images/lightbox/lightbox-blank.gif'
        });
        
        if (window.applyTheme) {
            window.applyTheme();
        }
    })
})(jQuery);
