﻿function gPopHideTip ()
{
	// Hide the tip in the popup handle
	$('handle1').innerHTML =
		"<table style='width:100%;'><tr>"
		+ "<td id='divTrace' style='padding-left:5px; font-style:italic; cursor:move;'>&nbsp;</td>"
		+ "<td style='width:25px;'><a href=\"javascript:gSetCharMode('fixed');\" title='Fixed'><span style='background:darkgreen; color:white; padding-left:4px; padding-right:4px;'>F</span></a></td>"
		+ "<td style='width:25px;'><a href=\"javascript:gSetCharMode('hover');\" title='Hover'><span style='background:#CCCCFF; color:white; padding-left:4px; padding-right:4px;'>H</span></a></td>"
		+ "<td style='width:25px;'><a href=\"javascript:gSetCharMode('detail');\" title='Detail'><span style='background:tomato; color:white; padding-left:4px; padding-right:4px;'>D</span></a></td>"
		+ "</tr></table>";
	
	gSetTrace ("More info: click in the popup <span style='font-size:smaller;'>(first switch to Fixed if in hover mode)</span>");
}

function gSetTrace (text)
{
	$('divTrace').innerHTML = text;
}

function f(eChar)
{
	// Hide the tip in the popup handle
	gPopHideTip ();

	// If the selection changes
	if (top.eCharSel != eChar)
	{
		// Update selection
		gSetSel (eChar);
		
		switch (top.sCharMode)
		{
			case 'fixed':
				
				if (top.bWordMode)
				{
					var sWord = gGetUniqueWord (eChar);
					if (sWord)
					{
						// alert ("unique = " + sWord);
						gPopShow (false);
						W (sWord);
					}
					else
					{
						if (!top.bPopVisible)
							gPopShow (true);
					}
				}
				else
				{
					if (!top.bPopVisible)
						gPopShow (true);
				}
				break;
			
			case 'detail':
				HZ (eChar.innerHTML.charCodeAt (0));
				break;
			
			default:
				break;
		}
	}
	
	// Else (the selection does not change)
	else
	{
		// Toggle to hover mode (from fixed, detailed) or fixed mode (from hover)
		if (top.sCharMode == 'hover')
		{
			gSetCharMode ('fixed');
			
			if (top.bWordMode)
			{
				var sWord = gGetUniqueWord (eChar);
				if (sWord)
				{
					// alert ("unique = " + sWord);
					gPopShow (false);
					W (sWord);
				}
				else
				{
					if (!top.bPopVisible)
						gPopShow (true);
				}
			}
		}
		
		else
			gSetCharMode ('hover');
	}
}

function gGetUniqueWord (eChar)
{
	var iPos = eChar.id.substr(1);
	var textChinese = top.sText;
	var nMaxLen = top.oParsedText.wordLength;
	var eWordPos = top.oParsedText.wordPos;
	
	var nWords = 0;
	
	var sChinese = null;
	
	// Look for the specified character and the previous ones
	for (var i = 0; i < nMaxLen; i++, iPos--)
	{
		// Stop at the beginning!
		if (iPos < 0)
			break;
		
		var eEntry = eWordPos[iPos];
		
		// debug: for Japanese, 3 fields per word
		var nFields = (top.sLangSource == 'jp' ? 4 : 1);
		for (var j = 0; j < eEntry.length; j = j+nFields)
		{
			var iWord = eEntry[j];
			var oWord = top.oParsedText.wordInfos[iWord];
			var nHanzi = oWord.n;
			
			// Ignore if the word is not long enough
			if (nHanzi <= i)
				continue;
				
			var w = oWord.w[0];
			sChinese = textChinese.substr (oWord.i, oWord.n);
			
			nWords++;
		}
	}
	
	if (nWords == 1)
		return sChinese;
	else
		return null;
}


function h (eChar)
{
	// Ignore if not in hover mode
	if (top.sCharMode != 'hover')
		return;
	
	// Hide/restore small popup if not applicable
	if (!top.bPopLarge)
	{
		if (!eChar  &&  top.bPopVisible)
		{
			gPopShow (false);
			top.bPopRestore = true;
		}
		
		if (eChar && top.bPopRestore)
		{
			gPopShow (true);
			top.bPopRestore = false;
		}
	}
	
	// Update selection
	if (top.bPopVisible)
		gSetSel (eChar);
	
	// Update display
	JmvcUpdateAllViews ();
}

function gPopClose()
{
	// Hide popup and clear selection
	gPopShow (false);
	gSetSel (null);
	
	// Set mode flag to 'fixed'
	top.sCharMode = 'fixed';
}

function gPopShow (bShow)
{
	// Show / hide popup
	if (bShow)
	{
		$('divPopLargeFrame').style.visibility = top.bPopLarge ? 'visible' : 'hidden';
		$('divPopSmallFrame').style.visibility = top.bPopLarge ? 'hidden' : 'visible';
	}
	else
	{
		$('divPopLargeFrame').style.visibility = 'hidden';
		$('divPopSmallFrame').style.visibility = 'hidden';
	}
	
	// Update flag
	top.bPopVisible	= bShow;
	
	// Update display
	top.bPopSizeChanged = true;
	JmvcUpdateAllViews();
}

function gSetPopLarge (bLarge)
{
	// Update popup mode
	top.bPopLarge = !top.bPopLarge;
	
	// Show relevant popup
	$('divPopLargeFrame').style.visibility = bLarge ? 'visible' : 'hidden';
	$('divPopSmallFrame').style.visibility = bLarge ? 'hidden' : 'visible';
	
	//Update display
	top.bPopSizeChanged = true;
	JmvcUpdateAllViews ();
}

function gSetCharMode (mode)
{
	// Hide the tip in the popup handle
	gPopHideTip ();

	// Set character mode
	top.sCharMode = mode;

	// Update display
	top.bCharModeChanged = true;
	JmvcUpdateAllViews ();
	
	switch (mode)
	{
	
	// Switch to Detail: show detail and hide popup
	case 'detail':
		
		var eChar = top.eCharSel;
		if (!eChar)
			break;
		HZ (eChar.innerHTML.charCodeAt(0));
		gPopShow (false);
		
		break;
	
	// Switch to Fixed or Hover: show popup
	case 'fixed':
	case 'hover':
		gPopShow (true);
		break;
	
	default:
		break;
	}
}

function OnWritePopLarge ()
{
	// Ignore if not fixed or hover
	switch (top.sCharMode)
	{
	case ('fixed'):
	case ('hover'):
		break;
	default:
		return;
	}
	
	// Ignore if popup not visible or current popup is large
	if (!top.bPopVisible  ||  !top.bPopLarge)
		return;
	
	// Check update flags
	if (!(top.bCharSelChanged || top.bPopSizeChanged))
		return;

	var eChar	= top.eCharSel;
	if (!eChar)
		return;
	
	var iPos	= eChar.id.substr(1);

	// debug
	var	eCharInfo = null;
	if (top.oTextChars)
		eCharInfo = top.eListCharInfos [top.eListCharPos[iPos].getAttribute ('i')];
	
	// debug
	var buf = new StringBuffer ();
	
	// HEADER
	buf.append (gGetHanziBase (eCharInfo));
	
	// CEDICT
	buf.append (gGetDicInfo (eChar));
	
	// debug
	$('divPopLarge').innerHTML = buf;
}

function gPopGetLink (bBorderTop)
{
	var sBorderTop = "";
	if (bBorderTop)
		sBorderTop = "border-top:lightgrey solid 1px;";
	else
		sBorderTop = "border-top:#B0C4DE solid 1px;";
		
	return "<tr><td style='background:#B0C4DE; " + sBorderTop + " text-align:right; padding-right:0px; font-size: 8pt; font-style:italic; font-family: Trebuchet MS, Arial, Geneva, sans-serif;'><a href='javascript:gSetPopLarge(true);' style='color:white; padding-left:5px; padding-right:5px; margin-right:0; text-decoration:none;'>more...</a></td><td style='" + sBorderTop + "'>&nbsp;</td><td style='" + sBorderTop + "'>&nbsp;</td></tr>";
}

function OnWritePopSmall ()
{
	// Ignore if not fixed or hover
	switch (top.sCharMode)
	{
	case ('fixed'):
	case ('hover'):
		break;
	default:
		return;
	}
	
	// Ignore if popup not visible or current popup is large
	if (!top.bPopVisible  ||  top.bPopLarge)
		return;
	
	// Check update flags
	if (!(top.bCharSelChanged || top.bPopSizeChanged))
		return;
	
	var eChar	= top.eCharSel;
	if (!eChar)
		return;
	
	var ePop = $('divPopSmall');
	var ePopFrame = $('divPopSmallFrame');
	
	// Set popup content
	ePop.innerHTML = gGetDicInfo (eChar);
	
	// X Pos
	var nLeft = eChar.offsetLeft;
	var nWidthChar = eChar.offsetWidth;
	var nWidthPopup = ePop.offsetWidth;
	var nWidthText = $('pageText').offsetWidth;
	
	if ((nLeft + nWidthPopup + 20) > nWidthText)
		nLeft -= (nWidthPopup - nWidthChar - 15);
	else
		nLeft -= 5;
	
	ePopFrame.style.left = nLeft + "px";
	
	// Y Pos
	var nTop = eChar.offsetTop;
	var nHeightChar = eChar.offsetHeight;
	var nHeightPopup = ePop.offsetHeight;
	var nHeightText = JGetFrameOuterContent('frameText').offsetHeight - JGetFrameHeader('frameText').offsetHeight;
	var nDelta = 12;
	
	var nCumulativeScrollOffset = $('pageText').cumulativeScrollOffset().top;
	
	var bBelow = true;
	var nBottom = nTop + nHeightChar + nHeightPopup + 4  - nCumulativeScrollOffset + nDelta;	// Pb : il faut ajouter les marges du popup
	if (nBottom >= nHeightText)
		bBelow = false;
	
	var bAbove = true;
	if (nTop - nCumulativeScrollOffset - nDelta < nHeightPopup + 4)						// Pb : il faut ajouter les marges du popup
		bAbove = false;
	
	// Switch position if necessary and useful
	if ((top.bPopBelow  &&  !bBelow && bAbove)  ||  (!top.bPopBelow && !bAbove && bBelow))
		top.bPopBelow = !top.bPopBelow;
	
	// Set popup position	
	if (top.bPopBelow)
		nTop += nHeightChar + nDelta;
	else
		nTop -= (nHeightPopup + nDelta);
	ePopFrame.style.top = nTop + "px";
}

function gGetDicInfo (eChar)
{
	var iPos = eChar.id.substr(1);
	var textChinese = top.sText;
	
	var nMaxLen = top.oParsedText.wordLength;
	var eWordPos = top.oParsedText.wordPos;
	
	var buf = new StringBuffer ();
	
	// Look for the specified character and the previous ones
	for (var i = 0; i < nMaxLen; i++, iPos--)
	{
		// Stop at the beginning!
		if (iPos < 0)
			break;
		
		var eEntry = eWordPos[iPos];
		
		// debug: for Japanese, 3 fields per word
		var nFields = (top.sLangSource == 'jp' ? 4 : 1);
		for (var j = 0; j < eEntry.length; j = j+nFields)
		{
			var iWord = eEntry[j];
			var oWord = top.oParsedText.wordInfos[iWord];
			var nHanzi = oWord.n;
			
			// Ignore if the word is not long enough
			if (nHanzi <= i)
				continue;
				
			bFirst = true;
			
			for (var k = 0; k < oWord.w.length; k++)
			{
				var w = oWord.w[k];
				var sMeaning	= w.d;
				if (top.sDict == 'handedict')
				{
					var len = sMeaning.indexOf('/');
					if (len >= 0)
						sMeaning = sMeaning.substr(0, len);
				}
				
				if (bFirst)
				{
					var sChinese;
					var bHighlight = false;
					
					if (top.sLangSource == 'jp' && w.s)
					{
						sChinese = w.s;
						if (w.s != textChinese.substr (oWord.i, oWord.n))
							bHighlight = true;
						
						if (eEntry[j+3] >= 0)
							bHighlight = true;
					}
					else
						sChinese = textChinese.substr (oWord.i, oWord.n);
					
					if (top.bPopLarge)
					{
						if (top.bWordMode)
						{
							// buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh' style='background:whitesmoke;'>");
							buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh'>");
						}
						else
							buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh'>");
					}
					else
					{
						if (top.bWordMode)
							buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh' style='background:darksalmon; font-weight:normal; border-top:lightgrey solid 1px;'>");
						else
							buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh' style='background:#8CB5E7; color:white; border-top:lightgrey solid 1px;'>");
					}
					
					// debug
					if (bHighlight)
					{
						if (top.bPopLarge)
							buf.append ("<span style='font-weight:bold;'>");
						else
							buf.append ("<span style='font-weight:bold; color:white;'>");
					}
					
					// WORD
					if (top.bWordMode)
					{
						if (top.bPopLarge)
						{
							// buf.append ("<a style='color:darkred; font-weight:normal;' href=\"javascript:W('" + sChinese + "');\">" + sChinese + "</a>");
							buf.append ("<a href=\"javascript:W('" + sChinese + "');\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
						}
						else
							buf.append ("<a style='color:white;' href=\"javascript:W('" + sChinese + "');\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					}
					// CHAR
					else
					{
						for (k2 = 0; k2 < sChinese.length; k2++)
						{
							var hz = sChinese.charAt(k2);
							var c = hz.charCodeAt(0);
							
								if (0x3400 <= c && c <= 0x9FAF)
								{
									if (top.bPopLarge)
										buf.append ("<a href='javascript:HZ(" + c + ");' title='Show character:  " + hz + "'>" + hz + "</a>");
									else
										buf.append ("<a style='color:white;' href='javascript:HZ(" + c + ");' title='Show character:  " + hz + "'>" + hz + "</a>");
								}
								else
									buf.append(hz);
						}
					}
					
					// debug
					if (bHighlight)
						buf.append ("</span>");
					
					if (top.bPopLarge)
						buf.append ("</td><td class='tdPinyin' style='padding-left:5px;' >" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
					else
						buf.append ("</td><td class='tdPinyin' style='padding-left:5px; border-top:lightgrey solid 1px;'>" + w.p + "</td><td class='tdDefinition' style='color:black; border-top:lightgrey solid 1px;'>" + sMeaning + "</td></tr>");
					
					// debug
					if (bHighlight)
					{
						var iPos = eEntry[j+1];
						var nLen = eEntry[j+2];
						
						var sChineseText = top.sText.substr(iPos, nLen);
						
						buf.append ("<tr><td class='tdMoreDic' style='background-color:#8CB5E7; color:white;'>" + sChineseText + " </td>"
							
							+ "<td class='tdMoreType' colspan='2'>" + gVconjType(eEntry[j+3]) + "</td>"
							+ "</tr>");
					}
					
					bFirst = false;
				}
				else
				{
					if (top.bPopLarge)
						buf.append ("<tr><td class='tdChinese'>&nbsp;</td><td class='tdPinyin' style='padding-left:5px;'>" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
					else
					{
						if (top.bWordMode)
							buf.append ("<tr><td class='tdChinese' style='background:darksalmon;'>&nbsp;</td><td class='tdPinyin' style='padding-left:5px;'>" + w.p + "</td><td class='tdDefinition' style='color:black;'>" + sMeaning + "</td></tr>");
						else
							buf.append ("<tr><td class='tdChinese' style='background:#8CB5E7;'>&nbsp;</td><td class='tdPinyin' style='padding-left:5px;'>" + w.p + "</td><td class='tdDefinition' style='color:black;'>" + sMeaning + "</td></tr>");
					}
				}
			}
		}
	}
	
	var s = buf.toString ();
	if (!s.length)
		s = "<tr><td class='gray'>(none)</td></tr>";
	
	if (top.bPopLarge)
		return "<div class='hd1'>Dictionary</div><table>" + s + "</table>";
	else
		return "<table style='width:100%; height:48px;'>" + s + "</table>";
}

function gSetSel (eChar)
{
	// Update selection
	top.ePrevSel		= top.eCharSel;
	top.eCharSel		= eChar;
	
	// Update display
	top.bCharSelChanged	= true;
	JmvcUpdateAllViews ();
}

function gKey2Uni (n)
{
	var keys = new Array (
		"\u4E00", "\u4E28", "\u4E36", "\u4E3F", "\u4E59", "\u4E85", "\u4E8C", "\u4EA0", "\u4EBA", "\u513F",
		"\u5165", "\u516B", "\u5182", "\u5196", "\u51AB", "\u51E0", "\u51F5", "\u5200", "\u529B", "\u52F9",
		"\u5315", "\u531A", "\u5338", "\u5341", "\u535C", "\u5369", "\u5382", "\u53B6", "\u53C8", "\u53E3",
		"\u56D7", "\u571F", "\u58EB", "\u5902", "\u590A", "\u5915", "\u5927", "\u5973", "\u5B50", "\u5B80",
		"\u5BF8", "\u5C0F", "\u5C22", "\u5C38", "\u5C6E", "\u5C71", "\u5DDB", "\u5DE5", "\u5DF1", "\u5DFE",
		"\u5E72", "\u5E7A", "\u5E7F", "\u5EF4", "\u5EFE", "\u5F0B", "\u5F13", "\u5F50", "\u5F61", "\u5F73",
		"\u5FC3", "\u6208", "\u6236", "\u624B", "\u652F", "\u6534", "\u6587", "\u6597", "\u65A4", "\u65B9",
		"\u65E0", "\u65E5", "\u66F0", "\u6708", "\u6728", "\u6B20", "\u6B62", "\u6B79", "\u6BB3", "\u6BCB",
		"\u6BD4", "\u6BDB", "\u6C0F", "\u6C14", "\u6C34", "\u706B", "\u722A", "\u7236", "\u723B", "\u723F",
		"\u7247", "\u7259", "\u725B", "\u72AC", "\u7384", "\u7389", "\u74DC", "\u74E6", "\u7518", "\u751F",
		"\u7528", "\u7530", "\u758B", "\u7592", "\u7676", "\u767D", "\u76AE", "\u76BF", "\u76EE", "\u77DB",
		"\u77E2", "\u77F3", "\u793A", "\u79B8", "\u79BE", "\u7A74", "\u7ACB", "\u7AF9", "\u7C73", "\u7CF8",
		"\u7F36", "\u7F51", "\u7F8A", "\u7FBD", "\u8001", "\u800C", "\u8012", "\u8033", "\u8080", "\u8089",
		"\u81E3", "\u81EA", "\u81F3", "\u81FC", "\u820C", "\u821B", "\u821F", "\u826E", "\u8272", "\u8278",
		"\u864D", "\u866B", "\u8840", "\u884C", "\u8863", "\u897E", "\u898B", "\u89D2", "\u8A00", "\u8C37",
		"\u8C46", "\u8C55", "\u8C78", "\u8C9D", "\u8D64", "\u8D70", "\u8DB3", "\u8EAB", "\u8ECA", "\u8F9B",
		"\u8FB0", "\u8FB5", "\u9091", "\u9149", "\u91C6", "\u91CC", "\u91D1", "\u9577", "\u9580", "\u961C",
		"\u96B6", "\u96B9", "\u96E8", "\u9751", "\u975E", "\u9762", "\u9769", "\u97CB", "\u97ED", "\u97F3",
		"\u9801", "\u98A8", "\u98DB", "\u98DF", "\u9996", "\u9999", "\u99AC", "\u9AA8", "\u9AD8", "\u9ADF",
		"\u9B25", "\u9B2F", "\u9B32", "\u9B3C", "\u9B5A", "\u9CE5", "\u9E75", "\u9E7F", "\u9EA5", "\u9EBB",
		"\u9EC4", "\u9ECD", "\u9ED1", "\u9EF9", "\u9EFD", "\u9F0E", "\u9F13", "\u9F20", "\u9F3B", "\u9F4A",
		"\u9F52", "\u9F8D", "\u9F9C", "\u9FA0", "\u9FA6");
	return keys [n-1];
}

function gVconjType (nType)
{
	switch (nType)
	{
	case 0:	return 'plain, negative, nonpast';
	case 1:	return 'polite, non-past';
	case 2:	return 'conditional';
	case 3:	return 'volitional';
	case 4:	return 'te-form';
	case 5:	return 'plain, past';
	case 6:	return 'plain, negative, past';
	case 7:	return 'passive';
	case 8:	return 'causative';
	case 9:	return 'potential or imperative';
	case 10:	return 'imperative';
	case 11:	return 'polite, past';
	case 12:	return 'polite, negative, non-past';
	case 13:	return 'polite, negative, past';
	case 14:	return 'polite, volitional';
	case 15:	return 'adj. -> adverb';
	case 16:	return 'adj., past';
	case 17:	return 'polite';
	case 18:	return 'polite, volitional';
	case 19:	return 'passive or potential';
	case 20:	return 'passive (or potential if Grp 2)';
	case 21:	return 'adj., negative';
	case 22:	return 'adj., negative, past';
	case 23:	return 'adj., past';
	case 24:	return 'plain verb';
	case 25:	return 'desire';
	case 26:	return 'desire, past';
	case 27:	return 'negative participle';
	case 28:	return 'masu stem';
	case 29:	return 'provisional';
	case 30:	return 'te-form, negative';
	case 31:	return 'imperative';
	case 32:	return 'negative base';
	case 33:	return 'adj., -ness form';
	case 34:	return 'adj., special';
	default:	return "";
	}
}


