// ********** Common
var m_dStart;
var m_bFocus = 0;

// ********** Const
var m_iRlMove = 10;
var m_iCvWt = 640;
var m_iCvHt = 640;
var m_iCvMg = 50;
var m_iDataCnt = 10;

// ********** Draw
var m_elmTx;
var m_elmMs;
var m_elmPn;
var m_elmCv;
var m_ctxCv;
var m_elmNb;

var m_iAnime;
var m_timAnime;
var m_strDefine;

var m_astrID = new Array();
var m_astrText = new Array();
var m_aiX = new Array();
var m_aiY = new Array();
var m_aiW = new Array();
var m_aiH = new Array();
var m_aiS = new Array();
var m_aiOpt0 = new Array();
var m_aiOpt1 = new Array();
var m_aiOpt2 = new Array();

var m_iPictCnt = 0;
var m_strHTML = '';
var m_iMinX = 0;
var m_iMinY = 0;
var m_iMaxX = 0;
var m_iMaxY = 0;
var m_iZoom;

var m_strAmaKey = '';

window.onload = sbInit;

function sbInit() {
	// Init Canvas
	m_elmPn = document.getElementById('heimen_pn');
	m_elmCv = document.getElementById('heimen_cv');
	m_elmTx = document.getElementById('heimen_text');
	m_elmMs = document.getElementById('heimen_ms');
	m_ctxCv = m_elmCv.getContext('2d');
	
	// Create NameBox
	m_elmNb = document.getElementById('heimen_nb');
	
	// Start Timer
	m_timAnime = setInterval('sbAnime()',100);
}

function sbAnime() {
	var astrPict = new Array();
	var astrDefine = new Array();
	var strDefine;
	var iRange;
	var iCvSize;
	var strHTML;
	
	strDefine = '' + document.getElementById('heimen_res').value;
	
	// Start Animation
	if (strDefine != '' && strDefine != m_strDefine) {
		m_iAnime = 0;
		m_strDefine = strDefine;
		
		// Init
		astrDefine = strDefine.split('~');
		m_strHTML = astrDefine[1];
		astrPict = astrDefine[2].split(',');
		if (astrDefine.length > 3) {
			m_strAmaKey = astrDefine[3];
		} else {
			m_strAmaKey = '';
		}

		
		// Count Relation
		if (astrPict == '') {
			m_iPictCnt = 0;
		} else {
			m_iPictCnt = astrPict.length / m_iDataCnt;
		}
		iPictIdx = -1;
		
		// Get Min And Max
		m_iMinX = 0.5;
		m_iMinY = 0.5;
		m_iMaxX = 0.5;
		m_iMaxY = 0.5;
		for (iIdx = 0; iIdx < m_iPictCnt; iIdx++) {
			m_astrID[iIdx] = astrPict[iIdx * m_iDataCnt + 0];
			m_astrText[iIdx] = astrPict[iIdx * m_iDataCnt + 1];
			m_aiX[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 2]);
			m_aiY[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 3]);
			m_aiW[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 4]);
			m_aiH[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 5]);
			m_aiS[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 6]);
			m_aiOpt0[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 7]);
			m_aiOpt1[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 8]);
			m_aiOpt2[iIdx] = Number(astrPict[iIdx * m_iDataCnt + 9]);
			
			m_iMinX = (m_iMinX == 0.5 || (m_aiX[iIdx]) < m_iMinX) ? (m_aiX[iIdx]) : m_iMinX;
			m_iMinY = (m_iMinY == 0.5 || (m_aiY[iIdx]) < m_iMinY) ? (m_aiY[iIdx]) : m_iMinY;
			m_iMaxX = (m_iMaxX == 0.5 || (m_aiX[iIdx] + m_aiW[iIdx]) > m_iMaxX) ? (m_aiX[iIdx] + m_aiW[iIdx]) : m_iMaxX;
			m_iMaxY = (m_iMaxY == 0.5 || (m_aiY[iIdx] + m_aiH[iIdx]) > m_iMaxY) ? (m_aiY[iIdx] + m_aiH[iIdx]) : m_iMaxY;
		}
		iRange = (m_iMaxY - m_iMinY > m_iMaxX - m_iMinX) ? m_iMaxY - m_iMinY : m_iMaxX - m_iMinX;
		iRange = (iRange == 0) ? 1 : iRange;
		iCvSize = ((m_iCvWt < m_iCvHt) ? m_iCvWt : m_iCvHt) - m_iCvMg * 2;
		m_iZoom = iCvSize / iRange;
		
		// Adjust X And Y
		for (iIdx = 0; iIdx < m_iPictCnt; iIdx++) {
			m_aiW[iIdx] = Math.floor(m_aiW[iIdx] * m_iZoom);
			m_aiH[iIdx] = Math.floor(m_aiH[iIdx] * m_iZoom);
			m_aiX[iIdx] = Math.floor((m_aiX[iIdx] - m_iMinX) * m_iZoom + m_iCvMg + (iCvSize - (m_iMaxX - m_iMinX) * m_iZoom) / 2);
			m_aiY[iIdx] = Math.floor((m_aiY[iIdx] - m_iMinY) * m_iZoom + m_iCvMg + (iCvSize - (m_iMaxY - m_iMinY) * m_iZoom) / 2);
		}
		
		m_ctxCv.clearRect(0,0,m_iCvWt,m_iCvHt);
		m_elmNb.innerHTML = '';
		m_elmMs.innerHTML = '';
		m_elmTx.value = '';
		
		// ********** Time
		if (m_bFocus == 0) {
			document.getElementById('heimen_wait').style.display = 'none';
			document.getElementById('heimen_all').style.display = 'block';
			document.getElementById('heimen_req0').focus();
			m_bFocus = 1;
		}
		m_dStart = new Date();
		
		strHTML = m_strHTML.replace(/#/g, "\n") + "\n";
		if (m_elmTx.value == '') {
			m_elmTx.value += strHTML + "\n";
		}
		
//alert(m_iMinX + "\n" + m_iMinY + "\n" + m_iMaxX + "\n" + m_iMaxY)
	}
	
	var iIdx;
	var iMotion;
	if (m_iAnime < m_iRlMove * m_iPictCnt) {
		// Draw Label
		iIdx = Math.floor(m_iAnime / m_iRlMove);
		iMotion = (m_iAnime - (iIdx * m_iRlMove));
		sbDrawLabel(iIdx,iMotion)
	}
	
	// End Animation
	if (m_iAnime == m_iRlMove * m_iPictCnt + 2) {
		m_ctxCv.lineWidth = 0.5;
		m_ctxCv.strokeStyle = '#ccc';
		m_ctxCv.strokeRect(1,1,m_iCvWt - 2,m_iCvHt - 2);
		strHTML = m_strHTML.replace(/#/g, '<br />') + '<br />';
		if (m_elmMs.innerHTML == '') {
			m_elmMs.innerHTML = strHTML;
		}
		//Amazon
		if (m_strAmaKey != '') {
			sbGetAmazon('links','heimen-22','',EscapeUTF8(m_strAmaKey),'',300,2);
		}
	}
	
	// Add FrameCount
	if (m_iAnime <= m_iRlMove * m_iPictCnt + 2) {
		m_iAnime ++;
	}
}

function sbDrawLabel(iIdx,iMotion) {
	var strID = m_astrID[iIdx];
	var strText = m_astrText[iIdx];
	var iX = m_aiX[iIdx];
	var iY = m_aiY[iIdx];
	var iW = m_aiW[iIdx];
	var iH = m_aiH[iIdx];
	var iS = m_aiS[iIdx];
	var iOpt0 = m_aiOpt0[iIdx];
	var iOpt1 = m_aiOpt1[iIdx];
	var iOpt2 = m_aiOpt2[iIdx];
	var ilineWidth;
	var iLoc0;
	var iLoc1;
	var strTemp;
	
	if        (strID == '') {
	} else if (strID == 'room' || strID == 'home') {
		//Opt1:Door
		//Opt2:Window
		//Opt3:
		
		ilineWidth = (strID == 'room' ? 1 : 2);
		if        (iMotion == 0) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.beginPath(); m_ctxCv.moveTo(iX     ,iY     ); m_ctxCv.lineTo(iX + iW,iY     ); m_ctxCv.stroke();
		} else if (iMotion == 1) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.beginPath(); m_ctxCv.moveTo(iX     ,iY + iH); m_ctxCv.lineTo(iX + iW,iY + iH); m_ctxCv.stroke();
		} else if (iMotion == 2) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.beginPath(); m_ctxCv.moveTo(iX     ,iY     ); m_ctxCv.lineTo(iX     ,iY + iH); m_ctxCv.stroke();
		} else if (iMotion == 3) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.beginPath(); m_ctxCv.moveTo(iX + iW,iY     ); m_ctxCv.lineTo(iX + iW,iY + iH); m_ctxCv.stroke();
		} else if (iMotion == 4) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.strokeRect(iX      - m_iZoom * 0.1,iY      - m_iZoom * 0.1,m_iZoom * 0.2,m_iZoom * 0.2);
		} else if (iMotion == 5) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.strokeRect(iX + iW - m_iZoom * 0.1,iY      - m_iZoom * 0.1,m_iZoom * 0.2,m_iZoom * 0.2);
		} else if (iMotion == 6) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.strokeRect(iX      - m_iZoom * 0.1,iY + iH - m_iZoom * 0.1,m_iZoom * 0.2,m_iZoom * 0.2);
		} else if (iMotion == 7) { m_ctxCv.strokeStyle = '#000'; m_ctxCv.lineWidth = ilineWidth; m_ctxCv.strokeRect(iX + iW - m_iZoom * 0.1,iY + iH - m_iZoom * 0.1,m_iZoom * 0.2,m_iZoom * 0.2);
		} else if (iMotion == 8) {
			if (strID == 'room') {
				m_ctxCv.strokeStyle = '#888'; m_ctxCv.lineWidth = ilineWidth; 
				m_ctxCv.beginPath();
				iLoc0 = (iOpt0 == 0 || iOpt0 == 24) ? 0 : 1;
				iLoc1 = (iOpt0 == 0 || iOpt0 == 36) ? 0 : 1;
				if (iLoc0 == 0) {
					m_ctxCv.moveTo(iX + iW * iLoc1 - m_iZoom * 0.2 * (iLoc1 * 2 - 1),iY + iH * iLoc1);
					m_ctxCv.arc   (iX + iW * iLoc1 - m_iZoom * 0.2 * (iLoc1 * 2 - 1),iY + iH * iLoc1,m_iZoom * 1,(180 * iLoc1 + 270) * Math.PI / 180,(180 * iLoc1 +   0) * Math.PI / 180,0);
				} else {
					m_ctxCv.moveTo(iX + iW * iLoc1,iY + iH * (1 - iLoc1) + m_iZoom * 0.2 * (iLoc1 * 2 - 1));
					m_ctxCv.arc   (iX + iW * iLoc1,iY + iH * (1 - iLoc1) + m_iZoom * 0.2 * (iLoc1 * 2 - 1),m_iZoom * 1,(180 * iLoc1 + 180) * Math.PI / 180,(180 * iLoc1 + 270) * Math.PI / 180,0);
				}
				m_ctxCv.stroke();
			}
		} else if (iMotion == 9) {
			strTemp = strText.replace(/#/g, '<br />');
			strTemp = strTemp.replace(/%0%/g, '<span class="alpha">');
			strTemp = strTemp.replace(/%1%/g, '</span>');
			m_elmNb.innerHTML += '<table style="left:' + iX + 'px; top:' + iY + 'px; width: ' + iW + 'px;height: ' + iH + 'px;"><tr><td>' + strTemp + '</td></tr></table>';
			
			strTemp = strText.replace(/#/g, '');
			strTemp = strTemp.replace(/%0%/g, '');
			strTemp = strTemp.replace(/%1%/g, '');
			m_elmTx.value += strTemp + "\n";
		} else if (iMotion == 30) { m_ctxCv.fillStyle   = '#fff';                        m_ctxCv.fillRect(iX + 1,iY + 1,iW - 2,iH - 2);
		}
	} else if (strID == '') {
	} else if (strID == '') {
	} else if (strID == '') {
	}
}

function fnSend_anoheimen() {
	var strReq;
	var strVal0;
	
	strVal0 = document.getElementById('heimen_req0').value.replace(/\n|\r/g,';');
	
	strReq = '';
	strReq +=  'nm=' + EscapeEUCJP(strVal0);
	
	strReq += '&mode=' + document.getElementById('heimen_mode').value;
	if (m_iAnime == 0 || m_iAnime > m_iRlMove * m_iPictCnt) {
		fnGetValue_anoheimen(strReq,'heimen_res');
	}
	return false;
}
function fnSend_changemode() {
	m_elmTx.style.display = (m_elmTx.style.display == 'block') ? 'none' : 'block';
	m_elmPn.style.display = (m_elmTx.style.display == 'block') ? 'none' : 'block';
	return false;
}

// EnterKey Event
function fnFocus_anoheimen(elmNum){
	elmNum.style.backgroundColor = '#eeeecc';
}
function fnBlur_anoheimen(elmNum){
	elmNum.style.backgroundColor = '';
}
