var isIE=document.all?true:false;
function $(vid){return document.getElementById(vid);}
function googleHint(key){
  if($('gsuggest'))$('gsuggest').removeNode(true);
  if (document.readyState=="complete"){
    var sg=document.body.appendChild(document.createElement('script'));
    sg.language='javascript';
    sg.id='gsuggest';
    sg.charset='utf-8';
    
   }
}
var searchurl_bd,searchurl_g;
searchurl_bd={
'web':'http://www.baidu.com/s?tn=nviexs_pg&ct=&lm=-1&kw=&word='
,'mp3':'http://mp3.baidu.com/m?tn=nviexs_pg&ct=134217728&lm=-1&word='
,'img':'http://image.baidu.com/i?tn=nviexs_pg&ct=201326592&lm=-1&word='
,'v':'http://video.baidu.com/v?tn=nviexs_pg&ct=301989888&rn=20&word='
,'other':'http://zhidao.baidu.com/q?tn=ikaslist&ct=17&lm=&word='
};

searchurl_g={
'web':'/gg/'
,'mp3':'http://mp3.gougou.com/search?id=51299&mimetype=all&search='
,'img':'http://image.baidu.com/i?tn=nviexs_pg&ct=201326592&lm=-1&word='
,'v':'http://video.google.cn/videosearch?ie=gb2312&q='
,'other':'http://www.yahoo.cn/s?pid=hp&v=web&p='
};


var searchurl = searchurl_bd['web'];
showobj = $('q');
function bdhint(){$('q').value=$('q1').value; myhint(event,$('q1'));searchurl=searchurl_bd[currTab];}
function ghint(){$('q1').value=$('q').value; myhint(event,$('q')); searchurl=searchurl_g[currTab];}
function mover(ob){ob.focus();ob.style.backgroundColor="#FFFFD8";}
function mout(ob){ob.style.backgroundColor="#FFFFFF";}
function mfocus(ob){ob.select();}


function UrlEncode(str){
    var ret="";
    var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        var c=str2asc(chr);
        if(parseInt("0x"+c) > 0x7f){
            ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
        }else{
            if(chr==" "){
                ret+="+";
            }else if(strSpecial.indexOf(chr)!=-1){
                ret+="%"+c.toString(16);
            }else{
                ret+=chr;
            }
        }
    }
    return ret;
}

function myhint(event,obj)
{
   showobj=obj;
   if(!isIE)  return;
   var keyword=showobj;
   var h=$('suggest');
   if(!keyword.value || !keyword.value.length || event.keyCode==27 || event.keyCode==13){
       h.style.display='none';
       return;
   }
   if(event.keyCode==38 || event.keyCode==40){
     if(h.style.display=='none') return;
       if(event.keyCode==38){
         if(h._i==-1)h._i=h.firstChild.rows.length-1;
         else{
	     h._i--;
         } 
      }else{
         h._i++;
      } 
    for(var i=0;i<h.firstChild.rows.length;i++)h.firstChild.rows[i].className="trout";
      if(h._i >= 0 && h._i < h.firstChild.rows.length)with(h.firstChild.rows[h._i]){
        className="trover";
		tmp=cells[0].innerHTML.split("</EM>");
        keyword.value=tmp[1].replace("</A>",'');
      }else{
        keyword.value=h._kw;
        h._i=-1;
      } 
    }else{
      h._i=-1;
      h._kw=keyword.value;
      googleHint(keyword.value);
      var pos=getPosition(keyword);
		//alert(pos.y);
      with(h.style){
        left=pos.x;
        top=pos.y+keyword.offsetHeight;
        width=keyword.offsetWidth - 1;
      }
    } 
}
window.google={} ;
window.google.ac={} ;
window.google.ac.Suggest_apply=function(a,b,c,d){
 if(!c || c.length<3) return;
 if(b != showobj.value) return;
 var ihtml='';
 if (document.readyState=="complete")
 {
	for(var j=1;j<c.length;j+=2){
		url=searchurl+UrlEncode(c[j]);
		ihtml+='<tr onmouseover="this.className=\'trover\';" onmouseout="this.className=\'trout\';">';
		ihtml+='<td>';
		ihtml+='<a href="'+url+'" target="_blank" onclick="$(\'suggest\').style.display=\'none\';"><em>' +c[j+1] +'</em>' +c[j] +'</a>';
		ihtml+='</td></tr>';
	}
	$('suggest').innerHTML='<table width="100%" class="suggestTable">'+ihtml+'</table>';
	$('suggest').style.display="block";
	//alert($('suggest').innerHTML);
  }else{
	  //alert('');
    //setTimeout("myhint(event)", 2000);
  }
};
function getPosition(ele){
	var overflown = [];
	var el = ele, left = 1, top = 2;
	do {
		left += el.offsetLeft || 0;
		top += el.offsetTop || 0;
		el = el.offsetParent;
	} while (el);
	return {'x': left, 'y': top};
}