/*
 * Updates the scale every navigate event.
 * Generates a navigate event when "Enter" is pressed.
 */

function ScaleBox(EventManager, ScaleMap) {
	var ScaleCenterPoint;

	var ScaleInput = document.createElement('input');
	ScaleInput.style.textAlign = 'right';
	ScaleInput.style.height = '15px';
	ScaleInput.style.fontSize = '13px';
	ScaleInput.size = '12';
	var ScaleGo = document.createElement('input');
//	ScaleGo.style.height = '15px';
	ScaleGo.style.fontSize = '13px';
	ScaleGo.value = 'Go!';
	ScaleGo.type = 'button';

	var ScaleJumper = document.createElement('div');
	ScaleJumper.style.zIndex = 10000;
	ScaleJumper.style.border = 'solid 3px black';
	ScaleJumper.style.visibility = 'hidden';
	ScaleJumper.style.position = 'absolute';
	ScaleJumper.style.bottom = '1px';
	ScaleJumper.style.height = 'auto';
	ScaleJumper.style.background = 'white';

	function populateScaleJumper() {
		for(var i in SCALEBOX_JUMPER_INCREMENTS) {
			var link = document.createElement('input');
			link.style.display = 'block';
			link.style.color = 'black';
			link.style.textAlign = 'right';
			var offsetW = 0;
			if(document.all) {
				offsetW -= 6;
			}
			link.style.width = ScaleInput.offsetWidth + offsetW + 'px';
			//link.appendChild(document.createTextNode(i));
			link.style.border = 'none';
			link.value = i;
			link.setAttribute('scale',SCALEBOX_JUMPER_INCREMENTS[i]);
			link.style.cursor = 'default';
			/*link.href = 'javascript:function nil() { return false; }';*/
			link.onfocus = onFocus;
			link.onmouseover = function () {
				this.style.background = '#DDD';
			}
			link.onmouseout = function () {
				this.style.background = 'white';
			}
			link.onclick = function () {
				ScaleInput.value = this.getAttribute('scale');
				ScaleInput.onblur();
				ScaleGo.onclick();
			}
			ScaleJumper.appendChild(link);
		}
	}

	ScaleGo.onclick = function(event) {
		if(window.event) { event = window.event; }
		EventManager.changePointScale(ScaleCenterPoint, inchScaleToPixelScale(ScaleMap, stripCommas(ScaleInput.value)));
	}

	function onFocus(event) {
		if(window.event) { event = window.event; }
		var left = ScaleInput.offsetLeft;
		var top = ScaleInput.offsetTop+ScaleInput.offsetHeight;
		var width = ScaleInput.offsetWidth;
		if(document.all) {
			left = objectClientPosition(ScaleInput).x;
			top += 4;
		}

		ScaleInput.style.background = '#DDD';
		ScaleJumper.style.visibility = 'visible';
		ScaleJumper.style.top = 'auto';
		ScaleJumper.style.bottom = top+'px';
		ScaleJumper.style.left = left + 'px';
		ScaleJumper.style.right = 'auto';
		ScaleJumper.style.zIndex = 10000;
	}

	ScaleInput.onblur = function(event) {
		if(window.event) { event = window.event; }
		if(document.all && event.srcElement == ScaleJumper) {
			return false;
		}
		ScaleInput.style.background = 'white';
		ScaleJumper.style.visibility = 'hidden';
	}
	
	ScaleInput.onkeypress = function(event) {
		if(window.event) { event = window.event; } 
		if(event.keyCode == 13) {
			EventManager.changePointScale(ScaleCenterPoint, inchScaleToPixelScale(ScaleMap, stripCommas(this.value)));
		}
		if(event.keyCode == 27) {
			this.value = Math.round(this.originalValue);
		}
	}
	this.onNavigate = function (psScale) {
		var scale = Math.round(pixelScaleToInchScale(ScaleMap, psScale.Radius));
		ScaleInput.value = commifyNumber(scale);
		ScaleInput.originalValue = scale;
		ScaleCenterPoint = psScale.Center;
	}
	
	this.setParent = function (p) {
		p.appendChild(ScaleInput);
		p.appendChild(ScaleGo);
		if(document.all) {
			document.getElementsByTagName('body')[0].appendChild(ScaleJumper);
		} else {	
			p.appendChild(ScaleJumper);
		}
		ScaleJumper.onfocus = onFocus;
		ScaleInput.onfocus = onFocus;
		populateScaleJumper();
	}

}
