/* * *
	developer: bitl at mail dot ru
	2004-09-16: v 0.1 alpha
	2004-09-17: v 0.9 alpha
	2004-09-20: v 1.0 alpha
	2004-09-24: v 1.0 beta
	2004-09-27: v 1.2 beta [+layersToForm, +submitLayers]
 * * */


/* * *
	MapControl class
 * * */

function MapControl( img_id, selector_id,  gp_label_elm_id_prefix, dis_label_elm_id, area_label_elm_id, canvas_id ){
	this.img_elm = document.getElementById( img_id );

	this.unregister_on_submit = true;

	this.geo_point_X_label_elm = document.getElementById( gp_label_elm_id_prefix + "x" );
	this.geo_point_Y_label_elm = document.getElementById( gp_label_elm_id_prefix + "y" );
	this.distance_label_elm = document.getElementById( dis_label_elm_id );
	this.distance_hint_label_elm = document.getElementById( dis_label_elm_id +"_hint" );
	this.area_label_elm = document.getElementById( area_label_elm_id );

	//window.alert( "2b" );
	if( canvas_id ){
		//window.alert( "3a" );
		this.canvas = document.getElementById( canvas_id );

		//window.alert( "3b" );
		this.js_graph = new jsGraphics( canvas_id );
		//window.alert( "3c" );
		this.js_graph.setColor( "#00ff00" );
		this.canvas.style.top = this.img_elm.clientTop;
		this.canvas.style.left = this.img_elm.clientLeft;
		this.canvas.style.display = "";
	}

	//window.alert( "2c" );

	/* possible values: MODE_ZOOM_IN, MODE_ZOOM_OUT, MODE_MEASURE_DISTANCE, MODE_MEASURE_AREA, MODE_QUERY, MODE_PAN */
	//this.mode = "MODE_ZOOM_IN";
	this.mode = "MODE_QUERY";

	this.img_elm.style.cursor = "crosshair";

	this.selector_x = 0;
	this.selector_y = 0;
	this.prev_x = 0;
	this.prev_y = 0;
	this.select_started = false;

	/* assign methods */

	this.imageObjectInit = MapControl_imageObjectInit;
	this.imageObjectUse = MapControl_imageObjectUse;

	this.cross = MapControl_cross;

	this.showGeoPointLabel = MapControl_showGeoPointLabel;
	this.hideGeoPointLabel = MapControl_hideGeoPointLabel;

	this.setMapMeters = MapControl_setMapMeters;
	this.setMaxExtent = MapControl_setMaxExtent;

	this.displayGeoPoint = MapControl_displayGeoPoint;

	this.fixClientX = MapControl_fixClientX;
	this.fixClientY = MapControl_fixClientY;
	this.client2imgX = MapControl_client2imgX;
	this.client2imgY = MapControl_client2imgY;
	this.imgRect2geo = MapControl_imgRect2geo;
	this.clientRect2geo = MapControl_clientRect2geo;

	this.startZoomIn = MapControl_startZoomIn;
	this.startQuery = MapControl_startQuery;

	this.stopZoomIn = MapControl_stopZoomIn;
	this.stopQuery = MapControl_stopQuery;

	this.adjustSelectorRect = MapControl_adjustSelectorRect;

	this.setModeZoomIn = MapControl_setModeZoomIn;
	this.isModeZoomIn = MapControl_isModeZoomIn;
	this.setModeMeasureDistance = MapControl_setModeMeasureDistance;
	this.isModeMeasureDistance = MapControl_isModeMeasureDistance;
	this.setModeMeasureArea = MapControl_setModeMeasureArea;
	this.isModeMeasureArea = MapControl_isModeMeasureArea;

	this.setModeZoomOut = MapControl_setModeZoomOut;
	this.isModeZoomOut = MapControl_isModeZoomOut;

	this.setModeQuery = MapControl_setModeQuery;
	this.isModeQuery = MapControl_isModeQuery;

	this.setModePan = MapControl_setModePan;
	this.isModePan = MapControl_isModePan;

	this.startMeasureDistance = MapControl_startMeasureDistance;
	this.stopMeasureDistance = MapControl_stopMeasureDistance;
	//this.cancelMeasureDistance = MapControl_cancelMeasureDistance;
	this.displayDistanceTo = MapControl_displayDistanceTo;

	this.startMeasureArea = MapControl_startMeasureArea;
	this.stopMeasureArea = MapControl_stopMeasureArea;
	this.selectedGeoRect = MapControl_selectedGeoRect;
	this.displayAreaTo = MapControl_displayAreaTo;

	this.isMouseReallyOut = MapControl_isMouseReallyOut;
	this.stopSelectors = MapControl_stopSelectors;

	this.selectorStyleZoomIn = MapControl_selectorStyleZoomIn;
	//this.selectorStyleMeasureDistance = MapControl_selectorStyleMeasureDistance;
	this.selectorStyleMeasureArea = MapControl_selectorStyleMeasureArea;

	this.assignButtonImages = MapControl_assignButtonImages;
	this.switchButtonImages = MapControl_switchButtonImages;
	this.switchButtonZoomOut = MapControl_switchButtonZoomOut;
	this.blinkButtonZoomOut = MapControl_blinkButtonZoomOut;
	this.switchButtonZoomFull = MapControl_switchButtonZoomFull;
	this.switchButtonPrint = MapControl_switchButtonPrint;
	this.switchButtonHelp = MapControl_switchButtonHelp;
	this.switchButtonBug = MapControl_switchButtonBug;

	this.setForm = MapControl_setForm;
	this.ext2form = MapControl_ext2form;

	this.submitExtentImg = MapControl_submitExtentImg;
	this.submitExtentGeo = MapControl_submitExtentGeo;

	//this.extentRect = MapControl_extentRect;

	//this.setConstraintRect = MapControl_setConstraintRect;
	this.panDirection = MapControl_panDirection;
	this.panGeoXY = MapControl_panGeoXY;

	this.zoomIn = MapControl_zoomIn;
	this.zoomInClick = MapControl_zoomInClick;
	this.zoomOut = MapControl_zoomOut;
	this.zoomFull = MapControl_zoomFull;
	this.changePixelSize = MapControl_changePixelSize;

	this.queryXYtoAction = MapControl_queryXYtoAction;
	this.queryXY_usingForm = MapControl_queryXY_usingForm;

	this.layersToForm = MapControl_layersToForm;
	this.getCheckedLayerBoxIds = MapControl_getCheckedLayerBoxIds;
	this.submitLayers = MapControl_submitLayers;

	this.unregisterOnSubmit = MapControl_unregisterOnSubmit;

	this.mouseMoved = MapControl_mouseMoved;

	this.onSetMode = MapControl_onSetMode;

	this.setModeQuery();
}

function MapControl_imageObjectInit( image_path ){
	this.imageMap = new Image();
	this.imageMap.src = image_path;
}

function MapControl_imageObjectUse(){
	this.img_elm.src = this.imageMap.src;
}

function MapControl_cross( x, y, halfsize, color ){
	this.js_graph.setColor( color );
	this.js_graph.drawLine( x - halfsize, y, x + halfsize, y );
	this.js_graph.drawLine( x, y - halfsize, x, y + halfsize );
}

function MapControl_selectorStyleZoomIn(){
	/*
	this.selector.style.background = "none";

	a_style = "1pt solid green"; // "2pt dashed blue";
	this.selector.style.borderLeft = a_style;
	this.selector.style.borderRight = a_style;
	this.selector.style.borderTop = a_style;
	this.selector.style.borderBottom = a_style;
	*/
}

/*
function MapControl_selectorStyleMeasureDistance(){
	this.selector.style.backgroundColor = "yellow";

	this.selector.style.borderLeft = "1pt solid blue";
	this.selector.style.borderRight = "1pt solid blue";
	this.selector.style.borderTop = "1pt solid blue";
	this.selector.style.borderBottom = "1pt solid blue";
}
*/

function MapControl_selectorStyleMeasureArea(){
	//this.selector.style.backgroundColor = "green";
	this.selector.style.background = "none";

	this.selector.style.borderLeft = "1pt dashed green";
	this.selector.style.borderRight = "1pt dashed green";
	this.selector.style.borderTop = "1pt dashed green";
	this.selector.style.borderBottom = "1pt dashed green";
}

function MapControl_showGeoPointLabel(){
	if( this.geo_point_X_label_elm.style.display == "none" ){ // avoid blinking if already shown
		this.geo_point_X_label_elm.style.display = "";
		this.geo_point_Y_label_elm.style.display = "";
	}
}

function MapControl_hideGeoPointLabel(){
	this.geo_point_X_label_elm.style.display = "none";
	this.geo_point_Y_label_elm.style.display = "none";
}

function MapControl_setMapMeters( meters_left, meters_top, meters_width, meters_height ){
	this.meters_left = meters_left;
	this.meters_top = meters_top;
	this.meters_width = meters_width;
	this.meters_height = meters_height;

	//this.metersInX = meters_width / this.img_elm.width;
	//this.metersInY = meters_height / this.img_elm.height;
	this.metersInPixel = Math.max( meters_width, meters_height ) / this.img_elm.width;

	this.pixOffsetForX = this.pixOffsetForY = 0;
	if( meters_width < meters_height ){
		this.pixOffsetForX = ( this.img_elm.width - meters_width / this.metersInPixel ) / 2;
	}else if( meters_width > meters_height ){
		this.pixOffsetForY = ( this.img_elm.height - meters_height / this.metersInPixel ) / 2;
	}

	this.extentRect = new OrderedRect( this.meters_left, this.meters_top,
		this.meters_left + this.meters_width, this.meters_top + this.meters_height );
}

function MapControl_setMaxExtent( min_x, min_y, max_x, max_y ){
	this.maxExtentRect = new OrderedRect( min_x, min_y, max_x, max_y );
}

function MapControl_displayGeoPoint( clientX, clientY ){
	if( this.geo_point_X_label_elm.style.display != "none" ){
		r = this.clientRect2geo( new OrderedRect( clientX, clientY, clientX, clientY ) );
		this.geo_point_X_label_elm.innerHTML = Math.round( r.minX() );
		this.geo_point_Y_label_elm.innerHTML = Math.round( r.minY() );
	}
}

function MapControl_fixClientX( clientX ){
	return document.body.scrollLeft ? clientX + document.body.scrollLeft : clientX;
}

function MapControl_fixClientY( clientY ){
	return document.body.scrollTop ? clientY + document.body.scrollTop : clientY;
}

function MapControl_client2imgX( clientX ){
	return this.fixClientX( clientX ) - GetPositionPoint( this.img_elm ).x;
}

function MapControl_client2imgY( clientY ){
	return this.fixClientY( clientY ) - GetPositionPoint( this.img_elm ).y;
}


function MapControl_imgRect2geo( orect ){
	return new OrderedRect(
		this.meters_left + ( orect.minX() - this.pixOffsetForX ) * this.metersInPixel,
		this.meters_top + (this.img_elm.height - orect.minY() + this.pixOffsetForY ) * this.metersInPixel,
		this.meters_left + ( orect.maxX() - this.pixOffsetForX ) * this.metersInPixel,
		this.meters_top + (this.img_elm.height - orect.maxY() + this.pixOffsetForY ) * this.metersInPixel );
}

function MapControl_clientRect2geo( orect ){
	r = new OrderedRect( this.client2imgX( orect.minX() ), this.client2imgY( orect.minY() ),
		this.client2imgX( orect.maxX() ), this.client2imgY( orect.maxY() ) );
	return this.imgRect2geo( r );
}

function MapControl_startZoomIn( clientX, clientY ){
	this.setModeZoomIn();
	this.js_graph.setColor( "green" );
	this.selector_x = clientX;
	this.selector_y = clientY;
	this.select_started = true;
}

function MapControl_startQuery( clientX, clientY ){
	this.setModeQuery();
	this.js_graph.setColor( "green" );
	this.selector_x = clientX;
	this.selector_y = clientY;
	this.select_started = true;
}

function MapControl_stopZoomIn(){
	this.select_started = false;
	if( this.js_graph ) this.js_graph.clear();
}

function MapControl_stopQuery(){
	this.select_started = false;
	if( this.js_graph ) this.js_graph.clear();
}

function MapControl_adjustSelectorRect( clientX, clientY ){
	//window.status = "1111";

	var min_x = Math.min( this.selector_x, clientX );
	var min_y = Math.min( this.selector_y, clientY );
	var max_x = Math.max( this.selector_x, clientX );
	var max_y = Math.max( this.selector_y, clientY );

	//window.status = "2222";
	/*
	this.selector.style.left = min_x +"px";
	this.selector.style.top = min_y +"px";
	this.selector.style.width = ( max_x - min_x ) +"px";
	this.selector.style.height = ( max_y - min_y ) +"px";
	*/
	this.js_graph.clear();
	if( userBrowser.isNS() ){
		this.js_graph.drawLine( min_x +1, min_y +1, min_x +1, max_y -1 );
		this.js_graph.drawLine( min_x +1, max_y -1, max_x -1, max_y -1 );
		this.js_graph.drawLine( max_x -1, min_y +1, max_x -1, max_y -1 );
		this.js_graph.drawLine( min_x +1, min_y +1, max_x -1, min_y +1 );
	}else{
		this.js_graph.drawLine( min_x -1, min_y -1, min_x -1, max_y -3 );
		this.js_graph.drawLine( min_x -1, max_y -3, max_x -3, max_y -3 );
		this.js_graph.drawLine( max_x -3, min_y -1, max_x -3, max_y -3 );
		this.js_graph.drawLine( min_x -1, min_y -1, max_x -3, min_y -1 );
	}
	this.js_graph.paint();
	//window.status = "3333";
}

function MapControl_setModeZoomIn(){
	this.onSetMode();
	if( this.mode != "MODE_ZOOM_IN" ){
		//this.select_started = false;
		this.mode = "MODE_ZOOM_IN";
		//this.js_graph.clear();
		this.switchButtonImages( this.btn_zoom_in );
	}
}

function MapControl_isModeZoomIn(){ return this.mode == "MODE_ZOOM_IN"; }

function MapControl_setModeMeasureDistance(){
	this.onSetMode();
	if( this.mode != "MODE_MEASURE_DISTANCE" ){
		//this.js_graph.clear();
		//this.select_started = false;
		this.mode = "MODE_MEASURE_DISTANCE";
		this.switchButtonImages( this.btn_line );
	}
}

function MapControl_isModeMeasureDistance(){ return this.mode == "MODE_MEASURE_DISTANCE"; }

function MapControl_setModeMeasureArea(){
	this.onSetMode();
	if( this.mode != "MODE_MEASURE_AREA" ){
		//this.select_started = false;
		this.mode = "MODE_MEASURE_AREA";
		//this.js_graph.clear();
		this.switchButtonImages( this.btn_poly );
	}
}

function MapControl_isModeMeasureArea(){ return this.mode == "MODE_MEASURE_AREA"; }

function MapControl_setModeZoomOut(){
	this.onSetMode();
	if( this.mode != "MODE_ZOOM_OUT" ){
		//this.select_started = false;
		this.mode = "MODE_ZOOM_OUT";
		//this.js_graph.clear();
		this.switchButtonImages( this.btn_zoom_out );
	}
}

function MapControl_isModeZoomOut(){ return this.mode == "MODE_ZOOM_OUT"; }

function MapControl_setModeQuery(){
	this.onSetMode();
	if( this.mode != "MODE_QUERY" ){
		//this.select_started = false;
		this.mode = "MODE_QUERY";
		//this.js_graph.clear();
		this.switchButtonImages( this.btn_query );
	}
}

function MapControl_isModeQuery(){ return this.mode == "MODE_QUERY"; }

/* stop:
        this.select_started = false;
        this.selector.style.display = "none";
        this.js_graph.clear();
*/

function MapControl_setModePan(){
	this.onSetMode();
	if( this.mode != "MODE_PAN" ){
		//this.select_started = false;
		this.mode = "MODE_PAN";
		//this.js_graph.clear();
		// this.switchButtonImages(
	}
}

function MapControl_isModePan(){ return this.mode == "MODE_PAN"; }

function MapControl_startMeasureDistance( clientX, clientY ){
	this.setModeMeasureDistance();
	//this.selectorStyleMeasureDistance();

	this.selector_x = this.prev_x = clientX;
        this.selector_y = this.prev_y = clientY;

	this.total_distance = 0;

	this.cross( clientX, clientY, 4, "#00ff00" );
	this.js_graph.paint();

        //this.selector.style.left = ( this.selector_x - 2 ) +"px";
        //this.selector.style.top = ( this.selector_y - 2 ) +"px";
        //this.selector.style.width = "4px";
        //this.selector.style.height = "4px";

        this.select_started = true;
        //this.selector.style.display = "";
	//this.distance_label_elm.style.display = "";
}

function MapControl_stopMeasureDistance( leaveDrawings ){
	this.select_started = false;
	if( !leaveDrawings ){
		if( this.selector ) this.selector.style.display = "none";
		this.distance_label_elm.style.display = "none";
		this.distance_hint_label_elm.style.display = "none";
		if( this.js_graph ) this.js_graph.clear();
	}
}

/*function MapControl_cancelMeasureDistance(){
	this.select_started = false;
	this.distance_label_elm.style.display = "none";
	if( this.js_graph ) this.js_graph.clear();
}*/

function MapControl_displayDistanceTo( clientX, clientY, do_draw ){
	//r = new OrderedRect( this.prev_x, this.prev_y, clientX, clientY );
	gr = this.clientRect2geo( new OrderedRect( this.prev_x, this.prev_y, clientX, clientY ) );
	var last_distance = Math.round( gr.diagonal() );

	this.distance_label_elm.innerHTML = "distance: "+ ( this.total_distance + last_distance ) + " meters";

	if( this.distance_label_elm.style.display == "none" ) this.distance_label_elm.style.display = "";
	if( this.distance_hint_label_elm.style.display = "none" ) this.distance_hint_label_elm.style.display = "";

	if( do_draw ){
		fixX = this.fixClientX( clientX );
		fixY = this.fixClientY( clientY );
		this.total_distance += last_distance;
		this.cross( fixX, fixY, 4, "#00ff00" );
		this.js_graph.setColor( "green" );
		//window.alert( this.prev_x +","+ this.prev_y +" "+ fixX +","+ fixY );
		this.js_graph.drawLine( this.prev_x, this.prev_y, fixX, fixY );
		this.js_graph.paint();
		this.prev_x = fixX;
		this.prev_y = fixY;
	}
}

function MapControl_startMeasureArea( clientX, clientY ){
	this.setModeMeasureArea();
	//this.selectorStyleMeasureArea();

	this.selector_x = clientX;
        this.selector_y = clientY;

	this.selector.style.left = this.selector_x +"px";
	this.selector.style.top = this.selector_y +"px";
	this.selector.style.width = "1px";
	this.selector.style.height = "1px";
        this.selector.style.display = "";

        this.select_started = true;
	this.area_label_elm.style.display = "";
}

function MapControl_stopMeasureArea(){
	this.select_started = false;
	this.selector.style.display = "none";
	this.area_label_elm.style.display = "none";
	this.js_graph.clear();
}

function MapControl_selectedGeoRect( clientX, clientY ){
	return this.clientRect2geo( new OrderedRect( this.selector_x, this.selector_y, clientX, clientY ) );
}

function MapControl_displayAreaTo( clientX, clientY ){
	this.adjustSelectorRect( clientX, clientY );

	gr = this.selectedGeoRect( clientX, clientY );
	this.area_label_elm.innerHTML = "area: "+ Math.round( gr.area() ) + " m<SUP>2</SUP>";
}

function MapControl_isMouseReallyOut( clientX, clientY ){
	var imgX = this.client2imgX( clientX );
	var imgY = this.client2imgY( clientY );
	//window.alert( imgX +"/"+ imgY +" : "+ this.img_elm.width +"/"+ this.img_elm.height );
	return ( imgX < 1 ) || ( imgX > this.img_elm.width ) || ( imgY < 1 ) || ( imgY > this.img_elm.height );
}

function MapControl_stopSelectors( leaveDrawings ){
	if( this.isModeZoomIn() ){
		map_control.stopZoomIn();
	}else if( this.isModeQuery() ){
		this.stopQuery();
	}else if( this.isModeMeasureDistance() ){
		this.stopMeasureDistance( leaveDrawings );
	}else if( this.isModeMeasureArea() ){
		this.stopMeasureArea();
	}
}

function MapControl_assignButtonImages(
	btn_zoom_in, src_zoom_in_on, src_zoom_in_off,
	btn_zoom_out, src_zoom_out_on, src_zoom_out_off,
	btn_zoom_full, src_zoom_full_on, src_zoom_full_off,
	btn_query, src_query_on, src_query_off,
	btn_print, src_print_on, src_print_off,
	btn_help, src_help_on, src_help_off,
	btn_bug, src_bug_on, src_bug_off,
	btn_line, src_line_on, src_line_off,
	btn_poly, src_poly_on, src_poly_off
){
	this.btn_zoom_in = btn_zoom_in;
	this.zoom_in_on = new Image();
	this.zoom_in_on.id = btn_zoom_in +"_on";
	this.zoom_in_on.src = src_zoom_in_on;
	this.zoom_in_off = new Image();
	this.zoom_in_off.id = btn_zoom_in +"_off";
	this.zoom_in_off.src = src_zoom_in_off;

	this.btn_zoom_out = btn_zoom_out;
	this.zoom_out_on = new Image();
	this.zoom_out_on.id = btn_zoom_out +"_on";
	this.zoom_out_on.src = src_zoom_out_on;
	this.zoom_out_off = new Image();
	this.zoom_out_off.id = btn_zoom_out +"_off";
	this.zoom_out_off.src = src_zoom_out_off;

	this.btn_zoom_full = btn_zoom_full;
	this.zoom_full_on = new Image();
	this.zoom_full_on.id = btn_zoom_full +"_on";
	this.zoom_full_on.src = src_zoom_full_on;
	this.zoom_full_off = new Image();
	this.zoom_full_off.id = btn_zoom_full +"_off";
	this.zoom_full_off.src = src_zoom_full_off;

	this.btn_query = btn_query;
	this.query_on = new Image();
	this.query_on.id = btn_query +"_on";
	this.query_on.src = src_query_on;
	this.query_off = new Image();
	this.query_off.id = btn_query +"_off";
	this.query_off.src = src_query_off;

	this.btn_print = btn_print;
	this.print_on = new Image();
	this.print_on.id = btn_print +"_on";
	this.print_on.src = src_print_on;
	this.print_off = new Image();
	this.print_off.id = btn_print +"_off";
	this.print_off.src = src_print_off;

	this.btn_help = btn_help;
	this.help_on = new Image();
	this.help_on.id = btn_help +"_on";
	this.help_on.src = src_help_on;
	this.help_off = new Image();
	this.help_off.id = btn_help +"_off";
	this.help_off.src = src_help_off;

	this.btn_bug = btn_bug;
	this.bug_on = new Image();
	this.bug_on.id = btn_bug +"_on";
	this.bug_on.src = src_bug_on;
	this.bug_off = new Image();
	this.bug_off.id = btn_bug +"_off";
	this.bug_off.src = src_bug_off;

	this.btn_line = btn_line;
	this.line_on = new Image();
	this.line_on.id = btn_line +"_on";
	this.line_on.src = src_line_on;
	this.line_off = new Image();
	this.line_off.id = btn_line +"_off";
	this.line_off.src = src_line_off;

	this.btn_poly = btn_poly;
	this.poly_on = new Image();
	this.poly_on.id = btn_poly +"_on";
	this.poly_on.src = src_poly_on;
	this.poly_off = new Image();
	this.poly_off.id = btn_poly +"_off";
	this.poly_off.src = src_poly_off;
}

function MapControl_switchButtonImages( btn_id ){
	switch( btn_id ){
		case this.btn_zoom_in:
			document.images[ this.btn_zoom_in ].src = this.zoom_in_on.src;
			document.images[ this.btn_zoom_out ].src = this.zoom_out_off.src;
			document.images[ this.btn_query ].src = this.query_off.src;
			document.images[ this.btn_line ].src = this.line_off.src;
			document.images[ this.btn_poly ].src = this.poly_off.src;
			break;
		case this.btn_zoom_out:
			document.images[ this.btn_zoom_in ].src = this.zoom_in_off.src;
			document.images[ this.btn_zoom_out ].src = this.zoom_out_on.src;
			document.images[ this.btn_query ].src = this.query_off.src;
			document.images[ this.btn_line ].src = this.line_off.src;
			document.images[ this.btn_poly ].src = this.poly_off.src;
			break;
		case this.btn_query:
			document.images[ this.btn_zoom_in ].src = this.zoom_in_off.src;
			document.images[ this.btn_zoom_out ].src = this.zoom_out_off.src;
			document.images[ this.btn_query ].src = this.query_on.src;
			document.images[ this.btn_line ].src = this.line_off.src;
			document.images[ this.btn_poly ].src = this.poly_off.src;
			break;
		case this.btn_line:
			document.images[ this.btn_zoom_in ].src = this.zoom_in_off.src;
			document.images[ this.btn_zoom_out ].src = this.zoom_out_off.src;
			document.images[ this.btn_query ].src = this.query_off.src;
			document.images[ this.btn_line ].src = this.line_on.src;
			document.images[ this.btn_poly ].src = this.poly_off.src;
			break;
		case this.btn_poly:
			document.images[ this.btn_zoom_in ].src = this.zoom_in_off.src;
			document.images[ this.btn_zoom_out ].src = this.zoom_out_off.src;
			document.images[ this.btn_query ].src = this.query_off.src;
			document.images[ this.btn_line ].src = this.line_off.src;
			document.images[ this.btn_poly ].src = this.poly_on.src;
			break;

	}
}


function MapControl_switchButtonZoomOut( status ){
	document.images[ this.btn_zoom_out ].src = status ? this.zoom_out_on.src : this.zoom_out_off.src;
}

function MapControl_switchButtonZoomFull( status ){
	document.images[ this.btn_zoom_full ].src = status ? this.zoom_full_on.src : this.zoom_full_off.src;
}

function MapControl_switchButtonPrint( status ){
	document.images[ this.btn_print ].src = status ? this.print_on.src : this.print_off.src;
}

function MapControl_switchButtonHelp( status ){
	document.images[ this.btn_help ].src = status ? this.help_on.src : this.help_off.src;
}

function MapControl_switchButtonBug( status ){
	document.images[ this.btn_bug ].src = status ? this.bug_on.src : this.bug_off.src;
}

function MapControl_blinkButtonZoomOut(){
	/*
	var msec = 250;
	document.images[ this.btn_zoom_out ].border = "1";
	this.switchButtonZoomOut(0);
	pause( msec );
	document.images[ this.btn_zoom_out ].border = "0";
	this.switchButtonZoomOut(1);
	pause( msec );
	document.images[ this.btn_zoom_out ].border = "1";
	this.switchButtonZoomOut(0);
	pause( msec );
	document.images[ this.btn_zoom_out ].border = "0";
	this.switchButtonZoomOut(1);
	*/

	this.switchButtonImages( this.btn_zoom_out )
}

function MapControl_setForm( a_form ){
	this.form = a_form;
}

function MapControl_ext2form( orect, a_form ){
	a_form.elements[ "min_x" ].value = orect.minX();
	a_form.elements[ "min_y" ].value = orect.minY();
	a_form.elements[ "max_x" ].value = orect.maxX();
	a_form.elements[ "max_y" ].value = orect.maxY();
}

function MapControl_submitExtentImg( orect ){
	this.submitExtentGeo( this.imgRect2geo( orect ) );
}


function MapControl_submitExtentGeo( orect ){

	orect.squarizeByLessSide();

	this.ext2form( orect, this.form );
	this.layersToForm( this.form, this.form );
	this.form.elements[ "top_folder" ].value = current_folder;

	if( this.unregister_on_submit ){
		event_core__dispatcher.unregister( "img_map" );
		event_core__dispatcher.unregister( "img_refmap" );
	}

	this.form.submit();
}

function MapControl_setConstraintRect( r ){
	this.constraint_rect = r;
}

function MapControl_panDirection( direction ){
	rect = "";
	switch( direction ){
		case "SE":
			rect = new OrderedRect( this.meters_left + this.meters_width, this.meters_top - this.meters_height,
				this.meters_left + 2 * this.meters_width, this.meters_top );
		break;
		case "S":
			rect = new OrderedRect( this.meters_left, this.meters_top - this.meters_height,
				this.meters_left + this.meters_width, this.meters_top );
		break;
		case "SW":
			rect = new OrderedRect( this.meters_left - this.meters_width, this.meters_top - this.meters_height,
				this.meters_left, this.meters_top );
		break;
		case "W":
			rect = new OrderedRect( this.meters_left - this.meters_width, this.meters_top,
				this.meters_left, this.meters_top + this.meters_height );
		break;
		case "NW":
			rect = new OrderedRect( this.meters_left - this.meters_width, this.meters_top + this.meters_height,
				this.meters_left, this.meters_top + 2 * this.meters_height );
		break;
		case "N":
			rect = new OrderedRect( this.meters_left, this.meters_top + this.meters_height,
				this.meters_left + this.meters_width, this.meters_top + 2 * this.meters_height );
		break;
		case "NE":
			rect = new OrderedRect( this.meters_left + this.meters_width, this.meters_top + this.meters_height,
				this.meters_left + 2 * this.meters_width, this.meters_top + 2 * this.meters_height );
		break;
		case "E":
			rect = new OrderedRect( this.meters_left + this.meters_width, this.meters_top,
				this.meters_left + 2 * this.meters_width, this.meters_top + this.meters_height );
		break;
	}
	rect.applyConstraint( this.maxExtentRect, true );
	//rect.squarizeByLessSide();
	this.submitExtentGeo( rect );
}

function MapControl_panGeoXY( x, y ){
	halfWidth = Math.round( this.extentRect.halfWidth() );
	halfHeight = Math.round( this.extentRect.halfHeight() );
	//this.submitExtentGeo( new OrderedRect( x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight ) );
	this.submitExtentGeo( new OrderedRect( x - halfWidth * 2, y, x, y + halfHeight * 2) );
}

function MapControl_zoomIn( clientX, clientY ){
	if( this.selector_x != clientX && this.selector_y != clientY ){
		img_rect = new OrderedRect(
			this.client2imgX( this.selector_x ), this.client2imgY( this.selector_y ),
			this.client2imgX( clientX ), this.client2imgY( clientY ) );

		//img_rect.squarizeByLessSide();

		this.stopSelectors();
		this.submitExtentImg( img_rect );
	}else{
		this.zoomInClick( clientX, clientY );
	}
}

function MapControl_zoomInClick( clientX, clientY ){
	rect = this.clientRect2geo( new OrderedRect( clientX, clientY, clientX, clientY ) );
	c_x = rect.minX();
	c_y = rect.minY();
	//half_of_new_size = Math.round( Math.min( this.meters_width, this.meters_height ) / 4 );
	half_of_new_size = Math.round( Math.max( this.meters_width, this.meters_height ) / 4 );
	this.submitExtentGeo( new OrderedRect( c_x - half_of_new_size, c_y - half_of_new_size,
		c_x + half_of_new_size, c_y + half_of_new_size ) );
}

/*
function MapControl_zoomOutXY( clientX, clientY ){
	r = this.clientRect2geo( new OrderedRect( clientX, clientY, clientX, clientY ) );
	geoX = r.minX();
	geoY = r.minY();
	//
	//rect.applyConstraint( this.maxExtentRect, false );
	//this.submitExtentGeo( rect );

	this.submitExtentGeo( new OrderedRect( geoX - this.meters_width, geoY - this.meters_height,
		geoX + this.meters_width, geoY + this.meters_height )
	);
}
*/

function MapControl_zoomOut( scale ){
	minX = this.meters_left - (scale - 1)/2*this.meters_width;
	minY = this.meters_top - (scale - 1)/2*this.meters_height;
	//
	rect = new OrderedRect( minX, minY, minX + this.meters_width * scale, minY + this.meters_height * scale );
	rect.applyConstraint( this.maxExtentRect, false );
	this.submitExtentGeo( rect );
}

function MapControl_zoomFull(){
	this.submitExtentGeo( new OrderedRect( 0, 0, 0, 0 ) );
}

function MapControl_changePixelSize( a_select ){
	var size = a_select.options[ a_select.selectedIndex ].value;
	this.form.elements[ "pix_size" ].value = size;

        //this.form.elements[ "map_img_src" ].value = "/cgi-bin/mapserv4?map=%2FUsers%2Fbelize%2Fbiodiversity.bz%2Fbbms.map&mode=map"
	//	+"&mapsize=" + size +"+"+ size
	//	+"&mapext="+ this.meters_left +"+"+ this.meters_top +"+"
	//		+ (this.meters_left + this.meters_width)
	//		+"+"+ (this.meters_top + this.meters_height);

	this.submitExtentGeo( this.extentRect );
}

function MapControl_queryXYtoAction( clientX, clientY, event_unregister, new_action, new_target ){
	rect = this.clientRect2geo( new OrderedRect( clientX, clientY, clientX, clientY ) );
	this.form.elements[ "query_x" ].value = Math.round( rect.minX() );
	this.form.elements[ "query_y" ].value = Math.round( rect.minY() );
	if( event_unregister ){
		event_core__dispatcher.unregister( "img_map" );
		event_core__dispatcher.unregister( "img_refmap" );
	}
	//this.submitExtentGeo( this.extentRect );
	this.layersToForm( this.form, this.form );
	old_action = this.form.action;
	old_target = this.form.target;
	this.form.action = new_action;
	this.form.target = new_target;
	this.form.submit();
	this.form.action = old_action;
	this.form.target = old_target;
}

function MapControl_queryXY_usingForm( clientX, clientY, a_form, event_unregister ){
	rect = this.clientRect2geo( new OrderedRect( clientX, clientY, clientX, clientY ) );
	a_form.elements[ "query_x" ].value = Math.round( rect.minX() );
	a_form.elements[ "query_y" ].value = Math.round( rect.minY() );

	this.layersToForm( this.form, a_form );
	if( event_unregister ){
		event_core__dispatcher.unregister( "img_map" );
		event_core__dispatcher.unregister( "img_refmap" );
	}

	a_form.submit();
}

function MapControl_getCheckedLayerBoxIds( checks_form ){
	boxIds = Array();
	elms = checks_form.elements;
	for( i = 0; i < elms.length; i++ ){
		ei = elms[i];
		if( 0 == ei.id.indexOf( "check" ) && ei.checked ){
			boxIds.push( ei.id );
		}
	}
	return boxIds;
}

function MapControl_layersToForm( checks_form, dst_form ){
	dst_form.elements[ "visible_layers" ].value = this.getCheckedLayerBoxIds( checks_form ).join( "," );
}

function MapControl_submitLayers(){
	this.submitExtentGeo( this.extentRect );
}

function MapControl_unregisterOnSubmit( flag ){
	this.unregister_on_submit = flag;
}

function MapControl_mouseMoved( x, y ){
	return this.selector_x != x || this.selector_y != y;
}

function MapControl_onSetMode(){
	this.stopSelectors( false );
	//if( this.isModeMeasureDistance() ) this.stopMeasureDistance( false );
}

/* * * EOF * * */
