
var VIEWBOX_INSTANCE = 0;	/* Instance Counter */

function ViewBox(EventManager, ViewMap) {
	var ViewListId = 'ViewBox'+VIEWBOX_INSTANCE+'-';
	VIEWBOX_INSTANCE++;

	var KnownViews = new Array();	/* Track the known views */

	this.addView = function(viewXMLObj) {
		var title = viewXMLObj.getAttribute('title');
		KnownViews[title] = viewXMLObj;
		refreshViewBox();
	}


	function addViewToList(viewList, viewXMLObj) {
		var title = viewXMLObj.getAttribute('title');
		var option = document.createElement('option');
		option.appendChild(document.createTextNode(title));
		option.setAttribute('id',ViewListId+'Option-'+title);
		option.value = viewXMLObj.firstChild.nodeValue;
		viewList.appendChild(option);
		option = null;
	}

	this.removeView = function(viewTitle) {
//		viewsList.removeChild(document.getElementById('ViewBoxOption-'+viewTitle));
		if(KnownViews[viewTitle]) {
			delete KnownViews[viewTitle];
		}
		refreshViewBox();
	}

	function refreshViewBox() {
		if(document.getElementById(ViewListId+'List')) {
			var vList = document.getElementById(ViewListId+'List');
			var vOptions = vList.getElementsByTagName('option');
			for(var i = vOptions.length - 1; i > 0; i--) {		/* The Zero-th Option should be the blank one. */
				vList.removeChild(vOptions[i]);
			}
			for(view in KnownViews) {
				addViewToList(vList, KnownViews[view]);
			}
		}
	}

	function changeView() {
		var viewsList = this;
		var viewOptions = viewsList.getElementsByTagName('option');
		var circView = convertViewString(viewOptions[viewsList.selectedIndex].value, ViewMap.getMapSize());
		EventManager.changePointScale(circView.Center, circView.Radius);
		for(var i = 0; i < viewOptions.length; i++) {
			viewOptions.selected = false;
		}
		viewOptions[0].selected = true;
	}

	this.setParent = function(p) {
		var viewsList = document.createElement('select');		/* Create the select box */
		viewsList.setAttribute('id', ViewListId+'List');		/* Set the List Id */
		viewsList.appendChild(document.createElement('option'));	/* Create the blank/reset option */
		viewsList.onchange = changeView;
		refreshViewBox();
		p.appendChild(document.createTextNode('Jump To:'));
		p.appendChild(viewsList);
		viewsList = null;
	}
}
