// ********** Common
var m_dStart;
var m_bFocus = 0;

// ********** Const
var m_iWait = 5;
var m_iRlMove = 20;
var m_iRlGroup = 1;
var m_iCvWt = 600;
var m_iCvHt = 200;
var m_iCvWt2 = 300;
var m_iCvHt2 = 500;
var m_iPcSize;
var m_iPcSize2;
var m_iMarginX;
var m_iMarginY;

var m_iLineWidth = 1;

// ********** Draw
var m_elmPnm;
var m_elmPns;
var m_iMainLayer = 2;
var m_astrArrayCv = new Array('mnst','mndm','spst','spdm');
var m_aelmCv = [];
var m_actxCv = [];

var m_iAnime;
var m_timAnime;
var m_strDefine;
var m_astrText = [];
var m_aiX0 = [];
var m_aiY0 = [];
var m_aiX1 = [];
var m_aiY1 = [];
var m_aiX2 = [];
var m_aiY2 = [];
var m_iStPiece = 0;
var m_iDyPiece = 0;
var m_dStart;
var m_iShotLast;

var m_strReq = '';

window.onload = sbInit;

function sbInit() {
	// Init Canvas
	m_elmPnm = document.getElementById('yurai_pn');
	m_elmPns = document.getElementById('yurai_pns');
	for (iIdx = 0; iIdx < m_astrArrayCv.length; iIdx++) {
		strIdx = m_astrArrayCv[iIdx];
		m_aelmCv[strIdx] = document.getElementById('yurai_cv' + iIdx);
		m_actxCv[strIdx] = m_aelmCv[strIdx].getContext('2d');
	}
	sbInitGame();
	
	// Start Timer
	m_timAnime = setInterval('sbAnime()',200);
}

// ********************************************************************************
function sbInitGame() {
	var iIdx,strIdx,iTemp;
	
	// ********** Resize Canvas
	m_elmPnm.style.width  = m_iCvWt + 'px';
	m_elmPnm.style.height = m_iCvHt + 'px';
	m_elmPns.style.width  = m_iCvWt2 + 'px';
	m_elmPns.style.height = m_iCvHt2 + 'px';
	for (iIdx = 0; iIdx < m_astrArrayCv.length; iIdx++) {
		strIdx = m_astrArrayCv[iIdx];
		if (iIdx < m_iMainLayer) {
			m_aelmCv[strIdx].width  = m_iCvWt;
			m_aelmCv[strIdx].height = m_iCvHt;
		} else {
			m_aelmCv[strIdx].width  = m_iCvWt2;
			m_aelmCv[strIdx].height = m_iCvHt2;
		}
		m_actxCv[strIdx].lineWidth = m_iLineWidth;
	}
	
	// ********** Clear And Hidden
	m_actxCv['mnst'].clearRect(0,0,m_iCvWt,m_iCvHt);
	m_actxCv['mndm'].clearRect(0,0,m_iCvWt,m_iCvHt);
	m_actxCv['spst'].clearRect(0,0,m_iCvWt2,m_iCvHt2);
	m_actxCv['spdm'].clearRect(0,0,m_iCvWt2,m_iCvHt2);
	
	// ********** Init Value
	m_strDefine = '';
}

function sbAnime() {
	var iIdx,iIdx2;
	var iMaxX,iMaxY;
	var iPcW,iPcH;
	var strDefine;
	var astrDefine = [];
	var iPar;
	var iXi,iYi;
	
	strDefine = '' + document.getElementById('yurai_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(',');
		m_aiX2 = astrDefine[5].split(',');
		m_aiY2 = astrDefine[6].split(',');
		
		if (m_astrText.length > 1) {
			if (m_astrText[1] != '') {
				document.getElementById('yurai_alert').innerHTML = '<span class="info">' + m_astrText[1] + '</span>';
			}
		}
		
		// 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]);
			m_aiX2[iIdx] = Number(m_aiX2[iIdx]);
			m_aiY2[iIdx] = Number(m_aiY2[iIdx]);
			iMaxX = (m_aiX1[iIdx] > iMaxX) ? m_aiX1[iIdx] : iMaxX;
			iMaxY = (m_aiY1[iIdx] > iMaxY) ? m_aiY1[iIdx] : iMaxY;
			iMaxX = (m_aiX2[iIdx] > iMaxX) ? m_aiX2[iIdx] : iMaxX;
			iMaxY = (m_aiY2[iIdx] > iMaxY) ? m_aiY2[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_iPcSize2 = m_iPcSize * (m_iCvWt2 / m_iCvWt)
		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_aiX2[iIdx] = m_aiX2[iIdx] * m_iPcSize + m_iMarginX;
			m_aiY2[iIdx] = m_aiY2[iIdx] * m_iPcSize + m_iMarginY;
		}
		
		sbDrawBack(true);
		
		// Clear Canvas
		m_actxCv['mnst'].clearRect(0,0,m_iCvWt,m_iCvHt);
		m_actxCv['mndm'].clearRect(0,0,m_iCvWt,m_iCvHt);
		m_actxCv['spst'].clearRect(0,0,m_iCvWt2,m_iCvHt2);
		m_actxCv['spdm'].clearRect(0,0,m_iCvWt2,m_iCvHt2);
		
		// Static Circle
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			sbDrawPiece(m_actxCv['mnst'],m_aiX0[iIdx],m_aiY0[iIdx],m_iPcSize,true);
		}
		
		m_iShotLast = -1;
		
		// ********** Time
		if (m_bFocus == 0) {
			document.getElementById('yurai_wait').style.display = 'none';
			document.getElementById('yurai_all').style.display = 'block';
			document.getElementById('yurai_req').focus();
			m_bFocus = 1;
		}
		m_dStart = new Date();
	}
	
	var iShotIdx;
	if (m_iAnime >= 0 && m_iAnime <= m_iWait + m_iRlMove) {
		// Clear Canvas
		m_actxCv['mndm'].clearRect(0,0,m_iCvWt,m_iCvHt);
		
		iPar = (m_iAnime - m_iWait) / m_iRlMove;
		iPar = (iPar < 0 ? 0 : iPar);
		
		iShotIdx = Math.floor(iPar * (m_iCvHt2 / (m_iCvWt2 * m_iCvHt / m_iCvWt) - 1));
		
		// Snap Canvas
		if (iShotIdx > m_iShotLast) {
			for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
				sbDrawPiece(m_actxCv['spst'],m_aiX0[iIdx] * m_iCvWt2 / m_iCvWt,m_aiY0[iIdx] * m_iCvWt2 / m_iCvWt + iShotIdx * (m_iCvWt2 * m_iCvHt / m_iCvWt),m_iPcSize2,true);
			}
		}
		
		// Dynamic Circle
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			iXi = m_aiX1[iIdx] + (m_aiX2[iIdx] - m_aiX1[iIdx]) * iPar;
			iYi = m_aiY1[iIdx] + (m_aiY2[iIdx] - m_aiY1[iIdx]) * iPar;
			sbDrawPiece(m_actxCv['mndm'],iXi,iYi,m_iPcSize,false);
			
			// SnapShot
			if (iShotIdx > m_iShotLast) {
				sbDrawPiece(m_actxCv['spdm'],iXi * m_iCvWt2 / m_iCvWt,iYi * m_iCvWt2 / m_iCvWt + iShotIdx * (m_iCvWt2 * m_iCvHt / m_iCvWt),m_iPcSize2,false);
			}
		}
		
		// SnapShot
		if (iShotIdx > m_iShotLast) {
			m_iShotLast = iShotIdx;
		}
	}
	
	// End Animation
	if (m_iAnime == m_iWait + m_iRlMove + m_iWait) {
		sbDrawBack(false);
		if (m_astrText.length > 2) {
			sbGetAmazon('links','yurai-22','',EscapeUTF8(m_astrText[2]),'',220,2);
		}
		if (m_astrText.length > 4) {
			if (m_astrText[3] != '' && m_astrText[4] != '') {
				if (document.getElementById('yurai_link')) {
					document.getElementById('yurai_link').innerHTML = '<a href="' + m_astrText[3] + '" class="info" target="_blank">' + m_astrText[4] + '</a>';
				} else {
					document.getElementById('yurai_alert').innerHTML = '<a href="' + m_astrText[3] + '" class="info" target="_blank">' + m_astrText[4] + '</a>';
				}
			}
		}
		
		//Check Time
		dNow = new Date();
	}
	
	// Add FrameCount
	if (m_iAnime >= 0 && m_iAnime <= m_iWait + m_iRlMove + m_iWait) {
		m_iAnime ++;
	}
}

function sbDrawBack(bStart) {
	var strColor = bStart ? '#ffa' : '#fff';
	m_elmPnm.style.backgroundColor = strColor;
	m_elmPns.style.backgroundColor = strColor;
	if (bStart) {
		m_actxCv['mnst'].fillStyle = '#888';
		m_actxCv['mndm'].fillStyle = '#aa6';
		m_actxCv['spst'].fillStyle = '#888';
		m_actxCv['spdm'].fillStyle = '#aa6';
		m_actxCv['mnst'].strokeStyle = '#000';
		m_actxCv['mndm'].strokeStyle = '#000';
		m_actxCv['spst'].strokeStyle = '#000';
		m_actxCv['spdm'].strokeStyle = '#000';
	}
}

function sbDrawPiece(ctxCv,iXi,iYi,iSize,bStatic) {
	iXi = Math.floor(iXi);
	iYi = Math.floor(iYi);
	iSize = Math.floor(iSize);
	ctxCv.fillRect(iXi,iYi,iSize,iSize);
	ctxCv.strokeRect(iXi,iYi,iSize,iSize);
}

function fnSend_anoyurai() {
	var strReq;
	strReq = '';
	strReq += 'nm=' + EscapeEUCJP(document.getElementById('yurai_req').value);
	strReq += '&mode=' + document.getElementById('yurai_mode').value;
	if (m_iAnime > m_iWait + m_iRlMove + 2 || strReq != m_strReq) {
		m_iAnime = -1;
		document.getElementById('yurai_alert').innerHTML = '';
		fnGetValue_anoyurai(strReq,'yurai_res');
		m_strReq = strReq;
	}
	return false;
}
function fnViewText_anoyurai() {
	m_elmPnm.style.display = (m_elmPnm.style.display == 'block') ? 'none' : 'block';
	m_elmPns.style.display = (m_elmPnm.style.display == 'block') ? 'none' : 'block';
	return false;
}

// EnterKey Event
var m_elmFocus;
function fnFocus_anoyurai(elmNum){
	m_elmFocus = elmNum
	elmNum.style.backgroundColor = '#ffc';
	elmNum.select();
}
function fnBlur_anoyurai(elmNum){
	elmNum.style.backgroundColor = '';
}
function fnGetKeycode_anoyurai(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_anoyurai(e){
	if(fnGetKeycode_anoyurai(e) == 13) {
		if        (m_elmFocus.id == 'yurai_send') { document.getElementById('yurai_req').focus();  fnSend_anoyurai();
		} else if (m_elmFocus.id == 'yurai_req')  { document.getElementById('yurai_send').focus(); fnSend_anoyurai();
		}
		return false;
	}
}

document.onkeypress = fnKeyPress_anoyurai;
