// ==============================================================
// js_drawing.js by Bjorn Johansson 2004
// Based on the JavaScript Vector Graphics library by Walter Zorn:
// http://www.walterzorn.com
// You may alter the code in this file as much as You like...
// Many thanks goes to Mr. Walter Zorn 
// ==============================================================

// toolMode 12 = Selectionline
// toolMode 13 = Selectionpolygon
// toolMode 20 = Measuretool
// toolMode 22 = Selectioncircle
// toolMode 1003 = mesurepolygon

var toolMode=20;
var rb = new jsGraphics("rubberband"); //rubberbandlayer
var pl = new jsGraphics("polyline"); // polylinelayer
var pg = new jsGraphics("polygon"); // polylinelayer
var ci = new jsGraphics("circle"); // polylinelayer
var pllb = new jsGraphics("polylinelabel"); // polylinelayer
var pglb = new jsGraphics("polygonlabel"); // polylinelayer
var lbl = new jsGraphics("divText"); // polylinelayer

var plneu=false;

// ==============================
// USER SETTINGS
// ==============================
var showLength = true; // Show length information..or not.
var showRubberbandLength = true; // Show the length of the rubberband
var savePoly = false; // Save polygon coords (real map coords). NOT IMPLEMENTED PROPERLY YET!!!
// ==============================
// ==============================

var clickdivlinex=new Array();
var clickdivliney=new Array();
var clickmaplinex=new Array();
var clickmapliney=new Array();
var clickmaplinelength=new Array();

var clickdivpolyx=new Array();
var clickdivpolyy=new Array();
var clickmappolyx=new Array();
var clickmappolyy=new Array();

var clickdivtextx=new Array();
var clickdivtexty=new Array();
var clickmaptextx=new Array();
var clickmaptexty=new Array();
var clickmaptext=new Array();

var clickPointGKX=new Array();
var clickPointGKY=new Array();

var lastclicklinex=-1;
var lastclickliney=-1;
var lastclickpolyx=-1;
var lastclickpolyy=-1;
var startclickx=-1;
var startclicky=-1;
var lastclickmapx=-1;
var lastclickmapy=-1;
var startclickmapx=-1;
var startclickmapy=-1;



var activecircle=0;

var oldt="";
var newtext="";

var startX = 0;
var startY = 0;
var lastclicklinex = 0;
var lastclickliney = 0;
var circleCenterX = 0;
var circleCenterY = 0;
var activate_rb = false;
var close_pl = false;
var close_pg = false;


var startNewSelection = true;
var circleCentreSaved=false;
var linewidthrb=2;
function plsetColor(){
	pl.setColor(rgbToHex(linecolor));
}
function pgsetColor(){
	pg.setColor(rgbToHex(pgbordercolor));
}
function lblsetColor(){
	lbl.setColor(rgbToHex(lblFontColor));
}
function lblsetFont(){
	lbl.setFont(lblFont,lblFontSize,Font.PLAIN);
}


function setlblcolor(c){
	lblFontColor=c;
	document.msg.text.style.color=rgbToHex(lblFontColor);
	lbl.setColor(rgbToHex(lblFontColor));
	drawgraphics();
}
function setlblsize(){
	if (document.msg.sz.value>0 && document.msg.sz.value<100){
		lblFontSize=document.msg.sz.value+"px";
		lbl.setFont(lblFont,lblFontSize,Font.PLAIN);
		drawgraphics();
	}
}

// ================================================
// Start up the jsgraphics
// ================================================
function activateRubberband(e){
	if ((toolMode==22)&&(!circleCentreSaved)) // Save circle centre points
	{
		circleCenterX = mapX;
		circleCenterY = mapY;
		circleCentreSaved=true;
	}
	activate_rb = true;
	// Get hold of the starting coordinates.
	if(activeTool == "measure"){
	}else if(activeTool == "measurepoly"){
	}else if(activeTool == "measurecircle"){
	}
}

// ================================================
// Reset the rubberband line.
// ================================================
function deactivateRubberband()
{
	rb.clear();
	activate_rb = false;
	//document.getElementById("messeninfo").value="";
}

// ================================================
// Clear out the polygon layer.
// ================================================
function resetPolyline(){
	lastclicklinex=-1;
	lastclickliney=-1;
	clickmaplinex.length=null;
	clickmapliney.length=null;
	clickdivlinex.length=null;
	clickdivliney.length=null;
	pl.clear();
	resetPolylineText();
}
function resetPolylineText(){
	pllb.clear();
}
function resetPolygon(){
	lastclicklinex=-1;
	lastclickliney=-1;
	startclickx=-1;
	startclicky=-1;
	clickmappolyx.length=null;
	clickmappolyy.length=null;
	clickdivpolyx.length=null;
	clickdivpolyy.length=null;
	pg.clear();
	resetPolygonText();
}
function resetCircle(){
	ci.clear();
}
function resetPolygonText(){
	pglb.clear();
}
function resetText(){
	lbl.clear();
	clickmaptextx.length=null;
	clickmaptexty.length=null;
	clickmaptext.length=null;
	clickdivtextx.length=null;
	clickdivtexty.length=null;
}
// ================================================
// Draw the rubberband in the div element.
// ================================================
function drawRubberband(e){	
	rb.clear();
	if (mouseX!=-1 && mouseY!=-1) {
		if(activeTool=="measure"){
			if (lastclicklinex>-1 && lastclickliney>-1){
				rb.drawLine(lastclicklinex,lastclickliney,mouseX,mouseY);
				if (showRubberbandLength){
					displayRubberbandLength(currentMeasure); 
				}
				rb.paint();
			}
		}else if (activeTool=="measurepoly"){
			if (lastclickpolyx>-1 && lastclickpolyy>-1){
				rb.drawLine(lastclickpolyx,lastclickpolyy,mouseX,mouseY);
				if (startclickx>-1 && startclicky>-1){
					rb.drawLine(startclickx,startclicky,mouseX,mouseY);
				}
				if (showRubberbandLength){
					displayRubberbandLength(currentMeasure); 
				}
				rb.paint();
			}
		}
	}
}
function displayRubberbandLength(d){
	var textX, textY;
	var lx=lastclicklinex;
	var ly=lastclickliney;
	if (activeTool == "measurepoly"){
		lx=lastclickpolyx;
		ly=lastclickpolyy;
	}
	var dx = parseInt(mouseX-lx);
	var dy = parseInt(mouseY-ly);
	textX  = parseInt((lx+(dx/2)));
	textY = parseInt(ly+(dy/2));
	rb.drawString(d,textX,textY);
	/*
	if (activeTool == "measurepoly"){
		var lcurrentmeasure=calcDistance(startclickmapx,startclickmapy,mapX,mapY);
		var dx2 = parseInt(mouseX-startclickx);
		var dy2 = parseInt(mouseY-startclicky);
		textX  = parseInt((startclickx+(dx2/2)));
		textY = parseInt(startclicky+(dy2/2));
		rb.drawString(lcurrentmeasure,textX,textY);
	}
	*/
}

// ================================================
// Draw the polyline.
// ================================================
function drawPolyline(){
	var ldivlinex=new Array();
	var ldivliney=new Array();
	for (var i = clickdivlinex.length-1; i > -1;i--){
		if (clickdivlinex[i]!=-1 && clickdivliney[i]!=-1){
			pl.drawRect((clickdivlinex[i]-1),(clickdivliney[i]-1),3,3);
			ldivlinex[ldivlinex.length]=clickdivlinex[i];
			ldivliney[ldivliney.length]=clickdivliney[i];
		}else{
			if (ldivlinex.length>1){
				pl.drawPolyline(ldivlinex,ldivliney);
			}
			ldivlinex.length=0;
			ldivliney.length=0;
		}
	}
	pl.paint();
}
function drawText(){
	for (var i = clickdivtextx.length-1; i > -1;i--){
		lbl.drawString(clickmaptext[i],clickdivtextx[i],clickdivtexty[i]);
	}
	lbl.paint();
}
function finishPolyline() {
	if (activate_rb){
		if (activeTool == "measure"){
			clickmaplinex[clickmaplinex.length]=-1;
			clickmapliney[clickmapliney.length]=-1;
			clickdivlinex[clickdivliney.length]=-1;
			clickdivliney[clickdivliney.length]=-1;
		}
		if (activeTool == "measurepoly"){
			clickmappolyx[clickmappolyx.length]=startclickmapx;
			clickmappolyy[clickmappolyy.length]=startclickmapy;
			clickmappolyx[clickmappolyx.length]=-1;
			clickmappolyy[clickmappolyy.length]=-1;
			clickdivpolyx[clickdivpolyy.length]=-1;
			clickdivpolyy[clickdivpolyy.length]=-1;
			writeProtokoll("inishPolyline\n"+clickmappolyx);
		}
		drawgraphics();
		deactivateRubberband();
	}
}

function drawPolygon(){
	var ldivpolyx=new Array();
	var ldivpolyy=new Array();
	for (var i = clickdivpolyx.length-1; i > -1;i--){
		if (clickdivpolyx[i]!=-1 && clickdivpolyy[i]!=-1){
			pg.drawRect((clickdivpolyx[i]-1),(clickdivpolyy[i]-1),3,3);
			ldivpolyx[ldivpolyx.length]=clickdivpolyx[i];
			ldivpolyy[ldivpolyy.length]=clickdivpolyy[i];
		}else{
			if (ldivpolyx.length>1){
				pg.drawPolyline(ldivpolyx,ldivpolyy);
			}
			ldivpolyx.length=0;
			ldivpolyy.length=0;
		}
	}
	pg.paint();
}
// ================================================
// Draw the 'moving' circle...
// ================================================
function drawDynamicCircle(e){
	var x1 = mouseX;
	var y1 = mouseY;
	var dx = parseInt(x1-lastclicklinex);
	var dy = parseInt(y1-lastclickliney);
	var ch = Math.round(2*(Math.sqrt((Math.pow(dx,2))+(Math.pow(dy,2)))))/1;	
	var cx = parseInt(lastclicklinex-(ch/2));
	var cy = parseInt(lastclickliney-(ch/2));
	rb.drawEllipse(cx,cy,ch,ch);
}
function drawCircle(e)
{
	addElementCircle();
	var x1 = mouseX;
	var y1 = mouseY;
	var dx = parseInt(x1-lastclicklinex);
	var dy = parseInt(y1-lastclickliney);
	var ch = Math.round(2*(Math.sqrt((Math.pow(dx,2))+(Math.pow(dy,2)))))/1;	
	var cx = parseInt(lastclicklinex-(ch/2));
	var cy = parseInt(lastclickliney-(ch/2));
	ci.drawEllipse(cx,cy,ch,ch);
}



// ================================================
// Close the polyline and make it look like a filled polygon.
// ================================================
function closePolygon()
{
	close_pg = true;
	// Draw the last line
	drawPolygon();
	close_pg = false;
}



// ================================================
// Display the length on every polyline segment.
// calculates the label position.
// ================================================
function displayLength(p,x,y,d){
	var xsize = x.length;
	var ysize = y.length;
	var textX, textY;
	var dx = parseInt(x[xsize-1])-(x[xsize-2]);
	var dy = parseInt(y[ysize-1])-(y[ysize-2]);
	
	var l = Math.round(Math.sqrt((Math.pow(dx,2))+(Math.pow(dy,2))))/1;
	l = parseInt(l*10+(5/10))/10;
	// Fix text X placement
	if (dx<0) textX = parseInt(x[xsize-2]+(dx/2));
	else textX  = parseInt((x[xsize-2]+(dx/2)));
	textX  = parseInt((x[xsize-2]+(dx/2)));
	// Fix text Y placement
	if (dy<0) textY = parseInt(y[ysize-2]+(dy/2));
	else textY = parseInt(y[ysize-2]+(dy/2));
	textY = parseInt(y[ysize-2]+(dy/2));
	if (d>0){	
		p.drawString2(d,textX,textY);
	}
}
function drawgraphics(){
	pl.clear();
	pllb.clear();
	pg.clear();
	pglb.clear();
	lbl.clear();
	rb.setStroke(linewidthrb);
	rb.setColor(rgbToHex(rbcolor));
	rb.setFont("verdana","10px",Font.ITALIC);
		
	pl.setColor(rgbToHex(linecolor));
	pl.setStroke(linewidth);
	pg.setColor(rgbToHex(pgbordercolor));
	pg.setStroke(linewidthpoly);

	pllb.setColor(rgbToHex(pllblcolor));
	//pllb.setStroke(linewidth);

	pllb.setFont(lblFont,"9px",Font.PLAIN);
	pglb.setColor(rgbToHex(pglblcolor));
	//pglb.setStroke(linewidth);
	pglb.setFont(lblFont,"9px",Font.PLAIN);

	lbl.setColor(rgbToHex(lblFontColor));
	lbl.setStroke(linewidth);
	lbl.setFont(lblFont,lblFontSize+"px",Font.PLAIN);

	if (clickmaplinex.length>0){
		clickdivlinex.length=0;
		clickdivliney.length=0;
		var x=0;
		for (var i = 0; i < clickmaplinex.length;i++){
			if (clickmaplinex[i]!=-1 && clickmapliney[i]!=-1){
				var mp=convertMapToPixel(clickmaplinex[i],clickmapliney[i]);
				clickdivlinex[clickdivlinex.length]=mp[0];
				clickdivliney[clickdivliney.length]=mp[1];
				lastclicklinex=mp[0];
				lastclickliney=mp[1];
				x++;
				if (showLength && x>1){
					var l=clickdivlinex.length;
					var m=calcDistance(clickmaplinex[i-1],clickmapliney[i-1],clickmaplinex[i],clickmapliney[i]);
					displayLength(pllb,clickdivlinex.slice(l-2,l),clickdivliney.slice(l-2,l),m);
				}
				
			}else{
				x=0;
				clickdivlinex[clickdivlinex.length]=-1;
				clickdivliney[clickdivliney.length]=-1;
			}

		}
		drawPolyline(clickdivlinex,clickdivliney);
		pllb.paint();
						
	}
	if (clickmappolyx.length>0){
		clickdivpolyx.length=0;
		clickdivpolyy.length=0;
		var X=0;
		for (var i = 0; i < clickmappolyx.length;i++){
			if (clickmappolyx[i]!=-1 && clickmappolyy[i]!=-1){
				var mp=convertMapToPixel(clickmappolyx[i],clickmappolyy[i]);
				
				clickdivpolyx[clickdivpolyx.length]=mp[0];
				clickdivpolyy[clickdivpolyy.length]=mp[1];
				if (startclickx==-1){
					startclickx=mp[0];
					startclicky=mp[1];
				}
				lastclickpolyx=mp[0];
				lastclickpolyy=mp[1];
				x++;
				if (showLength && x>1){
					var l=clickdivpolyx.length;
					var m=calcDistance(clickmappolyx[i-1],clickmappolyy[i-1],clickmappolyx[i],clickmappolyy[i]);
					displayLength(pglb,clickdivpolyx.slice(l-2,l),clickdivpolyy.slice(l-2,l),m);
				}
			
			}else{
				x=0;
				startclickx=-1;;
				startclicky=-1;
				clickdivpolyx[clickdivpolyx.length]=-1;
				clickdivpolyy[clickdivpolyy.length]=-1;
			}
		}
		drawPolygon(clickdivpolyx,clickdivpolyy);
		pglb.paint();

	}
	if (clickmaptextx.length>0){
		clickdivtextx.length=0;
		clickdivtexty.length=0;
		var X=0;
		for (var i = 0; i < clickmaptextx.length;i++){
			var mp=convertMapToPixel(clickmaptextx[i],clickmaptexty[i]);
			clickdivtextx[clickdivtextx.length]=mp[0];
			clickdivtexty[clickdivtexty.length]=mp[1];
			lbl.drawString2(clickmaptext[i],clickdivtextx[i],clickdivtexty[i]);
		}
		lbl.paint();
	}
	
}



