//───────────────────────────────────────────
//内容
//	地図の操作（ドラッグ・クリック・スライダ等）
//
//(C) Nihon Computer Graphic Co.,Ltd.
//───────────────────────────────────────────

var bRubber		= true;					//ラバーDIV			表示、非表示
var bRubberMsg	= true;					//ラバーメッセージDIV	表示、非表示
var divRubber		= null;				//ラバーDIV
var divRubbermsgin	= null;				//ラバーメッセージDIV
var divRubbermsgout	= null;				//ラバーメッセージDIV


//───────────────────────────────────────────
//	スライダ
//───────────────────────────────────────────
// スライダの登録
function regSlider( lenbar, firstpoint, lab ){
	var objSlider = getDivFromName( "sliderball" );
	Slider( objSlider, lab, lenbar, 0, ( lenbar - 1 ), firstpoint, slide, null, slide_ing, null, slide_ed, null );
}
// スライダ操作前
function slide(ctrl,client,pos){
	DBH_preSlidePos = pos;
	//
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		divRubber = createLayer( 0, 0, 0, 0 );
		setDivStyleAttribute( divRubber, "fontSize", "0px" );
		setDivStyleAttribute( divRubber, "border", "1px solid #FF00FF" );
		setDivZIndex( divRubber, 840 );
		moveDivTo( divRubber, DBH_mapX, DBH_mapY );
		resizeDivTo( divRubber, DBH_mapWid, DBH_mapHei );
		if( !bRubber ){
			setDivVisibility( divRubber, false );
		}
		else{
			setDivVisibility( divRubber, true );
		}
		//メッセージレイヤ作成
		var strHtml;
		strHtml = "<table cellpadding='1' cellspacing='0' bgcolor='#ffffff' style='filter:alpha(opacity=85);' border='1' bordercolor='#CFCFCF'><tr><td><table cellpadding='0' cellspacing='0'  border='0'><tr><td class='nom' nowrap>" +DBH_ZoomInMsg+ "</td></tr></table></td></tr></table>";
		divRubbermsgin  = createLayer( 0, 0, 0, 0, null, strHtml );
		setDivZIndex( divRubbermsgin, 850 );
		strHtml = "<table cellpadding='1' cellspacing='0' bgcolor='#ffffff' style='filter:alpha(opacity=85);' border='1' bordercolor='#CFCFCF'><tr><td><table cellpadding='0' cellspacing='0'  border='0'><tr><td class='nom' nowrap>" +DBH_ZoomOutMsg+ "</td></tr></table></td></tr></table>";
		divRubbermsgout = createLayer( 0, 0, 0, 0, null, strHtml );
		setDivZIndex( divRubbermsgout, 850 );
	}
}
// スライダ操作中
function slide_ing(ctrl,client,pos){
	var dScl = GetSliderScale( pos, divSlideLen );
	slideScale( dScl );
	//
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		var dWid, dHei, strCol, iX, iY;
		var dRate = parseInt(document.paramform.scl.value) / dScl;
		if( parseInt(document.paramform.scl.value) < dScl ){
			dWid = parseFloat( DBH_mapWid * dRate );
			dHei = parseFloat( DBH_mapHei * dRate );
			strCol = "#FF0000";
		}
		else{
			dWid = parseFloat( DBH_mapWid / dRate );
			dHei = parseFloat( DBH_mapHei / dRate );
			strCol = "#0000FF";
		}
		iX = DBH_mapX + parseInt( ( DBH_mapWid - dWid ) / 2.0 );
		iY = DBH_mapY + parseInt( ( DBH_mapHei - dHei ) / 2.0 );
		setDivStyleAttribute( divRubber, "border", "2px solid " + strCol );
		moveDivTo( divRubber, iX, iY );
		resizeDivTo( divRubber, Math.round( dWid ), Math.round( dHei ) );

		var dspmsgX, dspmsgY
		dspmsgX = iX;
		if( dHei < 30 )
			dspmsgY = iY + dHei;
		else
			dspmsgY = iY;
		//
		if( parseInt(document.paramform.scl.value) < dScl ){
			setDivVisibility( divRubbermsgin, false );
			moveDivTo( divRubbermsgout, dspmsgX, dspmsgY );
			setDivVisibility( divRubbermsgout, true );
		}
		else{
			setDivVisibility( divRubbermsgout, false );
			moveDivTo( divRubbermsgin, dspmsgX, dspmsgY );
			setDivVisibility( divRubbermsgin, true );
		}
		if( !bRubberMsg || !bRubber ){
			setDivVisibility( divRubbermsgin, false );
			setDivVisibility( divRubbermsgout, false );
			if( !bRubber ){
				setDivVisibility( divRubber, false );
			}
		}
	}
}
// スライダ操作後
function slide_ed(ctrl,client,pos){
	setDivVisibility( divRubber, false );
	setDivVisibility( divRubbermsgin, false );
	setDivVisibility( divRubbermsgout, false );

	if( DBH_preSlidePos == pos ){	//スライダが移動していない場合は処理しません
		return;
	}
	//
	for(i = createLayerNoStart; i <= createLayerNoEnd; i++){
		divicon = getDivFromName('_js_layer_' + i);
		if( divicon ) setDivVisibility(divicon, false);
	}

	var dScl = GetSliderScale( pos, divSlideLen );
	
	var scLeft = document.body.scrollLeft;
	var scTop  = document.body.scrollTop;
	if( null == scLeft ) scLeft = 0;
	if( null == scTop )  scTop  = 0;
	
	changeMapscale("", Math.round(dScl));
}
// スライダのスケール表示
function slideScale(scl){
	if( 1000.0 > scl ){
		tmp = String( parseInt( scl ) ).substr( 0, 2 ) + "0m";
	}
	else if( 10000.0 > scl ){
		var tmp = String( scl / 1000 );
		if( 1 == tmp.length ) tmp = tmp + ".0";
		tmp = tmp.substring( 0, 3 ) + "km";
	}
	else if( 20000.0 > scl ){
		tmp = String( parseInt( scl / 1000.0 ) ) + "km";
	}
	else if( 100000.0 > scl ){
		tmp = String( parseInt( scl / 1000.0 ) ).substr( 0, 1 ) + "0km";
	}
	else{
		tmp = String( parseInt( scl / 1000.0 ) ).substr( 0, 2 ) + "0km";
	}
	
	if( document.sclform )
		document.sclform.scl.value = tmp
}

arrScl = new Array( 250.0, 500.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0, 100000.0, 200000.0 );

// スライダの位置からスケールを取得します
function GetSliderScale( pos, lenbar ){
	var dScl;
	var dLenSpan = lenbar / 8.0;

	pos = Math.round( pos );
	dScl = arrScl[2];													//デフォルト
	if( ( lenbar - 1 ) <= pos ){
		dScl = arrScl[0];												//250
	}
	else if( pos <= 0 ){
		dScl = arrScl[8];												//200000
	}
	else{
		for( n = 0; n <= 7; n++ ){
			if( ( ( lenbar - 1 ) - ( dLenSpan * ( n + 1 ) ) ) < pos ){	//250〜200000
				dScl = arrScl[n] + ( ( ( lenbar - 1 ) - ( dLenSpan * n ) ) - pos ) * ( ( arrScl[( n + 1 )] - arrScl[n] ) / dLenSpan );
				break;
			}
		}
	}
	dScl = Math.round( dScl );
	return dScl;
}
// スケールからスライダの位置を取得します
function GetSliderPosition( dScl, lenbar ){
	var pos;
	var dLenSpan = lenbar / 8.0;

	dScl = Math.round( dScl );
	pos = 0;													//デフォルト
	if( dScl <= arrScl[0] ){									//〜250
		pos = ( lenbar - 1 );
	}
	else if( arrScl[8] <= dScl ){								//200000〜
		pos = 0;
	}
	else{
		for( n = 1; n <= 8; n++ ){
			if( dScl < arrScl[n] ){								//250〜200000
				pos = ( ( lenbar - 1 ) - dLenSpan * ( n - 1 ) ) - dLenSpan * ( dScl - arrScl[( n - 1 )] ) / ( arrScl[n] - arrScl[( n - 1 )] );
				break;
			}
		}
	}

	pos = Math.round( pos );
	return pos;
}
//───────────────────────────────────────────

//───────────────────────────────────────────
//	マウスドラッグ・クリック
//───────────────────────────────────────────
// マウスドラッグの登録（地図）
function regDrag( clk ){
	var mp0 = getDivFromName('mainmap0');		//地図画像
	if("1" == clk){				//クリック移動
		ech_attachMouseDrag( mp0, null, null, null, null, null, null, mapclick, null );
		setDivStyleAttribute(getDivFromName("mainmap0"),"cursor","auto");
		setDivStyleAttribute(getDivFromName("mainmap1"),"cursor","auto");
		setDivStyleAttribute(getDivFromName("mainmap2"),"cursor","auto");
	}
	else if("2" == clk){		//ドラッグスクロール
		ech_attachMouseDrag( mp0, mapdrag, null, mapdraging, null, mapdraged, null, null, null );
		setDivStyleAttribute(getDivFromName("mainmap0"),"cursor","move");
		setDivStyleAttribute(getDivFromName("mainmap1"),"cursor","move");
		setDivStyleAttribute(getDivFromName("mainmap2"),"cursor","move");
	}
	else if("3" == clk){		//範囲拡大
		ech_attachMouseDrag( mp0, mapkaku, null, mapkakuing, null, mapkakued, null, null, null );
		setDivStyleAttribute(getDivFromName("mainmap0"),"cursor","crosshair");
		setDivStyleAttribute(getDivFromName("mainmap1"),"cursor","crosshair");
		setDivStyleAttribute(getDivFromName("mainmap2"),"cursor","crosshair");		
	}
	unlockLoading();
}
// ドラッグ移動前
function mapdrag(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		DBH_preX = ctrl.pageX;
		DBH_preY = ctrl.pageY;
		
		for(i = 0; i < cmnlayernoary.length; i++){
			divcmn = getDivFromName('_js_layer_' + cmnlayernoary[i]);
			if( divcmn ) setDivVisibility(divcmn, false);
		}
	}
}
// ドラッグ移動中
function mapdraging(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		var div;
		if(0 == DBH_mapload % 2)	div = getDivFromName('mainmap2');
		else						div = getDivFromName('mainmap1');
		var cpX, cpY;
		var ccX, ccY;
		cpX = ctrl.pageX;
		cpY = ctrl.pageY;
		ccX = ctrl.curX;
		ccY = ctrl.curY;
		
		if(DBH_mapX > cpX)
			cpX = DBH_mapX;
		else if(DBH_mapX+parseInt(DBH_mapWid) < cpX)
			cpX = DBH_mapX + parseInt(DBH_mapWid);
		if(DBH_mapY > cpY)
			cpY = DBH_mapY;
		else if(DBH_mapY+parseInt(DBH_mapHei) < cpY)
			cpY = DBH_mapY + parseInt(DBH_mapHei);

		if(DBH_mapX > ccX)
			ccX = DBH_mapX;
		else if(DBH_mapX+parseInt(DBH_mapWid) < ccX)
			ccX = DBH_mapX + parseInt(DBH_mapWid);
		if(DBH_mapY > ccY)
			ccY = DBH_mapY;
		else if(DBH_mapY+parseInt(DBH_mapHei) < ccY)
			ccY = DBH_mapY + parseInt(DBH_mapHei);
		moveDivBy(ctrl.div, cpX - ccX, cpY - ccY);
		moveDivBy(div, cpX - ccX, cpY - ccY);

		var clipTop    = DBH_preY - cpY;
		var clipRight  = parseInt(DBH_mapWid) + DBH_preX - cpX;
		var clipBottom = parseInt(DBH_mapHei) + DBH_preY - cpY;	
		var clipLeft   = DBH_preX - cpX;
		setDivClip(ctrl.div, clipTop, clipRight, clipBottom, clipLeft);
		setDivClip(div, clipTop, clipRight, clipBottom, clipLeft);
	}
}
// ドラッグ移動後
function mapdraged(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		var cpX, cpY;
		cpX = ctrl.pageX;
		cpY = ctrl.pageY;
		
		if(DBH_mapX > cpX)
			cpX = DBH_mapX;
		else if(DBH_mapX+parseInt(DBH_mapWid) < cpX)
			cpX = DBH_mapX + parseInt(DBH_mapWid);
		if(DBH_mapY > cpY)
			cpY = DBH_mapY;
		else if(DBH_mapY+parseInt(DBH_mapHei) < cpY)
			cpY = DBH_mapY + parseInt(DBH_mapHei);

		var ofsX  = DBH_preX - cpX;
		var ofsY  = DBH_preY - cpY;
		var moveX = (parseInt(DBH_mapWid) / 2.0) + ofsX;
		var moveY = (parseInt(DBH_mapHei) / 2.0) + ofsY;
		var posX  = new refvar();
		var posY  = new refvar();
		var newX, newY;

		GetPosOffset(parseFloat(document.paramform.x.value), parseFloat(document.paramform.y.value),
			parseInt(document.paramform.scl.value), parseInt(DBH_mapWid), parseInt(DBH_mapHei),
			parseInt(moveX), parseInt(moveY), posX, posY);
		newX = String(posX.value);
		newY = String(posY.value);

		changeMapxy("", newX, newY);
	}
}
// 範囲拡大前
function mapkaku(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		DBH_preX = ctrl.pageX;
		DBH_preY = ctrl.pageY;
		
		//
		divRubber = createLayer( 0, 0, 0, 0 );
		setDivStyleAttribute( divRubber, "fontSize", "0px" );
		setDivStyleAttribute( divRubber, "border", "2px solid #0000FF" );
		setDivZIndex( divRubber, 840 );
	}
}
// 範囲拡大中
function mapkakuing(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		var cpX, cpY;
		cpX = ctrl.pageX;
		cpY = ctrl.pageY;
		
		if(DBH_mapX > cpX)
			cpX = DBH_mapX;
		else if(DBH_mapX+parseInt(DBH_mapWid) < cpX)
			cpX = DBH_mapX + parseInt(DBH_mapWid);
		if(DBH_mapY > cpY)
			cpY = DBH_mapY;
		else if(DBH_mapY+parseInt(DBH_mapHei) < cpY)
			cpY = DBH_mapY + parseInt(DBH_mapHei);
		
		var mx, my;
		var mwid, whei;
		if(0 < DBH_preX - cpX){
			mx = cpX;
			mwid = DBH_preX - cpX;
		}
		else{
			mx = DBH_preX;
			mwid = cpX - DBH_preX;
		}
		if(0 < DBH_preY - cpY){
			my = cpY;
			mhei = DBH_preY - cpY;
		}
		else{
			my = DBH_preY;
			mhei = cpY - DBH_preY;
		}
		moveDivTo( divRubber, mx, my );
		resizeDivTo( divRubber, mwid, mhei );
		setDivVisibility( divRubber, true );
	}
}
// 範囲拡大後
function mapkakued(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		setDivVisibility( divRubber, false );
		var cpX, cpY;
		cpX = ctrl.pageX;
		cpY = ctrl.pageY;
		
		if( (DBH_preX == cpX)&&(DBH_preY == cpY) ){	//範囲指定されていない場合は処理しません
			return;
		}
		//
		for(i = createLayerNoStart; i <= createLayerNoEnd; i++){
			divicon = getDivFromName('_js_layer_' + i);
			if( divicon ) setDivVisibility(divicon, false);
		}
		if(DBH_mapX > cpX)
			cpX = DBH_mapX;
		else if(DBH_mapX+parseInt(DBH_mapWid) < cpX)
			cpX = DBH_mapX + parseInt(DBH_mapWid);
		if(DBH_mapY > cpY)
			cpY = DBH_mapY;
		else if(DBH_mapY+parseInt(DBH_mapHei) < cpY)
			cpY = DBH_mapY + parseInt(DBH_mapHei);
			
		var yokoH = Math.abs(DBH_preX - cpX) / parseInt(DBH_mapWid);
		var tateH = Math.abs(DBH_preY - cpY) / parseInt(DBH_mapHei);
		var yoko0 = (DBH_preX - cpX);
		var tate0 = (DBH_preY - cpY);
		var dScl;
		
		if(tateH > yokoH)
			dScl = (Math.abs(tate0) / parseInt(DBH_mapHei)) * parseInt(document.paramform.scl.value);
		else
			dScl = (Math.abs(yoko0) / parseInt(DBH_mapWid)) * parseInt(document.paramform.scl.value);
		
		if(Math.round(dScl) > 0){
			var moveX = ( (DBH_preX - DBH_mapX)+(cpX - DBH_mapX) ) / 2.0;
			var moveY = ( (DBH_preY - DBH_mapY)+(cpY - DBH_mapY) ) / 2.0;		
			var posX  = new refvar();
			var posY  = new refvar();
			GetPosClick(parseFloat(document.paramform.x.value), parseFloat(document.paramform.y.value),
				parseInt(document.paramform.scl.value), parseInt(DBH_mapWid), parseInt(DBH_mapHei),
				parseInt(moveX), parseInt(moveY), posX, posY);
			document.paramform.x.value   = String(posX.value);
			document.paramform.y.value   = String(posY.value);
			var newscl;
			if( 250.0 > dScl )
				newscl = 250;
			else
				newscl = Math.round(dScl);

			changeMapscale("", newscl);
		}
	}
}
// クリック移動
function mapclick(ctrl,client){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		for(i = 0; i < cmnlayernoary.length; i++){
			divcmn = getDivFromName('_js_layer_' + cmnlayernoary[i]);
			if( divcmn ) setDivVisibility(divcmn, false);
		}

		var cpX, cpY;
		cpX = ctrl.pageX;
		cpY = ctrl.pageY;
		
		var moveX = cpX - DBH_mapX;
		var moveY = cpY - DBH_mapY;
		var posX  = new refvar();
		var posY  = new refvar();
		var newX, newY, sclX, sclY;

		GetPosClick(parseFloat(document.paramform.x.value), parseFloat(document.paramform.y.value),
			parseInt(document.paramform.scl.value), parseInt(DBH_mapWid), parseInt(DBH_mapHei),
			parseInt(moveX), parseInt(moveY), posX, posY);
		newX = String(posX.value);
		newY = String(posY.value);
		sclX = parseInt(parseFloat(DBH_mapWid) / 2.0) - moveX;
		sclY = parseInt(parseFloat(DBH_mapHei) / 2.0) - moveY;
		
		DBH_mapmoving = true;
		scrollSlide(sclX, sclY);
		changeMapxy("", newX, newY);
	}
}
//───────────────────────────────────────────

//───────────────────────────────────────────
// 八方向スクロール
//───────────────────────────────────────────
function scroll8(dir){
	if( (!DBH_maploading)&&(!DBH_mapmoving) ){
		for(i = 0; i < cmnlayernoary.length; i++){
			divcmn = getDivFromName('_js_layer_' + cmnlayernoary[i]);
			if( divcmn ) setDivVisibility(divcmn, false);
		}

		var minX = new refvar();
		var minY = new refvar();
		var maxX = new refvar();
		var maxY = new refvar();
		var moveX, moveY;
		var newX, newY, sclX, sclY;
		var curX   = document.paramform.x.value;
		var curY   = document.paramform.y.value;
		var curScl = document.paramform.scl.value;
		
		GetRectClient(parseFloat(curX), parseFloat(curY), parseInt(curScl), parseInt(DBH_mapWid), parseInt(DBH_mapHei),
						minX, minY, maxX, maxY);
		moveX = (maxX.value - minX.value) * 0.8;		//移動量：画面の4/5
		moveY = (maxY.value - minY.value) * 0.8;
			
		if("1" == dir){			//北西
			newX = String( parseFloat(curX) - moveX );
			newY = String( parseFloat(curY) + moveY );
			sclX = parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}
		else if("2" == dir){	//北
			newX = curX;
			newY = String( parseFloat(curY) + moveY );
			sclX = 0;
			sclY = parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}
		else if("3" == dir){	//北東
			newX = String( parseFloat(curX) + moveX );
			newY = String( parseFloat(curY) + moveY );
			sclX = -parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}	
		else if("4" == dir){	//西
			newX = String( parseFloat(curX) - moveX );
			newY = curY;
			sclX = parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = 0;
		}
		else if("5" == dir){	//東
			newX = String( parseFloat(curX) + moveX );
			newY = curY;
			sclX = -parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = 0;
		}
		else if("6" == dir){	//南西
			newX = String( parseFloat(curX) - moveX );
			newY = String( parseFloat(curY) - moveY );
			sclX = parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = -parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}
		else if("7" == dir){	//南
			newX = curX;
			newY = String( parseFloat(curY) - moveY );
			sclX = 0;
			sclY = -parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}
		else if("8" == dir){	//南東
			newX = String( parseFloat(curX) + moveX );
			newY = String( parseFloat(curY) - moveY );
			sclX = -parseInt( parseFloat(DBH_mapWid) * 0.8 );
			sclY = -parseInt( parseFloat(DBH_mapHei) * 0.8 );
		}

		DBH_mapmoving = true;
		changeMapxy("", newX, newY);
		scrollSlide(sclX, sclY);
	}
}
//───────────────────────────────────────────

//───────────────────────────────────────────
//	指定スケール変更
//───────────────────────────────────────────
function scaleappoint(scl){
	if( (250 <= parseInt(scl))&&(200000 >= parseInt(scl)) ){
		for(i = createLayerNoStart; i <= createLayerNoEnd; i++){
			divicon = getDivFromName('_js_layer_' + i);
			if( divicon ) setDivVisibility(divicon, false);
		}
		
		changeMapscale("", scl);
	}
}
//───────────────────────────────────────────


//───────────────────────────────────────────
//	移動
//───────────────────────────────────────────
// スライド開始
function scrollSlide(xdelta, ydelta) {
	DBH_mapmoving = true;
	DBH_slidePercent = 0;
	DBH_slideX = xdelta;
	DBH_slideY = ydelta;
	slideAction();
}
// スライド動作関数
function slideAction()
{
	var xmt = (DBH_slidePercent * DBH_slideX) / 100;
	var ymt = (DBH_slidePercent * DBH_slideY) / 100;
    if(100 < DBH_slidePercent){
		DBH_mapmoving = false;
    }
    else{
		var mp0, mp;
		var clipLeft	= -xmt;
		var clipTop		= -ymt;
		var clipRight	= DBH_mapWid;
		var clipBottom	= DBH_mapHei;
		if(0 < xmt){
			clipLeft	= 0;
			clipRight	= DBH_mapWid - xmt;
		}
		if(0 < ymt){
			clipTop		= 0;
			clipBottom	= DBH_mapHei - ymt;
		}
		mp0 = getDivFromName("mainmap0");
		if(0 == DBH_mapload % 2){
			mp = getDivFromName("mainmap2");
		}
		else {
			mp = getDivFromName("mainmap1");
		}
		setDivClip(mp0, clipTop+1, clipRight, clipBottom, clipLeft+1);
		setDivClip(mp,  clipTop+1, clipRight, clipBottom, clipLeft+1);
		moveDivTo(mp0, DBH_mapX + xmt, DBH_mapY + ymt);
		moveDivTo(mp,  DBH_mapX + xmt, DBH_mapY + ymt);
		setTimeout("slideAction()", DBH_slideInterval);
	}
	DBH_slidePercent += DBH_slidePitch;
}
//───────────────────────────────────────────
