//Copyright Stardict.org & TuDienTiengViet.Net
var dict_is_ie = true;
var dict_host = 'http://www.tudientiengviet.net/';
var dict_help = 'http://www.tudientiengviet.net/';
//var dict_host = 'http://localhost/OVDP2/';
//var dict_help = 'http://localhost/OVDP2/';
var dict_enable = true;
var dict_old_word = "";
var dict_moving = 0;
var dict_onmove = 0;
var dict_onlayer = 0;
var dict_startx = 0;
var dict_starty = 0;
var dict_cx = 0;
var dict_cy = 0;
var dict_x = 0;
var dict_y = 0;
var dict_layer;
var dict_iframe;
var dict_key = false;
var dict_dict = '';
//var dict_call_uncheck = true;

function dictInit(default_dict){
var agt = navigator.userAgent.toLowerCase();
  dict_is_ie = (agt.indexOf("msie")!=-1 && document.all);
  var h = '<div id="dict_layer" style="position:absolute;z-index:6000;display:none;background-color:#FFFFFF;';
  if (dict_is_ie) h += 'filter:Alpha(Opacity=96);';
  h += '"><table width="430" cellspacing="0" cellpadding="0" ';
  h += 'style="border-top:1px solid #cc0000;border-left:1px solid #cc0000;';
  h += 'border-right:1px solid #cc0000;border-bottom:1px solid #cc0000;';
  h += '"><tr><td>';
  h += '<div width="100%" style="cursor:move;background-color:#CC0000;border:0px;" onmouseover="dict_onmove=1;" onmouseout="dict_onmove=0;">' ;
  h += '<table width="100%"><tr><td align="left" width="80%" style="background-color:#CC0000;">';
  h += '<div style="font-weight: bold;color:#FFFFFF;font-size:14px;background-color:#CC0000;">TuDienTiengViet.Net</div>';
  h += '</td>';
  h += '<td align="right" style="background-color:#CC0000;">';
  h += '<a href="'+dict_help+'" target="_blank" title="Help">';
  h += '<img src="'+dict_host+'images/help.gif" style="border:none;display:inline;" align="absmiddle" />';
  h += '</a> ';
  h += '<a href="javascript:dictClose()" title="Close">';
  h += '<img src="'+dict_host+'images/close.gif" style="border:none;display:inline;" align="absmiddle" />';
  h += '</a>';
  h += '</td></tr></table>';
  h += '</div>';
  
  h += '<table border="0" cellspacing="4" cellpadding="3" width="100%" align="center" onmouseover="dict_onlayer=1;" onmouseout="dict_onlayer=0;">';
  h += '<tr><td><fieldset color="#00c0ff">';
  h += '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">';
  h += '<tr><td width="100%">';
  h += '<iframe id="dictFrame" name="dictFrame" HEIGHT="220" src="about:blank" FRAMEBORDER="0" width="100%"></iframe>';
  h += '</td></tr><tr align="center"><td width="100%" height="20">';
  h += '<font color="#000000" size="1" face="Arial">&copy;2007 ';
  h += '<a href="http://www.tudientiengviet.net" target="_blank"><b><font color="#2EA8ED">Open Vietnamese Dictionaries Project</font></b></a></font>';
  h += '</td></tr></table></fieldset></td></tr></table>';
  h += '</td></tr></table></div>';
  document.write(h);

  dict_layer = document.getElementById('dict_layer');
  dict_iframe = document.getElementById('dictFrame');
  dictClose();
  if (dictRCookie("dictstate") == '1' && dict_enable) dict_enable = false;
  dictUpdateStatus();
  dict_dict = (default_dict == '') ? 'en-vi' : default_dict;
  //alert(dict_dict);
  dictChoose(dict_dict);
  dictOptions();
  if (dict_is_ie) {
    document.attachEvent("onmousemove", dictMove);
    document.attachEvent("ondblclick", dictQuery);
    document.attachEvent("onkeydown", dictKey);
    document.attachEvent("onmouseup", dictUnCheck);
    document.attachEvent("onselectstart", dictSelect);
    document.attachEvent("onmousedown", dictCheck);
    window.attachEvent("onload", dictUpdateStatus);
  }else {
    document.addEventListener("mousemove", dictMove, true);
    document.addEventListener("dblclick", dictQuery, true);
    document.addEventListener("keydown", dictKey, true);
    document.addEventListener("mouseup", dictUnCheck, true);
    document.addEventListener("selectstart", dictSelect, true);
    document.addEventListener("mousedown", dictCheck, true);
    window.addEventListener("load", dictUpdateStatus, true);
  }
  var img = new Image();
  img.src = dict_host+"images/indicator.gif";
}

function dictGetSel()
{
	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 dictGetPos(event){
  if (dict_is_ie) {
    dict_x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    dict_y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }else {
    dict_x = event.clientX + window.scrollX;
    dict_y = event.clientY + window.scrollY;
  }
//  alert(dict_x);
}


function dictSelect(e){
    if (dict_moving == 2)
        return false;
    else
        return true;
}

function dictCheck(e) {
    dictGetPos(e);
    var cx = 0;
    var cy = 0;
    var obj = dict_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(dict_moving>0){
        if(dict_onmove == 1){
            dict_moving = 2;
            dict_startx = dict_x;
            dict_starty = dict_y;
        }else if(dict_x < cx || dict_x > (cx + 240) || dict_y < cy || (!dict_onlayer && dict_y > (cy + 100) ) ){
	    dictClose();
        }else{
            
            dict_moving = 1;
        }
    }
    
}

function dictUnCheck(e)  {
    if(dict_moving == 1){
        if (dict_is_ie) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }else{
            e.preventDefault();
        }
        return false;
    }
    if(dict_moving == 2) {
        dict_cx = dict_x;
        dict_cy = dict_y;
        dict_moving = 1;
        return false;
    }
}

function dictQuery(e)  {
	dictUnCheck(e);
	if (!dict_key)
    {
    	dictGetPos(e);
    }
    else
    {
    	dict_x=10;
    	dict_y=10;
    }
    dict_key = false;
    if (!dict_enable) return true;

    var word = dictGetSel();
    if(dict_is_ie) word=word.replace(/^\s*|\s*$/g,"");
    word=""+word;
    if(word == "" || word.length > 16 || word == dict_old_word) return true;

    dictShow(word);

}

function dictDisplay(){
    var dx=262;
    var dy=264;
    dict_y += 8;
    dict_x += 16;
    if(dict_is_ie){
        if (document.documentElement.offsetHeight && document.body.scrollTop+document.documentElement.scrollTop+document.documentElement.offsetHeight - dict_y < dy){
            dict_y = document.body.scrollTop+document.documentElement.scrollTop + document.documentElement.offsetHeight - dy;
            dict_x += 14;
        }
        if (document.documentElement.offsetWidth && document.body.scrollLeft+document.documentElement.scrollLeft+document.documentElement.offsetWidth - dict_x < dx){
            dict_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 - dict_y < dy) {
            dict_y = document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dy;
            dict_x += 14;
        }
        if (self.innerWidth && document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dict_x < dx) {
            dict_x = document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dx;
        }
    }
    dict_cx = dict_x;
    dict_cy = dict_y;
    dict_startx = dict_x;
    dict_starty = dict_y;
    dict_layer.style.left = dict_cx+'px';
    dict_layer.style.top = dict_cy+'px';
    dict_layer.style.display="";
    dict_moving = 1;
}

function dictShow(word){
    dictDisplay();
    try{
        dict_iframe.src='about:blank';
        iframeWin = window.frames.dictFrame;
        iframeWin.document.open();
        iframeWin.document.write('<html><body><img src="'+dict_host+'images/indicator.gif" /><b><font color="#666666">Đang tra cứu từ <font color="green">'+word+'</font>...</b></font></body></html>');
        iframeWin.document.close();
    }catch(x){
    }
    var u=dict_host+'quickdict.php?';
    u += 'word='+encodeURIComponent(word);
    el_dict = document.getElementById('dict_list');    
    if (el_dict)
    {
    	u += '&dict='+encodeURIComponent(el_dict.value);
    }
    else 
    {
    	u += '&dict=en-vi';
    }

    setTimeout(function(){dict_iframe.src=u;},250);
    dict_old_word = word;
}

function dictMove(e){
    if(dict_moving==2) {
        dictGetPos(e);
        dict_x = dict_x-dict_startx+dict_cx;
        dict_y = dict_y-dict_starty+dict_cy;
        if (document.documentElement.scrollWidth - dict_x < 262) {
            dict_x = document.documentElement.scrollWidth - 262;
        }
        dict_layer.style.left = dict_x+'px';
        dict_layer.style.top = dict_y+'px';
    }
}

function dictClose() {
    try
    {
        dict_moving = 0;
        dict_onmove = 0;
        dict_onlayer = 0;
    	dict_layer.style.display="none";
        setTimeout(function(){dict_old_word="";},500);
    	if(window.sf) sf();
    }
    catch (x)
    {
    }

}

function dictWCookie(name,value)
{
    var date=new Date();
    var now=date.getTime();
    date.setTime(now+365*24*60*60*1000);
    document.cookie=name+"="+value+"; path=/; expires="+date.toGMTString();
}

function dictRCookie(name)
{
    var cookie=String(document.cookie);
    var pos=cookie.indexOf(name+"=");
    if(pos!=-1){
        var end=cookie.indexOf("; ",pos);
        return cookie.substring(pos+name.length+1,end==-1?cookie.length:end);
    }
    return "";
}

function dictEnable(){
  if (dict_enable){
    dict_enable = false;
    dictWCookie("dictstate", '1');
  }else{
    dict_enable = true;
    dictWCookie("dictstate", '0');
  }
  dictUpdateStatus();
}

function dictUpdateStatus(){
  var el = document.getElementById('dict_status');
  if(el){
    el.innerHTML = dictStatus();
  }
}

function dictStatus(){
    if (dict_enable){
       return '[Tra nhanh: <a href="javascript:dictEnable()" title="Disable &quot;Select and Translate&quot;">Đang bật</a>]';
    }else{
      return '[Tra nhanh: <a href="javascript:dictEnable()" title="Enable &quot;Select and Translate&quot;">Đang tắt</a>]';
    }
}

function dictSet(){
	dict_dict = document.getElementById('dict_list').value;
//	alert(dict_dict);
	return true;
}

function dictChoose(default_dict){
	var el = document.getElementById('dict_choose');
	if (el){
		var h= '[Dictionary : <select name="dict_list" id="dict_list" onchange="dictSet();">';
		if (dict_dict == 'en-vi')
			h += '<option value="en-vi" selected>English - Vietnamese</option>';
		else
			h += '<option value="en-vi">English - Vietnamese</option>';
		
		if (dict_dict == 'vi-en')
			h += '<option value="vi-en" selected>Vietnamese - English</option>';
		else
			h += '<option value="vi-en">Vietnamese - English</option>';
		
		if (dict_dict == 'de-vi')
			h += '<option value="de-vi" selected>German  - Vietnamese</option>';
		else
			h += '<option value="de-vi">German  - Vietnamese</option>';

		if (dict_dict == 'vi-de')
			h += '<option value="vi-de" selected>Vietnamese - German</option>';
		else
			h += '<option value="vi-de">Vietnamese - German</option>';

		if (dict_dict == 'fr-vi')
			h += '<option value="fr-vi" selected>French  - Vietnamese</option>';
		else
			h += '<option value="fr-vi">French  - Vietnamese</option>';

		if (dict_dict == 'vi-fr')
			h += '<option value="vi-fr" selected>Vietnamese - French</option>';
		else
			h += '<option value="vi-fr">Vietnamese - French</option>';

		if (dict_dict == 'ru-vi')
			h += '<option value="ru-vi" selected>Russian - Vietnamese</option>';
		else
			h += '<option value="ru-vi">Russian - Vietnamese</option>';

		if (dict_dict == 'cn-vi')
			h += '<option value="cn-vi" selected>Chinese  - Vietnamese</option>';
		else
			h += '<option value="cn-vi">Chinese  - Vietnamese</option>';

		if (dict_dict == 'jp-vi')
			h += '<option value="jp-vi" selected>Japanese - Vietnamese</option>';
		else
			h += '<option value="jp-vi">Japanese - Vietnamese</option>';

		if (dict_dict == 'vi-jp')
			h += '<option value="vi-jp" selected>Vietnamese - Japanese</option>';
		else
			h += '<option value="vi-jp">Vietnamese - Japanese</option>';

		if (dict_dict == 'kr-vi')
			h += '<option value="kr-vi" selected>Korean - Vietnamese</option>';
		else
			h += '<option value="kr-vi">Korean - Vietnamese</option>';

		if (dict_dict == 'vi-kr')
			h += '<option value="vi-kr" selected>Vietnamese - Korean</option>';
		else
			h += '<option value="vi-kr">Vietnamese - Korean</option>';

		if (dict_dict == 'cz-vi')
			h += '<option value="cz-vi" selected>Czech - Vietnamese</option>';
		else
			h += '<option value="cz-vi">Czech - Vietnamese</option>';

		if (default_dict == 'es-vi')
			h += '<option value="es-vi" selected>Spanish - Vietnamese</option>';
		else
			h += '<option value="es-vi">Spanish - Vietnamese</option>';

		if (dict_dict == 'vi-es')
			h += '<option value="vi-es" selected>Vietnamese - Spanish</option>';
		else
			h += '<option value="vi-es">Vietnamese - Spanish</option>';

		if (dict_dict == 'no-vi')
			h += '<option value="no-vi" selected>Norwegian - Vietnamese</option>';
		else
			h += '<option value="no-vi">Norwegian - Vietnamese</option>';

		if (dict_dict == 'pt-vi')
			h += '<option value="pt-vi" selected>Portuguese - Vietnamese</option>';
		else
			h += '<option value="pt-vi">Portuguese - Vietnamese</option>';

		h += '</select>]';
		el.innerHTML = h;
	}
}

function dictUpdateFunction()
{
	var el = document.getElementById('dict_functions').value;
	if (el == 1)
	{
		if (dict_is_ie)
		{
			document.detachEvent("onmouseup",dictUnCheck);
			document.attachEvent("onmouseup",dictQuery);
		}
		else
		{
		    document.removeEventListener("mouseup", dictUnCheck, true);
		    document.addEventListener("mouseup", dictQuery, true);
		}
	}
	else
	{
		if (dict_is_ie)
		{
			document.detachEvent("onmouseup",dictQuery);
			document.attachEvent("onmouseup",dictUnCheck);
		}
		else
		{
		    document.removeEventListener("mouseup", dictQuery, true);
		    document.addEventListener("mouseup", dictUnCheck, true);
		}
	}
}

function dictOptions(){
	var el = document.getElementById('dict_options');
	if (el){
		var h= '[Options : <select name="dict_functions" id="dict_functions" onchange="dictUpdateFunction()">';
		h += '<option value="1">Auto + Double click</option>';
		h += '<option value="2" selected>Ctrl-Shift-A + Double click</option>';
		h += '</select>]';
		el.innerHTML = h;
	}
}

function dictKey(event) 
{
	if(event.type == 'keydown' && ( (event.keyCode == "A".charCodeAt(0)) || (event.keyCode == "a".charCodeAt(0)) ) )
	{
		if ( (event.ctrlKey) && (event.shiftKey) )
			{
				dict_key = true;
				dictQuery(event);
			}
	}
	//return true
}
