
/*
	Initialises overview. This will create the box for the current map and
	establish the handlers for defining a new box.
*/
OVERVIEW_INITIALISED = false;

// Variable for the overview refresh thread
var setOverview;

function jgis_initOverview(){	
	if ( !jgis.bbox || jgis.bbox == null) {
		if ( setOverview == null ) {
		// Start thread to get the bounds later
			setOverview = window.setInterval("jgis_initOverview()",200);
		}
		return;
	}		
	var bnds = jgis.bbox;		
	if ( OVERVIEW_INITIALISED == true ||
	     bnds == null) return;
    var mb = overviewUtils.mapToImageBounds(bnds.xmin, bnds.xmax, bnds.ymin, bnds.ymax);
    if (mb == null ) {
    	if ( setOverview == null )setOverview = window.setInterval("jgis_initOverview()",200);
    	return;
    }
	if ( !jgis.overview ) jgis.overview = new Object();
	// Create box for existing map bounds
	jgis.overview.mapBounds = new Box(overviewUtils.boundsId, 
				mb.x,mb.getImageY(), 
				mb.width,mb.height,true,
				'red', 2);
	// Clear possible refresh thread
	if ( setOverview != null ) window.clearInterval(setOverview);
	OVERVIEW_INITIALISED = true;	
}

/*
	Callback when user has pressed the mouse button within the overview
*/
function jgis_overview_down(target, formName, evt){
	if ( !evt ) evt = window.event;
	var evtUtils = new EventUtils();
	
	// form used to submit the request 	
	var frm = document.forms[formName];
	// actual bounds of the image
	var bnds = jgis.bbox;		
	if ( !bnds || !frm) return;
	
	var start = evtUtils.getCoord(evt,target);

	target.onclick = click_handler;
	target.onmousemove = move_handler;
	target.onmouseup = up_handler;
	target.onmousedown = null;
	
	evtUtils.stopEvent(evt);
	
	// Handles mouse up.
	function up_handler(evt){
		if ( !evt ) evt = window.event;
		evtUtils.stopEvent(evt);
		var mh = target.onmousemove;
		target.onmousemove = null;
		target.onmouseup = null;
		var c = evtUtils.getCoord(evt,target);
		// Not moved !!
		var b = mh.dragBox;		
		if ( !b || (start.x == c.x && start.y == c.y) ) return;
		var nwbb = overviewUtils.imageToWorldBounds(b.getBounds());
		frm.action = frm.action+"&bbox="+nwbb.asBoundsString();
		jgis.submit(frm)
	}
	
	// Handle move (drawing box)	
	function move_handler(evt){
		if ( !evt ) evt = window.event;
		if ( evt.srcElement && evt.srcElement != target ) {
			evtUtils.stopEvent(evt);
			return;
		}
		var c = evtUtils.getCoord(evt,target);
		// Not moved !!
		if ( start.x == c.x && start.y == c.y ) return;

		var x = Math.min(start.x, c.x);
		var y = Math.min(start.y, c.y);
		var w = Math.max(Math.abs(start.x - c.x),1);
		var h = Math.max(Math.abs(start.y - c.y),1);
		
		if ( !move_handler.dragBox ){	
			move_handler.dragBox = new Box(overviewUtils.boundsId, x,y,w,h,true, 'blue',1);
		} else {
			var b = move_handler.dragBox;
			b.moveTo(x,y);
			b.setWidth(w);
			b.setHeight(h);
		}
		evtUtils.stopEvent(evt);	
	}
	
	// Handle clicks
	function click_handler(evt){
		if ( !evt ) evt = window.event;
		target.onclick = null;
		var c = evtUtils.getCoord(evt,target);
		// Moved no click!!
		if ( start.x != c.x || start.y != c.y ) return;	
		// up_handler(evt);
		// Lets display a box where the user has clicked
	    var mb = overviewUtils.mapToImageBounds(bnds.xmin, bnds.xmax, bnds.ymin, bnds.ymax);
		var b = new Bounds(mb.x, mb.getImageY(), mb.width, mb.height);
		// Center the box around the 'click'
		b.centerTo(c);
		// Create a new visible box
		var nb = new Box(overviewUtils.boundsId,b.x, b.y, b.width, b.height,true,
					'blue', 1);
		nb.show();
		// Calculate the new world bounds and submit the request
	    mb = overviewUtils.imageToWorldBounds(c.x-1, c.y-1, c.x+1, c.y+1);
		var nwbb = new Bounds(bnds.xmin, bnds.ymin, bnds.xmax - bnds.xmin, bnds.ymax - bnds.ymin);
		nwbb.centerTo(new Coord(mb.x, mb.y));	
		frm.action = frm.action+"&bbox="+nwbb.asBoundsString();
		evtUtils.stopEvent(evt);	
		jgis.submit(frm)
	}
}
