
$.fn.tooltip = function(){
    $(this).hover(function(){
        var node = $(this);
        node.attr('title', '');
        if (!$('#tooltip').length) $('div.body').append('<div id="tooltip" style="display:none;"></div>');
        var tooltip = $('.tooltip', node.parent());
        if (window.tooltip_timer) clearTimeout(window.tooltip_timer);
        if (tooltip.length){
            window.tooltip_timer = setTimeout(function(){
                var pos = node.position();
                var box = $('#tooltip').html('<div>' + tooltip.html() + '</div>').css({top: pos.top - $('#tooltip').height() - 25, left: pos.left - 5});
                if (jQuery.browser.msie) box.show();
                else box.fadeIn(500);
            }, 300);
        }
    }, function(){
        if (window.tooltip_timer) clearTimeout(window.tooltip_timer);
        $('#tooltip').hide();
    });
    return this;
};

$.fn.scrolling = function(){
    $(this).each(function(){
        $('.up a, .down a', this).data('id', '#' + $(this).attr('id').substr(7)).click(function(){
            var list = $($(this).data('id'));
            if (!list.data('count')) list.data('count', $('li.active', list).length);
            var count = list.data('count');
            if ($('li', list).length > count){
                var nodes = $('li.active', list).removeClass('active').hide();
                if (nodes.length){
                    if ($(this).parent().hasClass('down')){
                        var node = nodes.eq(nodes.length-1).next();
                        if (!node.length) node = $('li:first', list);
                        for (var i = 0; i < count; i++){
                            if (node.length){
                                node.addClass('active').hide();
                                node = node.next();
                            } else break;
                        }
                    }
                    else{
                        var node = nodes.eq(0).prev();
                        var limit = count;
                        if (!node.length){
                            node = $('li:last', list);
                            limit = $('li', list).length % count;
                            if (!limit) limit = count;
                        }
                        for (var i = 0; i < limit; i++){
                            if (node.length){
                                node.addClass('active').hide();
                                node = node.prev();
                            } else break;
                        }
                    }
                }
                $('li.active', list).fadeIn();
            }
            return false;
        });
    });
};

jQuery(function($){
    $(document).mouseup(function(){
        if (window._open_lengths)
            $('#lengths').animate({height:0}, 100, function(){ $('#lengths').hide(); window._open_lengths = false; });
    });
    $('#open-lengths').click(function(){
        if (!window._open_lengths){
            if (!$('#lengths')[0].height)
                $('#lengths')[0].height = $('#lengths').height();
            $('#lengths').css({height:0, display:'block'}).animate({height:$('#lengths')[0].height}, 200, function(){ window._open_lengths = true; });
        }
    });
    $('#lengths li').click(function(){
        var count = $(this).html();
        $('#lengths li.select').removeClass('select');
        $(this).addClass('select');
        $('#open-lengths').html(count);
        $('#crossword-length').val(count);
        $('#field_2 input').each(function(i){
            $(this)[(i >= count ? 'fadeOut' : 'fadeIn')]();
        });
    });
    $('#switch-type a').click(function(){
        var count = parseInt($('#open-lengths').html());
        $('#info li').removeClass('select').hide();
        if ($(this).hasClass('simple')){
            $('#field_2 input').each(function(i){
                $(this)[(i >= count ? 'hide' : 'show')]();
            });
            $('#open-lengths').fadeOut(200);
            $('#crossword-length').val(0);
            $('#field_2').hide();
            $('#field_1').fadeIn(200);
            $('#info li.simple').addClass('select').hide().fadeIn(200);
        }
        else{
            $('#open-lengths').fadeIn(200);
            $('#crossword-length').val(count);
            $('#field_1').hide();
            $('#field_2').fadeIn(200);
            $('#info li.crossword').addClass('select').hide().fadeIn(200);
        }
        $('#switch-type a').removeClass('select');
        $(this).addClass('select');
        return false;
    });
    if ($('ul.words').length && $('ul.pages').length){
        $('ul.pages').hide();
        var a = $('ul.pages li:last a');
        var href = $('ul.pages a:first').attr('href').replace(/\/\d+/, '/$');
        var active = parseInt($('ul.pages span').html());
        $('<ul class="browse"><li><a href="' + href.replace('$', (active > 1 ? active : $('ul.pages li').length) - 1) + '">&uarr;</a></li><li><a href="' + (a.length ? a[0].href : href.replace('$', 1)) + '">&darr;</a></li><li class="num"><span>page</span>' + active + '</li></ul>').insertBefore('ul.pages');
    }
    $('#images a').tooltip();
    if ($('#videos a').length) $('#videos a').tooltip();
    $('#scroll-examples, #scroll-blogs, #scroll-concepts').scrolling();
    if ($('#scroll-keywords li').length) $('#scroll-keywords').scrolling();
    if ($('#scroll-tweets li').length) $('#scroll-tweets').scrolling();
    $('a.more').click(function(){
        var id = this.href.substr(this.href.lastIndexOf('#'));
        var me = this;
        if ($(this).hasClass('more-close'))
            $(id).hide('fast', function(){ me.className = 'more'; });
        else{
            $(window).scrollTop($(me).position().top);
            $(id).show('fast', function(){ me.className = 'more-close'; });
        }
        return false;
    });
});

$(window).load(function(){
    $('div.images li img, div.videos li img').each(function(){
        var img = $(this);
        img.css('margin-left', ((60 - img.width()) / 2) + 'px');
    });
});
