/**
	Utilities used to convert the overview bounds to image bounds and vice versa.
*/

function OverviewUtils(){};

overviewUtils = new OverviewUtils();



/*
	Converts the given map bounds to the pixel bounds of the overview 
	image.
*/
OverviewUtils.prototype.mapToImageBounds = function( 
											 mapxmin,
											 mapxmax,
											 mapymin,
											 mapymax){
										
		var img = xbGetElementById(this.imageId);
		if ( img.width == 0) return null;
		var f = function(wl,wu,p,wx){
			// wl, wu sind World lower und upper bounds
			// P ist die Ausdehnung in Pixel
			// wx ist die Koordinate f?r die der Pixelwert gesucht wird. 
	    	var px = Math.round((wx-wl)/((wu-wl)/p));
	    	return px;
	    }
	    var xl = f(this.xmin,this.xmax,img.width,mapxmin);
	    var xh = f(this.xmin,this.xmax,img.width,mapxmax);
		var yl = f(this.ymin,this.ymax,img.height,mapymin);
	    var yh = f(this.ymin,this.ymax,img.height,mapymax);
	    // Begrenzung nach links und unten
	    xl = Math.max(-2,xl);
	    yl = Math.max(-5,yl);
	    var mb = new Bounds(xl,yl,xh-xl, yh-yl); 
	    mb.setImageWidth(img.width);
	    mb.setImageHeight(img.height);
		return mb;									 
}

OverviewUtils.prototype.imageToWorldBounds = function(){
		var imgxmin,imgxmax,imgymin, imgymax;
									 
		if ( arguments.length == 1) {
			var bnds = arguments[0];
			imgxmin = bnds.x;
			imgxmax = bnds.x + bnds.width;
			imgymin = bnds.y;
			imgymax = bnds.y + bnds.height;			
		} else {
			imgxmin = arguments[0];
			imgxmax = arguments[1];
			imgymin = arguments[2];
			imgymax = arguments[3];
		}	
		var img = xbGetElementById(this.imageId);
									
		var dwbx = (this.xmax - this.xmin)/img.width;									 
		var dwby = (this.ymax - this.ymin)/img.height;									 
	    
	    var xl = parseFloat(this.xmin) + parseFloat(dwbx * imgxmin);
	    var xh = xl + (dwbx * (imgxmax-imgxmin));
	    
	    var yl = parseFloat(this.ymin) + parseFloat(dwby * (img.height - imgymax));
	    var yh = yl + (dwbx * (imgymax-imgymin));
	    
	    var mb = new Bounds(xl,yl,xh-xl, yh-yl); 
		return mb;									 
}