var linecvArray;
var linesoArray;
var lineIdx;
var charIdx;
var anim;
var newPoem;
var app;
var anim;
var noAudio = true;
var nrow=12;
var ncol=12;
var rpl=2;
var showing = false;

function initPoem(poem)
{
	anim = false;
	sync = false;
	clear();
	getPoem(poem,0);
}

function stopTrace()
{
	document.poemTitle.stopTrace();
	document.poem.stopTrace();
	
}

function clearPoem()
{
	stopTrace();
	document.poemTitle.clear();
	document.poem.clear();
}

function clear()
{
	document.poemTitle.stopTrace();
	document.author.stopTrace();
	document.poem.stopTrace();
	document.poemTitle.clear();
	document.author.clear();
	document.poem.clear();
}
function redraw()
{
	document.audio.play();
	clear();
	finishFetching();
}

function startPoem()
{
}

function getPoem(poem,lineNo)
{
	document.audio.stopPlay();
	sync = false;
	anim = false;
	parent.requestor.location.replace("/common/fcg/estroke.fcg?task=getPoem&poem="+poem + "&line=" + lineNo);
	if(lineNo == 0)
		document.speaker.src="/common/transparent.gif";
}


function setLine(lineNo,cv,so)
{
	if(lineNo == 0){
		linecvArray = new Array;
		linesoArray = new Array;
	}
	linecvArray[lineNo] = cv;
	linesoArray[lineNo] = so;
}

function animatePoem()
{
	clearTimeout(synTmr);
	clearTimeout(atmr);
	clearTimeout(bctmr);
	sync = false
	clear();
	document.audio.stopPlay();
	lineIdx = 0;
	charIdx = 0;
	for(var i=0; i<linecvArray.length; ++i)
		showOutline(i);
	anim = true; 
	lineIdx = 0;
	charIdx = 0;
	drawChar();
}

function finishFetching(poem,syncInfo)
{
	clearTimeout(atmr);
	clearTimeout(bctmr);
	clearTimeout(synTmr);
	noAudio = (syncInfo.length == 0);
	if(noAudio)
		document.speaker.src = '/common/transparent.gif';
	else
		document.speaker.src = '/common/speaker.gif';
	var temp = poem.split('.');
	
	lineIdx = 0;
	charIdx = 0;
	anim = true;
	if(!noAudio)
		document.audio.loadAudio("/common/" + temp[0]+'.wav',syncInfo);
	for(var i=0; i<linecvArray.length; ++i)
		showOutline(i);
	drawChar();
}

function highlightMarker(marker)
{
	for(var l = 0; l<linecvArray.length; ++l){
		if(l == 1)
			++l;
		for(var c = 0; c<linecvArray[l].length; ++c){
			if(linecvArray[l][c].length <= 2)
				continue;
			if(--marker == 0){
				if(l == 0)
					document.poemTitle.highlight(
						linecvArray[l][c],linesoArray[l][c],
						l*rpl+Math.floor(c/ncol),c%ncol);
				else
					document.poem.highlight(linecvArray[l][c],
						linesoArray[l][c],(l-2)*rpl+Math.floor(c/ncol),c%ncol);
			}
		}
		
	}
}
var lastMarker = -1;
function drawAuthor()
{
	for(var c=0; c<linecvArray[1].length; ++c)
		document.author.showOutline(linecvArray[1][c],linesoArray[1][c],0,c);
}

function startAnimate()
{
	//dummy
}
var sync=false;
function startSync()
{
	if(noAudio)
		return;
	if(!document.audio.supportAudio())
	{
		alert("Security exception in applet: redirecting you to using absolute IP. This seems to fix the problem in Firefox");
		location.replace('http://202.81.244.195/online/tangPoem.html');
		return;
	}
	clearTimeout(synTmr);
	clearTimeout(atmr);
	clearTimeout(bctmr);
	anim = false;
	sync = true;
	lastMarker = -1;
	clearPoem();
	document.audio.play();
	syncAudio();
}
var synTmr;
var atmr;
var bctmr;
var lastidx=0;

function syncAudio()
{
	clearTimeout(synTmr);
	clearTimeout(atmr);
	clearTimeout(bctmr);
	marker = document.audio.getMarker();
	if(marker < 0)
		sync = false
	else if(marker !=lastMarker){ 
		lastMarker = marker;
		if(marker > 0)
			highlightMarker(marker);
	}	
	if(sync){
		synTmr = setTimeout("syncAudio()",10);
	}
}


function showOutline(lineNo)
{
	switch(lineNo){
		case 0:
			app = document.poemTitle;
			loff = 0;
			break;	
		case 1:
			app = document.author;
			loff = 1;
			break;
		default:
			app = document.poem;
			loff = 2;
	}
	for(var i=0; i<linecvArray[lineNo].length; ++i){
		app.showOutline(linecvArray[lineNo][i],linesoArray[lineNo][i],
			(lineNo-loff)*rpl+Math.floor(i/ncol),i%ncol);	
	}
}

function drawChar()
{
	if(sync)	
		return;
	var loff=0;	
	switch(lineIdx){
		case 0:
			app = document.poemTitle;
			break;	
		case 1:
			app = document.author;
			break;
		default:
			app = document.poem;
			loff = 2;
	}
		
	app.noOutline();
	app.drawChar(linecvArray[lineIdx][charIdx],linesoArray[lineIdx][charIdx],
			(lineIdx-loff)*rpl + Math.floor(charIdx/ncol),charIdx%ncol);
	clearTimeout(atmr);
	atmr = setTimeout("animateNext()",100);
}

function skipSpace()
{
	++charIdx;
	if(charIdx >= linecvArray[lineIdx].length){
		if(++lineIdx == 1)
			++lineIdx;
		if(lineIdx < linecvArray.length)
			charIdx = 0;
		else 
			return;
	}
	if(linesoArray[lineIdx][charIdx] == 0)
		drawChar();
	else{
		clearTimeout(bctmr);
		bctmr = setTimeout("betweenChar()",500);
	}
}
function betweenChar()
{
	if(charIdx < linecvArray[lineIdx].length)
		drawChar();
	else{
		if(++lineIdx == 1)
			++lineIdx;
		if(lineIdx < linecvArray.length){
			charIdx = 0;
			drawChar();
		}
		else
			anim = false;
	}
}
function animateNext()
{
	if(!app.isAnimating()){
		if(!anim)
			return;
		skipSpace();
	}
	else {
		clearTimeout(atmr);
		atmr = setTimeout("animateNext()",100);
	}
}

function hide(oid)
{
	o = document.getElementById(oid);
	o.style.visibility = 'hidden';
}
function show(oid)
{
	o = document.getElementById(oid);
	o.style.visibility = 'visible';
}
function showLogin()
{
	login.style.left = (screen.width-400)/2;
	loginFrame.location.href="/login.html";
	show("login");
	j_stop();
}

