// ********** Common
var m_dStart;
var m_bFocus = 0;

// ********** Const
var m_iWait = 1;
var m_iRlMove = 20;
var m_iRlGroup = 1;
var m_iCvWt = 600;
var m_iCvHt = 200;
var m_iPcSize;
var m_iMarginX;
var m_iMarginY;

// ********** Draw
var m_elmCv1;
var m_ctxCv1;
var m_elmCv2;
var m_ctxCv2;

var m_iAnime;
var m_timAnime;
var m_strDefine;
var m_astrText = new Array();
var m_aiX0 = new Array();
var m_aiY0 = new Array();
var m_aiX1 = new Array();
var m_aiY1 = new Array();
var m_iStPiece = 0;
var m_iDyPiece = 0;
var m_dStart;

var m_strReq = '';

window.onload = sbInit;

function sbInit() {
	// Init Canvas
	m_elmCv1 = document.getElementById('hanabi_cv');
	m_ctxCv1 = m_elmCv1.getContext('2d');
	m_elmCv2 = document.getElementById('hanabi_cv2');
	m_ctxCv2 = m_elmCv2.getContext('2d');
	
	// Start Timer
	m_timAnime = setInterval('sbAnime()',50);
}

function sbAnime() {
	var iIdx;
	var iMaxX,iMaxY;
	var iPcW,iPcH;
	var strDefine;
	var astrDefine = new Array();
	var iPar;
	var iXi,iYi;
	
	strDefine = '' + document.getElementById('hanabi_res').value;
	
	// Start Animation
	if (strDefine != '' && strDefine != m_strDefine) {
		m_iAnime = 0;
		m_strDefine = strDefine;
		
		// Init
		astrDefine = strDefine.split('~');
		m_astrText = astrDefine[0].split(',');
		m_aiX0 = astrDefine[1].split(',');
		m_aiY0 = astrDefine[2].split(',');
		m_aiX1 = astrDefine[3].split(',');
		m_aiY1 = astrDefine[4].split(',');
		
		if (m_astrText.length > 1) {
			document.getElementById('hanabi_alert').innerHTML = '<span class="info">' + m_astrText[1] + '</span><br />';
		}
		
		// Count Relation
		if (astrDefine[1] == '') {
			m_iStPiece = 0;
		} else {
			m_iStPiece = m_aiX0.length;
		}
		if (astrDefine[3] == '') {
			m_iDyPiece = 0;
		} else {
			m_iDyPiece = m_aiX1.length;
		}
		
		// Adjust & Search Label & Calc Node
		iMaxX = 0;
		iMaxY = 0;
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			m_aiX0[iIdx] = Number(m_aiX0[iIdx]);
			m_aiY0[iIdx] = Number(m_aiY0[iIdx]);
			iMaxX = (m_aiX0[iIdx] > iMaxX) ? m_aiX0[iIdx] : iMaxX;
			iMaxY = (m_aiY0[iIdx] > iMaxY) ? m_aiY0[iIdx] : iMaxY;
		}
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			m_aiX1[iIdx] = Number(m_aiX1[iIdx]);
			m_aiY1[iIdx] = Number(m_aiY1[iIdx]);
			iMaxX = (m_aiX1[iIdx] > iMaxX) ? m_aiX1[iIdx] : iMaxX;
			iMaxY = (m_aiY1[iIdx] > iMaxY) ? m_aiY1[iIdx] : iMaxY;
		}
		iPcW = Math.floor((m_iCvWt - 20) / (iMaxX + 1));
		iPcH = Math.floor((m_iCvHt - 20) / (iMaxY + 1));
		m_iPcSize = (iPcW < iPcH) ? iPcW : iPcH;
		m_iMarginX = Math.floor((m_iCvWt - m_iPcSize * (iMaxX + 1)) / 2);
		m_iMarginY = Math.floor((m_iCvHt - m_iPcSize * (iMaxY + 1)) / 2);
		
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			m_aiX0[iIdx] = m_aiX0[iIdx] * m_iPcSize + m_iMarginX;
			m_aiY0[iIdx] = m_aiY0[iIdx] * m_iPcSize + m_iMarginY;
		}
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			m_aiX1[iIdx] = m_aiX1[iIdx] * m_iPcSize + m_iMarginX;
			m_aiY1[iIdx] = m_aiY1[iIdx] * m_iPcSize + m_iMarginY;
		}
		
		m_elmCv1.style.backgroundColor = '#002';
		m_elmCv2.style.backgroundColor = '#000';
		
		// Top Canvas
		m_ctxCv2.clearRect(0,0,m_iCvWt,m_iCvHt);
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			sbDrawPiece(m_ctxCv2,m_aiX0[iIdx],m_aiY0[iIdx],1);
		}
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			sbDrawPiece(m_ctxCv2,m_aiX1[iIdx],m_aiY1[iIdx],1);
		}
		
		// ********** Time
		if (m_bFocus == 0) {
			document.getElementById('hanabi_wait').style.display = 'none';
			document.getElementById('hanabi_all').style.display = 'block';
			document.getElementById('hanabi_req').focus();
			m_bFocus = 1;
		}
		m_dStart = new Date();
	}
	
	if (m_iAnime >= 0 && m_iAnime <= m_iWait + m_iRlMove) {
		// Clear Canvas
		m_ctxCv1.clearRect(0,0,m_iCvWt,m_iCvHt);
		
		// Static Circle
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			sbDrawPiece(m_ctxCv1,m_aiX0[iIdx],m_aiY0[iIdx],1);
		}
		
		// Dynamic Circle
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			iPar = (m_iAnime - m_iWait) / (m_iRlMove);
			iPar = (iPar < 0 ? 0 : iPar);
			sbDrawPiece(m_ctxCv1,m_aiX1[iIdx],m_aiY1[iIdx],1 - iPar);
		}
	}
	
	// End Animation
	if (m_iAnime == m_iWait + m_iRlMove + 2) {
		m_elmCv1.style.backgroundColor = '#000';
		if (m_astrText.length > 2) {
			document.getElementById('hanabi_alert').innerHTML = '<span class="info">' + m_astrText[2] + '</span><br />';
		}
		
		//Check Time
		dNow = new Date();
//		alert(parseInt((dNow.getTime() - m_dStart.getTime())));
	}
	
	// Add FrameCount
	if (m_iAnime >= 0 && m_iAnime <= m_iWait + m_iRlMove + 2) {
		m_iAnime ++;
	}
}

function sbDrawPiece(ctxCv,iXi,iYi,iPar) {
	var iSize;
	var strCol;
	iXi = iXi + m_iPcSize * 5 / 12 + Math.random() * m_iPcSize / 6;
	iYi = iYi + m_iPcSize * 5 / 12 + Math.random() * m_iPcSize / 6;
	iSize = m_iPcSize / 2 * (1 + iPar) / 2;
	iSize = Math.round(Math.random() * iSize / 5 + iSize * 4 / 5);
	strCol = (Math.round(15 * (0.1 + iPar) / 1.1)).toString(16);
	ctxCv.fillStyle = '#' + strCol + strCol + strCol;
	ctxCv.beginPath();
	ctxCv.arc(iXi,iYi,iSize,0 * Math.PI / 180,0.1 * Math.PI / 180,1);
	ctxCv.fill();
}

function fnSend_anohanabi() {
	var strReq;
	strReq = '';
	strReq += 'nm=' + EscapeEUCJP(document.getElementById('hanabi_req').value);
	strReq += '&mode=' + document.getElementById('hanabi_mode').value;
	if (m_iAnime > m_iWait + m_iRlMove + 2 || strReq != m_strReq) {
		m_iAnime = -1;
		document.getElementById('hanabi_alert').innerHTML = '<br />';
		fnGetValue_anohanabi(strReq,'hanabi_res');
		m_strReq = strReq;
	}
	return false;
}
function fnViewText_anohanabi() {
	var elmText = document.getElementById('hanabi_pn2');
	elmText.style.display = (elmText.style.display == 'block') ? 'none' : 'block';
	return false;
}

// EnterKey Event
var m_elmFocus;
function fnFocus_anohanabi(elmNum){
	m_elmFocus = elmNum
	elmNum.style.backgroundColor = '#ccf';
	elmNum.select();
}
function fnBlur_anohanabi(elmNum){
	elmNum.style.backgroundColor = '';
}
function fnGetKeycode_anohanabi(e){
	if (document.all) {
		return event.keyCode;
	} else if (document.getElementById) {
		return (e.keyCode!=0) ? e.keyCode : e.charCode;
	} else if(document.layers){
		return e.which;
	}
}
function fnKeyPress_anohanabi(e){
	if(fnGetKeycode_anohanabi(e) == 13) {
		if        (m_elmFocus.id == 'hanabi_send') { document.getElementById('hanabi_req').focus();  fnSend_anohanabi();
		} else if (m_elmFocus.id == 'hanabi_req')  { document.getElementById('hanabi_send').focus(); fnSend_anohanabi();
		}
		return false;
	}
}

document.onkeypress = fnKeyPress_anohanabi;
