﻿//////////////////////////////////////
//		INITIALIZATION				//
//////////////////////////////////////

function gGlobalData ()
{
	// Language: top.sLangSource, top.sLangTarget and top.sDict are specified by the server
	top.sCLang = (top.sLangSource == 'jp' ? " lang='ja'" : " lang='zh'");
	
	// Check browser type
	top.bMSExplorer	= window.ActiveXObject ? true : false;
	
	// Data: text
	top.sText			= null;
	top.oParsedText		= null;
	top.otextChars		= null;
	top.eListCharPos	= null;
	top.eListCharInfos	= null;
	
	// Text font size
	top.nTextFontSize = '14pt';
	
	// Simpl/trad. mode
	top.bSimplTradText		= false;
	top.bSimplTradList		= false;
	top.bSimplTradDifferent = true;
	top.bSimplTradLinks		= true;
	
	// Data: misc infos
	top.info		= null;
	
	// Hanzi
	top.nHanzi		= 0;
	top.oHanziBase	= null;
	top.oHanziDict	= null;
	
	// Hanzi pages
	top.nNumPages		= 0;
	top.iCurrentPage	= 0;
	top.nEntriesPerPage = 100;

	// Performances for text analysis
	top.tBegin		= 0;
	top.tPost		= 0;
	top.tResponse	= 0;
	top.tEnd		= 0;
	top.tHost		= 0;				// Server time if any
	top.nLength		= 0;				// Response length
	
	// Character selection
	top.eCharSel	= null;				// Current
	top.ePrevSel	= null;				// Previous
	top.sCharMode	= 'fixed';			// Display mode { 'fixed' | 'hover' | 'detail' }
	top.bSimplified	= false;			// Character set in detailed mode
	
	// Popup
	top.bPopVisible	= true;
	top.bPopRestore	= false;
	top.bPopLarge	= false;
	top.bPopBelow	= false;
	
	// Word mode
	top.bWordModeAvailable	= false;
	top.bWordMode			= false;
	top.sCurrentWord		= null;
	
	// debug
	top.iLocalWordI = -1;
	top.iLocalWordJ = -1;
	
	// Debug options
	top.bDebugPost	= false;
}

// Cleanup on unload
function gCleanup ()
{	
	// Base data
	JCleanup ();
	
	// Toolbar
	if (top.oToolbar)
		top.oToolbar.cleanup();
	
	// App data
	top.sText		= null;
	top.oParsedText	= null;
	top.otextChars	= null;
	top.info		= null;
	top.oHanziBase	= null;
	top.oHanziDict	= null;
}

function ClearUpdate ()
{
	top.bMainTextChanged		= false;
	top.bMainListChanged		= false;
	top.bPerfsChanged			= false;
	top.bSimplTradTextChanged	= false;
	top.bSimplTradListChanged	= false;
	
	top.bHanziChanged			= false;
	
	top.bCharSelChanged			= false;
	top.bCharModeChanged		= false;
	top.bPopSizeChanged			= false;
	top.bSimplifiedChanged		= false;

	top.bKangxi214Changed		= false;
	top.bKangxiSeriesChanged	= false;
	top.bEtymSeriesChanged		= false;
	top.bPhon858Changed			= false;
	top.bPhonSeriesChanged		= false;
	
	top.bBtnFixedChanged		= false;
	top.bBtnHoverChanged		= false;
	top.bBtnDetailChanged		= false;
	top.bBtnSimplifiedChanged	= false;
	top.bBtnTraditionalChanged	= false;
	
	top.bWordChanged			= false;
}

function gInit (nXiti)
{
	// Init MVC base
	JmvcInit (ClearUpdate);

	// Init global data
	gGlobalData (nXiti);
	
	// debug
	new Draggable('divPopLargeFrame', 
		{
			scroll:window,
			handle:'handle1',
			starteffect: null,
			endeffect: null
		}
	);
	
	// Create the pages
	new JmvcPage ('mainText',			OnWriteMainText);
	new JmvcPage ('mainList',			OnWriteMainList);
	new JmvcPage ('simplTradText',		OnWriteSimplTradText);
	new JmvcPage ('simplTradList',		OnWriteSimplTradList);
	new JmvcPage ('perfs',				OnWritePerfs);
	new JmvcPage ('popLarge',			OnWritePopLarge);
	new JmvcPage ('popSmall',			OnWritePopSmall);
	new JmvcPage ('pageKangxi214',		OnWriteKangxi214);
	new JmvcPage ('pageKangxiSeries',	OnWriteKangxiSeries);
	new JmvcPage ('pageEtymSeries',		OnWriteEtymSeries);
	new JmvcPage ('pagePhon858',		OnWritePhon858);
	new JmvcPage ('pagePhonSeries',		OnWritePhonSeries);
	new JmvcPage ('hanzi',	 			OnWriteHanzi);
	new JmvcPage ('word',	 			OnWriteWord);
	
	// debug
	new JmvcView ('viewPerfs');
	JmvcSetPage ('viewPerfs', 'perfs');
	
	// Preload toolbar
	gPreloadImages ();
	
	// Set the focus on the text area if defined
	var divFormIndex = $('formIndex');
	if (divFormIndex)
		divFormIndex.getElementsByTagName ('textarea')[0].focus ();
}

/////////////////////////////////////////////////
//		LAYOUT				//
/////////////////////////////////////////////////

function gInitLayout ()
{
	var divMain = document.getElementById ('divMain');
	
	divMain.style.overflow = 'hidden';
	
	divMain.innerHTML = "<div id='divToolbarContent' class='toolbar'></div><div id='divClient' class='client'></div>";
	
	// debug
	gInitToolbar ();
}

function gClearFrameInfo ()
{
	JSetHeaderHeight ('frameInfo', '40px');
	JGetFrameHeader('frameInfo').innerHTML = "";
	JGetFrameContent('frameInfo').scrollTop = 0;
	
	JGetFrameContent('frameInfo').style.padding = '10px';
	JGetFrameContent('frameInfo').style.paddingBottom = '100px';
}

function gCheckLayout ()
{
	if (!$('divClient'))
		gInitLayout ();
	
	// debug
	if (!$('frameInfo'))
	{
		JSetFrames (1);
		JGetFrame (0).setAttribute ('id', 'frameInfo');
		
		// debug
		new JmvcView ('frameInfo');	
	}
	
	// debug
	gClearFrameInfo ();
}

/////////////////////////////////////////////////
//		LOADERS				//
/////////////////////////////////////////////////

function gSetInfoLoader (divName)
{
	gSetLoader (JGetFrameContent('frameInfo'), null);
}

function gSetInitialLoader (divName, text)
{
	var div = $(divName);
	if (!div)
	{
		alert ("gSetInitialLoader: '" + divName + "' is not defined.");
		return;
	}

	document.getElementById (divName).innerHTML = "<table style='width:100%; height:70%;'><tr>"
		+ "<td style='width:50%; text-align:right; padding-right:1em;'><img src='pictures/ajax-loader1.gif' /></td>"
		+ "<td style='width:50%; font-size:12pt;'>" + (text ? text : "Loading...")
		+ "</td></tr></table>";
}

function gSetLoader (div, text)
{
	$(div).innerHTML = "<table style='width:100%; height:70%;'><tr><td style='text-align:center;'>"
			+ "<table><tr><td><img src='pictures/ajax-loader1.gif' /></td><td style='font-size:12pt; padding-left:1em;'>" + (text ? text : "Loading...") + "</td></tr></table>"
			+ "</td></tr></table>";
}

function gSetLoaderPart (div, text)
{
	$(div).innerHTML = "<table><tr><td style='padding-top:3pt; padding-bottom:3pt;vertical-align:center;'><img src='pictures/ajax-loader2.gif' /></td>"
					+ "<td style='color:gray; vertical-align:center; padding-left:10pt;'>" + (text ? text : "Loading...")  + "</td></tr></table>";
}

/////////////////////////////////////////////////
//		PARSED TEXT			//
/////////////////////////////////////////////////

function gGetLocalText (text)
{
	var textLocal		= "";
	var nNewLine		= 0;
	var uCharNewLine	= 0;
	
	for (var i = 0; i < text.length; i++)
	{
		uChar = text.charCodeAt(i);
		cChar = text.charAt(i);
		
		/*
		 * Just keep one or two new lines, replace with <br/>
		 * The character(s) for a new line can be CR LF or just LF or just CR:
		 *   - Use CR or LF but not both.
		 *   - The first CR or LF found specifies which one is used.
		 */
		 
		// Set the new line character if not yet done
		if ((!uCharNewLine)  &&  (uChar == 13  ||  uChar == 10))
			uCharNewLine = uChar;
			
		// Ignore in new lines: blank, tab, ideographic space
		if (nNewLine  &&  (uChar==32  ||  uChar==9 || uChar==0x3000))
			continue;
		
		// For a new line character
		if ((uChar == 13)  ||  (uChar == 10))
		{
			// Ignore if not the reference character
			if (uChar != uCharNewLine)
				continue;
			
			// Ignore if more than 2 new lines
			if (nNewLine >= 2)
				continue;
			
			// Add an HTML new line
			textLocal	+= '<br/>';
			
			// Update count and continue
			nNewLine++;
			continue;
		}
		
		// debug : UTILISER StringBuffer !
		textLocal += cChar;
			
		nNewLine = 0;
	}
	
	return textLocal;
}

function gEncode (text)
{
	// (Info: done initially with regular expressions - it's shorter but did not work for long texts with Konqueror)
	
	var buf = new StringBuffer ();
	var cChar;
	for (var i = 0; i < text.length; i++)
	{
		cChar = text.charAt(i);
		switch (cChar)
		{
			case '-':		buf.append ("-2D");		break;
			case '%':		buf.append ("-25");		break;
			case '&':		buf.append ("-26");		break;
			case '"':		buf.append ("-22");		break;
			case '\'':		buf.append ("-27");		break;
			case '?':		buf.append ("-3F");		break;
			default:
				buf.append (cChar);
				break;
		}
	}
	return (buf.toString ());
}

function gDecode (text)
{
	var buf = new StringBuffer ();
	var uChar, cChar;
	
	// Scan the text; unescape and append
	for (var i = 0; i < text.length; i++)
	{
		cChar = text.charAt(i);
		if (cChar == '-')
		{
			buf.append (String.fromCharCode (parseInt (text.substring (i+1, i+3), 16)));
			i += 2;
		}
		else
			buf.append (cChar);
	}
	
	return buf.toString ();
}

function gGetUserText ()
{
	var divFormIndex = $('formIndex');
	
	return divFormIndex.getElementsByTagName ('textarea')[0].value;
}

function gSetUserText (text)
{
	var divFormIndex = $('formIndex').getElementsByTagName ('textarea')[0].value = text;
}

function gParseText (text)
{
	// Clear perfs
	gPerfsClear ();
	
	// If a text is specified: includes escape sequences
	if (text)
	{
		// Unescape for the reference text
		top.sText = gDecode(text);
		
		// Keep the escaped text as specified
		textParse = text;
	}
	
	// Else (common case: user input)
	else
	{
		// Get the user input
		text = gGetUserText ();
		
		if (!text)
		{
			alert ("No text is specified. Paste a " + (top.sLangSource == 'cn' ? "Chinese text" : "Japanese text") + " and confirm.");
			return;
		}
		
		// Format for local display
		top.sText = gGetLocalText (text);
		
		// Get the escaped text
		textParse = gEncode (top.sText);
	}
	
	// Set initial loader - default loader would be: JmvcSetDivLoader ('mainText', "machin");
	gSetInitialLoader ('divMain', "Reading text...");
	
	// Load data
	gLoadData ('parse_words.php', OnTextWords, 'post', 'lang=' + top.sLangTarget + '&dict=' + top.sDict + '&text=' + textParse);
	gLoadData ('parse_chars.php', OnTextChars, 'post', 'text=' + textParse);
}

function gMenuDictionary (sDict, sLangTarget, sName, sTitle)
{
	// For the same dictionary, just set name and selection
	if (sDict == top.sDict  &&  sLangTarget == top.sLangTarget)
		return "<li class='sel'>" + sName + "</li>";
		
		
	// (Else) For another dictionary...
	
	//Infotip: show title if any
	var sTip = "";
	if (sTitle)
	{
		sTip = sTitle;
		if (top.bMSExplorer)
			sTip += "\n\r";
	}
	
	// Infotip: for IE, explain new tab with IE7
	if (top.bMSExplorer)
		sTip += ("(IE7: Ctrl+click for a new tab)");
	
	// Complete the text
	var text = "<li";
	if (sTip.length)
		text += (" title='" + sTip + "'");
	text += "><a href=\"javascript:gSameText('" + sDict + "', '" + sLangTarget + "');\">" + sName + "</a></li>";
	
	return text;
}

function gAddMenuSameText ()
{
	var text = "<form id='formSameText' action='index.php' target='_blank' method='post' style='margin-bottom: 0; padding-bottom: 0;'>"
	+ "<span title='Same text with another dictionary'>Same text</span>"
	+ "<ul class='level3'>"
	
	+ "<input type='hidden' name='dict'>"
	+ "<input type='hidden' name='lang'>"
	+ "<input type='hidden' name='text'>";
	
	// Japanese
	if (top.sLangSource == 'jp')
	{
		text += (
			gMenuDictionary ("jmdict", "en", "English", null)
			+ gMenuDictionary ("jmdict", "de", "Deutsch", null)
			+ gMenuDictionary ("jmdict", "fr", "Français", null)
			+ gMenuDictionary ("jmdict", "ru", "Русский", null)
			+ gMenuDictionary ("ddb", "en", "DDB - Soothill", "Digital Dictionary of Buddhism")
			);
	}
	
	// Chinese
	else
	{
		text += (
			gMenuDictionary ("cedict", "en", "English", null)
			+ gMenuDictionary ("handedict", "de", "Deutsch", null)
			+ gMenuDictionary ("ddb", "en", "DDB - Soothill", "Digital Dictionary of Buddhism")
			);
	}
	
	text += ("</ul>" + "</form>");
	
	$('divSameText').innerHTML = text;
}

function gSameText (sDict, sLangTarget)
{
	var eListInputs = $('formSameText').getElementsByTagName ('input');
	
	// Initialize the "Same text" form
	for (var i = 0; i < eListInputs.length; i++)
	{
		var eInput = eListInputs[i];
		switch (eInput.name)
		{
		case 'dict':		eInput.value = sDict;					break;
		case 'lang':		eInput.value = sLangTarget;				break;
		case 'text':		eInput.value = gEncode (top.sText);		break;
		}
	}
		
	// Submit form
	$('formSameText').submit();
}

function OnTextWords (url, transport)
{
	// Get the JSON object
	top.oParsedText = eval ("(" + transport.responseText + ")");
	
	// Time stamps
	gPerfsResponse (transport.responseText.length, top.oParsedText.time);

	// Try next steps
	OnTextWordsAndChars();
}

function OnTextChars (url, transport)
{
	// Save data
	top.oTextChars = transport.responseXML.documentElement;
	top.eListCharPos = top.oTextChars.getElementsByTagName ("p");
	top.eListCharInfos = top.oTextChars.getElementsByTagName ("i");

	// Try next steps
	OnTextWordsAndChars();
}

// debug
function OnTextWordsAndChars()
{
	if (!top.oParsedText || !top.oTextChars)
		return;
	
	// Set word mode if available
	switch (top.sDict)
	{
	case 'ddb':
	case 'handedict':
		top.bWordModeAvailable	= true;
		top.bWordMode			= true;
		break;
	}

	// debug
	gInitLayout ();
	
	JSetFrames (2);
	
	var frameText = JGetFrame (0);
	
	frameText.setAttribute('id', 'frameText');
	JGetFrameContent (frameText).setAttribute('id', 'pageText');
	
	JGetFrame(1).setAttribute('id', 'frameInfo');
	
	// Create views
	new JmvcView ('frameText');
	new JmvcView ('frameInfo');
	new JmvcView ('viewPopLarge');
	new JmvcView ('viewPopSmall');
	
	// Set pages
	gSetStandardText(false);
	gSetMainList (false);
	JmvcSetPage ('viewPopLarge', 'popLarge');
	JmvcSetPage ('viewPopSmall', 'popSmall');
	
	// Set text size
	gSetTextSize (0);
	
	// Update menu for dictionaries
	gAddMenuSameText ();
	
	// Update all views
	JmvcUpdateAllViews ();
	
	// Show the popup DIV
	if (top.sCharMode == 'fixed')
		gPopShow (true);
	
	// Show perfs
	gPerfsShow ();

	// Set SIMPLIFIED flags
	top.bSimplified = gIsSimplifiedText();
}

function gIsSimplifiedText ()
{
	// Japanese: always simplified
	if (top.sLangSource == 'jp')
		return false;
	
	// alert (top.eListCharInfos);
	
	// Look for maximum 5 simplified or traditional characters
	var nSimplified = 0;
	var nTraditional = 0;
	for (var i = 0; i < top.eListCharInfos.length; i++)
	{
		var eChar = top.eListCharInfos[i];
		if (eChar.getElementsByTagName('kS').length)		nTraditional++;
		if (eChar.getElementsByTagName('kT').length)		nSimplified++;
		
		if (nSimplified >= 5  ||  nTraditional >= 5)
			break;
	}
	
	return (nSimplified >= nTraditional);
}

/////////////////////////////////////////////////
//		CHARACTER SET			//
/////////////////////////////////////////////////

function gSetSimplified (bSimplified)
{
	top.bSimplified		= bSimplified;
	top.bSimplifiedChanged	= true;
	
	JmvcUpdateAllViews ();
	
	// Update word page if visible
	if (JmvcIsPageVisible('word'))
		gShowWord (top.sCurrentWord);
}

/////////////////////////////////////////////////
//		LANGUAGE				//
/////////////////////////////////////////////////

function gSetDictionary (sDict, sLang)
{
	top.sLangTarget = sLang;
	top.sDict = sDict;
	
	$('lang').value = sLang;
	$('dict').value = sDict;
	
	var sParams;
	switch (sDict)
	{
		case 'cedict':		sParams = 'lang=en';			break;
		case 'handedict':		sParams = 'lang=de';			break;
		case 'jmdict':		sParams = 'lang=' + sLang;		break;
		case 'ddb':			sParams = 'dict=ddb';			break;
		case 'soothill':		sParams = 'dict=soothill';		break;
		case 'stardict':		sParams = 'dict=stardict';		break;
	}
	
	$('linkHome').href = 'index.php?' + sParams;
}

/////////////////////////////////////////////////
//     STRING BUILDER				//
/////////////////////////////////////////////////


 function StringBuffer() { 
   this.buffer = []; 
 } 

 StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
 }; 

 StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
 }; 

////////////////////////////////////////////////////////////////////

function gStandardizeUserText ()
{
	gSetUserText (gGetLocalText (gGetUserText ()));
}
	
function gNojxChars (bProfile)
{
	// Normalize the user text
	gStandardizeUserText ();
	
	// Set the profile parameter
	document.getElementById('profile').value = (bProfile ? 1 : 0);
	
	// Change the form action to "parse_chars.php"
	var divFormIndex = $('formIndex').action = "parse_chars.php";
	
	// Submit form
	var divFormIndex = $('formIndex').submit ();
}

function gNojxWords (bProfile)
{
	// Normalize the user text
	gStandardizeUserText ();
	
	// Set the profile parameter
	document.getElementById('profile').value = (bProfile ? 1 : 0);
	
	// Submit form
	var divFormIndex = $('formIndex').submit();
}

////////////////////////////////////////////////////////////////////

function OnWritePerfs ()
{
	if (!top.bPerfsChanged)
		return;

	var text = "<table>";
	
	var tTotal = (top.tEnd - top.tBegin) / 1000;
	var tLocal = (top.tPost - top.tBegin + top.tEnd - top.tResponse) / 1000;
	var tHost = top.tHost / 1000;
	var tRemote = (top.tResponse - top.tPost) / 1000;
	var tNetwork = tRemote - top.tHost/1000;
	
	var nLength = top.nLength/1024;
	
	if (tHost)
	{
		text += ("<tr><td class='perfsValue'>" + (tHost + 0.05).toFixed(1) + " s</td><td class='perfsLabel'>host</td></tr>");
		text += ("<tr><td class='perfsValue'>" + (tNetwork + 0.05).toFixed(1) + " s</td><td class='perfsLabel'>network</td></tr>");
	}
	else
	{
		text += ("<tr><td class='perfsValue'>&nbsp;</td><td class='perfsLabel'>&nbsp;</td></tr>");
		text += ("<tr><td class='perfsValue'>" + (tRemote + 0.05).toFixed(1) + " s</td><td class='perfsLabel'>remote</td></tr>");
	}
		
	text += ("<tr><td class='perfsValue'>" + (tLocal + 0.05).toFixed(1) + " s</td><td class='perfsLabel'>local</td></tr>");
	text += ("<tr><td class='perfsValue' style='padding-top:2px; border-top:slategray solid 1px;'>" + (tTotal + 0.05).toFixed(1)
		+ " s</td><td class='perfsLabel' style='padding-top:2px;'>" + (nLength ? ((nLength + 0.05).toFixed(1) + " k") : "&nbsp;") + "</td></tr>");
	
	text += "</table>";
	
	$('divPerfs').innerHTML = text;
	
	// Show the perf DIV
	$('divPerfs').style.visibility = 'visible';
}

function gPerfsClear ()
{
	top.tBegin = new Date().valueOf();
	top.tHost = 0;
	top.nLength = 0;
}

function gPerfsShow ()
{
	// Time stamp
	top.tEnd = new Date().valueOf();
	
	// Show perfs
	top.bPerfsChanged = true;
	JmvcUpdateAllViews ();
}

function gPerfsPost ()
{
	top.tPost = new Date().valueOf();
}

function gPerfsResponse (nLength, tHost)
{
	top.tResponse = new Date().valueOf();
	top.nLength = nLength ? parseInt(nLength) : 0;
	top.tHost = tHost;
}

function gXitiMain (sPageXiti, nXiti, sHostXiti)
{
	var Xt_param = 's=' + nXiti + '&p=' + sPageXiti;
	
	try {Xt_r = top.document.referrer;}
	catch(e) {Xt_r = document.referrer; }
	
	var Xt_h = new Date();
	
	var Xt_i = '<img width="39" height="25" border="0" ';
	Xt_i += 'src="http://' + sHostXiti + '.xiti.com/hit.xiti?'+Xt_param;
	Xt_i += '&hl='+Xt_h.getHours()+'x'+Xt_h.getMinutes()+'x'+Xt_h.getSeconds();
	
	if(parseFloat(navigator.appVersion)>=4)
	{
		var Xt_s=screen;
		Xt_i+='&r='+Xt_s.width+'x'+Xt_s.height+'x'+Xt_s.pixelDepth+'x'+Xt_s.colorDepth;
	}
	
	if (Xt_r && Xt_r.length > 0)
		Xt_r = Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$');
	document.write(Xt_i+'&ref='+Xt_r+'" title="Internet Audience">');
}

function gXitiAjax (sPageXiti)
{
	// Return is stats are not active
	if (!top.nXiti)
		return "";
		
	// new
	var Xt_param = ('s=' + top.nXiti + '&p=' + sPageXiti);
	
	// Begin text
	text = "<div style='text-align:right;'>"
		+ "<img src='http://" + top.sHostXiti + ".xiti.com/hit.xiti?" + Xt_param;
		// Time
	var Xt_h = new Date();
	var xTime = Xt_h.getHours() + "x" + Xt_h.getMinutes() + "x" + Xt_h.getSeconds();
	text += ("&hl=" + xTime);

	// Screen resolution
	if (parseFloat (navigator.appVersion) >= 4)
		text += ("&r=" + screen.width + "x" + screen.height + "x" + screen.pixelDepth + "x" + screen.colorDepth);

	// Referrer
	var Xt_r;
	try {Xt_r = top.document.referrer;}
	catch(e) {Xt_r = document.referrer; }
	
	if (Xt_r && Xt_r.length > 0)
		Xt_r = Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$');
		
	text += ("&ref=" + Xt_r);

	// End text
	text += "' border='0' width='4' /></div>";
	
	return text;
}

//////////////////////////
//     MAIN TEXT		//
//////////////////////////

function OnWriteMainText ()
{
	// Detect Safari for a problem with line folding
	var bSafari = (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) ? true : false;
	
	if (top.bMainTextChanged)
	{
		var textChinese = top.sText;
		var uCharCode, uChar;
	
		var buf = new StringBuffer ();
		
		
		// debug
		buf.append (
			"<div id='divPopSmallFrame'  style='position:absolute; visibility:visible; z-index:100; left:137px; top:151px; width:450px; height:auto; background:#ECF4FC; border-style:solid; border-width:1px 2px 2px 2px; border-color:lightgrey gray gray lightgrey;'>"
				+ "<table style='width:100%;'><tr>"
					+ "<td style='vertical-align:top;'>"
						+ "<div id='divPopSmall' style='height:auto; background:#ECF4FC;'>"
							+ "<div style='text-align:right; margin-top:15px; margin-right:10px; font-size:10pt; font-style:italic; font-weight:bold; color:black;'>"
								+ "Use this button at any time to switch to the <a href='javascript:gSetPopLarge(true);'>FULL</a> popup &nbsp;<img src='pictures/arrow_small.png'/>"
							+ "</div>"
							+ "<div style='text-align:center; margin-top:15px; margin-bottom:15px; margin-right:10px; font-size:10pt; font-style:italic; font-weight:bold; color:black;'>"
								+ "<a href='javascript:gSetPopLarge(true);'>Smarthanzi basics: how to use this site</a>"
							+ "</div>"
						+ "</div>"
					+  "</td>"
					+ "<td style='width:17px; height:48px; vertical-align:top; background:#B0C4DE; border-left: solid #B0C4DE 1px;'><table><tr>"
					+ "<td style='width:17px; border-top:lightgrey solid 1px;' onclick='gPopClose();'><a  onmouseover=\"this.childNodes.item(0).src='pictures/closeMedium.png';\" onmouseout=\"this.childNodes.item(0).src='pictures/close.png';\"><img src='pictures/close.png'/></a></td>"
					+ "</tr><tr>"
					+ "<td style='width:17px;' onclick='gSetPopLarge(true);'><a  onmouseover=\"this.childNodes.item(0).src='pictures/maxMedium.png';\" onmouseout=\"this.childNodes.item(0).src='pictures/max.png';\"><img src='pictures/max.png'/></a></td>"
					+ "</tr></table></td>"
				+ "</tr></table>"
			+ "</div>"
			);
		
		// No hanzi: set a global link in order to hide the popup without showing HTML markers
		var bNullSection = false;
		
		for (var j= 0; j < textChinese.length; j++)
		{
			uCharCode	= textChinese.charCodeAt (j);
			uChar	= textChinese.charAt(j);
			if (uCharCode >= 0x3400  &&  uCharCode <= 0x9FAF)	// debug INCLURE L'EXTENSION A
			{
				if (bNullSection)
					buf.append ("</span>");
				bNullSection = false;
				buf.append ("<span id='_" + j + "' onmouseover='h(this);' onclick='f(this);'>" + uChar + "</span>");
				
				// Safari: add a space so that lines can be folded correctly
				if (bSafari)
					buf.append ("<span style='font-size:1px;'> </span>");
			}
			else
			{
				if (!bNullSection)
					buf.append ("<span onmouseover='h(null);'>");
				buf.append (uChar);
				bNullSection = true;

			}
		}
		
		// End of text
		if (!bNullSection)
			buf.append ("<span onmouseover='h(null);'>");
		
		// debug
		if (!$('pageText'))
			alert ("OnWriteMainText : 'pageText' is not defined.");
		
		// Get the XiTi page
		var sXiti;
		if (top.sLangSource == 'cn')		sXiti = (top.bSimplified ? 'cs' : 'ct');
		else						sXiti = top.sLangSource;
		sXiti += "_text_";
		switch (top.sDict)
		{
		case 'ddb':
		case 'soothill':
		case 'stardict':
			sXiti += top.sDict;
			break;
			
		default:
			sXiti += top.sLangTarget;
			break;
		}
		
		$('pageText').innerHTML = "<div" + top.sCLang + " class='zh'>" + buf.toString() + gXitiAjax (sXiti) + "</div>";
		
		var text = "<table id='textHeader' style='width:100%; height:100%;'><tr>"
			+ "<td class='steplabel'>Font size:</td>"
			+ "<td class='step'><a href='javascript:gSetTextSize(-2);'>&nbsp;-2&nbsp;</a></td>"
			+ "<td class='step'><a href='javascript:gSetTextSize(-1);'>&nbsp;-1&nbsp;</a></td>"
			+ "<td class='step'><a href='javascript:gSetTextSize(0);'>&nbsp;0&nbsp;</a></td>"
			+ "<td class='step'><a href='javascript:gSetTextSize(1);'>&nbsp;+1&nbsp;</a></td>"
			+ "<td class='step'><a href='javascript:gSetTextSize(2);'>&nbsp;+2&nbsp;</a></td>"
			+ "<td>&nbsp;</td>";
		
		if (top.sLangSource=='cn')
		{
			text += "<td class='serBack' Style='width:200px;'><a href='javascript:gSetSimplTradText();'>";
			text += (gIsSimplifiedText() ? "View traditional text" : "View simplified text");
			text += "</a></td></tr></table>";
		}
				
		text += "</tr></table>";
		
		JGetFrameHeader('frameText').innerHTML = text;

		// Show dictionary info
		gSetDicInfo();
	}
	
	if (top.bCharSelChanged)
	{
		// Clear highlight for the previous selection
		var eChar = top.ePrevSel;
		if (eChar)
			eChar.className = null;
		
		// Highlight the current selection
		eChar = top.eCharSel;
		if (!eChar)
			return;
		
		switch (top.sCharMode)
		{
		case 'page':
			eChar.className = 'charModePage';
			break;
		
		case 'hover':
			eChar.className = 'charModeHover';
			break;
		
		case 'detail':
			eChar.className = 'charModeDetail';
			break;
		
		default:
			eChar.className = 'charModeFixed';
			break;
		}
	}
	
	if (top.bCharModeChanged)
	{
		var eChar = top.eCharSel;
		if (!eChar)
			return;
		
		switch (top.sCharMode)
		{
		case 'page':
			eChar.className = 'charModePage';
			break;
		
		case 'hover':
			eChar.className = 'charModeHover';
			break;
		
		case 'detail':
			eChar.className = 'charModeDetail';
			break;
		
		default:
			eChar.className = 'charModeFixed';
			break;
		}
	}
}

function gSetTextSize (nSize)
{
	var fontSize;
	
	switch (nSize)
	{
		case -2:		fontSize = '10pt';		break;
		case -1:		fontSize = '12pt';		break;
		case +1:		fontSize = '16pt';		break;
		case +2:		fontSize = '18pt';		break;
		default:		fontSize = '14pt';		break;
	}
	
	// debug
	top.nTextFontSize = fontSize;
	
	if (JGetFrameContent('frameText'))
		JGetFrameContent('frameText').style.fontSize = fontSize;
	
	// debug
	if ($('divSimplTradVariant'))
		$('divSimplTradVariant').style.fontSize = fontSize;
	if ($('divSimplTradBase'))
		$('divSimplTradBase').style.fontSize = fontSize;
}

function gSetDicInfo()
{
	$('divDictionaryInfo').innerHTML = "<div><table style='margin-left:auto; margin-right:0;'><tr>"
		+ "<td style='font-size:smaller; border:#FFBD69 solid 1px;'><a href='" + top.oParsedText.dictUrl
			+ "' target='_blank' style='color:gray; background:#E5E0D6; padding-left:5px; padding-right:5px; text-decoration:none;'>"
			+ top.oParsedText.dictUrl + "</a></td>"
		+ "<td style='font-size:smaller;'><a href='credits.php' target='_blank' style='color:darkslategray; text-decoration:none; padding-left:10px; padding-right:15px;'>"
			+ top.oParsedText.dictName + ", " + top.oParsedText.dictDate + "</a></td></tr></table></div>";
}

//////////////////////////
//     MAINLIST			//
//////////////////////////

function OnWriteMainList ()
{
	if (!top.bMainListChanged)
		return;
	
	var buf;
	
	if (top.sLangSource == 'cn')
	{
		buf = new StringBuffer();
		
		// Begin main header table
		buf.append ("<table  class='ht1'><tr><td>");
		buf.append ("<table><tr style='overflow:hidden;'><td class='hh1'>Main list" + "</td></tr></table>");
		
		// Link to Simpl/trad.
		buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
		
		// End main header table
		buf.append ("</td></tr></table>");
		
		// Write the header
		JGetFrameHeader ('frameInfo').innerHTML = buf.toString();
	}
	
	var bufLeft		= new StringBuffer ();
	var bufRight	= new StringBuffer ();
	
	for (var i = 0; i < top.oParsedText.wordInfos.length; i++)
	{
		var bFirst = true;
		var oWord = top.oParsedText.wordInfos[i];
		var nHanzi = oWord.n;					// debug : nHanzi ??
		
		for (var j = 0; j < oWord.w.length; j++)
		{
			var w		= oWord.w[j];
			var sPinyin		= w.p;
			var sMeaning	= w.d;
			if (top.sDict == 'handedict')
			{
				var len = sMeaning.indexOf('/');
				if (len >= 0)
					sMeaning = sMeaning.substr(0, len);
			}
			

			buf = (nHanzi > 1 ? bufLeft : bufRight);
			
			if (bFirst)
			{
				// debug
				var bHighlight = false;
				
				// debug
				buf.append ("<tr>");
				
				// debug
				if (top.sDict == 'ddb')
				{
					if (w.x)
						buf.append ("<td class='tdXref'><a class='aXref' href=\"http://www.buddhism-dict.net/cgi-bin/xpr-ddb.pl?"
							+ w.x + "\" target='_blank' title=\"non-members can log in with 'guest'\")>DDB</a></td>");
						
					else
						buf.append ("<td></td>");
					
				}
				
				var sChinese;
				if (top.sLangSource == 'jp' && w.s)
				{
					sChinese = w.s;
					if (sChinese != top.sText.substr (oWord.i, oWord.n))
							bHighlight = true;
				}
				else
					sChinese = top.sText.substr (oWord.i, oWord.n);
				
				// buf.append ("<tr><td" + top.sCLang + " class='tdChinese zh'>");
				buf.append ("<td" + top.sCLang + " class='tdChinese zh'>");
				
				switch (top.sDict)
				{
				case 'ddb':
					buf.append ("<a href=\"javascript:W('" + sChinese + "');\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				case 'handedict':
					buf.append ("<a href=\"javascript:L(" + i + ");\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				default:
					// debug
					if (bHighlight)
						buf.append ("<span style='background:whitesmoke; font-weight:bold;'>");
					
					for (var k = 0; k < sChinese.length; k++)
					{
						var hz = sChinese.charAt(k);
						var c = hz.charCodeAt(0);
						if (c >= 0x3400  &&  c <= 0x9FA5)
							buf.append ("<a href='javascript:HZ(" + c + ");' title='Show character:  " + hz + "'>" + hz + "</a>");
						else
							buf.append (hz);
					}
					
					// debug
					if (bHighlight)
						buf.append ("</span>");
					break;
				}
				
				buf.append ("</td><td class='tdPinyin'>" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
				bFirst = false;
			}
			else
			{
				if (top.sDict == 'ddb')
					buf.append ("<tr><td>&nbsp;</td><td class='tdChinese'>&nbsp;</td><td class='tdPinyin'>" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
				else
					buf.append ("<tr><td class='tdChinese'>&nbsp;</td><td class='tdPinyin'>" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
			}
		}
	}
	
	sLeft = bufLeft.toString ();
	sRight = bufRight.toString ();
	
	// debug
	buf = new StringBuffer ();
		
	if (top.bMSExplorer)
	{
		buf.append ("<table style='width:100%; height:100%;'><tr><td style='width:50%;'><div id='divWordsLeft' class='info'>");
		
		if (sLeft.length)
			buf.append ("<table style='width:100%;'>" + sLeft + "</table>");
		
		buf.append ("</div></td><td style='width:50%;'><div id='divWordsRight' class='info'>");
		
		if (sRight.length)
			buf.append ("<table style='width:100%;'>" + sRight + "</table>");
		
		buf.append ("</div></td></tr></table></div></td></tr></table>");
	}
	
	else
	{
		buf.append ("<div class='infoLeft'><div id='divWordsLeft' class='infoPadding'>");
		
		if (sLeft.length)
			buf.append ("<table style='width:100%;'>" + sLeft + "</table>");
		
		buf.append ("</div></div><div class='infoRight'><div id='divWordsRight' class='infoPadding'>");
		
		if (sRight.length)
			buf.append ("<table style='width:100%;'>" + sRight + "</table>");
		
		buf.append ("</div></div>");
	}
	
	// debug
	JGetFrameOuterContent('frameInfo').innerHTML = "<div class='content'>" + buf.toString() + "</div>";
}

//////////////////////////
//	PAGE SWITCHING		//
//////////////////////////

function gSetStandardView(bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	gSetStandardText(false);
	gSetMainList(false);
	
	if (bUpdate)
		JmvcUpdateAllViews ();
}

function gSetStandardText(bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	top.bSimplTradText = false;
	JmvcSetPage('frameText', 'mainText');
	top.bMainTextChanged = true;
	$('pageText').style.background = 'snow';
	$('frameText').style.background = 'snow';
	
	if (bUpdate)
		JmvcUpdateAllViews();
}

function gSetMainList (bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	top.bSimplTradList = false;
	gClearFrameInfo ();
	
	if (top.sLangSource != 'cn')
	{
		JSetHeaderHeight('frameInfo', '0px');
		JGetFrameHeader('frameInfo').style.fontSize = '1px';
	}
	
	JmvcSetPage ('frameInfo', 'mainList');
	top.bMainListChanged = true;
	
	if (bUpdate)
		JmvcUpdateAllViews ();
}

function gSetSimplTradView(bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	gSetSimplTradText(false);
	gSetSimplTradList(false);
	
	if (bUpdate)
		JmvcUpdateAllViews ();
}

function gSetSimplTradText(bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	top.bSimplTradText = true;
	JmvcSetPage('frameText', 'simplTradText');
	top.bSimplTradTextChanged = true;
	$('pageText').style.background='azure';
	$('frameText').style.background = 'azure';
	
	if (bUpdate)
		JmvcUpdateAllViews ();
}

function gSetSimplTradList (bUpdate)
{
	if (bUpdate === undefined)
		bUpdate = true;
	
	top.bSimplTradList = true;
	gClearFrameInfo ();
	JmvcSetPage ('frameInfo', 'simplTradList');
	top.bSimplTradListChanged = true;
	
	if (bUpdate)
		JmvcUpdateAllViews ();
}

//////////////////////////
// SIMPLTRAD TEXT		//
//////////////////////////

function OnWriteSimplTradText ()
{
	if (!top.bSimplTradTextChanged)
		return;
	
	// HEADER

	var text = "<table id='textHeader' style='width:100%; height:100%;'><tr><td style='width:300px;'>";
	
	text += "<table><tr>"
		+ "<td class='steplabel'>Font size:</td>"
		+ "<td class='step'><a href='javascript:gSetTextSize(-2);'>&nbsp;-2&nbsp;</a></td>"
		+ "<td class='step'><a href='javascript:gSetTextSize(-1);'>&nbsp;-1&nbsp;</a></td>"
		+ "<td class='step'><a href='javascript:gSetTextSize(0);'>&nbsp;0&nbsp;</a></td>"
		+ "<td class='step'><a href='javascript:gSetTextSize(1);'>&nbsp;+1&nbsp;</a></td>"
		+ "<td class='step'><a href='javascript:gSetTextSize(2);'>&nbsp;+2&nbsp;</a></td>";
	
	text += "</tr></table></td>";
		
		// If a text is available add the link to the word lists
		if (top.bSimplTradLinks)
		{
			text += ("<td class='serBack' style='font-style:italic;'><a href='javascript:gToggleSimplTradLinks(); 'style='font-style:italic; color:darkorange; padding-left:5px; padding-right:5px;' title='Hide links to copy into Word or Open Office'>Hide links to copy</a></td>");
		}
		else
		{
			text += ("<td class='serBack'><a href='javascript:gToggleSimplTradLinks();' style='font-style:italic; background:darkorange; color:white; padding-left:5px; padding-right:5px;' title='Hide links only to copy into Word or Open Office'>Show links</a></td>");
		}
		
		text += "<td>&nbsp;</td>"
		+ "<td class='serBack'><a href='javascript:gSetStandardText();'>Standard view</a></td></tr></table>"
	
	JGetFrameHeader('frameText').innerHTML = text;
	
	
	// TEXT
	
	var textChinese = top.sText;
	var bSimplified = gIsSimplifiedText();

	var buf = new StringBuffer ();
	
	buf.append("<table style='width:100%;'><tr>");
	buf.append("<td style='width:50%; vertical-align:top; padding-left:15px;'>");
	if (bSimplified)
	{
		buf.append ("<div class='hd1'>Traditional variant</div>");
		buf.append("<div id='divSimplTradVariant' style='color:black;"
			+ "font-size:" + top.nTextFontSize + ";'>"
			+ gSimplTradGetText(textChinese, "kT") + "</div>");
	}
	else
	{
		buf.append ("<div class='hd1'>Simplified variant</div>");
		buf.append("<div id='divSimplTradVariant' style='color:black; font-size:" + top.nTextFontSize + ";'>" + gSimplTradGetText(textChinese, "kS") + "</div>");
	}
	
	buf.append("</td><td style='width:50%; vertical-align:top; padding-left:16px;'>");
	
	buf.append ("<div class='hd1'>Base text</div>");
	
	buf.append("<div id='divSimplTradBase' style='font-size:" + top.nTextFontSize + "; cursor:auto;'>" + top.sText + "</div>");
	
	buf.append("</td>");
	buf.append("</tr></table>");
	
	// debug
	if (!$('pageText'))
		alert ("OnWriteMainText : 'pageText' is not defined.");
	
	// Get the XiTi page
	var sXiti;
	sXiti = top.sLangSource + (bSimplified ? "_s2t_" : "_t2s_");
	switch (top.sDict)
	{
	case 'ddb':
	case 'soothill':
	case 'stardict':
		sXiti += top.sDict;
		break;
		
	default:
		sXiti += top.sLangTarget;
		break;
	}
	
	$('pageText').innerHTML = buf.toString() + gXitiAjax (sXiti);
}

function gSimplTradGetText(text, variant)
{
	// Detect Safari for a problem with line folding
	var bSafari = (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) ? true : false;
	
	var buf = new StringBuffer ();
	
	for (var j= 0; j < text.length; j++)
	{
		var uCharCode	= text.charCodeAt (j);
		var uChar	= text.charAt(j);
		
		if (uCharCode >= 0x3400  &&  uCharCode <= 0x9FAF)	// debug INCLURE L'EXTENSION A
		{
			// debug
			var eCharInfo = top.eListCharInfos [top.eListCharPos[j].getAttribute ('i')];
			
			if (eCharInfo)
			{
				// debug UTILISER PROTOTYPE ?
				var c = eCharInfo.getAttribute ('c');
				var bDone = false;
				var nTrad = eCharInfo.getElementsByTagName (variant).length;
				
				if (nTrad == 0)
				{
					if (top.bSimplTradLinks)
						buf.append("<a href='javascript:HZ(" + c.charCodeAt(0) + ");' style='color:gray; text-decoration:none;'>" + c + "</a>");
					else
						buf.append(c);
				}
				else
				{
					for (var i = 0; i < eCharInfo.getElementsByTagName (variant).length; i++)
					{
						if (bDone)
							buf.append (",");
						else if (nTrad > 1)
							buf.append ("[");
						
						var cS = eCharInfo.getElementsByTagName (variant)[i].getAttribute('c');
						if (top.bSimplTradLinks)
							buf.append("<a href='javascript:HZ(" + cS.charCodeAt(0) + ");' style='color:darkblue; text-decoration:none;'>" + cS + "</a>");
						else
							buf.append("<span style=''>" + cS + "</span>");
						bDone = true;
					}
						
					if (bDone && nTrad > 1)
						buf.append ("]");
				}
				
				// Safari: add a space so that lines can be folded correctly (only for hanzi, hide <br>)
				if (bSafari)
					buf.append ("<span style='font-size:1px;'> </span>");
			}
			
			// debug SEMBLE INUTILE
			else
			{
				buf.append ("<a href='javascript:HZ(" + uChar.charCodeAt(0) + ");' style='color:red; background:yellow; text-decoration:none;'>" + uChar + "</a>");
			}
		}
		
		else
			buf.append(uChar);
	}
	
	return "<div" + top.sCLang + " class='zh'"
		+ (!top.bSimplTradLinks ? " style='cursor:text;'" : "")
		+ ">" + buf.toString() + "</div>";
}

function gToggleSimplTradLinks()
{
	top.bSimplTradLinks = !top.bSimplTradLinks;
	gSetSimplTradText();
}

//////////////////////////
//   SIMPLTRAD LIST		//
//////////////////////////

function OnWriteSimplTradList ()
{
	// debug
	if (!top.bSimplTradListChanged)
		return;
	
	// Show dictionary info
	gSetDicInfo();
	
	var buf = new StringBuffer();
	
	// Begin main header table
	buf.append ("<table  class='ht1'><tr>");
	buf.append ("<td style='width:300px;'><table><tr style='overflow:hidden;'><td class='hh1'>Words: simplified, traditional</td></tr></table></td>");
	
	// If a text is available add the link to the word lists
	if (top.bSimplTradDifferent)
	{
		buf.append ("<td class='serBack' style='font-weight:bold;'><span style='background:gainsboro; padding-left:5px; padding-right:5px;'>Different only</span></td>");
		buf.append ("<td class='serBack'><a href='javascript:gSetSimplTradDifferent(false);'>All words</a></td>");
	}
	else
	{
		buf.append ("<td class='serBack'><a href='javascript:gSetSimplTradDifferent(true);'>Different only</a></td>");
		buf.append ("<td class='serBack' style='font-weight:bold;'><span style='background:gainsboro; padding-left:5px; padding-right:5px;'>All words</span></td>");
	}
	buf.append ("<td>&nbsp;</td>");
	buf.append ("<td class='serBack'><a href='javascript:gSetMainList();'>Main list</a></td>");
	
	// End main header table
	buf.append ("</tr></table>");
	
	// Write the header
	JGetFrameHeader ('frameInfo').innerHTML = buf.toString();
	
	var buf = new StringBuffer();
	
	for (var i = 0; i < top.oParsedText.wordInfos.length; i++)
	{
		var bFirst = true;
		var oWord = top.oParsedText.wordInfos[i];
		var nHanzi = oWord.n;					// debug : nHanzi ??
		
		for (var j = 0; j < oWord.w.length; j++)
		{
			var w		= oWord.w[j];
			
			// debug
			var sSimplified = w.s;
			var sTraditional = w.t;
			
			var sPinyin		= w.p;
			var sMeaning	= w.d;
			if (top.sDict == 'handedict')
			{
				var len = sMeaning.indexOf('/');
				if (len >= 0)
					sMeaning = sMeaning.substr(0, len);
			}
			
			if (sSimplified != sTraditional || !top.bSimplTradDifferent)
			{
				// debug
				buf.append ("<tr>");
				
				// debug
				if (top.sDict == 'ddb')
				{
					if (w.x)
						buf.append ("<td class='tdXref'><a class='aXref' href=\"http://www.buddhism-dict.net/cgi-bin/xpr-ddb.pl?"
							+ w.x + "\" target='_blank' title=\"non-members can log in with 'guest'\")>DDB</a></td>");
						
					else
						buf.append ("<td></td>");
				}
				
				var sChinese;
				
				// Simplified
				sChinese = sSimplified;
				
				buf.append ("<td" + top.sCLang + " class='tdChinese zh'>");
				
				switch (top.sDict)
				{
				case 'ddb':
					buf.append ("<a href=\"javascript:W('" + sChinese + "');\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				case 'handedict':
					buf.append ("<a href=\"javascript:L(" + i + ");\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				default:
					
					for (var k = 0; k < sChinese.length; k++)
					{
						var hz = sChinese.charAt(k);
						var c = hz.charCodeAt(0);
						if (c >= 0x3400  &&  c <= 0x9FA5)
							buf.append ("<a href='javascript:HZ(" + c + ");' title='Show character:  " + hz + "'>" + hz + "</a>");
						else
							buf.append (hz);
					}
					
					break;
				}
				buf.append("</td>");
				
				// Traditional
				sChinese = sTraditional;
				
				buf.append ("<td" + top.sCLang + " class='tdChinese zh'>");
				
				switch (top.sDict)
				{
				case 'ddb':
					buf.append ("<a href=\"javascript:W('" + sChinese + "');\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				case 'handedict':
					buf.append ("<a href=\"javascript:L(" + i + ");\" title='Show word:  " + sChinese + "'>" + sChinese + "</a>");
					break;
					
				default:
					
					for (var k = 0; k < sChinese.length; k++)
					{
						var hz = sChinese.charAt(k);
						var c = hz.charCodeAt(0);
						if (c >= 0x3400  &&  c <= 0x9FA5)
							buf.append ("<a href='javascript:HZ(" + c + ");' title='Show character:  " + hz + "'>" + hz + "</a>");
						else
							buf.append (hz);
					}
					
					break;
				}
				buf.append("</td>");
				
				buf.append ("<td class='tdPinyin'>" + w.p + "</td><td class='tdDefinition'>" + sMeaning + "</td></tr>");
				bFirst = false;
			}
		}
	}
	
	var s = buf.toString();
	
	// debug
	buf = new StringBuffer ();
	
	if (s.length)
	{
		if (top.bMSExplorer)
			buf.append ("<div class='info'><table style='width:100%;'>" + s + "</table></div>");
		else
			buf.append ("<div class='infoPadding'><table style='width:100%;'>" + s + "</table></div>");
	}
	
	// debug
	JGetFrameOuterContent('frameInfo').innerHTML = "<div class='content'>" + buf.toString() + "</div>";
}

function gSetSimplTradDifferent(bDifferent)
{
	top.bSimplTradDifferent = bDifferent;
	
	top.bSimplTradListChanged = true;
	
	JmvcUpdateAllViews();
}

//////////////////////////////
//			TOOLBAR			//
//////////////////////////////


function gInitToolbar ()
{
	top.oToolbar = new JToolbar ();
	
	top.oToolbar.addSeparator (false);
	top.oToolbar.addButton ('fixed', 0, 23, "gSetCharMode('fixed');");
	top.oToolbar.addButton ('hover', 23, 23, "gSetCharMode('hover');");
	top.oToolbar.addButton ('detail', 46, 23, "gSetCharMode('detail');");
	
	if (top.sLangSource == 'cn')
	{
		top.oToolbar.addSeparator (true);
		top.oToolbar.addButton ('standard', 115, 69, "gSetStandardView();");
		top.oToolbar.addButton ('simpltrad', 184, 69, "gSetSimplTradView();");
		top.oToolbar.addSeparator (true);
		top.oToolbar.addButton ('simplified', 69, 23, "gSetSimplified(true);");
		top.oToolbar.addButton ('traditional', 92, 23, "gSetSimplified(false);");
	}

	// debug
	top.oToolbar.addText ("<div id='divDictionaryInfo'>&nbsp;</div>", "right");
	
	// debug
	new JmvcPage ('toolbar', OnUpdateToolbar);
	new JmvcView ('viewToolbar');
	JmvcSetPage ('viewToolbar', 'toolbar');
}

function OnUpdateToolbar ()
{
	if (JmvcIsView ('frameText'))
	{
		top.oToolbar.enable ('fixed', !top.bSimplTradText);
		top.oToolbar.enable ('hover', !top.bSimplTradText);
		top.oToolbar.enable ('detail', !top.bSimplTradText);
		
		top.oToolbar.enable ('standard', true);
		top.oToolbar.enable ('simpltrad', true);
		
		top.oToolbar.check ('fixed', top.sCharMode=='fixed');
		top.oToolbar.check ('hover', top.sCharMode=='hover');
		top.oToolbar.check ('detail', top.sCharMode=='detail');
		
		top.oToolbar.check ('standard', (!top.bSimplTradText && !top.bSimplTradList));
		top.oToolbar.check ('simpltrad', (top.bSimplTradText && top.bSimplTradList));
		
		top.oToolbar.enable ('word', top.bWordModeAvailable);
		top.oToolbar.enable ('char', top.bWordModeAvailable);
		
		top.oToolbar.check ('popsmall', !top.bPopLarge);
		top.oToolbar.check ('poplarge', top.bPopLarge);
		top.oToolbar.check ('word', top.bWordMode);
		top.oToolbar.check ('char', !top.bWordMode);
	}
	else
	{
		top.oToolbar.enable ('fixed', false);
		top.oToolbar.enable ('hover', false);
		top.oToolbar.enable ('detail', false);
		top.oToolbar.enable ('standard', false);
		top.oToolbar.enable ('simpltrad', false);
		top.oToolbar.enable ('popsmall', false);
		top.oToolbar.enable ('poplarge', false);
		top.oToolbar.enable ('word', false);
		top.oToolbar.enable ('char', false);
	}
	
	if (JmvcIsPageVisible ('hanzi')  ||  JmvcIsPageVisible('word'))
	{
		top.oToolbar.enable ('simplified', true);
		top.oToolbar.enable ('traditional', true);
		
		top.oToolbar.check ('simplified', top.bSimplified);
		top.oToolbar.check ('traditional', !top.bSimplified);
	}
	
	else
	{
		top.oToolbar.enable ('simplified', false);
		top.oToolbar.enable ('traditional', false);
	}
}

function gPreloadImages ()
{
	// Preload toolbar images
	(new Image(276,88)).src = "pictures/tbButtons.png";
	
	// Preload other images
	(new Image (17,16)).src = "pictures/close.png";
	(new Image (17,16)).src = "pictures/closeMedium.png";
}

/////////////////////////////////////////////////
//			HTTP				//
/////////////////////////////////////////////////

function gLoadData (url, action, method, data) 
{
	// Perfs
	gPerfsPost ();
	
	// Check the method parameter
	method = method.toLowerCase();
	
	// debug: force POST if the flag is set; if POST, add dummy data
	if (top.bDebugPost)
	{
		method = 'post';
		if (!data)
			data='dummy=0';
	}
	
	var req = new Ajax.Request (
		url,
		{
			method: method,
			parameters: data,
			onSuccess:  function(response)
			{
				action (url, response);
			}
		}
	);
}

////////////////////////////////////////////////////////////////

