// vim: sts=4:et
$(function() {
    var defaultValue = 'Search...';
    var livePreview = true;
    var prevImg = '';
    
    // Popup buttons
    $('.popups a.popup').click(function() {
        var win = $('#' + this.id + '-window');
        var pos = $(this).offset();
        if (win.css('display') == 'none') {
            $('.popup-window').hide();
            win.show().css('left', pos.left).css('top', pos.top+16);
        } else {
            $('.popup-window').hide();
        }
        return false;
    });
    $(document).click(function() {
        $('.popup-window').hide();
    });
    
    // Facebook comments
    $('.comments h5 a.fbcomments').click(function() {
        $('#facebook-comments').show();
        $('#local-comments').hide();
        return false;
    });
    $('.comments h5 a.localcomments').click(function() {
        $('#local-comments').show();
        $('#facebook-comments').hide();
        return false;
    });
    
    // Search auto-clear
    $('#id_search').focus(function() {
        if ($(this).val() == defaultValue) {
            $(this).val('');
        }
    });
    
    // Search auto-restore
    $('#id_search').blur(function() {
        if ($(this).val() == '') {
            $(this).val(defaultValue);
        }
    });
    
    // Like/Unlike
    $('a.like,a.unlike').each(function() {
        this.href += '?js=1';
    });
    
    // Overlay image effect
    $("img[rel]").overlay();
    
    // Link preview
    $('.sidebar .links li a.preview').mouseover(function() {
        var offset = $(this).offset();
        var left = offset.left+200;
        var top = offset.top - 150;
        if (top < 0) top = 0;
        $('#link-preview').show()
            .css('left', left)
            .css('top', top)
            .attr('src', $(this.parentNode).find('span.preview').html());
    }).mouseout(function() {
        $('#link-preview').hide();
    });
    
    // Comment preview
    var previewId = -1;
    function updateCommentPreview() {
        if (previewId < 0 && livePreview) previewId = setTimeout(function() {
            var name = $('#id_name').val();
            var url = $('#id_url').val();
            var email = $('#id_email').val();
            var text = $('#id_comment').val();
            var img = '';
            if (text.length <= 0 && url.length <= 0 && name.length <= 0 && email.length <= 0) {
                $('#comment-preview').hide();
            } else {
                if (!name) name = "(Please enter name)"
                if (url) name = '<a href="' + url + '">' + name + '</a>';
                if (email) {
                    img = '<img src="http://www.gravatar.com/avatar/' + hex_md5(email) + '.jpg?default=&size=48"/>';
                }
                if (img != prevImg) {
                    $('#comment-preview tr td.gravatar').html(img);
                    prevImg = img;
                }
                $('#comment-preview tr td .author strong').html(name);
                $('#comment-preview tr td .author .time').html('PREVIEW');
                if (text) {
                    var converter = new Showdown.converter();
                    text = converter.makeHtml(text);
                }
                $('#comment-preview tr td .text').html(text);
                $('#comment-preview').show();
            }
            previewId = -1;
        }, 250);
    }
    $('#id_comment,#id_name').keyup(function() {
        updateCommentPreview();
    }).keydown(function() {
        updateCommentPreview();
    }).change(function() {
        updateCommentPreview();
    });
    $('#id_email,#id_url').change(function() {
        updateCommentPreview();
    });
    $('#id_live_preview').click(function() {
        if (previewId > 0) clearTimeout(previewId);
        livePreview = $(this).attr('checked');
        if (!livePreview) $('#comment-preview').hide();
        else updateCommentPreview();
    });
});
