/*
$Id: libmap.js,v 1.5 2007/06/14 10:53:46 r-takahashi Exp $
Copyright(C) 1996-2005 INCREMENT P CORP.
*/

var BASE_UNIT_Y	= (17*60*60*256*2/3)/600;
var BASE_UNIT_X	= ((6+100)*60*60*256)/900;
var UNIT_SIZE_X	= 320;
var UNIT_SIZE_Y	= 240;



function CMap(minScale)
{
	var oOwner = this;

	this.Longitude		= 128761690;	
	this.Latitude		= 32836460;	
	this.Scale		= 2;		
	this.Cursor		= true;		
	this.LGauge		= false;	
	this.ExtParams		= "";		
	this.MapServURL		= "http://mapserv.incrementp.co.jp/cgi-bin/map/mapserv.cgi";
	this.OnChangedPosition	= null;
	this.OnChangedScale	= null;
	this.OnDraw		= null;
	this.OnResizeView	= null;


	var oLayer		= document.getElementById( "Layer" );
	var oUnitArray		= new Array();
	var nSaveScale		= -1;
	var nSaveLongitude	= -1;
	var nSaveLatitude	= -1;
	var bSaveLGauge		= -1;
	var bSaveCursor		= -1;
	var oScrollMap		= new CScrollMap( this );
	var oLGauge		= new CLGauge( oLayer );
	var nCenterX		= 0;
	var nCenterY		= 0;

		var oImgCenter	= document.createElement( "img" );
	if (minScale != "2"){
		oImgCenter.style.zIndex		= 10000;
		oImgCenter.style.position	= "absolute";
		oImgCenter.style.left		= -1000;
		oImgCenter.style.width  = 29;
		oImgCenter.style.height = 20;
		oImgCenter.src			= "cm.gif";
	}
		oLayer.appendChild( oImgCenter );
	

	
	this.Init = function() { }
	this.Draw = function() { }

	
	this.IntDraw = function()
	{
		
		if( ( nSaveLongitude != oOwner.Longitude ||
		      nSaveLatitude  != oOwner.Latitude ) && oOwner.OnChangedPosition )
		{
			oOwner.OnChangedPosition( oOwner.Longitude, oOwner.Latitude );
			nSaveLongitude = oOwner.Longitude;
			nSaveLatitude =  oOwner.Latitude;
		}
	
		
		if( nSaveScale != oOwner.Scale && oOwner.OnChangedScale )
		{
			oOwner.OnChangedScale( oOwner.Scale );
			nSaveScale = oOwner.Scale;
		}

		
		if( bSaveCursor != oOwner.Cursor )
		{
			
			var nWidth	= CMN_GetWindowWidth( window );
			var nHeight	= CMN_GetWindowHeight( window );
			nCenterX = Math.round(nWidth/2);
			nCenterY = Math.round(nHeight/2);

			
			oImgCenter.style.left	= nCenterX - oImgCenter.width /2;
			oImgCenter.style.top	= nCenterY - oImgCenter.height/2;
			if ( oOwner.Cursor )
			{
				oImgCenter.style.visibility = "visible";
			}
			else
			{
				oImgCenter.style.visibility = "hidden";
			}
			bSaveCursor  = oOwner.Cursor;
		}
	
		
		for( var i = 0 ; i < oUnitArray.length ; i++ )
		{
			oUnitArray[i].bUse = false;
		}
		
		var lUnitOffset = UnitOffset( oOwner.Scale );
		var oCalc = CalcUnitRange(	CMN_GetWindowWidth( window ),
						CMN_GetWindowHeight( window ),
						oOwner.Longitude,
						oOwner.Latitude,
						oOwner.Scale );
		var bUpdateArray	= false;
		for( var y = oCalc.lUnitRangeTop ; y <= oCalc.lUnitRangeBottom ; y+=lUnitOffset )
		{
			for( var x = oCalc.lUnitRangeLeft ; x <= oCalc.lUnitRangeRight ; x+=lUnitOffset )
			{	
				var oUnit = FindUnit( x, y, oOwner.Scale );
				if( oUnit == null )
				{
					oUnit = CMapPict( x, y, oOwner.Scale );
					oLayer.appendChild( oUnit );
					oUnitArray.push( oUnit );
					bUpdateArray = true;
					
					oUnit.src = MakeURL( x, y, oOwner.Scale );
				}
				else
				{
					oUnit.bUse	= true;
					var strURL = MakeURL( x, y, oOwner.Scale );
					if( oUnit.src != strURL )
					{
						oUnit.lowsrc	= oUnit.src;
						oUnit.src	= strURL;
					}
				}
				oUnit.style.left = 
					oCalc.ptBaseX + parseInt( (x-oCalc.lUnitRangeLeft)/lUnitOffset ) * UNIT_SIZE_X; 
				oUnit.style.top = 
					oCalc.ptBaseY - parseInt( (y-oCalc.lUnitRangeTop )/lUnitOffset ) * UNIT_SIZE_Y;
			}
		}
	
		
		for( var i = oUnitArray.length - 1 ; i >= 0 ; i-- )
		{
			if( !oUnitArray[i].bUse )
			{
				oLayer.removeChild( oUnitArray[i] );
				oUnitArray.splice( i, 1 );
			}
		}
	
		if ( !window.closed && oOwner.OnDraw )
		{
			oOwner.OnDraw( oLayer );
		}

		
		if ( oOwner.LGauge != bSaveLGauge )
		{
			oLGauge.Show( oOwner.LGauge );
			bSaveLGauge = oOwner.LGauge;
		}
		oLGauge.Draw( oOwner.Longitude, oOwner.Latitude, oOwner.Scale );
	}
	
	
	this.Move = function( lX, lY )
	{
		var oTargetPos = oOwner.GetMapPoint(	nCenterX + lX,
							nCenterY + lY );
		oOwner.Longitude = oTargetPos.Longitude;
		oOwner.Latitude	= oTargetPos.Latitude;
	}
	
	
	this.AdjustToView = function()
	{
		var nWidth	= CMN_GetWindowWidth( window );
		var nHeight	= CMN_GetWindowHeight( window );
	
		
		
		
		if( nWidth <= 0 && nHeight <= 0 )
		{
			window.setTimeout( oOwner.AdjustToView, 100 );
			return;
		}
		
		oLayer.style.width	= nWidth ;
		oLayer.style.height	= nHeight;
	
		
		bSaveCursor = -1;
		
		if( oOwner.OnResizeView )
		{
			oOwner.OnResizeView( nWidth, nHeight );
		}
	}
	
	
	this.GetMapPoint = function( nPixelX, nPixelY )
	{
		var nUnitCount = UnitOffset( oOwner.Scale );
		return oOwner.CreateLonLatInt(
				oOwner.Longitude + parseInt((nPixelX-nCenterX)*nUnitCount*900/UNIT_SIZE_X),
				oOwner.Latitude  - parseInt((nPixelY-nCenterY)*nUnitCount*600/UNIT_SIZE_Y));
	}
	
	
	this.GetViewPoint = function( nLongitude, nLatitude )
	{
		var nUnitCount = UnitOffset( oOwner.Scale );
		var oRet = new Object();
		oRet.PixelX = nCenterX + Math.round((nLongitude-oOwner.Longitude)*UNIT_SIZE_X/(nUnitCount*900));
		oRet.PixelY = nCenterY - Math.round((nLatitude- oOwner.Latitude )*UNIT_SIZE_Y/(nUnitCount*600));
		return	oRet;
	}
	
	
	this.Scroll = function( nLongitude, nLatitude )
	{
		var oPixelDst = oOwner.GetViewPoint( nLongitude, nLatitude );
		var oPixelSrc = oOwner.GetViewPoint( oOwner.Longitude, oOwner.Latitude );
		if(	Math.abs( oPixelDst.PixelX - oPixelSrc.PixelX ) > 2000 ||
			Math.abs( oPixelDst.PixelY - oPixelSrc.PixelY ) > 2000 )
		{
			oOwner.Longitude = nLongitude;
			oOwner.Latitude = nLatitude;
		}
		else
		{
			oScrollMap.Start( nLongitude, nLatitude );
		}
	}

	this.Stop = function()
	{
		oScrollMap.Stop();
	}
	
	
	this.LonLatStr10ToInt = function( temp )
	{
		var oRet = temp.match( "(E)([0-9\. \t,]+)(N)([0-9\. \t]+)" );
		if( oRet == null )
		{
			return	null;
		}
		return oOwner.CreateLonLatInt( fsdiv( oRet[2] ), fsdiv( oRet[4] ));
	}
	
	
	this.LonLatIntToStr10 = function( oLonLat )
	{
		return	"E" + IntToStr( Conv256to10( oLonLat.Longitude ), 10, "", "" ) +
				"N" + IntToStr( Conv256to10( oLonLat.Latitude ) , 10, "", "" );
	}
	
	
	this.CreateLonLatInt = function( nLongitude, nLatitude )
	{
		var oLonLat = new Object();
		oLonLat.Longitude  = parseInt(nLongitude);
		oLonLat.Latitude   = parseInt(nLatitude);
		return oLonLat;
	}

	this.PixelToLonLatSize = function( nPixelX, nPixelY, nScale )
	{
		return oOwner.CreateLonLatInt( 	parseInt(nPixelX*UnitOffset(nScale)*900/UNIT_SIZE_X),
						parseInt(nPixelY*UnitOffset(nScale)*600/UNIT_SIZE_Y) );
	}
	
	this.LonLatToPixelSize = function( nLonSize, nLatSize, nScale )
	{
		var oRet = new Object();
		oRet.PixelX = parseInt(nLonSize*UNIT_SIZE_X/(UnitOffset(nScale)*900));
		oRet.PixelY = parseInt(nLatSize*UNIT_SIZE_Y/(UnitOffset(nScale)*600));
		return	oRet;
	}

	function FindUnit( lX, lY, nScale )
	{
		for( var i = 0 ; i < oUnitArray.length ; i++ )
		{
			if(	oUnitArray[i].lX	== lX &&
				oUnitArray[i].lY	== lY &&
				oUnitArray[i].nScale	== nScale )
			{
				return	oUnitArray[i];
			}
		}
		return	null;
	}
	
	function MakeURL( x, y, nScale )
	{
		var lUnitOffset = UnitOffset( nScale );
		var oLonLat = oOwner.CreateLonLatInt(	x*900 + parseInt(900*lUnitOffset/2),
							y*600 + parseInt(600*lUnitOffset/2) );
		var strLonLat = LonLatToStr256( oLonLat );
		return oOwner.MapServURL + "?CPR=OFF&MAP=" + strLonLat + "&ZM=" + (13-nScale) + "&" + oOwner.ExtParams;
	}

	function IntToStr( v, b, pr, ps )
	{
		var	s = pr + v%b + ps;
		v = parseInt( v/b );
		s = v%60 + "." + s;
		v = parseInt( v/60 );
		s = v%60 + "." + s;
		v = parseInt( v/60 );
		return v + "." + s;
	}
	
	function LonLatToStr256( oLonLat )
	{
		return	"E" + IntToStr( oLonLat.Longitude, 256, "(", ")" ) +
				"N" + IntToStr( oLonLat.Latitude, 256, "(", ")" );
	}
	
	function fsdiv( strLL )
	{
		var			nK = new Array();
		var			oX = strLL;
		for( var i = 0 ; oX.length > 0 && i < 4 ; i++ )
		{
			var oCp = oX;
			var oRet = oX.match( "([0-9]+[\. ]*)" );
			oX = oCp.slice( oRet[1].length );
			nK[i] = oRet[1];
		}
	
		var			l1 = 0;
		var			l2 = 0;
		var			l3 = 0;
		var			l4 = 0;
		if( nK[0] != null )
		{
			l1 = nK[0]*60*60*256;
		}
		if( nK[1] != null )
		{
			l2 = nK[1]*60*256;
		}
		if( nK[2] != null )
		{
			l3 = nK[2]*256;
		}
		if( nK[3] != null )
		{
			var oY = nK[3].match( "([0-9]+)" );
			var xx = Math.pow( 10, oY[1].length );
			l4 = parseInt(oY[1]*256/xx);
		}
		
		return	l1 + l2 + l3 + l4;
	}
	
	function Conv256to10( v )
	{
		return	parseInt( ((v*20/256)+1)/2 );
	}


	function CalcUnitRange(	lScreenPixelSizeX, 
				lScreenPixelSizeY,
				nLongitude, 
				nLatitude, 
				nScale )
	{
		var lUnitOffset = UnitOffset( nScale );
	
		
		var lUnitX = LongitudeToUnitX( nScale, nLongitude );
		var lUnitY = LatitudeToUnitY( nScale, nLatitude );
	
		
		var lUnitPixelLocalX = (nLongitude-lUnitX*900)*UNIT_SIZE_X/(lUnitOffset*900);
		var lUnitPixelLocalY = UNIT_SIZE_Y - ((nLatitude-lUnitY*600)*UNIT_SIZE_Y/(lUnitOffset*600));
								
		
		var lUnitPixelOffsetX = nCenterX - Math.round(lUnitPixelLocalX);
		var lUnitPixelOffsetY = nCenterY - Math.round(lUnitPixelLocalY);
	
		
		var lUnitCountLeft	= _align( lUnitPixelOffsetX, UNIT_SIZE_X );
		var lUnitCountRight	= _align( lScreenPixelSizeX-lUnitPixelOffsetX-UNIT_SIZE_X, UNIT_SIZE_X );
		var lUnitCountTop	= _align( lUnitPixelOffsetY, UNIT_SIZE_Y );
		var lUnitCountBottom	= _align( lScreenPixelSizeY-lUnitPixelOffsetY-UNIT_SIZE_Y, UNIT_SIZE_Y );
	
		
		var lUnitPlotCountX = lUnitCountLeft + 1 + lUnitCountRight;
		var lUnitPlotCountY = lUnitCountTop + 1 + lUnitCountBottom;
		
		var oRet = new Object();
		
		oRet.lUnitRangeLeft	= lUnitX - lUnitCountLeft * lUnitOffset;
		oRet.lUnitRangeTop	= lUnitY - lUnitCountBottom * lUnitOffset;
		oRet.lUnitRangeRight	= lUnitX + lUnitCountRight * lUnitOffset;
		oRet.lUnitRangeBottom	= lUnitY + lUnitCountTop * lUnitOffset;
		
		
		oRet.ptBaseX = 0 + lUnitPixelOffsetX - UNIT_SIZE_X * lUnitCountLeft;
		oRet.ptBaseY = 0 + lUnitPixelOffsetY + UNIT_SIZE_Y + UNIT_SIZE_Y * (lUnitCountBottom-1);
	
		return	oRet;
	}

	oLGauge.SetPos( 8, 8 );
	window.setInterval( this.IntDraw, 1000/35 );
	
	return	this;
}

function CMapPict( x, y, Scale )
{
	var oUnit = document.createElement( "img" );
	var fCancel = function(e)
	{
		
		var oEvent = CMN_GetEvent( this.ownerDocument, e );
		window.focus();	
		CMN_PreventDefault( oEvent );
	}
	
	var fOnLoad = function()
	{
		
		if( CMN_IsLoadComplete( this ) )
		{
			this.style.zIndex = parseInt( this.style.zIndex ) ^ 1; 
		}
	}

	oUnit.galleryImg	= false;	
	
	oUnit.style.position	= "absolute";
	oUnit.style.zIndex	= 1;
	oUnit.width		= UNIT_SIZE_X;
	oUnit.height		= UNIT_SIZE_Y;
	
	oUnit.onmousedown	= fCancel;
	oUnit.oncontextmenu	= fCancel;
	oUnit.onerror		= function(e){};
	CMN_SetOnLoad( oUnit, fOnLoad );
	
	oUnit.lX		= x;
	oUnit.lY		= y;
	oUnit.nScale		= Scale;
	oUnit.bUse		= true;
	return	oUnit;
}



function CScrollMap( oMap )
{
	var nSMTimerID		= -1;

	var oDownPos	= new Object();
	var oBasePos	= new Object();
	var bDown	= false;
	var bMove	= false;

	this.Start = function( nLongitude, nLatitude )
	{
		var lSMMoveSpeed	= 900;
		var lSMMoveInterval	= 10;
		var nSMStartTick = GetTick();
		if( nSMTimerID != -1 )
		{
			return;
		}
		var oSMStart		= oMap.CreateLonLatInt( oMap.Longitude, oMap.Latitude );
		var oSMTarget		= oMap.CreateLonLatInt( nLongitude, nLatitude );

		nSMTimerID = setInterval( function()
		{
			var nDiff = GetTick() - nSMStartTick;
			if( nDiff >= lSMMoveSpeed )
			{
				nDiff = lSMMoveSpeed;
				window.clearInterval( nSMTimerID );
				nSMTimerID = -1;
			}
			var k = Math.sin( Math.PI*nDiff/(lSMMoveSpeed*2));
			nDiff = lSMMoveSpeed*k;
			oMap.Longitude = oSMStart.Longitude +
				parseInt((oSMTarget.Longitude - oSMStart.Longitude) * nDiff / lSMMoveSpeed );
			oMap.Latitude = oSMStart.Latitude + 
				parseInt((oSMTarget.Latitude  - oSMStart.Latitude ) * nDiff / lSMMoveSpeed );
		}, lSMMoveInterval );
	}
	
	function GetTick()
	{
		var d = new Date();
		return	eval(d/1);
	}
	
	this.Stop = function()
	{
		if( nSMTimerID == -1 )
		{
			return	false;
		}
		window.clearInterval( nSMTimerID );
		nSMTimerID = -1;
		return	true;
	}
	return	this;
}



function CLGauge( oLayer )
{	
	var aGageSizeList = [ 20, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000, 250000, 500000 ];
	var lLGScale = -1;
	var lLGX = 0;
	
	var oDvBarHB = CreateSpan( "white", 0, 5 );
	var oDvBarVB = CreateSpan( "white", 5, 0 );
	var oDvBarHF = CreateSpan( "black", 0, 2 );
	var oDvBarVF = CreateSpan( "black", 2, 0 );
	var oDvNum = CreateSpan( "white", "auto", "auto" );
	oDvNum.style.fontSize	= 10;
	oDvNum.style.overflow	= "hidden";
	oDvNum.style.padding	= "1";

	this.SetPos = function( lX, lY )
	{
		lLGX = lX;
		oDvBarHF.style.bottom	= lY;	oDvBarHF.style.left	= lX;
		oDvBarVF.style.left	= lX;	oDvBarVF.style.bottom	= lY;
		oDvBarHB.style.bottom	= lY-1;	oDvBarHB.style.left	= lX-1;
		oDvBarVB.style.bottom	= lY-1;	oDvBarVB.style.left	= lX-1;
		oDvNum.style.bottom	= lY-3;
	}
	
	this.Draw = function( nLongitude, nLatitude, nScale )
	{
		var dMtr	= aGageSizeList[nScale];
		var dSinLat	= Math.sin( nLongitude*0.0000000189 );
		var dLat	= dMtr / (6334832.034 / Math.pow( 1.0 - 0.006674372 * dSinLat * dSinLat, 1.5 ));
		var dWk		= nLatitude*0.0000000189 + dLat
		var dSinDLat	= Math.sin( dWk );
		var dLon	= dMtr/(6377397.155 * Math.cos( dWk )/Math.sqrt( 1.0 - 0.006674372 * dSinDLat * dSinDLat));
		var lBarWidth	= dLon*18774680 / UnitOffset( nScale );
		var lBarHeight	= dLat*21121516 / UnitOffset( nScale );
		oDvBarHF.style.width	= lBarWidth;
		oDvBarVF.style.height	= lBarHeight;
		oDvBarHB.style.width	= lBarWidth+1;
		oDvBarVB.style.height	= lBarHeight+1;
		oDvNum.style.left	= lLGX + lBarWidth + 4;
		if( lLGScale != nScale )
		{
			oDvNum.innerHTML	= (dMtr>=5000? dMtr/1000 + "km" : dMtr + "m");
			lLGScale = nScale;
		}
	}
	
	function CreateSpan( bgcol, width, height ) 
	{
		var oDv = document.createElement( "span" );
		oDv.style.backgroundColor	= bgcol;
		oDv.style.position		= "absolute";
		oDv.style.zIndex		= 30;
		oDv.style.fontSize		= 0;
		oDv.style.width			= width;
		oDv.style.height		= height;
		oDv.onselectstart		= function(e)
		{	
			var oEvent = CMN_GetEvent( this.ownerDocument, e );
			CMN_PreventDefault(oEvent);
		}
		oLayer.appendChild( oDv );
		return	oDv;
	}

	this.Show = function( bShow )
	{
		if( bShow )
		{
			oDvNum.style.visibility = "visible";
			oDvBarHB.style.visibility = "visible";
			oDvBarHF.style.visibility = "visible";
			oDvBarVB.style.visibility = "visible";
			oDvBarVF.style.visibility = "visible";
		}
		else
		{
			oDvNum.style.visibility = "hidden";
			oDvBarHB.style.visibility = "hidden";
			oDvBarHF.style.visibility = "hidden";
			oDvBarVB.style.visibility = "hidden";
			oDvBarVF.style.visibility = "hidden";
		}
	}
	return	this;
}



function UnitOffset( nScale )
{
	return 0x2000 >> (13 - nScale);
}

function _align(v,a)
{
	return	parseInt( (v+a-1)/a );
}

function LongitudeToUnitX( nScale, nLongitude )
{
	var lUnitCount = UnitOffset( nScale );
	var lUnitX = parseInt(nLongitude/900);
	return parseInt( (lUnitX-BASE_UNIT_X) / lUnitCount ) * lUnitCount + BASE_UNIT_X;
}

function LatitudeToUnitY( nScale, nLatitude )
{
	var lUnitCount = UnitOffset( nScale );
	var lUnitY = parseInt(nLatitude/600);
	return parseInt( (lUnitY-BASE_UNIT_Y) / lUnitCount ) * lUnitCount + BASE_UNIT_Y;
}


