// VARS
// general vars
var bAsync = true;

// live search vars
var bLiveSearch = false;
var bOpened = false;
var sLiveSearch = '';
var hDynamic = null;
var sChangedCurr = '';
var sChangedNew = '';
var iRelValue = 0;
var iScrollValue = 4;
var sSearchPreset = 'Suchbegriff eingeben...';

// research vars
var bChanged = true;
var bToggled = false;


// BIND
// bind buttons
function bindLivesearch() {
    // add no autocomplete attr. to form... w3c doesn't allow it in form-tag
    $('#search_block_form').attr('autocomplete', 'off');

    // check autofields       
    $('.JsAutoField').keypress(
        function(e) { 
            // enter key
            if(e.keyCode === 13) {
                // relocate
                if(iRelValue > 0) {
                    // get link object
                    var oLink = $('#LiveSearchResult' + iRelValue);
                    
                    // same tab?
                    if($(oLink).attr('onclick') == undefined) {
                        window.location = oLink.attr("href");                  
                    } else {
                        window.open(oLink.attr("href"));
                    }
                    
                    // close window
                    LiveSearchClose();
                
                    // do not submit
                    return false;                
                }

                // close window
                LiveSearchClose();
                
                // do submit
                return true;
            }
        }
    );
            
    // check autofields       
    $('.JsAutoField').keyup(
        function(e) { 
            // enter key
            if(e.keyCode !== 13) {
                // arrows
                if(e.keyCode === 38 || e.keyCode === 40) {
                    // up arrow
                    if(e.keyCode === 38) {
                        // highlight result
                        if($('#LiveSearchResult' + (iRelValue - 1)).length == 1) {
                            // update cursor
                            iRelValue--;
                        }
                    }
                        
                    // down arrow
                    else if(e.keyCode === 40) {
                        // highlight result
                        if($('#LiveSearchResult' + (iRelValue + 1)).length == 1) {
                            // update cursor
                            iRelValue++;
                        }
                    }
                    
                    // remove highlights
                    $('.AutoFieldResults a').removeClass('active');
                    
                    // highlight selected
                    $('#LiveSearchResult' + iRelValue).addClass('active');

                    // get object
                    var oElement = $('.AutoFieldResults');
                    var oPositionSrc = $('.AutoFieldResults div').offset();
                    var oPosition = $('#LiveSearchResult' + iRelValue).offset();
           
                    // var scrolling height
                    var iHeight = oPosition.top - oPositionSrc.top - (parseInt(oElement.css('height')) / 2);

                    // check value
                    if(iHeight < 0) {
                        iHeight = 0;                               
                    }

                    // scroll to
                    oElement.scrollTop(iHeight); 
                }
                
                // tha rest
                else {
                    // save input value
                    sChangedNew = $('.JsAutoField').val();
                    
                    // update typed value
                    if(sChangedNew != '') {
                        $('#JsTyped').html(sChangedNew);                    
                    }
                    
                    // sart
                    if(!bLiveSearch) {
                        // control autofield
                        AutoField('.JsAutoField');        
                    }            
                }
            }
        }
    );           
    
    // check autofields
    $('.JsAutoField').click(
        function() {
            // delete default value
            if($('.JsAutoField').val() == sSearchPreset) {
                // hide dummy pwd and show real, finally focus it again
                $('.JsAutoField').val('');         
            }
            
            // focus field
            $('.JsAutoField').focus();           
            
            // close current search
            if(bOpened && !bLiveSearch) {
                // close window
                LiveSearchClose();    

                // prevent following the link
                return true;
            }
            
            // prevent following the link
            return true;
        }            
    );
    
    // if search field's open and input field changed
    $('.JsAutoField').change(
        function() {
            if(bOpened && !bLiveSearch) {
                $('body > div').click(
                    function() {
                        // close window
                        LiveSearchClose();    
                        
                        // prevent following the link
                        return true;
                    }
                );
            }
            
            // prevent following the link
            return true;
        }            
    );
}


// LIVE SEARCH
// control auto field
function AutoField(sLiveSearch, bLoop) {
    // loop
    var bAgain = (bLoop == true) ? true : false;
    
    // block any live searches
    bLiveSearch = true;

    // remove old
    if(hDynamic != null) {
        hDynamic.remove();
    }

    // dims
    var aPosition = null;
    
    // get position and dimensions
    aPosition = $('#Page').offset(true);
    aPosition.top += 215;
    aPosition.left += 300;
    aPosition.width = 470;        

    // copy loader
    hDynamic = jQuery('<div id="ResearchLiveSearchDyn">' + $('#ResearchLiveSearch').html() + '</div>').css('position', 'absolute').appendTo('body');

    // move to field
    $(hDynamic).css(aPosition);
    
    // report open search
    bOpened = true;    

    // bind link
    $('#ResearchLiveSearchDyn .AutoFieldClose').click(
        function() {
            // close window
            LiveSearchClose();

            // collapse search
            ajaxSearch.collapse(true);
            
            // prevent following the link
            return false;
        } 
    );    
    
    // save input value
    sChangedCurr = sChangedNew;
        
    // search
    LiveSearch('#ResearchLiveSearchDyn .AutoFieldResults', sChangedCurr);

    // return
    return true;
}
 
// get auto field results
function LiveSearch(sElement, sValue) {
    // check value
    if(sValue == undefined) {
        return false;
    }
    
    // encode url
    sValue = $.URLEncode(sValue);
    
    // reset id value
    iRelValue = 0;
    
    // get data
    $.ajax(
        {
            type: 'GET',
            url: 'index.php',
            data: 'ajaxmode=1&ajaxvalue=' + sValue + '&' + sSessionName + '=' + sSessionId + '&rand=' + new Date().getTime(),
            success: function(data) {
                // return search results
                LiveSearchResults(sElement, data);
            },
            error: function(data) {
                // enable live search
                bLiveSearch = false;          
                        
                // reset buffers
                sChangedCurr = '';
                sChangedNew = '';
            }
        }
    );
} 

// get auto field results
function LiveSearchResults(sElement, sData) {
    // copy buffer
    $(sElement).html(sData);

    // again?
    if(sChangedCurr != sChangedNew) {
        // remove old
        if(hDynamic != null) {
            // show loader
            $('#ResearchLiveSearchDyn').html($('#ResearchLiveSearch').html());
            
            // equal
            sChangedCurr = sChangedNew;
            
            // perform search. no repositioning required
            LiveSearch('#ResearchLiveSearchDyn .AutoFieldResults', sChangedCurr);
        } 
    } else {
        // bind option links
        $('#ResearchLiveSearchDyn .AutoFieldSelect').click(
            function() { 
                // changed
                bChanged = true;
                
                // close winow
                LiveSearchClose();
                
                // follow the link
                return true;                                  
            }
        );
        
        // reset buffers
        sChangedCurr = '';
        sChangedNew = ''; 
        
        // enable search
        bLiveSearch = false;     
    }
    
    // return
    return true;
}

// close auto field results
function LiveSearchClose() {
    // close winow
    if(hDynamic != null) {
        hDynamic.remove();
    }
    
    // del
    sLiveSearch = null;
    
    // enable search
    bLiveSearch = false;
    
    // reset buffers
    sChangedCurr = '';
    sChangedNew = ''; 
    
    // report closed search
    bOpened = false;    
}

// reset auto field values if no id or empty
function LiveSearchResetId() {
    // return
    return true;
}

// reset auto field values if no id or empty
function LiveSearchReset(bDelete) {
    // empty field?
    if(bDelete == true) {
        // update
        $(sLiveSearch).val('');  
    }
    
    // return
    return true;
}

// INIT
$(document).ready(
    function() {
        // autofield
        bindLivesearch();
    }
);

