﻿// Download by http://www.codefans.net
var DIV_BG_COLOR = "#ffffff";
var DIV_HIGHLIGHT_COLOR = "#4ec1f4";
var DIV_FONT = "宋体";
var DIV_PADDING = "2px";
var DIV_BORDER = "1px solid #CCC";
var queryField;
var divName;
var ifName;
var lastVal = "";
var val = "";
var globalDiv;
var divFormatted = false;


//var typeCheck=document.getElementById('m_name');页面类别 true 资源查询,false 影片信息查询
function checkExecType() //返回直则搜索资源，假则搜索影片信息
{
    alert(document.all("m_name"));  
    if(document.all("m_name")!=null)
    {
       if(document.getElementById('m_name').checked==true)
       {
          return true;
       }   
       else
       {
          return false;
       }
    }
    else  //首页
    {
       return true;
    }
    
}
function InitQueryCode(queryFieldName, hiddenDivName)
{    
    queryField = document.getElementById( queryFieldName );
    queryField.onblur = hideDiv;
    queryField.onkeydown = keypressHandler;
    queryField.autocomplete = "off";
    
    if( hiddenDivName )
    {
        divName = hiddenDivName;
    }
    else
    {
        divName = "querydiv";
    }
    
    ifName = "queryiframe";
    setTimeout("mainLoop()",500);
}

function getDiv(divID)
{
    if(!globalDiv)
    {
        if(!document.getElementById(divID))
        {
            var newNode = document.createElement("div");
            newNode.setAttribute("id", divID);
            document.body.appendChild(newNode);
        }
        globalDiv = document.getElementById(divID);
        var x = queryField.offsetLeft;
        var y = queryField.offsetTop + queryField.offsetHeight;
        var parent = queryField;
        while(parent.offsetParent)
        {
            parent = parent.offsetParent;
            x += parent.offsetLeft;
            y += parent.offsetTop;
        }
        if(!divFormatted)
        {
            globalDiv.style.backgroundColor = DIV_BG_COLOR;
            globalDiv.style.fontFamily = DIV_FONT;
            globalDiv.style.padding = DIV_PADDING;
            globalDiv.style.border = DIV_BORDER;
            
            globalDiv.style.width = "298px";
            globalDiv.style.fontSize = "100%";            
            globalDiv.style.position = "absolute";
            globalDiv.style.left = x + "px";
            globalDiv.style.top = y + "px";
            globalDiv.style.visibility = "hidden";
            globalDiv.style.zIndex = 10000;
            divFormatted = true;
			
        }
    }
    return globalDiv;
}

function showQueryDiv(resultArray)
{
    var div = getDiv(divName);
    while( div.childNodes.length > 0 )
    {
        div.removeChild(div.childNodes[0]);
    }
    for(var i = 0; i < resultArray.length; i++)
    {
        var result = document.createElement("div");
        result.style.cursor = "pointer";
        result.style.padding = "2px 0px 2px 0px";
        result.style.width = div.style.width;//Add width    
        var value = document.createElement("span");    
        _unhighlightResult(result);
        result.onmousedown = selectResult;
        result.onmouseover = highlightResult;
        
        result.onmouseout = unhighlightResult;        
        
        value.className = "value";
        value.style.textAlign = "left";
        value.style.fontWeight = "normal";   
        value.style.fontSize="14px";     
        
        value.innerHTML = resultArray[i];
        result.appendChild(value);
        div.appendChild(result);        
    }
    showDiv(resultArray.length > 0);
}

function selectResult()
{
    _selectResult(this);
}
function _selectResult( item )
{
    var spans = item.getElementsByTagName("span");
        
    if( spans )
    {
        for(var i = 0; i < spans.length; i++)
        {
            if( spans[i].className == "value" )
            {
                queryField.value = spans[i].innerHTML;
                lastVar = val = escape( queryField.value );
                mainLoop();
                queryField.focus();
                /*if(document.all("m_name")!=null)
                {
                   if(document.getElementById('m_name').checked==true)
                   {
                      window.location.href='search.aspx?key='+lastVar;
                   }   
                   else
                   {
                      window.location.href='searchmovie.aspx?key='+lastVar;
                    }
                }
                else  //首页
                {
                    window.location.href='search.aspx?key='+lastVar;
                }*/
                var   url   =   window.location.href.split("//")[1].split("/")[0];   
                 
                window.location.href='http://'+url+'/search.aspx?key='+lastVar;
                showDiv( false );
                
                return;
            }
        }
    }
    
}


function highlightResult()
{
    _highlightResult( this );    
}

function _highlightResult( item )
{
    item.style.backgroundColor = DIV_HIGHLIGHT_COLOR;
}
function _mouseenter(item)
{
    item.style.backgroundColor="#cc0033";
    item.style.font.color="#ffffff";
}

function unhighlightResult()
{
    _unhighlightResult( this );
}

function _unhighlightResult( item )
{
    item.style.backgroundColor = DIV_BG_COLOR;
}

function showDiv( show )
{
    var div = getDiv( divName );
    if( show )
    {
        div.style.visibility = "visible";
    }
    else
    {
        div.style.visibility = "hidden";
    }
    adjustiFrame();
}

function hideDiv()
{
    showDiv( false );
}

function keypressHandler(evt)
{
    var div = getDiv( divName );
    if( div.style.visibility == "hidden" )
    {
        return true;
    }
    if( !evt && window.event )
    {
        evt = window.event;
        
    }
    var key = evt.keyCode;
    var btn=evt.button;
    
    var KEYUP = 38;
    var KEYDOWN = 40;
    var KEYENTER = 13;
    var KEYTAB = 9;
    var mousebtn=1;
    
    if(( key != KEYUP ) && ( key != KEYDOWN ) && ( key != KEYENTER ) && ( key != KEYTAB ))
    {
        return true;
    }
    var selNum = getSelectedSpanNum( div );
    var selSpan = setSelectedSpan( div, selNum );
   
    if( key == KEYENTER || key == KEYTAB||btn==mousebtn )
    {
        
        if( selSpan )
        {
            _selectResult(selSpan);
        }
     
      /*if(document.all("m_name")!=null)
        {
           if(document.getElementById('m_name').checked==true)
           {
              window.location.href='search.aspx?key='+lastVar;
           }   
           else
           {
              window.location.href='searchmovie.aspx?key='+lastVar;
            }
        }
        else  //首页
        {
            window.location.href='search.aspx?key='+lastVar;
        }*/
       // window.location.href='search.aspx?key='+lastVar;
         var   url   =   window.location.href.split("//")[1].split("/")[0];   
                 
                window.location.href='http://'+url+'/search.aspx?key='+lastVar;

        
        
        evt.cancelBubble= true;
        return false;
    }    
    else
    {
        if( key == KEYUP)
        {
            selSpan = setSelectedSpan( div, selNum - 1 );           
        }
        if( key == KEYDOWN )
        {
            selSpan = setSelectedSpan( div, selNum + 1 );
        }
        if( selSpan )
        {
            _highlightResult( selSpan );
        }
    }
    
    showDiv( true );
    return true;
}

function getSelectedSpanNum( div )
{
    var count = -1;
    var spans = div.getElementsByTagName("div");
    //我加的
    
    if( spans )
    {
        for( var i = 0; i < spans.length; i++)
        {
            count++;
            if( spans[i].style.backgroundColor != div.style.backgroundColor )
            {
                return count;
            }
        }
    }
    
    return -1;
}
function setSelectedSpan( div, spanNum )
{
    var count = -1;
    var thisDiv;
    var divs = div.getElementsByTagName("div");
    if( divs )
    {
        for( var i = 0; i < divs.length; i++ )
        {
            if( ++count == spanNum )
            {
                _highlightResult( divs[i] );
                
                thisDiv = divs[i];
            }
            else
            {
                _unhighlightResult( divs[i] );
            }
        }        
    }
    
    return thisDiv;
}

function adjustiFrame()
{
    if(!document.getElementById(ifName))
    {
        var newNode = document.createElement("iFrame");
        newNode.setAttribute("id", ifName);
        newNode.setAttribute("src","javascript:false;");
        newNode.setAttribute("scrolling","no");
        newNode.setAttribute("frameborder","0");
        document.body.appendChild( newNode );		
    }
    iFrameDiv = document.getElementById( ifName );
    var div = getDiv( divName );    
    try
    {
        iFrameDiv.style.position = "absolute";        
        iFrameDiv.style.width = div.offsetWidth;
        iFrameDiv.style.height = div.offsetHeight;
        iFrameDiv.style.top = div.style.top;
        iFrameDiv.style.left = div.style.left;
        iFrameDiv.style.zIndex = div.style.zIndex - 1;
        iFrameDiv.style.visibility = div.style.visibility;           
    }
    catch (e)
    {}
}