var autoComplete =
{
    __forbidden: [ 13, 38, 40 ],
    storage: {},
    status: {},
    op: { szerzo: 'alnev', cimkek: 'tag' },
    bind: function( id )
    {
        autoComplete.status[ id ] = false;
        autoComplete.storage[ id ] = new Array();
        jQuery( '#autocomplete_' + id ).css( 'top', ( jQuery( '#' + id ).position().top + jQuery( '#' + id ).outerHeight() + 1 ) + 'px' );
        jQuery( '#' + id )
            .keypress( function( e )
            {
                var key = e.charCode ? e.charCode : e.keyCode;
                if( key == 13 )
                {
                    if( !autoComplete.status[ id ] ) return true;
                    else
                    {
                        jQuery( this ).val( jQuery( '#autocomplete_' + id + ' ul li.selected' ).html() );
                        autoComplete.hide( id );
                        e.preventDefault();
                        return false;
                    }
                }
                else if( autoComplete.status[ id ] )
                {
                    if( key == 40 ) jQuery( '#found' + id + '_' + ( parseInt( jQuery( '#autocomplete_' + id + ' ul li.selected' ).attr( 'id' ).split( '_' )[ 1 ] ) + 1 ) ).mouseover();
                    else if ( key == 38 ) jQuery( '#found' + id + '_' + ( parseInt( jQuery( '#autocomplete_' + id + ' ul li.selected' ).attr( 'id' ).split( '_' )[ 1 ] ) - 1 ) ).mouseover();
                }
                else return true;
            } )
            .keyup( function( e )
            {
                var key = e.charCode ? e.charCode : e.keyCode;
                if( autoComplete.__forbidden.indexOf( key ) == -1 )
                {
                    var value = jQuery( this ).val();
                    if( value.length > 1 )
                    {
                        if( typeof autoComplete.storage[ id ][ value ] != 'undefined' )
                        {
                            if( autoComplete.storage[ id ][ value ].suggestions.length ) autoComplete.show( autoComplete.storage[ id ][ value ].suggestions, id );
                            else autoComplete.hide( id )
                        }
                        else
                        {
                            jQuery.ajax(
                            {
                                url: getHomeLinkPath() + '24ora/?op=search' + ( typeof autoComplete.op[ id ] != 'undefined' ? autoComplete.op[ id ] : '' ),
                                data: { s: value },
                                success: function( r )
                                {
                                    if( typeof r.suggestions != 'undefined' )
                                    {
                                        if( r.suggestions.length ) autoComplete.show( r.suggestions, id );
                                        autoComplete.storage[ id ][ value ]=r;
                                    }
                                    else autoComplete.hide( id );
                                },
                                dataType: 'json'
                            } );
                        }
                    }
                }
            } )
            .blur( function() { autoComplete.hide( id ); } )
    },
    show: function( items, target )
    {
        var html = '';
        for( i = 0; i < items.length; i++ )
        {
            html += '<li id="found' + target + '_' + ( i + 1 ) + '">' + items[ i ] + '</li>';
        }
        jQuery( '#autocomplete_' + target ).children( 'ul' ).html( html );
        if( !autoComplete.status[ target ] )
        {
            jQuery( '#autocomplete_' + target ).fadeIn( 200 );
            autoComplete.status[ target ] = 1;
        }
        jQuery( '#autocomplete_' + target ).children( 'ul' ).children( 'li' ).each( function( k, i )
        {
            if( !k ) jQuery( this ).addClass( 'selected' );
            jQuery( this )
                .mouseover( function()
                {
                    jQuery( '#autocomplete_' + target + ' ul li' ).removeClass( 'selected' );
                    jQuery( this ).addClass( 'selected' );
                } )
                .click( function()
                {
                    jQuery( '#' + target ).val( jQuery( this ).html() ).focus();
                    autoComplete.hide( target );
                } );
        } );
    },
    hide: function( id )
    {
        jQuery( '#autocomplete_' + id ).fadeOut( 200 );
        autoComplete.status[ id ] = false;
    }
};
