﻿//var urlStr="http://localhost/dict/";
var urlStr="http://dict.wenguo.com/";
var txtStatus="off";
function $$$(elem)
{
   return document.getElementById(elem);
}
function toggle(id)
{
    if($$$(id).style.display=='none'){
        $$$(id).style.display="";
    }
    else{
        $$$(id).style.display="none";
    }
}
function myAddPanel(url, title, desc)
{
    if ((typeof(window.sidebar) == 'object') && (typeof(window.sidebar.addPanel) == 'function'))//Gecko
    {
        window.sidebar.addPanel(title,url,desc);
    }
    else//IE
    {
        window.external.AddFavorite(url,title);
    }
    return false;
}
function switchworddict(dict,totaldicts){
    for(var i=0;i<totaldicts;i++){
        if($$$("wordnav_"+i)!=null&&$$$("worddict_"+i)!=null){
            $$$("wordnav_"+i).className="off";
            $$$("worddict_"+i).style.display="none";
        }
        if($$$("wordnav_"+dict)!=null&&$$$("worddict_"+dict)!=null){
            $$$("wordnav_"+dict).className="on";
            $$$("worddict_"+dict).style.display="";
        }
    }
}
function searchDict(dict){
    var word=document.getElementById("txtDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    //word=EncodeUtf8(word); modify by lihuiming 2008-10-24
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
     top.location.href=urlStr+dict+"/w/"+word;
     //top.location.href=urlStr+"word.aspx?dict="+dict+"&word="+word;
}
function searchDictMini(dict){
    var word=document.getElementById("txtDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
    top.location.href=urlStr+"mini/"+dict+"/w/"+word;
    //top.location.href=urlStr+"mini/mini.aspx?dict="+dict+"&word="+word;
}
function searchDictMobil(dict){
    var word=document.getElementById("txtDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
    top.location.href=urlStr+"m/"+dict+"/w/"+word;
    //top.location.href=urlStr+"m/word.aspx?d="+dict+"&w="+word;
}
function searchDictFoot(dict){
    var word=document.getElementById("txtFootDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
    //top.location.href=urlStr+"word.aspx?dict="+dict+"&word="+word;
    top.location.href=urlStr+dict+"/w/"+word;
}
function searchDictShift(dict){
    var word=document.getElementById("txtDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
    {   
        if(typeof(__dict)=="undefined")
        {
            var dictCookie=getcookie("Vedu_Dict");
            if(dictCookie=="")
                dict="en";
            else
                dict=dictCookie;
         }
        else
            dict=__dict;
    }
    location.href=urlStr+"web/"+dict+"/w/"+word;
    //location.href=urlStr+"web/word.aspx?dict="+dict+"&word="+word;
}
function setDictFocus()
{
    if(getcookie("Vedu_initable")=="off" || (typeof(Vedu_dict_enable)!="undefined" && Vedu_dict_enable==false))
    {
        txtStatus="off";
    }else
    {
        txtStatus="on";
    }
    ShiftDictEnable(false);
}
function setDictBlur()
{
    if(txtStatus=="off")
        ShiftDictEnable(false);
    else
        ShiftDictEnable(true);
}
function KeyDownDefault(VeduEvent,PageStr)
	{
	    if(!VeduEvent){ 
	        VeduEvent= window.event;
	    }
		if(VeduEvent.keyCode==13){ 
		    if(VeduEvent.ctrlKey){ 
		        switch(PageStr)
		        {
		            case "DP":
		            case "WP":
		                searchDict('cn_jp');
		                break;
		            case "SP":
		                searchDictShift('cn_jp');
		                break;
		            case "MP":
		                searchDictMini('cn_jp');
		                break;
		        }
		    }else{
		        switch(PageStr)
		        {
		            case "DP":
		            case "WP":
		                searchDict('jp');
		                break;
		            case "SP":
		                searchDictShift('jp');
		                break;
		            case "MP":
		                searchDictMini('jp');
		                break;
		        }
		    }
	        return false;
		}
	}
function searchDictLove(dict){
    var word=document.getElementById("txtLoveDictSearch").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
    top.location.href=urlStr+"recommend.aspx?hl="+dict+"&w="+word;
}
function searchFootDictLove(dict){
    var word=document.getElementById("txtLoveFoot").value;
    if(word==""){
        alert("请输入单词");
        return;
    }
    word=encodeURI(word);
    if(dict==null)
        dict=__dict;
    top.location.href=urlStr+"recommend.aspx?hl="+dict+"&w="+word;
}
function User(id,name,pic)
{
   this.ID=id;
   this.Name=name;
   this.Pic=pic;
}
function getCurrentUser()
{
   var cookie=GetCookie("UserProfile");
   if(cookie=="")
      return null;
   else
   {
      var profile=cookie.split("&");
      var id=parseInt(profile[0].split("=")[1]);
      var name=unescape(profile[1].split("=")[1]);
      var pic=profile[2].split("=")[1];
      return new User(id,name,pic);
   }
}
function writeUserProfile()
{
   var user=getCurrentUser();
   if(user==null)
   {
       document.write("<a href='http://v.wenguo.com/login.aspx?returnurl="+location.href+"'>登录</a>&nbsp; | &nbsp;<a href='http://v.wenguo.com/reg.aspx' target=\"_blank\">注册</a>");
   }
   else
   {
      document.write("<a href='http://v.wenguo.com/user/default.aspx'>"+user.Name+"</a>&nbsp; | &nbsp;<a href='http://v.wenguo.com/logout.aspx'>退出</a>");
   }
}
function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function closeWindow()
{  
  window.open('','_parent','');  
  window.close(); 
}   
function GetCookie(name)
{
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for(var i=0;i<arrCookie.length;i++)
    {
    	var index=arrCookie[i].indexOf("=");
    	var l=arrCookie[i].length;
    	var n=arrCookie[i].substring(0,index);
    	var v="";
    	if(l>index+1)
    	  v=arrCookie[i].substring(index+1,l);
	    if (n==name) 
	    {
		    return unescape(v);
	    }
    }
    return "";
}

function SetCookie (name, value)
{
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000*60*60*365);
    document.cookie = name + "=" + value +"; domain=dict.wenguo.com; path=/; expires=" + expires.toGMTString();
}

function ShiftVisible(aObjName){
        var targetElement=$$$(aObjName);
        if (targetElement.style.display == "none" || targetElement.style.display == "") {
        targetElement.style.display = "block";
        }
        else{
        targetElement.style.display = "none";
        }
         return false;
        }
        
function ShiftCommentArea(aObj){
        ShiftVisible(aObj+"_content");
        ShiftVisible(aObj+"_hint");
        return false;
    }
function addSymbol(str)
{
      var ubb=document.getElementById("txtDictSearch");
      var ubbLength=ubb.value.length;
      ubb.focus();
      if(typeof(document.selection)!="undefined")
      {
          document.selection.createRange().text=str;  
      }
      else
      {
          ubb.value=ubb.value.substr(0,ubb.selectionStart)+str+ubb.value.substring(ubb.selectionEnd,ubbLength);
      }
}

var DictHistory=function(){

    this.histories=new Array();
}

var DictHistoryItem=function(dict,word,count){
    this.Dict=dict;
    this.Word=word;
    this.Count=count;
}

DictHistory.prototype={

    RetriveHistory:function(){
    
        this.histories.splice(0,this.histories.length);
        var cookie=GetCookie("DictHistory");
        if(cookie=="")
            return;
        var items=cookie.split("###");
        var len=items.length;
        for(var i=0;i<len;i++){
            var values=items[i].split("|||");
            if(values.length!=3)
                continue;
            this.histories.push(new DictHistoryItem(values[0],escape(values[1]),values[2]));
        }
    },

    StoreHistory:function(){
        var len=this.histories.length;
        if(len==0)
            return;
        var cookievalue="";
        for(var i=0;i<len;i++){
            cookievalue+=this.histories[i].Dict+"|||"+this.histories[i].Word+"|||"+this.histories[i].Count+"###";
        }
        SetCookie("DictHistory",cookievalue);
    },

    AddHistoryItem:function(dict,word){
        word=escape(word);
        this.RetriveHistory();
        var len=this.histories.length;
        var isExist=false;
        for(var i=0;i<len;i++){
            if(this.histories[i].Dict==dict&&this.histories[i].Word==word){
                isExist=true;
                this.histories[i].Count=parseInt(this.histories[i].Count)+1;
                var currentItem=this.histories[i];
                this.histories.splice(i,1);
                this.histories.push(currentItem);
                break;
            }
        }
        if(!isExist){
            if(len==100){
                this.histories.splice(0,1);
            }
            this.histories.push(new DictHistoryItem(dict,word,1));
        }
        this.StoreHistory();
    },

    ToString:function(){
        this.RetriveHistory();
        var len=this.histories.length;
        if(len==0){
            document.write("<br/>暂无记录<br/>");
            return;
        }
        document.write("<ul>");
        for(var i=len-1;i>=0&&i>=len-10;i--){
            document.write("<li><a href='/"+this.histories[i].Dict+"/w/"+unescape(this.histories[i].Word)+"' target='_blank'>"+unescape(this.histories[i].Word)+"</a></li>");
        }
        document.write("</ul>")

    }

}
var DictHistoryObject=new DictHistory();


function flash(url, w, h, vars){
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ w +'" height="'+ h +'">');
    document.write('<param name="movie" value="' + url + '"><param name="quality" value="high">');
    document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
    document.write('<param name="loop" value="true">');
    document.write('<param name="FlashVars" value="' + vars + '">');
    document.write('<embed src="' + url + '" width="'+ w +'" height="'+ h +'" wmode="opaque" loop="true" FlashVars="' + vars + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'); document.write

('</object>'); 
}

function attachSpeaker(dict,file)
{
    var folder=file.substr(0,1)+"/"+file.substr(1,1);
    flash("http://dict.wenguo.com/flash/dictspeaker.swf",19,19,"soundfile=http://dict.wenguo.com/file/mp3/"+dict+"/"+folder+"/"+file+".mp3");
}


if(window.HTMLElement){
    HTMLElement.prototype.__defineGetter__ 
   ( 
     "innerText", 
      function (){ 
      var anyString = ""; 

      var childS = this.childNodes; 
      for(var i=0; i<childS.length;i++){ 
        if(childS[i].nodeType==1) 
           anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText; 
        else if(childS[i].nodeType==3) 
           anyString += childS[i].nodeValue; 
      } 
      return anyString; 
     } 
   ); 
}

function copyTrans(dictnum)
{
    var dict=$$$("worddict_"+dictnum);
    window.clipboardData.setData("Text",dict.innerText);
    alert("完成复制解释！");
}

function printTrans(dictnum)
{
    var dict=$$$("worddict_"+dictnum);
    dict.removeChild(dict.childNodes[0]);
    document.body.innerHTML="<div class='content'><div id='wordcontent'><div id='worddict_"+dictnum+"'>"+dict.innerHTML+"</div></div></div>";
    window.print();
}

//--------BEGIN---把中文字符转换成Utf8编码------------------------//
function EncodeUtf8_Pingbi(s1)
{
  var s = escape(s1);
  var sa = s.split("%");
  var retV ="";
  if(sa[0] != "")
  {
     retV = sa[0];
  }
  for(var i = 1; i < sa.length; i ++)
  {
       if(sa[i].substring(0,1) == "u")
       {
           retV += Hex2Utf8(Str2Hex(sa[i].substring(1,5)));
           
       }
       else retV += "%" + sa[i];
  }
  
  return retV;
}
function Str2Hex(s)
{
  var c = "";
  var n;
  var ss = "0123456789ABCDEF";
  var digS = "";
  for(var i = 0; i < s.length; i ++)
  {
     c = s.charAt(i);
     n = ss.indexOf(c);
     digS += Dec2Dig(eval(n));
       
  }
  //return value;
  return digS;
}
function Dec2Dig(n1)
{
  var s = "";
  var n2 = 0;
  for(var i = 0; i < 4; i++)
  {
     n2 = Math.pow(2,3 - i);
     if(n1 >= n2)
     {
        s += '1';
        n1 = n1 - n2;
      }
     else
      s += '0';
      
  }
  return s;
  
}
function Dig2Dec(s)
{
  var retV = 0;
  if(s.length == 4)
  {
      for(var i = 0; i < 4; i ++)
      {
          retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
      }
      return retV;
  }
  return -1;
} 
function Hex2Utf8(s)
{
 var retS = "";
 var tempS = "";
 var ss = "";
 if(s.length == 16)
 {
     tempS = "1110" + s.substring(0, 4);
     tempS += "10" +  s.substring(4, 10); 
     tempS += "10" + s.substring(10,16); 
     var sss = "0123456789ABCDEF";
     for(var i = 0; i < 3; i ++)
     {
        retS += "%";
        ss = tempS.substring(i * 8, (eval(i)+1)*8);
        
        
        
        retS += sss.charAt(Dig2Dec(ss.substring(0,4)));
        retS += sss.charAt(Dig2Dec(ss.substring(4,8)));
     }
     return retS;
 }
 return "";
}
//--------END---把中文字符转换成Utf8编码------------------------//



