// Search suggest
$( document ).ready( function( ) {
    
    var delayedExecution = {
        
        execute : function( elem, func ) {
            
            this.timeoutId = null;
            func.call( elem );
            
        },
        
        wait: function( elem, func ) {
            
            if ( this.timeoutId ) {
                
                window.clearTimeout( this.timeoutId );
                
            }
            
            var delayedExecution = this;
            
            this.timeoutId = window.setTimeout( function( ) {
                
                delayedExecution.execute( elem, func );
                
            }, 400 );
            
        }
        
    }; 

    /*
     * Disable browser autocompleting
     */
    $( '#searchInput' ).attr( 'autocomplete', 'off' );
    
    /*
     * The real function
     */
    $( '#searchInput' ).keyup( function( ) {
        
        delayedExecution.wait( this, function( ) {
            
            /*
             * Create variable
             */
            var searchbox       = $( this ).val( );
            
            /*
             * If input was empty
             */
            if ( searchbox == '' ) {
                
                suggestHide( );
                
            } else {
                
                /*
                 * Create AJAX call
                 */
                $.ajax( {
                    
                    type            : 'GET',
                    url             : 'ajax/search.php',
                    data            : 'q=' + searchbox,
                    cache           : false,
                    success         : function( html ) {
                        
                        /*
                         * If there were no results
                         */
                        if ( html == '' ) {
                            
                            /*
                             * Hide suggest box
                             */
                            suggestHide( );
                            
                        } else {
                            
                            /*
                             * Split rows
                             */
                            arrRows     = html.split( "\n" );
                            
                            /*
                             * Clear old results
                             */
                            $( '#suggestUL' ).empty( );
                            
                            /*
                             * Loop through rows
                             */
                            for( x in arrRows ) {
                                
                                /*
                                 * Split cells
                                 */
                                arrCells = arrRows[ x ].split( '|' );
                                
                                /*
                                 * Build LI element
                                 */
                                rowElement = buildElement( arrCells );
                                
                                /*
                                 * Append LI element to DOM
                                 */
                                $( '#suggestUL' ).append( rowElement );
                                
                            }
                            
                            /*
                             * Make resultDiv visible
                             */
                            var pos = $( '#searchInput' ).offset( );
                            $( '#suggestDiv' ).css( 'left', pos.left + 'px' ).show( 200 );
                            
                        }
                        
                    }
                    
                } );
                
            }
            
        } );
        
        return false;
        
    } ).blur( function( ) {
        
        suggestHide( );
        
    } );
    
} );

function buildElement( arrCells ) {
    
    rowElement = document.createElement( 'li' );
    rowElement.innerHTML = '<img src="thumbnails/' + arrCells[ 1 ] + '" alt="' + arrCells[ 0 ] + '" style="width: 83px; height: 55px; vertical-align: middle;"> <span style="vertical-align: middle;">' + arrCells[ 0 ] + '</span>';
    
    $( rowElement ).hover(
        
        function( ) { this.className = 'ac_over'; },
        function( ) { this.className = ''; }
        
    ).click(
        
        function( ) { window.location = arrCells[ 2 ]; }
        
    );
    
    return rowElement;
    
}

function suggestHide( ) {
    
    /*
     * Clear old results
     */
    $( '#suggestDiv' ).hide( 300, function( ) {
        
        $( '#suggestUL' ).empty( );
        
    } );
    
}
