﻿var currentActiveAutoCompletionItem = null;
function activeAutoCompletionItem(itemTr)
{
    if(currentActiveAutoCompletionItem)
    {
        currentActiveAutoCompletionItem.className = "AutoCompletionItem_unselected";    
    }
    
    if(itemTr)
    {
        itemTr.className = "AutoCompletionItem_selected";
        currentActiveAutoCompletionItem = itemTr;
    }
     
}

function selectAutoCompletionItem(itemTr)
{ 
    var txtQuery = document.getElementById('txtQuery');
    txtQuery.value = itemTr.info.FullText;
    query();
}

function activeAutoCompletionItemByOffset(offset)
{
    var tabAutoCompletion = document.getElementById('tabAutoCompletion').childNodes[0];
    if(tabAutoCompletion)
    {
        var indexOfNewActiveItem = 0;
        for(var i = 0; i < tabAutoCompletion.children.length; i++)
        {
            if(tabAutoCompletion.children[i] == currentActiveAutoCompletionItem)
            {
                indexOfNewActiveItem = i + offset;
                break;
            }
        }
        
        if(indexOfNewActiveItem < 0) 
        {
            indexOfNewActiveItem = 0;
        }
        else if(indexOfNewActiveItem >= tabAutoCompletion.children.length)
        {
            indexOfNewActiveItem = tabAutoCompletion.children.length - 1;
        }
         
        activeAutoCompletionItem(tabAutoCompletion.children[indexOfNewActiveItem]);
    }
    
}

function clearAutoCompletionList()
{   
    var tabAutoCompletion = document.getElementById('tabAutoCompletion');
    currentActiveAutoCompletionItem = null;
    lastAutoCompletionText = null;

    while(tabAutoCompletion.children.length > 0)
    {
        tabAutoCompletion.removeChild(tabAutoCompletion.children[0]);
    }
}

var lastAutoCompletionText = null;
function autoCompletion(text,type)
{
    closeAutoCompletion();
     
    if(text)
    { 
        text = text.trim();
             
        if(text.length > 0)
        {  
            if(lastAutoCompletionText != text)
            {
                var tabAutoCompletion = document.getElementById('tabAutoCompletion');
                
                //clearAutoCompletionList();
                closeAutoCompletion();
                var result = MasterPage.QueryAutoCompletionInfo(text,type).value;

                text = text.replace(/\[/gi,'\\[');
                text = text.replace(/\]/gi,'\\]');
                text = text.replace(/\(/gi,'\\(');
                text = text.replace(/\)/gi,'\\)');
                text = text.replace(/\{/gi,'\\}');
                text = text.replace(/\*/gi,'\\*');
                text = text.replace(/\./gi,'\\.');
                text = text.replace(/\?/gi,'\\?');
                text = text.replace(/\+/gi,'\\+');
                text = text.replace(/%/gi, '[%]');
                            
                if(result)
                {
                    if(result.length)
                    {
                        SetPosition('txtQuery','divAutoCompletion');
                        for(var i = 0; i < result.length; i++)
                        {
                            var info = result[i]; 
                            var tr =   tabAutoCompletion.insertRow(); 
                            tr.info = info;
                            tr.className = "AutoCompletionItem_unselected";  
                             
                            tr.onmouseover = function(){activeAutoCompletionItem(this);};
                            tr.onclick = function(){selectAutoCompletionItem(this);};
                            
                            var keywordRegExp = new RegExp(text, "i");
                            var td = tr.insertCell(); 
                            td.innerHTML = "<div class='AutoCompletionItem_text'>&nbsp;&nbsp;&nbsp;" + ((info.FullText).replace(keywordRegExp, "<font class='AutoCompletionItem_keyword'>$&</font>")) + "</div>"; 
                            tr.appendChild(td);
                            
                            td =  tr.insertCell(); 
                            td.innerHTML = "<div class='AutoCompletionItem_description'> -- " + ((info.Description).replace(keywordRegExp, "<font class='AutoCompletionItem_keyword'>$&</font>")) + "</div>"; 
                            tr.appendChild(td);
                          
                            //tabAutoCompletion.appendChild(tr);
                        }
                        
                        document.getElementById('divAutoCompletion').style.display='';
                        document.getElementById('divAutoCompletion').style.visibility='visible';
                    }

                }  
                
                lastAutoCompletionText = text;
             }
        } 
    }

}

function closeAutoCompletion()
{
    clearAutoCompletionList();
    document.getElementById('divAutoCompletion').style.display="";
    document.getElementById('divAutoCompletion').style.visibility='hidden';
}

var timerDelayCloseAutoCompletion = null;
function delayCloseAutoCompletion()
{
    cancelDelayCloseAutoCompletion();
    timerDelayCloseAutoCompletion = setTimeout( closeAutoCompletion,  2000 ); 
}

function cancelDelayCloseAutoCompletion()
{
    if(timerDelayCloseAutoCompletion)
    {
        clearTimeout(timerDelayCloseAutoCompletion);
    }
}

function query()
{
    var txtQuery = document.getElementById('txtQuery');
//    txtQuery.value = encodeURIComponent(txtQuery.value);//.replace(/\+/g, '%2B');
    txtQuery.value=txtQuery.value.replace(/</gi, '(');
    txtQuery.value=txtQuery.value.replace(/>/gi, ')');
    var type = document.getElementById('hiddenSearchType').value;
    if(type ==0)
    {
        if(txtQuery.value=='请输入您要找的歌曲或者歌手名')
        {
            location.href = "search.aspx";
        }
        else
        { 
            location.href = "search.aspx?type=0&keyword=" + escape(encodeURIComponent(txtQuery.value).trim());
        }
    }
    else
    {
        if(txtQuery.value=='请输入您要找的音乐盒名称')
        {
            location.href = "RingBoxList.aspx";
        }
        else
        { 
            location.href = "RingBoxList.aspx?type=8&keyword=" + escape(encodeURIComponent(txtQuery.value).trim());
        }
    }
    closeAutoCompletion();

}

var timerAutoCompletion = null;
function autoCompletion_onkeyup(textbox, keyCode)
{ 
    switch(keyCode)
    {
        case 38:    //上
            activeAutoCompletionItemByOffset(-1);
            break;
        case 40:    //下
            activeAutoCompletionItemByOffset(1);
            break;
        case 13:    //回车
            document.forms[0].onsubmit = function(){return false;};
            if(currentActiveAutoCompletionItem)
            {
                selectAutoCompletionItem(currentActiveAutoCompletionItem);
            }
            else
            {
                document.getElementById('btnQuery').click(); 
            } 
            return false;
            //break;
        default:
            if(timerAutoCompletion)
            {
                window.clearTimeout(timerAutoCompletion);
            }
            var type = document.getElementById('hiddenSearchType').value;
            
            timerAutoCompletion = window.setTimeout(function(){ autoCompletion(textbox.value,type) }, 100);
            break;
    }
}


function SetPosition(objFrom,objToSet)
{
    var from = document.getElementById(objFrom);
    var toControl = document.getElementById(objToSet);
    
    toControl.style.position = 'absolute';
    var x = from.offsetLeft;
    var parent =from;
    while(parent =parent.offsetParent)
    {
        x = x+parent.offsetLeft;
    }
    toControl.style.left = x+'px';
}