// ********** 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_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_aiX2 = new Array();
var m_aiY2 = 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('yurai_cv');
	m_ctxCv1 = m_elmCv1.getContext('2d');
	m_elmCv2 = document.getElementById('yurai_cv2');
	m_ctxCv2 = m_elmCv2.getContext('2d');
	
	// Start Timer
	m_timAnime = setInterval('sbAnime()',100);
}

function sbAnime() {
	var iIdx,iIdx2;
	var iMaxX,iMaxY;
	var iPcW,iPcH;
	var strDefine;
	var astrDefine = new Array();
	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>';
				document.getElementById('yurai_alert').style.display = 'block';
			}
		}
		
		// 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_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;
		}
		
		m_elmCv1.style.backgroundColor = '#ffa';
		m_elmCv2.style.backgroundColor = '#fff';
		
		// Top Canvas
		m_ctxCv2.clearRect(0,0,m_iCvWt,m_iCvHt);
		m_ctxCv2.fillStyle = '#888';
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			sbDrawPiece(m_ctxCv2,m_aiX0[iIdx],m_aiY0[iIdx]);
		}
		m_ctxCv2.fillStyle = '#aa6';
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			sbDrawPiece(m_ctxCv2,m_aiX1[iIdx],m_aiY1[iIdx]);
		}
		m_ctxCv1.lineWidth = 1;
		m_ctxCv1.strokeStyle = '#000';
		
		
		// ********** 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();
	}
	
	if (m_iAnime >= 0 && m_iAnime <= m_iWait + m_iRlMove) {
		// Clear Canvas
		m_ctxCv1.clearRect(0,0,m_iCvWt,m_iCvHt);
		
		// Static Circle
		m_ctxCv1.fillStyle = '#888';
		for (iIdx = 0; iIdx < m_iStPiece; iIdx++) {
			sbDrawPiece(m_ctxCv1,m_aiX0[iIdx],m_aiY0[iIdx]);
		}
		
		// Dynamic Circle
		m_ctxCv1.fillStyle = '#aa6';
		for (iIdx = 0; iIdx < m_iDyPiece; iIdx++) {
			iPar = (m_iAnime - m_iWait) / (m_iRlMove);
			iPar = (iPar < 0 ? 0 : iPar);
			iXi = m_aiX1[iIdx] + (m_aiX2[iIdx] - m_aiX1[iIdx]) * iPar;
			iYi = m_aiY1[iIdx] + (m_aiY2[iIdx] - m_aiY1[iIdx]) * iPar;
			sbDrawPiece(m_ctxCv1,Math.round(iXi),Math.round(iYi));
		}
	}
	
	// End Animation
	if (m_iAnime == m_iWait + m_iRlMove + 2) {
		document.getElementById('yurai_cv').style.backgroundColor = '#fff';
		if (m_astrText.length > 2) {
			sbGetAmazon('links','yurai-22','',EscapeUTF8(m_astrText[2]),'',220,2);
		}
		
		//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) {
	ctxCv.fillRect(iXi,iYi,m_iPcSize,m_iPcSize);
	ctxCv.strokeRect(iXi,iYi,m_iPcSize,m_iPcSize);
}

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 = '';
		document.getElementById('yurai_alert').style.display = 'none';
		fnGetValue_anoyurai(strReq,'yurai_res');
		m_strReq = strReq;
	}
	return false;
}
function fnViewText_anoyurai() {
	var elmText = document.getElementById('yurai_pn2');
	elmText.style.display = (elmText.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;
