
var REFERENCE_MAP_INSTANCE = 0;

function ReferenceMap (EventManager) {
	var ReferenceMapId = 'ReferenceMap'+REFERENCE_MAP_INSTANCE;
	REFERENCE_MAP_INSTANCE++;

	var ReferenceIndicatorId = ReferenceMapId+'Indicator';

	var MySelf = this;

	var myMap = new GMap(EventManager);

	var mapRelation = false;

	function referenceBoxMouseUp() {
		var box = document.getElementById(ReferenceIndicatorId+'-box');
		var centerX = (parseInt(box.style.left)) + parseInt(box.style.width) / 2;
		var centerY = (parseInt(box.style.top)) + parseInt(box.style.height) / 2;
		var groundPoint = myMap.pointToGroundUnits(new Point(centerX, centerY));
		EventManager.changePointScale(groundPoint, mapRelation.getScale());
	}

	function drawStaticReferenceBox() {
		var box = document.getElementById(ReferenceIndicatorId+'-box');
		if(mapRelation) {
			var dest_size = myMap.getMapSize();
			var dest_scale = myMap.getScale();
			var src_size = mapRelation.getMapSize();
			var src_scale = mapRelation.getScale();
			var src_point = mapRelation.getCenterPoint();

			var minx = src_point.x - src_scale * src_size.x/2;
			var maxx = src_point.x + src_scale * src_size.x/2;

			var miny = src_point.y - src_scale * src_size.y/2;
			var maxy = src_point.y + src_scale * src_size.y/2;

			var minPoint = myMap.groundUnitsToPixels(new Point(minx, maxy));
			var maxPoint = myMap.groundUnitsToPixels(new Point(maxx, miny));

			box.style.left = minPoint.x+'px';
			box.style.top = minPoint.y+'px';
			box.style.width = maxPoint.x - minPoint.x + 'px';
			box.style.height = maxPoint.y - minPoint.y + 'px';
		}
		/* If there's no map relation ... we really can't do anything */
	}

	this.onAddLayer = function(mapXMLObj) {
		var ref = new String(mapXMLObj.getAttribute('reference')).toLowerCase();
		if(ref == 'only' || ref == 'true') {
			myMap.addLayer(mapXMLObj);
			myMap.refreshLayers();
		}
	}

	this.onNavigate = function (psc) {
		if(REFERENCE_MAP_DYNAMIC) {
			myMap.autoFitLayers();
			if(!psc) {
				psc = new Circle(mapRelation.getCenterPoint(), mapRelation.getScale());
			}
			var scaledPsc = new Circle(psc.Center, psc.Radius);
			scaledPsc.Radius *= REFERENCE_MAP_MULTIPLIER;
			myMap.onChangePointScale(scaledPsc);
		} else {
			myMap.autoFitLayers();
			myMap.onChangePointScale(convertViewString(INITIAL_VIEW, myMap.getMapSize()));
		}
		drawStaticReferenceBox();
	}

	this.setParent = function (id) {
		var p = document.getElementById(id);

		var referenceBox = document.createElement('div');
		p.appendChild(referenceBox);
		referenceBox.className = 'ReferenceMapBox';
		referenceBox.id = ReferenceIndicatorId+'-box';

		var referenceMapDragger = new Draggable(referenceBox.id, referenceBox, true, true, true, true);
		attachEvent(referenceBox, 'mouseup', referenceBoxMouseUp);

		myMap.setParent(id);

		referenceBox.style.zIndex = 10000;
	}

	this.setMapRelation = function(map) {
		mapRelation = map;
	}

	this.initializeEvents = function () {
		EventManager.addEventListener('addlayer', ReferenceIndicatorId, MySelf.onAddLayer);
		EventManager.addEventListener('navigate', ReferenceIndicatorId, MySelf.onNavigate);
		EventManager.addEventListener('windowresize', ReferenceIndicatorId, MySelf.onNavigate);
//		EventManager.addEventListener('refreshlayers', ReferenceIndicatorId, MySelf.onNavigate);
/*		EventManager.addEventListener('navigate', ReferenceIndicatorId, 
			[myMap.onChangePointScale, myMap.clearPopups]);*/
	}
}
