
/*
	(C) Stefan Wilhelm,
			www.MeisterDerMagie.de
			www.AtWillys.de

	Datei	: ganthor.js
	Bez		: Ermöglicht Entfernungsmessungen anhand einer Karte des Kontinents

*/


/*
******************************************************************************************
******************************************************************************************
******************************************************************************************
*/

/* DHTML */

	function eById(id) 	{ return document.getElementById(id);}
	function getText(id)  	{ return document.getElementById(id).firstChild.nodeValue;}
	function setText(id,txt)	{ document.getElementById(id).firstChild.nodeValue = txt;}
	
	

/*
******************************************************************************************
******************************************************************************************
******************************************************************************************
*/

/* event handlers */

function gtMapCMEHandler(e) {
	if(!isMouseEvent(e)) return;
	switch(e.type) {
	
		case cME_MOUSEMOVE:
			var offsX = (isIE) ? (-eById("pageRegion").offsetLeft) : (0);
			var offsY = (isIE) ? (-150)  : (0);
			eById("floatLyr").style.visibility = "visible";
			eById("floatLyr").style.left = offsX + e.absCrds.x + ((e.relCrds.x > 680) ? (-220) : (20));
			eById("floatLyr").style.top  = offsY + e.absCrds.y + 20;
			break;
	
		case cME_CLICK:
			x1	= x0;
			y1	= y0;
			x0 	= e.relCrds.x;
			y0 	= e.relCrds.y;
			dx 	= x0 - x1;
			dy 	= y0 - y1;
			dPX = Math.sqrt(dx*dx + dy*dy);
			
			/* abs coords */
			if(x0-X_REF > 0) {
				setText("xmId", Math.abs(Math.floor((x0-X_REF) * kmPerPX))  + " S");
			} else {
				setText("xmId", Math.abs(Math.floor((x0-X_REF) * kmPerPX))  + " N");
			}
			
			if(y0-Y_REF > 0) {
				setText("ymId", Math.abs(Math.floor((y0-Y_REF) * kmPerPX))  + " O");
			} else {
				setText("ymId", Math.abs(Math.floor((y0-Y_REF) * kmPerPX))  + " W");
			}
			
			/* distance */
			setText("dmId", Math.floor(100 * dPX * kmPerPX) / 100);
			setText("ddId", Math.floor(100 * dPX * daysPerPX)  / 100);
			
			/* pixel coords */
			if(!IS_DEBUG==1) return;
			setText("pxId", x0);
			setText("pyId", y0);
			setText("dxId", dx);
			setText("dyId", dy);
			setText("dpxId",dPX);			
			break;	
	}
}

function bodyCMEHandler(e) {
	if(!isMouseEvent(e)) return;
	switch(e.type) {
		case cME_MOUSEMOVE:	
			eById("floatLyr").style.visibility = "hidden";
			break;
	}
}

function selectSpeedCMEHandler(e) {
	if(!isMouseEvent(e)) return;
	switch(e.type) {
		case cME_CLICK:	
			if(e.refObj.value != 0)
				daysPerPX = kmPerPX / e.refObj.value;
			break;
	}
}

/*
******************************************************************************************
******************************************************************************************
******************************************************************************************
*/

/* Variables */
	var isInit = false;
	var x0, y0, x1, y1, dx, dy, dPX, dm, dt;
	var kaleshAdarkm, kaleshAdarPX;
	var kmPerHour, hoursPerDay, relWalkTime, daysPerMonth;
	var kmPerPX, hoursPerPX, daysPerPX, monthsPerPX;
	var X_REF, Y_REF;
	var IS_DEBUG = 1;
	var isIE=(cMouseEvent_Browser.indexOf("IE") > -1) ? (true) : (false);

/* Init */
	if(eById("debugId")) {IS_DEBUG=1} else {IS_DEBUG=0};
	kaleshAdarkm = 1000;
	kmPerHour    = 4.5;
	hoursPerDay     = 24;
	relWalkTime  	= 0.4;
	daysPerMonth    = 30;
	kmPerPX     	= kaleshAdarkm/kaleshAdarPX;
	hoursPerPX  	= kmPerPX/kmPerHour;
	daysPerPX  		= hoursPerPX/hoursPerDay/relWalkTime;
	monthsPerPX  	= daysPerPX/daysPerMonth;
	X_REF = 512;
	Y_REF = 623;
	x0 = X_REF; y0 = Y_REF;
	x1 = X_REF; y1 = Y_REF;
	dx = 0; dy = 0; dPX = 0; dm = 0; dt = 0;

/* Init Events */
	var gtMapEvent = new cMouseEvent(eById("gtMap"),gtMapCMEHandler,cME_CLICK | cME_MOUSEMOVE);
	var bodyEvent =  new cMouseEvent(document.getElementsByTagName("body")[0],bodyCMEHandler, cME_MOUSEMOVE);
	var selectSpeedEvent =  new cMouseEvent(eById("selectSpeed"),selectSpeedCMEHandler, cME_CLICK);

/* Release */
	isInit = true;

