$().ready
(
    function ()
    {
        $.ajaxSetup( {
            cache: false
        } );

        $('.comments-reply').children('a').click(
            function() {
                $('.comments-add').not(':last').remove();

                var wrapper = $('.comments-add').clone(true);
                var form = $('.comments-form', wrapper);

                $(form).attr('action', $(form).attr('action') + '/' + $(this).attr('rel') );

                $(this).after($(wrapper).hide());
                $(wrapper).show('slow');
            }
        );

        $('.input-file').animate({opacity: 0}, 1, function () { $(this).removeClass('hidden'); } );
        $('.input-file-button').click( function () { $(this).fileUploadClick($(this).parent().children('.input-file')); } );
        $('.input-file').change( function() { $(this).parent().children('.input-file-text').val( $(this).val() ); } );

        $('a').not('[href^="' + window.location.protocol + '//' + window.location.host + '"]')
        .not('[href^="/"]').not('[href^="javascript:;"]').not('[href^="javascript"]').add('[href$=".png"]').add('[href^="/go"]').each(
            function () {
                $(this).attr('target', '_blank');
            }
        );

        var timeout = [];

        $('#navigation li').each(
            function (i, el) {
                $(el).hover( function() {
                    clearTimeout(timeout[i]);

                    var children = $(el).children('ul');
                        children.stop(true, true);

                    if ($(el).width() > $(children).width()) {
                        $(children).width($(el).width() + 1);
                    } else {
                        $(children).each(
                            function () {
                                $(this).css( { 'margin-left' : ( Math.ceil( ( $(el).width() - $(this).width() ) / 2 ) ) + 'px' });
                            }
                        );
                    }

                    $(children).slideDown('fast');
                },
                function() {
                    timeout[i] = setTimeout(
                        function () {
                            $(el).children('ul').stop(true, true).slideUp(0);
                        },
                        250
                    );
                } );
            }
        );
    }
);

$.fn.fileUploadClick = function (target)
{
    $(this).blur();
    $(target).click();
};

$.fn.autoComplete = function (callback) {
    var autoCompleteTime = null;
    var autoCompleteKeys = [];
    var autoCompleteVals = [];

    if (typeof $(this).attr('src') != 'undefined') {
        var input = $(this).clone(true).attr('autocomplete', 'off');
        var clone = '#' + $(input).attr('id') + '-value';

        autoCompleteEvent = function () {
            clearInterval(autoCompleteTime);

            var src = $(input).attr('src');
            var val = $(input).val();

            if ( val.length > 1 ) {
                autoCompleteTime = setInterval( function ()
                {
                    var i = $.inArray(val, autoCompleteKeys);

                    if ( -1 !== i ) {
                        autoCompleteResults(autoCompleteVals[i]);
                    } else {
                        $.getJSON(
                            src, { q : val },
                            function (data) {
                                autoCompleteKeys.push(val);
                                autoCompleteVals.push(data);

                                autoCompleteResults(data);
                            }
                        );
                    }

                    clearInterval(autoCompleteTime);
                }, 500);
            }
        };

        autoCompleteResults = function (data) {
            if (0 !== data.length) {
                $('.autocomplete').remove();

                var li;
                var wr = $('<div />').addClass('autocomplete').width($(input).innerWidth());
                var ul = $('<ul />');

                $(input).after($(wr).append(ul));

                $.each(
                    data,
                    function (i, el) {
                        li = $('<li><div>' + el.name + '</div></li>').click( function()
                        {
                            if (false === callback instanceof Function) {
                                $(input).val(el.name);
                                $(clone).val(el.id);
                            } else {
                                callback.call({'input' : input, 'clone' : clone}, el);
                            }
                            
                            $(wr).remove();
                            $(input).focus();
                        });

                        $('body').click(function(){ $(wr).remove(); $('body').unbind('click'); });

                        $(ul).append(li);
                    }
                );
            }
        };
        
        $(this).replaceWith($(input));
        $(input).keyup(autoCompleteEvent);
    }
};