////////////////////////////////////////////////////////////////////////////////////////////
// MappingControl controls client-side script
////////////////////////////////////////////////////////////////////////////////////////////

addNamespace("Mapping.Controls");

////////////////////////////////////////////////////////////////////////////////////////////

Mapping.Controls.PanOverlay = Class.create();
Mapping.Controls.PanOverlay.prototype =
{
	initialize: function(map)
	{
		this.map = map;
	},
	
	up: function()
	{
		Tools.ToolBox.quickUse("Pan", this.map, "pan(0, 100);");
	},
	
	left: function()
	{
		Tools.ToolBox.quickUse("Pan", this.map, "pan(100, 0);");
	},
	
	right: function()
	{
		Tools.ToolBox.quickUse("Pan", this.map, "pan(-100, 00);");
	},
	
	down: function()
	{
		Tools.ToolBox.quickUse("Pan", this.map, "pan(0, -100);");
	}
};

////////////////////////////////////////////////////////////////////////////////////////////

Mapping.Controls.Current = null;

Mapping.Controls.ZoomOverlay = Class.create();
Mapping.Controls.ZoomOverlay.prototype =
{
	initialize: function(map, barId, value)
	{
		this.scaleOffset = 23;
		this.scaleHeight = 33;
		this.map = map;
		this.position = Math.ceil(Math.round(this.scaleHeight / 3) / 2);
		if (value) this.position = value;
		this.range = new Core.Type.Range(0, this.scaleHeight, 3, this.position * 3);
		this.range.onchange = this.zoomBar;
		this.bar = document.getElementById(barId);
		this.timer = new Core.Timer();
		this.delay = 250;
		this.currentX = 0;
		this.capturing = false;
		this.scale = document.getElementById(barId.substring(0, barId.length - 3) + "Image");
		this.scaleTop = Core.Utility.getElementY(this.scale) + this.scaleOffset;
		this.bar.style.top = Core.Utility.toSize((this.scaleTop - this.map.top) + (this.position * this.range.extent) - (Core.Utility.getElementHeight(this.bar) / 2));
		
		this.bar.onmousedown = this.onmousedown;
	},
	
	onmousedown: function(e)
	{
		Core.Utility.setEvent(e);
		e = Core.Utility.lastEvent;
		
		if (e.srcElement)
			var control = eval(e.srcElement.id.substring(0, e.srcElement.id.length - 3));
		else
			var control = eval(e.target.id.substring(0, e.target.id.length - 3));
		
		Mapping.Controls.Current = control;
		
		control.capturing = true;
		control.currentY = Core.Utility.getClientY();
		
		control._onmousemove = document.onmousemove;
		document.onmousemove = control.onmousemove;
		control._onmouseup = document.onmouseup;
		document.onmouseup = control.onmouseup;
	},
	
	onmousemove: function(e)
	{
		var control = Mapping.Controls.Current;
		
		if (!control || !control.capturing)
			return;
		
		Core.Utility.setEvent(e);
		e = Core.Utility.lastEvent;
		
		control.currentY = Core.Utility.getClientY();
		
		var temp = control.currentY - control.scaleTop;
		
		if (control.currentY < control.scaleTop)
			temp = 0;
		else if (control.currentY > control.scaleTop + control.range.maximum)
			temp = control.range.maximum;
		
		var position = Math.floor(temp / control.range.extent);
		var adjustment = Math.round(position - control.position);
		
		if (adjustment == 1 || adjustment == 0 || adjustment == -1)
			return;
		
		control.range.setValue(temp);
	},
	
	onmouseup: function(e)
	{
		var control = Mapping.Controls.Current;
		
		if (!control || !control.capturing)
			return;
		
		document.onmousemove = control._onmousemove;
		document.onmouseup = control._onmouseup;
		
		control.capturing = false;
		Mapping.Controls.Current = null;
	},
	
	zoomIn: function()
	{
		Mapping.Controls.Current = this;
		this.range.setValue(this.range.value - (2 * this.range.extent));
		Mapping.Controls.Current = null;
	},
	
	zoomOut: function()
	{
		Mapping.Controls.Current = this;
		this.range.setValue(this.range.value + (2 * this.range.extent));
		Mapping.Controls.Current = null;
	},
	
	zoomBar: function()
	{
		var control = Mapping.Controls.Current;
		
		if (!control)
			return;
		
		var position = Math.floor(this.value / this.extent);
		var adjustment = Math.round(position - control.position);
		
		if (adjustment == 0)
			return;
		
		control.timer.delay = control.delay;
		control.timer.ontimer = function()
		{
			var factor = Math.abs(adjustment);
			
			Tools.ToolBox.get("Zoom").mode = (adjustment < 0) ? Mapping.ZoomOptions.ZoomIn : Mapping.ZoomOptions.ZoomOut;
			Tools.ToolBox.quickUse("Zoom", control.map, "zoomPoint(" + factor + ", new Mapping.IntegerPoint(Math.ceil(" + control.map.id + ".width / 2), Math.ceil(" + control.map.id + ".height / 2)));");
			control.position = position;
			
			Core.Debug.write("Zoom " + (adjustment < 0 ? "in" : "out") + " by factor of " + factor);
		}
		control.timer.start();
		
		control.bar.style.top = Core.Utility.toSize((control.scaleTop - control.map.top) + control.range.value - (Core.Utility.getElementHeight(control.bar) / 2));
	}
};



