function toggleReferenceMap() {
	if(ReferenceMap) {
		var ref_map = document.getElementById('reference_map');
		var state = 'hidden';
		if(ref_map.style.visibility == 'hidden') {
			state = 'visible';
		}
		ref_map.style.visibility = state;
		ReferenceMapObj.setVisibility(state);
		onResize();
	}
}

function toggleMapNavigator() {
	if(Navigator.getVisibility() != 'hidden') {
		Navigator.hideNavigator();
	} else {
		Navigator.showNavigator();
	}
}

/*
 * setTheme can dynamically change the theme.
 * This code might seem a bit odd, as it adds the themes and then removes them.
 * However, this is so that IE doesn't throw a fit upon the resize event.
 * So don't change that.
 */
function setTheme(color) {
	var THEMED_HEADER_URL = (new String(HEADER_URL)).replace(/%THEME%/i, color);
	document.getElementById('header_container').src = THEMED_HEADER_URL;

	var allImages = document.getElementsByTagName('img');
	for(var i = 0; i < allImages.length; i++) {
		if(allImages[i].getAttribute('psrc')) {
			var imgString = allImages[i].getAttribute('psrc').replace(/%THEME%/i, color);
			allImages[i].src = imgString;
		}
	}
	var head = document.getElementsByTagName('head')[0];
	var allLinks = document.getElementsByTagName('link');

	var subst = {
		'theme-styles' : 'skins/%THEME%/styles.css',
		'theme-menu' : 'skins/%THEME%/menubar.css'
	};
	for(var i = 0; i < allLinks.length; i++) {
		for(var id in subst) {
			if(allLinks[i].id == id) {
				allLinks[i].href = subst[id].replace(/%THEME%/,color);
			}
		}
	}
}

/*
 * The infamous "link to me" function.
 */

function linkToMe() {
	var cgi = new CGI();		// CGI Object for getting over-ride information.
	var loc = new String(window.location); // Cast to a string for our purposes.
	loc = loc.split('?')[0];	// Trim off anything after the request.
	loc = loc + '?';	
	var mapbook = cgi.param('mapbook');
	if(mapbook) { loc = loc + 'mapbook='+mapbook+'&'; }
	var mapLocation = Map.getCenterPoint().toString(',')+','+Map.getScale();
	loc = loc + 'zoomto=' +mapLocation+'&';

	var layers = Map.getLayerTitles();
	loc = loc + 'layers=';
	for(var i = 0; i < layers.length; i++) {
		loc = loc + escape(layers[i]) + ',';
	}
	loc = loc.substring(0,loc.length-1);

	window.location = loc;
}

/*
 * Display the Disclaimer
 */

function hideDisclaimer() {
	var toRemove = ['Disclaimer-Background', 'Disclaimer-Content'];
	for(var i = 0; i < toRemove.length; i++) {
		var elm = document.getElementById(toRemove[i]);
		var p = elm.parentNode;
		p.removeChild(elm);
	}
}

function showDisclaimer(mapbook) {
	var disclaimers = mapbook.getElementsByTagName('disclaimer');
	if(disclaimers.length < 1) { return false; }

	var discBg = document.createElement('div');
	var discContent = document.createElement('div');
	var body = document.getElementsByTagName('body')[0];

	discBg.setAttribute('id','Disclaimer-Background');
	discContent.setAttribute('id','Disclaimer-Content');
	body.appendChild(discBg);
	body.appendChild(discContent);

	var discMessage = '';
	for(var i = 0; i < disclaimers.length; i++) {
		discMessage = discMessage+disclaimers[i].firstChild.nodeValue+'<br/>';
	}

	discContent.innerHTML = discMessage;

	var discButtonBar = document.createElement('center');
	discButtonBar.style.width = '100%';
	discContent.appendChild(discButtonBar);

	var discClose = document.createElement('input');
	discClose.setAttribute('type','button');
	discClose.setAttribute('value', 'Accept');
	discButtonBar.appendChild(discClose);
	discClose.onclick = hideDisclaimer;
}

function displayMenu(rXML) {
	var menuBar = new Menubar();
	menuBar.setParent('middle_nav');
	menuBar.populateMenuBar(rXML);
}

function loadMenu() {
	var htMenubar = getHTTPObject();
	htMenubar.open('GET', MENU_SOURCE, true);
	htMenubar.onreadystatechange = function () {
		if(htMenubar.readyState == 4) {
			displayMenu(htMenubar.responseXML);
		}
	}
	htMenubar.send('');
}

function zoomToInitialView() {
	var markup = false;
	INITIAL_VIEW = new String(INITIAL_VIEW);
	if(INITIAL_VIEW.match(/MARKUP./)) {
		INITIAL_VIEW = INITIAL_VIEW.replace(/MARKUP./,'');
		markup = true;
	}
	var initialView = convertViewString(INITIAL_VIEW, Map.getMapSize());
	Events.changePointScale(initialView.Center, initialView.Radius, markup);
}

function startUpZoom() {
	zoomToInitialView();
	Events.removeEventListener('loadedmapbook', 'initialview');
}

function main() {
	var cgi = new CGI();
	if(cgi.param('skin')) {
		GEOMOOSE_SKIN = cgi.param('skin');
	}
	setTheme(GEOMOOSE_SKIN);


	if(cgi.param('title')) {
		document.title = cgi.param('title');
	} else {
		document.title = GEOMOOSE_TITLE;
	}

	/* Load the custom scripts */
	var head = document.getElementsByTagName('head')[0];
	for(var i = 0; i < CUSTOM_SCRIPTS.length; i++) {
		var script = document.createElement('script');
		script.setAttribute('type','text/javascript');
		script.setAttribute('src', CUSTOM_SCRIPTS[i]);
		head.appendChild(script);
		script = null
	}
	head = null;

	document.getElementById('disclaimer_organization').innerHTML = GEOMOOSE_ORGANIZATION;

	/* Load the Tabs */
	addTabPanel(document.getElementById('tabs'));
	for(var i = 0; i < TABS_TITLES.length; i++) {
		Tabs.addTab(TABS_TITLES[i], TABS_IDS[i]);
	}
	Tabs.showTab(TABS_TITLES[0]);

	/* Allow this to be set in Configuration.js */
	if(SHOW_MAP_NAVIGATOR) {
		var sliderbar = new MapSlider(Events, Map);
		sliderbar.setParent('map');
		sliderbar.initializeEvents();
	}

	/* Resize everything... */
	Events.triggerEvent('windowresize', null);
	Events.setPrimaryMap(Map);

	var viewFromURL = cgi.param('zoomto');
	var markup = cgi.param('target');

	if(viewFromURL) {
		if(markup == 'true' || markup == 'on') {
			INITIAL_VIEW = 'MARKUP*'+viewFromURL;
		} else {
			INITIAL_VIEW = viewFromURL;
		}
	}
	/* Hide/show the reference map container */
	if(!ReferenceMap) {
		document.getElementById('reference_map').style.display = 'none';
	}

	/* Get things to fit the window */
	onResize();
	/* Setup the initial view */
	zoomToInitialView();

	/* Load up the preset zooms */
	ViewBox.setParent('jumpToDiv');

	/* Get the mapbook */
	getDefaultMapbook(Events, Map); 

	/* Load the menu */
	loadMenu();
}

