var pPreviousPattern = '' ;

function liveSearch_ExecuteSearch () {
    var pOptions = document.getElementById ( "mSearchModule").value ;
    var pPattern = document.getElementById ( "mSearchPattern" ).value ;

    if ( pPattern != pPreviousPattern && pPattern.length >= 3 ) {
        $( '#searchresult' ).load ( "liveSearch.php?" , { options : pOptions , query : pPattern } , function ( pContent , pState ) { if ( pState == 'success' ) { $( '#searchresult' ).show () } } ) ;
    }

    pPreviousPattern = pPattern ;
}

function liveSearch_ClearSearch ( noValue ) {
    $( '#searchresult' ).hide () ;

    if ( noValue == true ) {
        document.getElementById ( "mSearchPattern" ).value = '' ;
    }

    pPreviousPattern = '' ;
}

function liveSearch_Init () {
    var pObject   = document.getElementById ( "mSearchPattern" ) ;
    
    if ( pObject ) {
        var pFunction = liveSearch_KeyPressed ;
    
        if ( navigator.userAgent.indexOf ( "Safari" ) > 0 ) {
            pObject.addEventListener ( "keydown" , pFunction , false ) ;
        } else if ( navigator.product == "Gecko" ) {
            pObject.addEventListener ( "keypress" , pFunction , false ) ;
        } else {
            pObject.attachEvent ( "onkeydown" , pFunction ) ;
        }
    
        $( "#searchPattern" ).attr ( "autocomplete" , "off" ) ;
    }
}

function liveSearch_KeyPressed ( pNum ) {
    var pResult    = document.getElementById ( "result" ) ;
    var pDirection = pNum.keyCode == 40 ? getNextNode : getPreviousNode ;
    var pPattern   = document.getElementById ( "mSearchPattern" ).value ;

    switch ( pNum.keyCode ) {
        case 37 : // key left
            document.getElementById ( "mSearchPattern" ).value = pPattern.substring( 0 , pPattern.length - 1 ) ;
            return false ;
            break ;

        case 40 : // key down
        case 38 : // key up
            if ( !document.getElementById ( "pointer" ) ) {
                var pActivate = pDirection ( pResult , "a" , "self" ) ;
            } else {
                if ( pDirection ( document.getElementById ( "pointer" ).parentNode , "a" ) ) {
                    var pActivate = pDirection ( document.getElementById ( "pointer" ).parentNode , "a" ) ;
                } else {
                    var pActivate = pDirection ( pResult , "a" , "self" ) ;
                }

                document.getElementById ( "pointer" ).id = '' ;
            }

            pActivate.id = "pointer" ;

            break ;

        case 27 : // ESC
            liveSearch_ClearSearch ( true ) ;
            break ;

        case 13 : // Enter
            if ( document.getElementById ( "pointer" ) ) {
                document.location.href = document.getElementById ( "pointer" ).href ;
            }
            break ;
    }
}

function getPreviousNode ( o , typeOfNode ) {
    if ( arguments[2] == null ) {
        o = o.previousSibling ;
    }

    while ( o && o.nodeName.toLowerCase() != typeOfNode.toLowerCase () ) {
        c = o.childNodes ;

        for ( var i = ( c.length - 1 ) ; i >= 0 ; i-- ) {
            pSub = getPreviousNode ( c[i] , typeOfNode , false )  ;

            if ( pSub  ) {
                return pSub ;
            }
        }
        o = o.previousSibling;
    }

    if ( o ) {
        return o ;
    } else {
        return '' ;
    }
};

function getNextNode ( o , typeOfNode ) {
    if ( arguments[2] == null ) {
        o = o.nextSibling ;
    }

    while ( o && o.nodeName.toLowerCase() != typeOfNode.toLowerCase () ) {
        c = o.childNodes ;

        for ( var i = 0 ; i < c.length ; i++ ) {
            pSub = getNextNode ( c[i] , typeOfNode , false )  ;

            if ( pSub  ) {
                return pSub ;
            }
        }
        o = o.nextSibling;
    }

    if ( o ) {
        return o ;
    } else {
        return '' ;
    }
};

$(document).ready ( function () {
    $("#mSearchModule").each ( function () {
        this.onfocus = function () { liveSearch_ClearSearch ( false ) ; }
        this.onchange = this.onblur = function () { document.getElementById( "mSearchPattern" ).focus () ; } ;
    } ) ;

    $("#mSearchPattern").each ( function () {
        this.onkeypress = this.onfocus = function () { window.setTimeout ( "liveSearch_ExecuteSearch();" , 300 ) ; } ;
        this.onblur = function () { window.setTimeout ( "liveSearch_ClearSearch( false )" , 2000 ) ; }
    } ) ;

    liveSearch_Init () ;
} ) ;

