var text = "";
var time_variable;
function getXMLObject(){
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch (e2) {
       xmlHttp = false;
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
     xmlHttp = new XMLHttpRequest();
   }
   return xmlHttp;
}
var xmlhttp = new getXMLObject();
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("st_translatebox").innerHTML=xmlhttp.responseText+"&nbsp;<img id=\"st_close\" src=\"modules/harvest/live_translation/templates/base/images/close.gif\" />";
	   document.getElementById("st_close").onclick = st_hideTranslateBox;
     }
   }
} 
function ajaxPost(){
	setCookie("st_lang_to",document.getElementById("st_lang_to").value,1);
  var getdate = new Date();
  if(xmlhttp) { 
    xmlhttp.open("POST","modules/harvest/live_translation/translate.php",true);
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send();
  }
}

function getCookie(c_name){
if(document.cookie.length>0){
	c_start=document.cookie.indexOf(c_name + "=");
	if (c_start!=-1){
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if(c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	}
}
return "";
}
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}
function st_initCapture(){
	var IE = /*@cc_on!@*/false;
	if(!IE){
		window.captureEvents(Event.CLICK);
		document.onmouseup = captureTranslate;
	}
	else{
		document.onmouseup = captureTranslate;
	}
}
function getSelection() {
	if(document.getSelection){
		return document.getSelection();
	}
	else if(document.selection){
		return document.selection.createRange().text;
	}
	else{
		return window.getSelection();
	}
}
function emptySelection() {
	if(document.getSelection){
		return document.getSelection().removeAllRanges();
	}
	else if(document.selection){
		return document.selection.blur();
	}
	else{
		return window.getSelection().removeAllRanges();
	}
}
function openTranslate(){
	var st_selectbox = document.getElementById("st_selectbox");
	var st_translatebox = document.getElementById("st_translatebox");
	var translate = _t('_TRANSLATE');
	if(st_translatebox){
		document.body.removeChild(st_translatebox);
	}
	var element = document.createElement("div");
	element.setAttribute("id","st_translatebox");
	element.setAttribute("style","position:absolute;left:"+st_selectbox.style.left+";top:"+st_selectbox.style.top+";");
	element.innerHTML = "<form action=\"javascript:ajaxPost();\" id=\"st_form\"><select id=\"st_lang_to\"><option  value=\"af\">Afrikaans</option><option  value=\"sq\">Albanian</option><option  value=\"ar\">Arabic</option><option  value=\"be\">Belarusian</option><option  value=\"bg\">Bulgarian</option><option  value=\"ca\">Catalan</option><option  value=\"zh-CN\">Chinese (Simplified)</option><option  value=\"zh-TW\">Chinese (Traditional)</option><option  value=\"hr\">Croatian</option><option  value=\"cs\">Czech</option><option  value=\"da\">Danish</option><option  value=\"nl\">Dutch</option><option  value=\"en\">English</option><option  value=\"et\">Estonian</option><option  value=\"tl\">Filipino</option><option  value=\"fi\">Finnish</option><option  value=\"fr\">French</option><option  value=\"gl\">Galician</option><option  value=\"de\">German</option><option  value=\"el\">Greek</option><option  value=\"iw\">Hebrew</option><option  value=\"hi\">Hindi</option><option  value=\"hu\">Hungarian</option><option  value=\"is\">Icelandic</option><option  value=\"id\">Indonesian</option><option  value=\"ga\">Irish</option><option  value=\"it\">Italian</option><option  value=\"ja\">Japanese</option><option  value=\"ko\">Korean</option><option  value=\"lv\">Latvian</option><option  value=\"lt\">Lithuanian</option><option  value=\"mk\">Macedonian</option><option  value=\"ms\">Malay</option><option  value=\"mt\">Maltese</option><option  value=\"no\">Norwegian</option><option  value=\"fa\">Persian</option><option  value=\"pl\">Polish</option><option  value=\"pt\">Portuguese</option><option  value=\"ro\">Romanian</option><option  value=\"ru\">Russian</option><option  value=\"sr\">Serbian</option><option  value=\"sk\">Slovak</option><option  value=\"sl\">Slovenian</option><option  value=\"es\">Spanish</option><option  value=\"sw\">Swahili</option><option  value=\"sv\">Swedish</option><option  value=\"th\">Thai</option><option  value=\"tr\">Turkish</option><option  value=\"uk\">Ukrainian</option><option  value=\"vi\">Vietnamese</option><option  value=\"cy\">Welsh</option><option  value=\"yi\">Yiddish</option></select><input type=\"submit\" value=\"Translate\" /></form>&nbsp;<img id=\"st_close\" src=\"modules/harvest/live_translation/templates/base/images/close.gif\" />";
	document.body.appendChild(element);
	document.getElementById("st_close").onclick = st_hideTranslateBox;
}
function captureTranslate(e){
	text = getSelection();
	if(getSelection() != "" && getSelection() != null){
		setCookie("st_query",text,1);
		st_showSelectBox();
		st_moveSelectBox(e);
		emptySelection();
	}
}
function st_moveSelectBox(e){
	var IE = /*@cc_on!@*/false;
	if(IE){
		var posx = event.x;
		var posy = event.y;
	}
	else{
		var posx = e.pageX;
		var posy = e.pageY;
	}
	var st_selectbox = document.getElementById("st_selectbox");
	st_selectbox.style.position = "absolute";
	st_selectbox.style.left = posx+"px";
	st_selectbox.style.top = posy+6+"px";
}
function st_hideSelectBox(){
	var st_selectbox = document.getElementById("st_selectbox");
	if(st_selectbox){
		document.body.removeChild(st_selectbox);
	}
}
function st_showSelectBox(){
	var st_selectbox = document.getElementById("st_selectbox");
	if(st_selectbox){
		document.body.removeChild(st_selectbox);
	}
	var element = document.createElement("div");
	element.setAttribute("id","st_selectbox");
	document.body.appendChild(element);
	element.onclick = openTranslate;
	element.onmouseout = st_hideSelectBox;
}
function st_hideTranslateBox(){
	var st_translatebox = document.getElementById("st_translatebox");
	if(st_translatebox){
		document.body.removeChild(st_translatebox);
	}
}
st_initCapture();