// Content sizing and window event functions for Atlas web gui.
// Draws layer bar and map frame to proper sizes.
// Handles onload, onresize, and onscroll events.
// Set mapwidth and mapheight in main html page to set default map size.
// Set either one to "fit" to fit that dimension to window size.
//
// October, 2006 - Russell Moffitt, Fernando Drabik


// Set some default values
var mapwidth = 'fit'; // Initial map dimensions
var mapheight = 'fit';
var cw = xClientWidth();
var ch = xClientHeight();
var mapframeborder_w = 0;
var mapframeborder_h = 0;
var imgborder = 0;

// Arrange layout on window load
xAddEventListener(window, "load", winOnLoad, false);

xAddEventListener(window, "resize", winOnResize, false);

function winOnLoad() { /* Uses DIVs:	layerform, mapform, mapholder */
	cw = 0; ch = 0;
	
	// Preload some images
	MM_preloadImages('images/collapseListOn.png','images/addLayerIconOn.png','images/zoomInOn.png','images/zoomOutOn.png','images/panOn.png','images/printOn.png','images/dataDownloadOn.png','images/mapRestoreOn.png','images/mapMaximizeOn.png')
	
	// Load map manager object
	mapLegendImg = xGetElementById('legendImg');
	mapLayerHandler = new LayerLord('layerLabels');
	mapWidth = 600;
	mapHeight = 400;
	mapServiceUrl = "/cgi-bin/oe/cdatMapper.py";
	//mapDefaultExtent = [-158, -157, -90, 90];
	mapDefaultExtent = [-178, -148, 13, 30];
	var mapHolder = xGetElementById('mapHolder');
	
	mapMan = new MapManager(mapHolder, mapLegendImg, mapLayerHandler, mapWidth, mapHeight, mapServiceUrl, mapDefaultExtent);
	

	
	
	// Load toolbox
	toolBox = new ToolBox(mapMan, 'tools');
	
	winOnResize(); // Size content until things fit
	// Setup Window Event Handlers
	xAddEventListener(window, "scroll", winOnScroll, false);
	
	// Send first map request at default extent
	//mapMan.requestMap();
	toolBox.activateTool('zoomin');
	
	setRegionExtent = function(extent) {
		mapMan.requestMap(extent);
	}

}

function winOnScroll() { /* Uses DIVs:	wrapall, footer, layersall */

	var slideTime = 500;
	var top = 17;
	var scrollh = xScrollTop();
	//alert(scrollh);
	if(scrollh > 69) {
		top = scrollh - 60;
	} else {
		top = 17;
	}

	xSlideTo('layerBoxContainer', xOffsetLeft('layerBoxContainer'), top, slideTime);
}



function winOnResize() {
	var w = xClientWidth();
	var h = xClientHeight();
	if (w!=cw || h!=ch) {
		cw = w;
		ch = h;
		sizeContent();
		var t = setTimeout('winOnResize();', 150);
	} else {
		mapMan.refreshSize();
	}
}

function sizeContent() { /* Uses DIVs:	screen, wrapall, footer, mapwrap, framewrap, screen */
	var contentTop = xPageY('wrapall');
	contentHeight = ch - contentTop - xHeight('footer');
	var layersRight = xOffsetLeft('layersall') + xWidth('layersall');
	var mapleftmargin = 5;
	sizeMap(mapwidth, mapheight, true);
	winOnScroll();
}

function sizeLinkbar() { /* Uses DIVs:	linkbar | IMG: linkedge */
	var linkbar = xGetElementById('linkbar');
	var linkedge = xGetElementById('linkedge');
	linkbar.style.width = '';
	linkedge.style.marginLeft = '0px';
	var lw = xWidth('linkbar');
	if (lw > cw) {// if linkbar bleeds off window
		// this is unecessary, redundant//shouldn't this resize the window to fit the bar instead?
		xWidth('linkbar', lw);
	} else {
		//resize linkbar to window dimensions)
		xWidth('linkbar', cw);
	}
	var edgemargin = xWidth('linkbar') -lw;
	if (edgemargin < 0) edgemargin = 0;
	linkedge.style.marginLeft = edgemargin +'px';
}

function changeMapSize() { /* Uses DIVs:	mapsize */
	// the map size tuple (w, h) is allocated to element 'mapsize' by the dropwon (id: mapsize) on the main page.Initial default values are "fit" 
	var sizes = xGetElementById('mapsize').value;
	var wh = sizes.split(",");
	mapwidth = wh[0];
	mapheight = wh[1];
	// Split tupple into linear array: (w,h) => array[i]
	
	winOnScroll; // size layer bar

	sizeContent(); // Resize page contents to fit new map
}

function sizeMap(mwidth, mheight, is_inner) {
	if (typeof mwidth == 'undefined' && typeof mheight == 'undefined') return false;
	if (mwidth != 'fit') mwidth = parseInt(mwidth);
	if (mheight != 'fit') mheight = parseInt(mheight);
	var mapOffsetX = xPageX('mapGUIContainer');
	var mapOffsetY = xPageY('mapHolder');
	var freewidth = xClientWidth() - mapOffsetX;
	var freeheight = xClientHeight() - mapOffsetY 
	var mapLegendWidth = xWidth('mapLegend');
	var toolBarWidth = xWidth('mapToolbar');

	mapWidth = xWidth('mapImage', parseInt(mwidth));
	mapHeight = xHeight('mapImage', parseInt(mheight));

	if (mwidth == 'fit') {
		mapWidth = freewidth - (mapLegendWidth + toolBarWidth + 4);
		mapWidth = xWidth('mapImage', mapWidth);
		mapHeight = freeheight - 10;
	}

	var legendHeight = xHeight('mapLegend', parseInt(mapHeight));
	var toolBarHeight = xHeight('mapToolbar', parseInt(mapHeight) + 20);	
	var newContainerWidth = mapWidth + mapLegendWidth + toolBarWidth + 4;
	containerWidth = xWidth('mapGUIContainer', newContainerWidth );
	mapHolderWidth = xWidth('mapHolder', mapWidth);
	mapHolderHeight = xHeight('mapHolder', mapHeight );
	if (mapHolderHeight + mapOffsetY + 10 > xClientHeight() ) {
		leftColumnHeight = xHeight('leftColumn', mapHolderHeight + mapOffsetY);
		shadowHeight = xHeight('shadow', mapHolderHeight + mapOffsetY);	
	} else {
		leftColumnHeight = xHeight('leftColumn', xClientHeight() - xPageY('leftColumn'));
		shadowHeight = xHeight('shadow', xClientHeight() - xPageY('shadow'));	
	}
	var mapMenuBarContent = xWidth('mapMenuBarContent', mapWidth + mapLegendWidth);
	mapMan.setMapSize(mapWidth, mapHeight);
	winOnScroll();
}


function sizeFrame(fwidth, fheight, is_inner) { /* Uses DIVs:	mapwrap, footer, framewrap, mapirameholder, frambar */
	cw = xClientWidth();
	ch = xClientHeight();
	
	if (typeof fwidth == 'undefined' && typeof fheight == 'undefined') return false;
	if (fwidth != 'fit') fwidth = parseInt(fwidth);
	if (fheight != 'fit') fheight = parseInt(fheight);
	var mapOffsetX = xPageX('mapGUIContainer');
	var mapOffsetY = xPageY('mapGUIContainer');

	var footshift = ch - xPageY('footer');
	var footh = (footshift > 0) ? footshift : 0;
	footh = xHeight('footer'); // IE explorer isn't subtracting scrollbar height from clientheight
	
	var freewidth = cw - mapOffsetX;
	var freeheight = ch - mapOffsetY ;
	var usedframewidth = mapframeborder_w*2 + imgborder*2;
	var fw = fwidth;
	if (is_inner) fw += usedframewidth;
	if (fwidth == 'fit') fw = freewidth;
	xWidth('framewrap', fw -10); //-10 is padding
	xWidth('frameholder', fw - usedframewidth -10);
	// Must give iframe explicit width or it doesn't behave well in IE
	var usedframeheight = xHeight('framebar') + mapframeborder_h*2 + imgborder*2;
	var fh = fheight;
	if (is_inner) fh += usedframeheight;
	if (fheight == 'fit') fh = freeheight;
	xHeight('frameholder', fh - usedframeheight);
}

function frameHeight(innerheight) {
	xHeight('frameholder', innerheight);
}
