var dictinfo_is_ie = true;
var dictinfo_host = 'http://www.stardict.org/';
var dictinfo_help = 'http://www.stardict.org/SelectHelp.php';
var dictinfo_enable = true;
var dictinfo_old_word = "";
var dictinfo_moving = 0;
var dictinfo_onmove = 0;
var dictinfo_onlayer = 0;
var dictinfo_startx = 0;
var dictinfo_starty = 0;
var dictinfo_cx = 0;
var dictinfo_cy = 0;
var dictinfo_x = 0;
var dictinfo_y = 0;
var dictinfo_layer;
var dictinfo_iframe;
var uid;
var bookname;

function dictinfoInit(){
  var infoagt = navigator.userAgent.toLowerCase();
  dictinfo_is_ie = (infoagt.indexOf("msie")!=-1 && document.all);
  var info_h = '<div id="dictinfo_layer" style="position:absolute;z-index:6000;display:none;background-color:#FFFEEE;';
  if (dictinfo_is_ie) 
  		info_h += 'filter:Alpha(Opacity=96);';
  info_h += '"><table width="250" cellspacing="0" cellpadding="0" ';
  info_h += 'style="border-top:1px solid #666666;border-left:1px solid #666666;';
  info_h += 'border-right:1px solid #666666;border-bottom:1px solid #666666;';
  info_h += '"><tr><td>';
  info_h += '<div width="100%" style="cursor:move;background-color:#FFFFFF;border:0px;" onmouseover="dictinfo_onmove=1;" onmouseout="dictinfo_onmove=0;">' ;
  info_h += '<table width="100%"><tr><td align="left" width="83%" style="background-color:#666666;">';
  info_h += '<div style="color:#ffffff;font-size:14px;background-color:#666666;">Dictionary Info-<b>StarDict.org</b></div>';
  info_h += '</td>';
  info_h += '<td align="right" style="background-color:#666666;">';
  info_h += '<a href="'+dictinfo_help+'" target="_blank" title="Help">';
  info_h += '<img src="'+dictinfo_host+'images/help.gif" style="border:none;display:inline;" align="absmiddle" />';
  info_h += '</a> '; 
  info_h += '<a href="javascript:dictinfoClose()" title="Close">';
  info_h += '<img src="'+dictinfo_host+'images/close.gif" style="border:none;display:inline;" align="absmiddle" />';
  info_h += '</a>';
  info_h += '</td></tr></table>';
  info_h += '</div>';
  
  info_h += '<table border="0" cellspacing="4" cellpadding="3" width="100%" align="center" onmouseover="dictinfo_onlayer=1;" onmouseout="dictinfo_onlayer=0;">';
  info_h += '<tr><td><fieldset color="#00c0ff">';
  info_h += '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">';
  info_h += '<tr><td width="100%">';
  info_h += '<div id=\"adddict\"></div>';
  info_h += '<iframe id="dictinfoFrame" name="dictinfoFrame" HEIGHT="220" src="about:blank" FRAMEBORDER="0" width="100%"></iframe>';
  //info_h += '<br/><a href=\"#\" onMouseOver=\"this.style.cursor=\'hand\'\"  onclick=\"addDict1(\''+ uid +'\',\''+bookname+'\',\'\')\">Add List of Dictionary</a>';
  info_h += '</td></tr><tr align="center"><td width="100%" height="20">';
  info_h += '<font color="#000000" size="1" face="Arial">&copy;2003-2007 ';
  info_h += '<a href="http://www.stardict.org" target="_blank"><b><font color="#2EA8ED">StarDict.org</font></b></a> On-line Dictionaries.</font>';
  info_h += '</td></tr></table></fieldset></td></tr></table>';
  info_h += '</td></tr></table></div>';
  document.write(info_h);

  dictinfo_layer = document.getElementById('dictinfo_layer');
  dictinfo_iframe = document.getElementById('dictinfoFrame');
  dictinfoClose();
  if (dictRCookie("dictinfostate") == '1' && dictinfo_enable) dictinfo_enable = false;
  dictinfoUpdateStatus();

  
  if (dictinfo_is_ie) {
    document.attachEvent("onmousemove", dictMove);
    document.attachEvent("ondblclick", dictQuery);
    document.attachEvent("onmouseup", dictQuery);
    document.attachEvent("onselectstart", dictinfoSelect);
    document.attachEvent("onmousedown", dictinfoCheck);
    window.attachEvent("onload", dictinfoUpdateStatus);
  }else {
    document.addEventListener("mousemove", dictMove, true);
    document.addEventListener("dblclick", dictQuery, true);
    document.addEventListener("mouseup", dictQuery, true);
    document.addEventListener("selectstart", dictinfoSelect, true);
    document.addEventListener("mousedown", dictinfoCheck, true);
    window.addEventListener("load", dictinfoUpdateStatus, true);
  }
  var img = new Image();
  img.src = dictinfo_host+"images/loading.gif";
}

function dictinfoDisplay(){
    var dx=250;
    var dy=252;
    dictinfo_y += 8;
    dictinfo_x += 16;
    if(dictinfo_is_ie){
        if (document.documentElement.offsetHeight && document.body.scrollTop+document.documentElement.scrollTop+document.documentElement.offsetHeight - dictinfo_y < dy){
            dictinfo_y = document.body.scrollTop+document.documentElement.scrollTop + document.documentElement.offsetHeight - dy;
            dictinfo_x += 14;
       				 }
        if (document.documentElement.offsetWidth && document.body.scrollLeft+document.documentElement.scrollLeft+document.documentElement.offsetWidth - dictinfo_x < dx){
            dictinfo_x = document.body.scrollLeft+document.documentElement.scrollLeft + document.documentElement.offsetWidth - dx;
        			 }
    }else{
        dx-=1;
        dy+=11;
        if (self.innerHeight && document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dictinfo_y < dy) {
            dictinfo_y = document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dy;
            dictinfo_x += 14;
        			 }
        if (self.innerWidth && document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dictinfo_x < dx) {
            dictinfo_x = document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dx;
        			 }
    	   }
    dictinfo_cx = dictinfo_x;
    dictinfo_cy = dictinfo_y;
    dictinfo_startx = dictinfo_x;
    dictinfo_starty = dictinfo_y;
    dictinfo_layer.style.left = dictinfo_cx+'px';
    dictinfo_layer.style.top = dictinfo_cy+'px';
    dictinfo_layer.style.display="";
    dictinfo_moving = 1;
}

function ShowDictInfo1(word, book){
			if(word==null || word==""){
				return;
			}
			if(book==null || book==""){
				return;
			}
			uid = word;
			bookname = book;
			$("adddict").innerHTML = "<br/><a href=\"#\" onMouseOver=\"this.style.cursor=\'hand\'\"  onclick=\"addDict1(\''+ uid +'\',\''+bookname+'\',\'\')\">Add List of Dictionary</a>";
    dictinfoDisplay();
    try{
        dictinfo_iframe.src='about:blank';
        iframeWin = window.frames.dictinfoFrame;
        iframeWin.document.open();
        iframeWin.document.write('<html><body><img src="'+dictinfo_host+'images/loading.gif" /><b><font color="#666666">Querying <font color="green">'+word+'</font>...</b></font></body></html>');
        iframeWin.document.close();
    }catch(x){
    		}
    var u=dictinfo_host+'dictinfo.php?';
    u += 'uid='+word;
    setTimeout(function(){dictinfo_iframe.src=u;},250);
    dictinfo_old_word = word;
}

function ShowDictInfo0(word, book){
			if(word==null || word==""){
				return;
			}
			if(book==null || book==""){
				return;
			}
			uid = word;
			bookname = book;
			$("adddict").innerHTML = "<br/><a href=\"#\" onMouseOver=\"this.style.cursor=\'hand\'\"  onclick=\"addDict(\''+ uid +'\',\''+bookname+'\',\'\')\">Add List of Dictionary</a>";
    dictinfoDisplay();
    try{
        dictinfo_iframe.src='about:blank';
        iframeWin = window.frames.dictinfoFrame;
        iframeWin.document.open();
        iframeWin.document.write('<html><body><img src="'+dictinfo_host+'images/loading.gif" /><b><font color="#666666">Querying <font color="green">'+word+'</font>...</b></font></body></html>');
        iframeWin.document.close();
    }catch(x){
    		}
    var u=dictinfo_host+'dictinfo.php?';
    u += 'uid='+word;
    setTimeout(function(){dictinfo_iframe.src=u;},250);
    dictinfo_old_word = word;
}

function dictinfoClose() {
    try
    {
        dictinfo_moving = 0;
        dictinfo_onmove = 0;
        dictinfo_onlayer = 0;
					 dictinfo_layer.style.display="none";
        setTimeout(function(){dictinfo_old_word="";},500);
    	if(window.sf) sf();
    }
    catch (x)
    {
    }
}
function dictinfoUpdateStatus(){
  var el = document.getElementById('dictinfo_status');
  if(el){
    el.innerHTML = dictinfoStatus();
  }
}

function dictinfoStatus(){
    if (dictinfo_enable){
       return '[<a href="'+dictinfo_help+'" title="Show &quot;Select and Translate&quot; help" target="_blank">Translate</a>: <a href="javascript:dictinfoEnable()" title="Disable &quot;Select and Translate&quot;">Enable</a>]';
    }else{
      return '[<a href="'+dictinfo_help+'" title="Show &quot;Select and Translate&quot; help" target="_blank">Translate</a>: <a href="javascript:dictinfoEnable()" title="Enable &quot;Select and Translate&quot;">Disable</a>]';
    }
}

function dictinfoMove(e){
    if(dictinfo_moving==2) {
        dictinfoGetPos(e);
        dictinfo_x = dictinfo_x-dictinfo_startx+dictinfo_cx;
        dictinfo_y = dictinfo_y-dictinfo_starty+dictinfo_cy;
        if (document.documentElement.scrollWidth - dictinfo_x < 262) {
            dictinfo_x = document.documentElement.scrollWidth - 262;
        			 }
        dictinfo_layer.style.left = dictinfo_x+'px';
        dictinfo_layer.style.top = dictinfo_y+'px';
    	  }    
}

function dictinfoGetPos(event){
  if (dictinfo_is_ie) {
    dictinfo_x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    dictinfo_y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }else {
    dictinfo_x = event.clientX + window.scrollX;
    dictinfo_y = event.clientY + window.scrollY;
  }
}

function dictinfoGetSel()
{
	if (window.getSelection) return window.getSelection();
	else if (document.getSelection) return document.getSelection();
	else if (document.selection) return document.selection.createRange().text;
	else return '';
}

function dictinfoSelect(e){
    if (dictinfo_moving == 2)
        return false;
    else
        return true;
}


function dictinfoCheck(e) {
    dictinfoGetPos(e);
    var cx = 0;
    var cy = 0;
    var obj = dictinfo_layer;
    if (obj.offsetParent){
        while (obj.offsetParent){
            cx += obj.offsetLeft;
            cy += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }else if (obj.x){
        cx += obj.x;
        cy += obj.y;
    }

    if(dictinfo_moving>0){
        if(dictinfo_onmove == 1){
            dictinfo_moving = 2;
            dictinfo_startx = dictinfo_x;
            dictinfo_starty = dictinfo_y;
        }else if(dictinfo_x < cx || dictinfo_x > (cx + 240) || dictinfo_y < cy || (!dictinfo_onlayer && dictinfo_y > (cy + 100) ) ){
	    dictinfoClose();
        }else{
            
            dictinfo_moving = 1;
        }
    }
    
}

function dictinfoQuery(e)  {
    if(dictinfo_moving == 1){
        if (dictinfo_is_ie) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }else{
            e.preventDefault();
        }
        return false;
    }
    if(dictinfo_moving == 2) {
        dictinfo_cx = dictinfo_x;
        dictinfo_cy = dictinfo_y;
        dictinfo_moving = 1;
        return false;
    }

    dictinfoGetPos(e);
    if (!dictinfo_enable) return true;

    var word = dictinfoGetSel();
    if(dictinfo_is_ie) word=word.replace(/^\s*|\s*$/g,"");
    word=""+word;
    if(word == "" || word.length > 16 || word == dictinfo_old_word) return true;

    dictShow(word);

}


function dictinfoEnable(){
  if (dictinfo_enable){
    dictinfo_enable = false;
    dictWCookie("dictinfostate", '1');
  }else{
    dictinfo_enable = true;
    dictWCookie("dictinfostate", '0');
  }
  dictinfoUpdateStatus();
}
