/** EditRoute Javascript file

*/

/** routeEditDisplay
	rtbox	--	the Element to display the route in
	rtTbody	--	table body to add each step into
	rtStepCnt
	rtDistance	--	the Element to display the distance in
..
	distStyle	--	function to display the distance
	map		-- the GMap to draw the route on
	markerManage -- the manager for the GMap
	route	-- the route datat
*/

function routeEditDisplay(rtboxcntrl,map,route,metric)
{	this.rtbox = rtboxcntrl;
	this.rtname = rtboxcntrl.routeName;
	this.rtTpane = rtboxcntrl.routeTPane;
	this.rtTbody = rtboxcntrl.routeTBody;
	this.rtStepCnt = rtboxcntrl.routeSteps;
	this.rtDistance = rtboxcntrl.routeDist;

	this.map = map;
//Prefer the open source MarkerManager vs the GMarkerManager if only because
//this has the capability to REMOVE markers once added.
//it requires a seperate script to be loaded of course:
//<script src="http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/src/markermanager.js" >
	this.markerManager = new MarkerManager( this.map, {trackMarkers:true, borderPadding:40} );
	if (typeof route=='object')
		this.linkToRoute(route);
	this.distMarkers = new Array();
	if (typeof metric!='undefined' && metric==true)
	{	this.distStyle= BRdCConv.distanceMetric;
		this.distCrawler = new distanceCrawler( this.distMarkers, BRdCConv.kilometerInMeters, makeDistanceMarkerMetric );
	}	else
	{	this.distStyle= BRdCConv.distanceImperial;
		this.distCrawler = new distanceCrawler( this.distMarkers, BRdCConv.mileInMeters, makeDistanceMarkerImperial );
	}
	this.clickCatchEnable();
}
routeEditDisplay.prototype = new routeDisplay;
routeEditDisplay.prototype.constructor = routeEditDisplay;
routeEditDisplay.prototype.baseClass = routeDisplay;
routeEditDisplay.prototype.updateStepCount = function(val)
{	this.rtStepCnt.nodeValue=val;
}
routeEditDisplay.prototype.updateDistance= function(val)
{	this.rtDistance.nodeValue=this.distStyle(val);
}
routeEditDisplay.prototype.updateName = function(rname)
{	this.rtname.nodeValue = rname;
}
routeEditDisplay.prototype.getRow = function(rownum)
{	return this.rtTbody.getElementsByTagName("tr")[rownum];
}
routeEditDisplay.prototype.getStepRows = function(stepnum)
{	var rCol = this.rtTbody.getElementsByTagName("tr");
	return new Array( rCol[stepnum*2+1], rCol[stepnum*2+2] );
}
routeEditDisplay.prototype.hiliteStep = function(stepnum)
{	var rowentries = this.getStepRows(stepnum);
	for (var i in rowentries)
		rowentries[i].setAttribute("class","hi");
}
routeEditDisplay.prototype.unhiliteStep = function(stepnum)
{	var rowentries = this.getStepRows(stepnum);
	for (var i in rowentries)
		rowentries[i].removeAttribute("class");
}
routeEditDisplay.prototype.makeStepRows = function(stepNum)
{	var rows = new Array(2);
	var i;
	for (i=0;i<2;i++)
		rows[i] = document.createElement("tr");

	rows[0].appendChild( document.createElement("td") );
	rows[0].firstChild.className = 'stepline';
	rows[0].firstChild.appendChild( document.createElement("img") );
	editimg_lineback.setDomImg(rows[0].firstChild.firstChild);
	rows[0].lastChild.appendChild( document.createTextNode( "(dist)") );
	rows[0].appendChild( document.createElement("td") );
	rows[0].lastChild.setAttribute("class","flags");
	rows[0].lastChild.appendChild( document.createTextNode( "path") );
	rows[0].appendChild( document.createElement("td") );
	rows[0].lastChild.colSpan="2";
	rows[0].lastChild.className='space';
/*	for (i=0;i<5;i++)
	{	rows[0].lastChild.appendChild( document.createElement('img') );
		rows[0].lastChild.lastChild.src = 'img/flagbutton.png';
		rows[0].lastChild.lastChild.width='18';
		rows[0].lastChild.lastChild.height='11';
		rows[0].lastChild.lastChild.title='example path flags button size';
	}
*/
	rows[1].appendChild( document.createElement("td") );
	rows[1].firstChild.className = 'mark';
	rows[1].firstChild.rowSpan = '2';
	rows[1].firstChild.appendChild( document.createElement("img") );
	rows[1].firstChild.firstChild.title = 'Marker on/off';
	editimg_nomarker.setDomImg(rows[1].firstChild.firstChild);
	rows[1].firstChild.firstChild.uref = this;
	rows[1].firstChild.firstChild.step = stepNum;
	rows[1].firstChild.firstChild.onclick = function()
		{	this.uref.markerToggle(this.step);
		}

	rows[1].appendChild( document.createElement("td") );
	rows[1].lastChild.className = 'stepline';
	rows[1].lastChild.appendChild( document.createTextNode( ' ' ) );
	rows[1].appendChild( document.createElement('td') );
	rows[1].lastChild.className = 'textbubble';
	rows[1].appendChild( document.createElement("td") );
	rows[1].lastChild.className = 'hd';
	rows[1].lastChild.appendChild( document.createTextNode( "Point:") );
	rows[1].appendChild( document.createElement("td") );
	rows[1].lastChild.appendChild( document.createTextNode(" ") );
	rows[1].lastChild.appendChild( document.createElement( 'img' ) );
	rows[1].lastChild.lastChild.title = 'Move Point';
	editimg_move.setDomImg(rows[1].lastChild.lastChild);
	rows[1].lastChild.lastChild.onclick = function()
		{	this.parentNode.upref.moveablePt(this.parentNode.BRdCPt, this.parentNode.step);
		}
	rows[1].lastChild.appendChild( document.createElement( 'img' ) );
	rows[1].lastChild.lastChild.title = 'Delete Point';
	editimg_delegray.setDomImg(rows[1].lastChild.lastChild);
	rows[1].lastChild.lastChild.onmouseover = function()
		{	editimg_delered.setDomImg(this);
			var xmarker = this.parentNode.BRdCPt.addMarker('delete',{clickable:false, icon:red_x});
			this.parentNode.upref.map.addOverlay( xmarker );
			this.onmouseout = function()
				{	editimg_delegray.setDomImg(this);
					this.parentNode.upref.map.removeOverlay( xmarker );
					delete this.onmouseout;
				}
		}
	return rows;
}
routeEditDisplay.prototype.updateStepTable = function(route, fromStep)
{	var stepRowsCnt = ((this.rtTbody.getElementsByTagName("tr").length-1) / 2);
	//GLog.write("Disp = "+toAddCnt+" Steps = "+this.route.steps.length + " fromStep = "+fromStep)
	if (this.route.steps.length==0)
	{	this.resetStepTable();
		return;
	}

	var i,j;	
	if (stepRowsCnt>this.route.steps.length)
	{	while (stepRowsCnt>this.route.steps.length)
		{	var stps = this.getStepRows(--stepRowsCnt);
			while (stps.length>0)
			{	j = stps.pop();
				this.rtTbody.removeChild( j );
				delete j;
			}
		}
	}	else
	{	for (i=stepRowsCnt;i<this.route.steps.length;i++)
		{	var defaultRowEntries = this.makeStepRows( (this.rtTbody.getElementsByTagName("tr").length-1) / 2 );
			this.updateStep( defaultRowEntries, this.route.steps[i], i );
			for (j in defaultRowEntries)
				this.rtTbody.appendChild( defaultRowEntries[j] );
		}
	}
	if (typeof fromStep=='number')
	{	for (i=fromStep;i<stepRowsCnt;i++)
			this.updateStep( this.getStepRows(i), this.route.steps[i], i );
	}
}
routeEditDisplay.prototype.updateStep = function( stepRows, stepdata, stepnum )
{	
	stepRows[0].firstChild.lastChild.nodeValue = this.distStyle(stepdata.distance);
	if (stepnum==this.route.steps.length-1 || stepdata.marker)
	{	editimg_linedown.setDomImg(stepRows[0].firstChild.firstChild);
		stepRows[1].childNodes[1].className='step';
		if (stepnum==this.route.steps.length-1)
		{	stepRows[1].childNodes[1].firstChild.nodeValue = 'End';
		}	else
		{	stepRows[1].childNodes[1].firstChild.nodeValue = (stepdata.markerid+'.');
		}
	}	else
	{	editimg_lineback.setDomImg(stepRows[0].firstChild.firstChild);
		stepRows[1].childNodes[1].className='stepline';
		stepRows[1].childNodes[1].firstChild.nodeValue = ' ';
	}
	if (stepdata.marker)
	{	if (stepRows[1].childNodes[2].firstChild==null)
		{	stepRows[1].childNodes[2].appendChild( document.createElement('img') );
			stepRows[1].childNodes[2].lastChild.title = 'Set Text/Point Type';
			editimg_settext.setDomImg(stepRows[1].childNodes[2].lastChild);
			stepRows[1].childNodes[2].lastChild.onclick = function()
				{	var ulnk = this.parentNode.parentNode.lastChild;
					ulnk.upref.stepMarkerEditText( ulnk.BRdCPt, ulnk.step, 
									ulnk.parentNode.childNodes[1].firstChild.nodeValue,
									ulnk.previousSibling.firstChild.nodeValue,
									ulnk.upref.route.steps[ulnk.step].stepInstruction );
				}
		}
		editimg_ptmarker.setDomImg(stepRows[1].firstChild.firstChild);
		stepRows[1].childNodes[3].firstChild.nodeValue= this.distStyle(stepdata.rdist);	
	}	else
	{	editimg_nomarker.setDomImg(stepRows[1].firstChild.firstChild);
		if (stepRows[1].childNodes[2].firstChild!=null)
			stepRows[1].childNodes[2].removeChild( stepRows[1].childNodes[2].firstChild );
		stepRows[1].childNodes[3].firstChild.nodeValue= 'Point:';
	}

	stepRows[1].lastChild.BRdCPt = stepdata.ptB;
	stepRows[1].lastChild.step = stepnum;
	stepRows[1].lastChild.upref = this;
	stepRows[1].lastChild.lastChild.onclick = function()
	{	this.onmouseout();
		this.parentNode.upref.route.deleteStepEndPt(this.parentNode.step, this.parentNode.BRdCPt);
		if (this.parentNode.parentNode.parentNode!=null)
			this.onmouseover();
	}
}
routeEditDisplay.prototype.makeStartPtStep = function()
{	var row = document.createElement("tr");
	row.appendChild( document.createElement("td") );
	row.firstChild.className='mark';
	row.firstChild.rowSpan='2';
	row.firstChild.appendChild( document.createElement("img") );
	routesml_start.setDomImg( row.firstChild.firstChild );
	row.appendChild( document.createElement("td") );
	row.lastChild.className='step';
	row.lastChild.appendChild( document.createTextNode( "Start" ) );
	row.appendChild( document.createElement('td') );
	row.lastChild.className = 'textbubble';
	row.lastChild.appendChild( document.createElement('img') );
	row.lastChild.lastChild.title = 'Set Text/Point Type';
	editimg_settext.setDomImg(row.lastChild.lastChild);
	row.lastChild.lastChild.onclick = function()
		{	var rhlink = this.parentNode.parentNode.lastChild.upref;
			rhlink.stepMarkerEditText( rhlink.route.startPt, -1, 
									'Start',
									'--',
									rhlink.route.startDescription );
		}

	row.appendChild( document.createElement("td") );
	row.lastChild.className = 'hd';
	row.lastChild.appendChild( document.createTextNode("Point:") );
	row.appendChild( document.createElement('td') );
	row.lastChild.upref = this;
	row.lastChild.appendChild( document.createElement( 'img' ) );
	row.lastChild.lastChild.title = 'Move Point';
	editimg_move.setDomImg( row.lastChild.lastChild );
	row.lastChild.lastChild.onclick = function()
		{	this.parentNode.upref.moveablePt(this.parentNode.upref.route.startPt, -1);
		}
	row.lastChild.appendChild( document.createElement( 'img' ) );
	row.lastChild.lastChild.title = 'Delete Point';
	editimg_delegray.setDomImg( row.lastChild.lastChild );
	row.lastChild.lastChild.onmouseover = function()
		{	editimg_delered.setDomImg(this);
			var xmarker = this.parentNode.upref.route.startPt.addMarker('delete',{clickable:false, icon:red_x});
			this.parentNode.upref.map.addOverlay( xmarker );
			this.onmouseout = function()
				{	editimg_delegray.setDomImg(this);
					this.parentNode.upref.map.removeOverlay( xmarker );
					delete this.onmouseout;
				}
		}
	row.lastChild.lastChild.onclick = function()
		{	this.onmouseout();
			this.parentNode.upref.route.deleteStartPt();
			if (this.parentNode.parentNode.parentNode!=null)
				this.onmouseover();
		}

	this.rtTbody.appendChild( row );
	return row;
}
routeEditDisplay.prototype.resetStepTable = function()
{	//clear table entries
	while (this.rtTbody.hasChildNodes())
		this.rtTbody.removeChild(this.rtTbody.lastChild);
	if (typeof this.route=='object')
	{	if (this.route.steps.length>0)
		{	this.makeStartPtStep();
			this.updateStepTable(this.route,0);
		}	else
		{	if (typeof this.route.startPt=='object')
			{	this.makeStartPtStep();
			}	else
			{	this.rtTbody.appendChild( document.createElement('tr') );
				this.rtTbody.appendChild( document.createElement('td') );
				this.rtTbody.lastChild.appendChild( document.createTextNode('Please enter a starting point') );
				this.rtTbody.appendChild( document.createElement( 'tr' ) );
				this.rtTbody.appendChild( document.createElement('td') );
				this.rtTbody.lastChild.appendChild( document.createTextNode('either by clicking on the map') );
				this.rtTbody.appendChild( document.createElement( 'tr' ) );
				this.rtTbody.appendChild( document.createElement('td') );
				this.rtTbody.lastChild.appendChild( document.createTextNode('or find it by its address') );
			}
		}
	}
}
routeEditDisplay.prototype.linkToRoute = function( route )
{	if (typeof this.route=='object')
		this.route.unlinkDisplay(this);
	this.route = route;
	this.route.linkDisplay(this);
	this.currStep = -1;
	this.updateStepCount(this.route.steps.length);
	this.updateDistance(this.route.distance/1000.0);
	this.resetStepTable(this.route);
}
routeEditDisplay.prototype.setStartPt = function(BRdCPt)
{	if (typeof this.route.startPt=='object')
	{	if (this.route.startPt==BRdCPt)
			return;
	}
	this.route.startPt = BRdCPt;
	BRdCPt.enableAsMarker();
	BRdCPt.addMarker('start',{clickable:true, icon:route_startmark});
	this.map.addOverlay( BRdCPt.getMarker('start') );
}
routeEditDisplay.prototype.addRoutePoint = function( BRdCPt )
{	var ptA;
	if (this.route.steps.length==0)
	{	if (typeof this.route.startPt!='object')
		{	this.setStartPt( BRdCPt );
			this.route.endPt = BRdCPt;
			this.resetStepTable(this.route);
			return;
		}
		else
			ptA = this.route.startPt;
	}	else
	{	if (this.currStep<0 || this.currStep>=this.route.steps.length)
			ptA = this.route.endPt;
		else
		{	this.map.removeOverlay(this.route.steps[this.currStep].overlay);
			++this.currStep;
			this.route.insertStep(this.currStep-1,this.route.steps[this.currStep-1].ptA,BRdCPt,this.route.steps[this.currStep-1].ptB);
			this.map.addOverlay(this.route.steps[this.currStep-1].makeOverlay('#0000AA',4,.75));
			this.map.addOverlay(this.route.steps[this.currStep].makeOverlay('#0000AA',4,.5));
			this.scrollToCurrStep();
			return;
		}
	}
	this.route.appendStep(ptA,BRdCPt);
	this.map.addOverlay(this.route.steps[this.route.steps.length-1].makeOverlay('#0000AA',4,.75));
	this.scrollToCurrStep();
}
routeEditDisplay.prototype.scrollToCurrStep=function()
{	if (this.rtTpane.className=='open')
	{	if (this.currStep<0 || this.currStep>=this.route.steps.length)
			this.rtTbody.lastChild.scrollIntoView(false);
		if (this.currStep>0)
			this.getStepRows( this.rtTbody-1 )[0].scrollIntoView(true);
	}
}
routeEditDisplay.prototype.moveablePt = function( BRdCPt, inStepNum )
{	if (typeof this.moverPtStep!='undefined')
	{	this.moveablePtEnd();
	}
	this.moverPtStep = inStepNum;
	this.move_marker = BRdCPt.addMarker('move',{draggable:true, icon:moveicon});
	this.map.addOverlay( this.move_marker );
	this.map.panTo( BRdCPt.coord );
	this.clickCatchDisable();
	this.clickCatcher = GEvent.bind(this.map, 'click', this, this.moveClick);
	this.moveDragCatcher = GEvent.bind(this.move_marker,'dragend',this,this.moveableDragged);
}
routeEditDisplay.prototype.moveablePtEnd = function()
{	delete this.moverPtStep;
	this.map.removeOverlay( this.move_marker );
	GEvent.removeListener(this.moveDragCatcher);
	delete this.moveDragCatcher;
	this.clickCatchDisable();
	this.clickCatchEnable();
}
routeEditDisplay.prototype.moveClick = function(overlay, clickPt)
{	if (typeof clickPt=='object')
	{	this.move_marker.setLatLng(clickPt);
		this.moveableDragged();
	}
}
routeEditDisplay.prototype.moveableDragged = function()
{	if (this.moverPtStep==-1)
	{	this.map.removeOverlay(this.route.startPt.getMarker('start'));
	}	else if (this.route.steps[this.moverPtStep].marker)
	{	this.map.removeOverlay(this.route.steps[this.moverPtStep].ptB.getMarker('mark'));
	}
	this.route.moveStepPoint( this.moverPtStep, new BRdCPoint( this.move_marker.getLatLng() ) );

	this.moveablePtEnd();
}
routeEditDisplay.prototype.clickCatchEnable = function()
{	if (typeof this.clickCatcher!='undefined')
		return;
	this.clickCatcher = GEvent.bind(this.map, 'click', this, this.mapClick);
}
routeEditDisplay.prototype.clickCatchDisable = function()
{	if (typeof this.clickCatcher!='undefined')
	{	GEvent.removeListener(this.clickCatcher);
		delete this.clickCatcher;
	}
}
routeEditDisplay.prototype.mapClick = function( overlay, clickPt )
{	if (typeof clickPt=='object')
		this.addRoutePoint(new BRdCPoint(clickPt) );
}
routeEditDisplay.prototype.markerToggle = function(stepNum)
{	if (stepNum>=this.route.steps.length)
		return;
	var inStep = this.route.steps[stepNum];
	inStep.marker = ((inStep.marker) ? false : true);
	if (inStep.marker)
	{	if (inStep.ptB.getMarker('mark')==null)
			inStep.ptB.addMarker('mark',{clickable:true,icon:route_blankmark});
		this.map.addOverlay(inStep.ptB.getMarker('mark'));
	}	else
	{	this.map.removeOverlay(inStep.ptB.getMarker('mark'));
	}
	var markerid = 1;
	for (var i in this.route.steps)
		if (this.route.steps[i].marker)
			this.route.steps[i].markerid = markerid++;
	this.updateStepTable(this.route, stepNum);
}
routeEditDisplay.prototype.stepMarkerEditText = function( BRdCPt, step, markerid, rdist, tval )
{	var domEntry = document.createElement('div');
	domEntry.className = 'graygreenback';
	domEntry.appendChild( document.createElement( 'h2' ) );
	domEntry.lastChild.appendChild( document.createTextNode(markerid + ': '+rdist) );
	domEntry.appendChild( document.createElement( 'hr' ) );
	domEntry.appendChild( document.createElement('h3') );
	domEntry.lastChild.appendChild( document.createTextNode('Step Info') );
	domEntry.appendChild( document.createElement('textarea') );
	domEntry.lastChild.className = 'inbubbletext';
	if (typeof tval=='string')
		domEntry.lastChild.value = tval;
	domEntry.lastChild.rows='8';
	domEntry.lastChild.cols='40';
	domEntry.lastChild.uref = this;
	domEntry.lastChild.step = step;
	domEntry.lastChild.onchange = function()
		{	//set value
			if (this.step==-1)
			{	this.uref.route.startDescription = this.value;
			}	else if (this.step<this.uref.route.steps.length)
			{	this.uref.route.steps[this.step].stepInstruction = this.value;
			}
		}
	this.map.openInfoWindow( BRdCPt.coord, domEntry );
}
routeEditDisplay.prototype.updateFromRoute=function(BRdCRoute,inStepNum)
{	if (this.route==BRdCRoute)
	{	this.updateStepTable(BRdCRoute,inStepNum);
		this.updateStepCount(BRdCRoute.steps.length);
		this.updateDistance(BRdCRoute.distance);
		this.makeDistanceMarkers( inStepNum, this.distCrawler );
	}
}



/**	gRouteDisplayControl

*/
function gRouteDisplayControl()
{	
}
gRouteDisplayControl.prototype = new GControl();
gRouteDisplayControl.prototype.initialize = function(map)
{	this.inmap = map;

	//create the dom elements for the route step display
	//create the trackable dom elements
	//remember textNodes can not be blank or some browsers excise them (safari)
	this.divElem = document.createElement('div');
	this.routeName = document.createTextNode('Untitled Route');
	this.routeSteps= document.createTextNode('0');
	this.routeDist = document.createTextNode('--');
	this.routeTPane= document.createElement('div');
	this.routeTBody= document.createElement('tbody');

	//put all the style elements in the .css so it can be browser quirk specific
	this.divElem.className = 'routebox';

	//create the top border box with the title entry
	this.divElem.appendChild( document.createElement('div') );
	this.divElem.lastChild.className = 'borderbox';
	this.divElem.firstChild.appendChild( document.createElement('img') );
	this.divElem.firstChild.firstChild.title = 'open/close';
	this.divElem.firstChild.firstChild.align='left';
	routebox_open.setDomImg(this.divElem.firstChild.firstChild);
	this.divElem.firstChild.firstChild.onclick= function()
		{	//remember in this function, *this* here refers to the <img> element
			if (this.parentNode.nextSibling.className == 'open')
			{	routebox_closed.setDomImg(this);
				this.parentNode.nextSibling.className = 'close';
			}	else
			{	routebox_open.setDomImg(this);
				this.parentNode.nextSibling.className = 'open';
			}
		}
	this.divElem.firstChild.appendChild( this.routeName );

	//create the (hideable) step table for the route
	this.divElem.appendChild( this.routeTPane );
	this.divElem.lastChild.className = 'open';
	this.divElem.lastChild.appendChild( document.createElement('table') );
	this.divElem.lastChild.firstChild.className = 'routeTable';
	this.divElem.lastChild.firstChild.border = '0';
	this.divElem.lastChild.firstChild.cellSpacing = '0';
	this.divElem.lastChild.firstChild.appendChild( this.routeTBody );
	this.divElem.lastChild.firstChild.firstChild.valign = 'top';	
	this.divElem.lastChild.firstChild.firstChild.appendChild( document.createElement('tr') );
	this.divElem.lastChild.firstChild.firstChild.firstChild.appendChild( document.createElement( 'td' ) );
	this.divElem.lastChild.firstChild.firstChild.firstChild.firstChild.appendChild( document.createTextNode('(This is where the route info goes)') );

	//create the bottom border box with the distance and step totals
	this.divElem.appendChild( document.createElement('div') );
	this.divElem.lastChild.className = 'borderbox';
	this.divElem.lastChild.appendChild( document.createElement('span') );
	this.divElem.lastChild.firstChild.appendChild( this.routeSteps );
	this.divElem.lastChild.appendChild( document.createTextNode(' steps, ') );
	this.divElem.lastChild.appendChild( document.createElement('span') );
	this.divElem.lastChild.lastChild.appendChild( this.routeDist );
	
	//add it to the map
	var inContainer = map.getContainer();
	inContainer.appendChild( this.divElem );
	return this.divElem;
}
gRouteDisplayControl.prototype.printable = function()
{	return false;
	//we don't want to mess up people's nice print out with a map-covering
	//route table (that may require scrolling to see all the elements) -- 
	//maybe we provide a separate printable version elsewhere...
}
gRouteDisplayControl.prototype.selectable = function()
{	return true;
}
gRouteDisplayControl.prototype.getDefaultPosition = function()
{	return new GControlPosition( G_ANCHOR_BOTTOM_RIGHT, new GSize( 8, 14 ) );
}




/**	gRouteActionControl
*/

function gRouteActionControl( rtDisp )
{	this.routeDisp = rtDisp;
	this.geocoder = new GClientGeocoder();
}
gRouteActionControl.prototype = new GControl();
gRouteActionControl.prototype.initialize = function(map)
{	this.inmap = map;

	this.divElem = document.createElement('div');
	this.pane = new Array(3);
	this.pane[0] = document.createElement('div');
	this.pane[1] = document.createElement('div');
	this.pane[2] = document.createElement('div');

	this.divElem.className = 'gRouteEditCntl';

	this.divElem.appendChild( document.createElement( 'div' ) );
	this.divElem.lastChild.className = 'map_click';
	this.divElem.lastChild.title = 'Enter Points by Clicking on Map';
	this.divElem.lastChild.upref = this;
	this.divElem.lastChild.onclick = function()
		{	this.upref.selectPane(0);	}
	this.divElem.lastChild.appendChild( document.createTextNode(' Map ') );
		
	this.divElem.appendChild( document.createElement( 'div' ) );
	this.divElem.lastChild.className = 'lat_lng';
	this.divElem.lastChild.title = 'Find Points by their Coordinates';
	this.divElem.lastChild.upref = this;
	this.divElem.lastChild.onclick = function()
		{	this.upref.selectPane(1);	}
	this.divElem.lastChild.appendChild( document.createTextNode('Lat/Lng') );
	
	this.divElem.appendChild( document.createElement( 'div' ) );
	this.divElem.lastChild.className = 'geocode';
	this.divElem.lastChild.title = 'Find Points by Address';
	this.divElem.lastChild.upref = this;
	this.divElem.lastChild.onclick = function()
		{	this.upref.selectPane(2);	}
	this.divElem.lastChild.appendChild( document.createTextNode('Address') );
	
	this.divElem.appendChild( this.pane[0] );
	this.divElem.appendChild( this.pane[1] );
	this.divElem.appendChild( this.pane[2] );
	
	this.pane[0].className = 'pane_close';
	this.pane[0].appendChild( document.createTextNode(' Click on the Map to add points.') );
	
	this.pane[1].className = 'pane_close';
	this.pane[1].appendChild( document.createTextNode(' Enter via Coordinates:') );
//	this.pane[1].appendChild( document.createElement('br') );
	this.pane[1].appendChild( document.createElement('span') );
	this.pane[1].lastChild.appendChild( document.createTextNode('Latitude:') );
	this.pane[1].appendChild( this.enterLatElem = document.createElement('input') );
	this.pane[1].lastChild.type = 'text';
	this.pane[1].lastChild.name = 'enterLat';
//	this.pane[1].lastChild.size = '16';
	this.pane[1].appendChild( document.createElement('span') );
	this.pane[1].lastChild.appendChild( document.createTextNode('Longitude:') );
	this.pane[1].appendChild( this.enterLngElem = document.createElement('input') );
	this.pane[1].lastChild.type = 'text';
	this.pane[1].lastChild.name = 'enterLng';
//	this.pane[1].lastChild.size = '16';
	this.pane[1].appendChild( document.createElement('input') );
	this.pane[1].lastChild.type='button';
	this.pane[1].lastChild.value='Find Point';
	this.pane[1].lastChild.upref = this;
	this.pane[1].lastChild.onclick = this.BRdCAddCoordPt;
	
	this.pane[2].className = 'pane_close';
	this.pane[2].appendChild( document.createTextNode(' Enter via Street Address:') );
	this.pane[2].appendChild( document.createElement('span') );
	this.pane[2].lastChild.appendChild( document.createTextNode('Street:') );
	this.pane[2].appendChild( this.enterStreetElem = document.createElement('input') );
	this.pane[2].lastChild.type = 'text';
	this.pane[2].lastChild.name = 'enterSt';
//	this.pane[2].lastChild.size = '18';
	this.pane[2].appendChild( document.createElement('span') );
	this.pane[2].lastChild.appendChild( document.createTextNode('City:') );
	this.pane[2].appendChild( this.enterCityElem = document.createElement('input') );
	this.pane[2].lastChild.type = 'text';
	this.pane[2].lastChild.name = 'enterCity';
//	this.pane[2].lastChild.size = '18';
	this.pane[2].appendChild( document.createElement('span') );
	this.pane[2].lastChild.appendChild( document.createTextNode('State:') );
	this.pane[2].appendChild( this.enterStateElem = document.createElement('input') );
	this.pane[2].lastChild.type = 'text';
	this.pane[2].lastChild.name = 'enterState';
//	this.pane[2].lastChild.size = '18';
	this.pane[2].appendChild( document.createElement('input') );
	this.pane[2].lastChild.type='button';
	this.pane[2].lastChild.value='Find Point';
	this.pane[2].lastChild.upref = this;
	this.pane[2].lastChild.onclick = this.BRdCGeoCode;
	
	this.selectPane(2);

	var inContainer = map.getContainer();
	inContainer.appendChild( this.divElem );
	return this.divElem;
}
gRouteActionControl.prototype.printable = function()
{	return false;
}
gRouteActionControl.prototype.selectable = function()
{	return true;
}
gRouteActionControl.prototype.getDefaultPosition = function()
{	return new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize( 8, 25 ) );
}
gRouteActionControl.prototype.selectPane = function(panenum)
{	if (typeof this.curpane=='number' && this.curpane<this.pane.length)
		this.pane[this.curpane].className='pane_close';
	if (panenum<this.pane.length)
	{	this.curpane = panenum;
		this.pane[panenum].className=('pane_'+panenum);
	}
}
gRouteActionControl.prototype.BRdCGeoCode = function()
{	//called from the button -- this is set to the <input> dom element itself
	var theButton = this;
	var address = '';
	var holdText = theButton.value;
	theButton.value = "...looking...";
	theButton.disabled = 'disabled';

	function addString(str, add)
	{	if (add!='')
		{	if (str!='')
				return add + ', ' + str;
			else
				return add;
		}
		return str;
	}

//	address = addString( address, document.getElementByName('enterCntry').value );
	var gRAC = theButton.upref;
	address = addString( address, gRAC.enterStateElem.value );
	address = addString( address, gRAC.enterCityElem.value );
	address = addString( address, gRAC.enterStreetElem.value );
		
	theButton.upref.geocoder.getLatLng(address,
		function(point)
		{	try
			{	if (!point)
					alert(address + " not found");
				else
				{	map.setCenter(point, map.getZoom() );
					theButton.upref.BRdCFoundPt( point, address );
				}
			}	catch(e)
				{	alert(e);
				}
			theButton.value = holdText;
			theButton.disabled = false;
		} );
}
gRouteActionControl.prototype.BRdCAddCoordPt = function()
{	//called from the button -- this is set to the <input> dom element itself
	var gRAC = this.upref;
	var lat = gRAC.enterLatElem.value;
	var lng = gRAC.enterLngElem.value;
	lat = parseFloat(lat);
	lng = parseFloat(lng);
	if (isNaN(lat) || isNaN(lng))
	{	gRAC.enterLatElem.value=(isNaN(lat))?'0.0':lat;
		gRAC.enterLngElem.value=(isNaN(lng))?'0.0':lng;
	}	else
	{	this.upref.BRdCFoundPt( new GLatLng(lat,lng), "Coordinates: "+lat +"Lat X "+lng+"Lng" );
	}		
}
gRouteActionControl.prototype.BRdCFoundPt = function( BRdCPtLoc, ptFromText )
{	var domEntry = document.createElement('div');
	domEntry.appendChild( document.createElement('h2') );
	domEntry.firstChild.appendChild( document.createTextNode("Found Point") );
	domEntry.appendChild( document.createElement('p') );
	domEntry.lastChild.style.border="1pt solid #afa";
	domEntry.lastChild.appendChild( document.createTextNode( ptFromText ) );
	domEntry.appendChild( document.createElement('input') );
	domEntry.lastChild.type='button';
	domEntry.lastChild.value="Add Point";
	domEntry.lastChild.rDisp= this.routeDisp;
	domEntry.lastChild.onclick = function()
		{	this.rDisp.addRoutePoint(new BRdCPoint( BRdCPtLoc, ptFromText ) );
			this.rDisp.map.closeInfoWindow();
		}
	var marker = new GMarker( BRdCPtLoc, {clickable:true, icon:route_blankmark} );
	this.routeDisp.map.setCenter( BRdCPtLoc, (this.routeDisp.map.getZoom()>13) ? this.routeDisp.map.getZoom() : 14 );
	this.routeDisp.map.addOverlay( marker );
	GEvent.addListener(marker,"infowindowclose",
		function()
		{	map.removeOverlay( marker );
			delete marker;
		}	);
	marker.openInfoWindow( domEntry );
}



