document.onmouseup = actOnMounseUp;

function actOnMounseUp(e) {
var str;
var url;
if(navigator.appName.indexOf("Microsoft") != -1){// is IE	//str = 'is IE';
	str = document.selection.createRange().text;
}
else {	str = document.getSelection();
}

if(str) {

	url = 'http://www.zhuyinlibrary.com/dic.php?str='+GB2312UTF8.GB2312ToUTF8(str);
	document.getElementById('dicdiv').innerHTML= 'Searching ... ...';
	ajaxGet(url,'dicdiv');
	staticbar();
}
else {	document.getElementById('dicdiv').innerHTML= 'none';
	closebar();
   }
}

function ajaxGet(url,returnDiv){
  var HTTP_get;
  try {    // Firefox, Opera 8.0+, Safari
   	HTTP_get=new XMLHttpRequest();
  }
  catch (e){    // Internet Explorer
    try{
    	HTTP_get=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try{
       	HTTP_get=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
   HTTP_get.onreadystatechange=function(){
      if(HTTP_get.readyState==4){
        if(returnDiv) document.getElementById(returnDiv).innerHTML =HTTP_get.responseText;
        }
      }
     HTTP_get.open('GET', url, true);
     HTTP_get.send(null);
}

GB2312UTF8  = {
Dig2Dec : function(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;  },
Hex2Utf8 : function(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(this.Dig2Dec(ss.substring(0,4)));
			retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
			}
		return retS;
	}
return "";
} ,
Dec2Dig : function(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;
	},
Str2Hex : function(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 += this.Dec2Dig(eval(n));
	}
	return digS;
},
GB2312ToUTF8 : function(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"){
				//alert(this.Str2Hex(sa[i].substring(1,5)));
				retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
				if(sa[i].length){    retV += sa[i].substring(5);  }
			}
			else{
				retV += unescape("%" + sa[i]);
				if(sa[i].length){    retV += sa[i].substring(5);  }
			}
		}
	return retV;
},
UTF8ToGB2312 : function(str1){
	var substr = "";
	var a = "";
	var b = "";
	var c = "";
	var i = -1;
	i = str1.indexOf("%");
	if(i==-1){
		 return str1;
	}
	while(i!= -1){
		if(i<3){
			substr = substr + str1.substr(0,i-1);
			str1 = str1.substr(i+1,str1.length-i);
			a = str1.substr(0,2);
			str1 = str1.substr(2,str1.length - 2);
			if(parseInt("0x" + a) & 0x80 == 0){
				substr = substr + String.fromCharCode(parseInt("0x" + a));
			}
			else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
				b = str1.substr(1,2);
				str1 = str1.substr(3,str1.length - 3);
				var widechar = (parseInt("0x" + a) & 0x1F) << 6;
				widechar = widechar | (parseInt("0x" + b) & 0x3F);
				substr = substr + String.fromCharCode(widechar);
				}
			else{
				b = str1.substr(1,2);
				str1 = str1.substr(3,str1.length - 3);
				c = str1.substr(1,2);
				str1 = str1.substr(3,str1.length - 3);
				var widechar = (parseInt("0x" + a) & 0x0F) << 12;
				widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
				widechar = widechar | (parseInt("0x" + c) & 0x3F);
				substr = substr + String.fromCharCode(widechar);
			}
		}
		else {
			substr = substr + str1.substring(0,i);
			str1= str1.substring(i);
		}
		i = str1.indexOf("%");
	}        return substr+str1;
}
};


/***********************************************
* Floating Top Bar script- &copy; Dynamic Drive (www.dynamicdrive.com)
* Sliding routine by Roy Whittle (http://www.javascript-fx.com/)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
//var startX = 3 //set x offset of bar in pixels
var startY = 100 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function closebar(){
if (persistclose)
document.cookie="remainclosed=1"
document.getElementById("dicdiv").style.visibility="hidden"
}

function staticbar(){
        barheight=document.getElementById("dicdiv").offsetHeight
        var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
        var d = document;
        function ml(id){
                var el=d.getElementById(id);
                if (!persistclose || persistclose && get_cookie("remainclosed")=="")
                el.style.visibility="visible"
                if(d.layers)el.style=el;
                el.sP=function(x,y){
                	//this.style.left=x+"px";
                	this.style.top=y+"px";
                	};
                //el.x = startX;
                if (verticalpos=="fromtop")
                el.y = startY;
                else{
                el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
                el.y -= startY;
                }
                return el;
        }
        window.stayTopLeft=function(){
                if (verticalpos=="fromtop"){
                var pY = ns ? pageYOffset : iecompattest().scrollTop;
                ftlObj.y += (pY + startY - ftlObj.y)/8;
                }
                else{
                var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
                ftlObj.y += (pY - startY - ftlObj.y)/8;
                }
                ftlObj.sP(ftlObj.x, ftlObj.y);
                setTimeout("stayTopLeft()", 10);
        }
        ftlObj = ml("dicdiv");
        stayTopLeft();
}

//if (window.addEventListener)
//window.addEventListener("load", staticbar, false)
//else if (window.attachEvent)
//window.attachEvent("onload", staticbar)
//else if (document.getElementById)
//window.onload=staticbar