var poemLine;
var poem;
var syncInfo;
var sync;
var synTmr;
var lastMarker=-1;
var lastChar;
function setDelay(val)
{
	document.estrokeApplet.setDelay(40 - val*4 + 4);
}

function finishPoemText(p,l,sInfo)
{
	poem = p;
	poemLine = l;
	syncInfo = sInfo;
	sync = true;
	var temp = poem.split('.');
	
	lastMarker = -1;
	if(document.audio.supportAudio()){
		document.audio.loadAudio(temp[0]+'.wav',syncInfo);
		document.audio.play();
		syncAudio();
	}
	else{
		alert("Your Java JRE does not support Audio\nYou need JRE 1.3 or above to support Audio.");
		displayPoem();
	}
}
function displayPoem()
{
	clearPoem();
	for(l=0; l<poemLine.length; ++l){
		if(l == 1){
			displayAuthor();
			++l;
		}
		for(c=0; c<poemLine[l].length; ++c){
			eval('document.poem.line' + l + '_' + c).value = poemLine[l].charAt(c);
		}
	}
}

function clearPoem()
{
	for(l = 0; l<5; ++l)	
		for(c = 0; c<24; ++c)	
			eval('document.poem.line' + l + '_' + c).value = "";
}

function displayAuthor()
{
	if(poemLine.length <= 1)
		return;
		
	for(c=0; c<poemLine[1].length; ++c){
		eval('document.poem.line0_' + (c+21)).value=poemLine[1].charAt(c);
	}
}

function syncAudio()
{
	clearTimeout(synTmr);
	marker = document.audio.getMarker();
	if(marker < 0)
		sync = false
	else if(marker !=lastMarker){ 
		lastMarker = marker;
		if(marker == 0){
			clearPoem();
			displayAuthor();
		}
		else
			displayMarker(marker);
	}	
	if(sync)
		synTmr = setTimeout("syncAudio()",10);
}

function nl(line)
{
	var nlstr = new Array;
	for(c = 0; c<line.length; ++c){
		nlstr[c*2] = line[c];
		nlstr[c*2+1] = '\r';
	}
	return nlstr;
}
function displayMarker(marker)
{
	for(l = 0; l<poemLine.length; ++l){
		if(l == 1)
			++l;
		for(c = 0; c<poemLine[l].length; ++c){
			if(poemLine[l].charAt(c) == ' ')
				continue;
			if(--marker == 0){
				ln = l;
				if(ln > 1)
					--ln
				eval('document.poem.line' + ln + '_'+ c).value = poemLine[l].charAt(c);
				return;
			}
		}
	}
}

function getPoem(poem)
{
	parent.requestor.location.replace("/estroke/fcg/estroke.fcg?task=getPoem&poem="+poem + "&textOnly=1" );
	if(!document.audio.supportAudio())
		document.audio.stopPlay();
	sync = false;
}

var lastX;
var lastY;
var mdown = false;

function hide(oid)
{
	o = document.getElementById(oid);
	o.style.visibility = 'hidden';
	document.myform.xx.focus();
}
/*
function show(oid)
{
	o = document.getElementById(oid);
	o.style.visibility = 'visible';
}
*/

function getContour(wc)
{
	if(wc.length > 0 && (wc.charCodeAt(0) == 10 || wc.charCodeAt(0) == 0 ||
			wc.charCodeAt(0) == 13)){
		if(document.myform.xx.value.length > 0)
			wc = document.myform.xx.value;
		else
			wc = document.myform.uni.value;
	}
	if(!wc.length || wc.charCodeAt(0) < 0x4e00 || wc.charCodeAt(0) > 0x9fa5){
		showVista();
		return false;
	}
	document.estrokeApplet.clear();
	document.myform.action = "/common/fcg/estroke.fcg?task=getContour";
	document.myform.uni.value = wc;
	document.myform.submit();
	return false;
} 
function variantType(vt)
{
	if(vt == 'm')
		return "Sem";
	else if(vt == 'p')
		return "Spec";
	else if(vt=='s')
		return "Simp";
	else if(vt=='t')
		return "Trad";
	else if(vt == 'z')
		return 'Z';
}
function newContour(cvec,sorder,def,phrases,pinyin,noOfStroke,unicode,
			big5, gb2312, radical,variants,vtype)
{
	showVista();
	document.estrokeApplet.clear();
	document.estrokeApplet.drawChar(cvec,sorder);	
	document.estroke.phrases.options.length = 0;
	document.estroke.def.value = def;
	document.estroke.pinyin.value = pinyin;
	document.estroke.noOfStroke.value = noOfStroke;
	document.estroke.unicode.value = unicode;
	document.estroke.big5.value = big5;
	document.estroke.gb2312.value = gb2312;
	document.estroke.radical.src = "radical/" + radical;
	for(i=0; i<phrases.length; ++i)
		document.estroke.phrases.options[i] = new Option(phrases[i],phrases[i]);
	for(i=0; i<variants.length; ++i){
		eval('document.estroke.v'+i).value = variants.charAt(i);
		eval('document.estroke.v'+i).style.visibility = 'visible';
		eval('document.estroke.vt'+i).value = variantType(vtype.charAt(i));
	
	}
	for(i=variants.length; i<5; ++i){
		eval('document.estroke.v'+i).style.visibility = 'hidden';
		eval('document.estroke.vt'+i).value = '';
		eval('document.estroke.vt'+i).value = '';
	}
	document.myform.xx.value='';
//	document.myform.xx.focus();
}

function setCookie(name,value)
{
	exp = new Date();
	exp.setYear(exp.getFullYear()+1);
	SetCookie(name,value,exp,"/");
}
function selectPlan(plan)
{
	document.estrokeApplet.selectPlan(plan);
	setCookie("plan",plan);
}
function selectPhrase()
{
	document.myform.action="/common/fcg/estroke.fcg?task=getDictionary";
	document.myform.unis.value = document.estroke.phrases.value;
	document.myform.submit();
}
function setDefinition(def)
{
	document.estroke.def.value = def;
}

function drawChar(c)
{
	document.myform.action = "/common/fcg/estroke.fcg?task=getContour"; 
	document.myform.uni.value=c;
	document.myform.submit();
}


function onDrag()
{
	o = document.getElementById('vista');
	if(o.style.visibility == 'visible')
		mySlider.placeSlider();
}
function onDragEnd()
{
	onDrag();
}
function showVista()
{
	for(i=0; i<5;++i){
		if(eval('document.estroke.v'+i).value.length)
			eval('document.estroke.v'+i).style.visibility = 'visible';
	}
	o = document.getElementById('vista');
	o.style.visibility = 'visible';

	o = document.getElementById('Slider1Button');
	if(o)
		o.style.visibility = 'visible';
	mySlider.placeSlider();
}

function hideVista()
{
	o = document.getElementById('Slider1Button');
	if(o)
		o.style.visibility = 'hidden';
	for(i=0; i<5;++i)
		eval('document.estroke.v'+i).style.visibility = 'hidden';
	hide('vista');
}
