
// map-image size

var screenWidth=getScreenWidth();
var screenHeight=getScreenHeight();
var windowWidth=0;
var pageHeight=561;
var infoBoxHeight=00;
var isMouseDown=false;
var informationTabWidth=315;
var mapPadding=30;
var displayOverviewMap=false;
var isBusy=false;

var activeToolBoxTab;
var isToolBoxOpen=true;
var isWizardBoxOpen=true;

//var mapWidth = 500;	//width, height and aspect ratio are set in setMapSize()
//var mapHeight = 500;
//var mapAspectRatio= 0;
//var dX=0; 			//diameter across extent east-and-west 		set in refreshMap()
//var dY=0; 			//diameter across extent north-and-south	set in refreshMap()
var centerX=0;		//center of map X value		set in refreshMap()
var centerY=0;		//center of map Y value		set in refreshMap()

// current map extent (set in the map frame)
//var minx = 0;
//var miny = 0;
//var maxx = 0;
//var maxy = 0;

// full extent info
//var fullWidth = 0; // width and height are set in initMap()
//var fullHeight = 0;
//var fullMinx = 0;
//var fullMiny = 0;
//var fullMaxx = 0;
//var fullMaxy = 0;

// special maps
var showWXImage = 0;
var showTrafficImage = 0;



// init variables used in traffic, wx maps
tmpWXMapMinx = '';
tmpWXMapMiny = '';
tmpWXMapMaxx = '';
tmpWXMapMaxy = '';
tmpTMapMinx = '';
tmpTMapMiny = '';
tmpTMapMaxx = '';
tmpTMapMaxy = '';

var showSource=false;

function showImgSource(){
	if(!showSource){document.getElementById('imgSource').style.display='block';showSource=true;}
	else{document.getElementById('imgSource').style.display='none';showSource=false;}
}

function getScreenWidth(){
	var meas = window.screen.width;
	return meas;
}

function getScreenHeight(){
	var meas = window.screen.height;
	return meas;
}
function getPageHeight(){
	var meas = getAbsY(document.getElementById("bottomLeftPositioner"));
	return meas;
}
function getInfoBoxHeight(){
	var meas = (getAbsY(document.getElementById("bottomLeftPositioner"))-getAbsY(document.getElementById("bottomOfWizardPositioner")))-42;
	if(meas<=60)meas=60;
	return meas;
}
function getWindowWidth(){
	var meas = window.innerWidth;
	if (meas == null) meas = document.body.clientWidth;
	return meas;
}

function getCorrectMapWidth(){
if(!resizeMap)return mapWidth;
	var meas = windowWidth-(informationTabWidth+(2*mapPadding));
	if(meas<=475)meas=475;
	//if(meas<=475)meas=475;
	else if(meas>=1284)meas=1284;
	return meas;
}

function getCorrectMapHeight(){
if(!resizeMap)return mapHeight;
	var additionalVerticalBits=417;//(isToolBoxOpen)?317:157;
	var meas = getPageHeight()-additionalVerticalBits;

	if(meas<=150)meas=150;
	else if(meas>=796)meas=796;
	return meas;
}

// set the original extent so resetMap can return to it
function setOriginalExtent() {
	if (originalSet == 0) {
		originalMinx = minx;
		originalMiny = miny;
		originalMaxx = maxx;
		originalMaxy = maxy;
		originalSet = 1;
	}
}

function setMapSize(w,h){
mapWidth = w;
mapHeight = h;
mapAspectRatio=w/h;
}


function setMapParams(newMinx,newMiny,newMaxx,newMaxy){
			minx = newMinx;
	        miny = newMiny;
	        maxx = newMaxx;
	        maxy = newMaxy;		
			setZoomLevel();
			//status="minx="+minx+"   miny="+miny+"   maxx="+maxx+"   maxy="+maxy;
			//alert("minx="+minx+"\nminy="+miny+"\nmaxx="+maxx+"\nmaxy="+maxy);
}


function testScreen(){
str=(screen.availHeight)+"\n";
str+=(screen.availWidth)+"\n\n\n";
str+=(screen.width)+"\n";
str+=(screen.height)+"\n\n\n";
str+=(window.screenLeft)+"\n";
str+=(window.screenTop)+"\n\n\n";
alert(str);

}



var statusCount=0;
function conditionBrowser(){

	windowWidth = getWindowWidth();
	mapWidth = getCorrectMapWidth();
	mapHeight = getCorrectMapHeight();
	
	statusCount+=1;
	//window.status="windowWidth:"+windowWidth+"      statusCount:"+statusCount+"    mapWidth:"+mapWidth+"      "+isMouseDown+"         mapHeight:"+mapHeight;
	
	changeMapHolderSize();
	positionLayer("overViewMap", 0, 0, positionLayerId);
	positionLayer(mapImageId,0,0,positionLayerId);
	positionLayer(mapImageId2,0,0,positionLayerId);
	//positionLayer("theZoomBox",0,0,positionLayerId);
	positionLayer(eventLayerId,0,0,positionLayerId);
	//positionLayer("toolBox",-(mapPadding/2),mapHeight+(mapPadding/2)+25,positionLayerId);
	//positionLayer("toolBoxTabs",2,-23,"toolBox");
	positionLayer("entireToolBox",-(mapPadding/2),mapHeight+(mapPadding/2)+5,positionLayerId);
	//positionLayer("toolBoxTabs",0,0,"entireToolBox");
	//alert("zzz"+document.getElementById("theLoadingImg"));
	positionLayer("theLoadingImg",(mapWidth/2)-(loadingWidth/2),(mapHeight/2)-(loadingHeight/2),positionLayerId); 
	//positionLayer("theErrorImg",(mapWidth/2)-150,(mapHeight/2)-40,positionLayerId); 
}
function maximizeBrowser(){
	if (window.screen) {
		var newWidth=(screen.availWidth<=1276)?screen.availWidth-4:1276;
		var newHeight=(screen.availHeight<=992)?screen.availHeight-4:992;
	}
	else {newWidth=1024;newHeight=690;}
	//window.moveTo(3,2);
	//window.resizeTo(newWidth,newHeight);
	
}

// this is called one time from the map frame
function initMap(newMinx,newMiny,newMaxx,newMaxy) {
	document.onmousedown = function(){isMouseDown=true;status=isMouseDown;};

	window.onresize=conditionBrowser;
		maximizeBrowser();
		conditionBrowser();
		
	changeMapHolderSize();
		showLayer(mapImageId);
	//AJAX LOADING ICON (top right red box)
	//DWRUtil.useLoadingMessage();
	DWREngine._errorHandler =  errorHandler;
	//alert(fullMinx+"\n"+fullMiny+"\n"+ fullMaxx+"\n"+ fullMaxy);

	fullExtent();

	//fullExtent(); //moved to send 'default extent for wizard' from wizard page;
	
	showLayer(eventLayerId);
	showLayer("toolBox");
	showLayer("toolBoxTabs");
	showLayer("mapContainer");
	setState("zoomBox");
	fullMinx = defaultServiceExtents[0][0];
	fullMiny = defaultServiceExtents[0][1];
	fullMaxx = defaultServiceExtents[0][2];
	fullMaxy = defaultServiceExtents[0][3];
	
    fullWidth = Math.abs(fullMaxx - fullMinx);
    fullHeight = Math.abs(fullMaxy - fullMiny);

	setZoomLevel();
	
}

function adjustInfoBoxHeight(){
infoBoxHeight=getInfoBoxHeight();
getLayerRef("infoBox1").style.height=infoBoxHeight;
getLayerRef("infoBox2").style.height=infoBoxHeight;
getLayerRef("infoBox3").style.height=infoBoxHeight;
getLayerRef("infoBox4").style.height=infoBoxHeight;
}

// expand/collapse the image placeholder
function changeMapHolderSize() {
	adjustInfoBoxHeight();
	getLayerRef(positionLayerId).style.height = mapHeight;
	getLayerRef(positionLayerId).style.width = mapWidth;
	
	getLayerRef(mapImageName).style.height = mapHeight;
	getLayerRef(mapImageName).style.width = mapWidth;
	getLayerRef(mapImageName2).style.height = mapHeight;
	getLayerRef(mapImageName2).style.width = mapWidth;
	
	getLayerRef(mapImageId).style.height = mapHeight;
	getLayerRef(mapImageId).style.width = mapWidth;
	getLayerRef(mapImageId2).style.height = mapHeight;
	getLayerRef(mapImageId2).style.width = mapWidth;
	
   	clipLayer(mapImageId,0,0,mapWidth,mapHeight);
	//getLayerRef('theZoomBox').style.height = mapHeight;
	//getLayerRef('theZoomBox').style.width = mapWidth;
	getLayerRef(eventLayerId).style.height = mapHeight;
	getLayerRef(eventLayerId).style.width = mapWidth;
	getLayerRef('transLayer').style.height = mapHeight;
	getLayerRef('transLayer').style.width = mapWidth;
	
//getLayerRef('toolBox').style.width = mapWidth+mapPadding;
	//getLayerRef('toolBoxes').style.height=getInfoBoxHeight();
	//getLayerRef('thePrintMapPositionImg').style.height = mapHeight;
	//getLayerRef('thePrintMapPositionImg').style.width = mapWidth;
	
}
// set corresponding zoom level (used in both frames)
function setZoomLevel() {
	var newWidth = Math.abs(maxx - minx);
	var currentZoomPercent = Math.round(newWidth/fullWidth * 100 * 1000)/1000.0;
	var zoomLevel = "10ZOOM";
	for (i = 0; i < zoomLevels.length; i++) {
		if (currentZoomPercent <= zoomLevels[i]) {
			zoomLevel = (i+1)+"ZOOM";
			//alert(zoomLevel);
			
			break;
		}
		else zoomLevel = zoomLevels.length+"ZOOM";
	}
	whichZOOM = zoomLevel;
	swapGroup(zoomLevel, "ZOOM");
}

// refresh the map to the original extent 
function resetMap() {
	refreshMap(originalMinx,originalMiny,originalMaxx,originalMaxy);
}
/*moved to map.js
// set the state of the map tools
function setState(mode) {
		var group='TOOL';
		var id='zoominTOOL';
	if(!hasError){
		if (mode == "zoomBox") {
			setZoomBoxEvents();
			id='zoominTOOL';
		} else if (mode == "zoomIn") {
			setZoomInEvents();
			id='zoominTOOL';
		} else if (mode == "zoomOut") {
			setZoomOutEvents();
			id='zoomoutTOOL';
		} else if (mode == "pan") {
			setPanEvents();
			id='panTOOL';
		} else if (mode == "identify") {
			setCustomMapClickEvents();
			id='identifyTOOL';
		}
		swapGroup(id,group);
		whichTOOL = id;
	}
}
	*/
function checkEnter(event, formname) {
	var code = event.keyCode;
	if (code==13) {
		tmp = false;
		if (formname == 'mapform') 
			tmp = validateMapForm();
		if (formname == 'routeform') 
			tmp = validateDirectionsForm();
		if (tmp) 
			eval("document." + formname + ".submit();");
	}
}
/*
function refreshUrl(newMinx,newMiny,newMaxx,newMaxy) {
    showLayer("theLoadingImg");
	isBusy=true;
   		var mapUrl = "mapEngine.cfm?fa=makeMap"
	        + "&MINX="+roundVal(newMinx)+"&MINY="+roundVal(newMiny)+"&MAXX="+roundVal(newMaxx)+"&MAXY="+roundVal(newMaxy)
			//+ "&POINTX="+getLayerRef('pointX').value+"&POINTY="+getLayerRef('pointY').value
			//+ "&awsDS_getMap="+escape(getLayerRef('awsDS_getMap').value)
	        + "&WIDTH="+mapWidth+"&HEIGHT="+mapHeight+"&s=1/myMap.gif";
	//}
    return mapUrl;
}

function clearSettings() {
	
	isBusy=false;
	hideLayer("theZoomBox");
	// we had moved and clipped that with a pan action
	positionLayer(mapImageId,0,0,positionLayerId);
	clipLayer(mapImageId,0,0,mapWidth,mapHeight);

	hideLayer("theLoadingImg");
	//alert(document.getElementById("mapImage").src);
	//alert(document.images[51].src);

	document.MapToolForm.imgSrc.value=document.getElementById("mapImage").src;
}

*/


/////////////////////////////////////////////////////


var activeWizardBoxTab=1;
function changeWizardBox(tab){
	if (tab!=activeWizardBoxTab){
		document.getElementById("wizardBoxTab"+activeWizardBoxTab).src=document.getElementById("wizardBoxTab"+activeWizardBoxTab).src.replace('_1','_0');
		document.getElementById("wizardBox"+activeWizardBoxTab).style.display="none";
		document.getElementById("wizardBoxTab"+tab).src=document.getElementById("wizardBoxTab"+tab).src.replace('_0','_1');
		document.getElementById("wizardBox"+tab).style.display="block";
		//alert(document.getElementById("wizardBox"+tab).style.display);
		activeWizardBoxTab=tab;
		adjustInfoBoxHeight();
		}
		maximizeWizardBox();
}


var activeInfoBoxTab=1;
function changeInfoBox(tab){
	if (tab!=activeInfoBoxTab){
		document.getElementById("infoBoxTab"+activeInfoBoxTab).src=document.getElementById("infoBoxTab"+activeInfoBoxTab).src.replace('_1','_0');
		document.getElementById("infoBox"+activeInfoBoxTab).style.display="none";
		document.getElementById("infoBoxTab"+tab).src=document.getElementById("infoBoxTab"+tab).src.replace('_0','_1');
		document.getElementById("infoBox"+tab).style.display="block";
		activeInfoBoxTab=tab;
		}
}


var activeToolBoxTab=1;
function changeToolBox(tab){
	if (tab!=activeToolBoxTab){
		document.getElementById("toolBoxTab"+activeToolBoxTab).src=document.getElementById("toolBoxTab"+activeToolBoxTab).src.replace('_1','_0');
		document.getElementById("toolBox"+activeToolBoxTab).style.display="none";
		document.getElementById("toolBoxTab"+tab).src=document.getElementById("toolBoxTab"+tab).src.replace('_0','_1');
		document.getElementById("toolBox"+tab).style.display="block";
		maximizeToolBox();
		activeToolBoxTab=tab;
		}
	else{
		if(isToolBoxOpen)minimizeToolBox();
		else maximizeToolBox();
		}
}


function toggleToolBox(val){
if(isToolBoxOpen) minimizeToolBox();
else maximizeToolBox();
}

function minimizeToolBox(){
isToolBoxOpen=false;
document.getElementById("div_toggleToolBox").innerHTML="Show Toolbox";
hideLayer("toolBox");
conditionBrowser();
}
function maximizeToolBox(){
isToolBoxOpen=true;
document.getElementById("div_toggleToolBox").innerHTML="Hide Toolbox";
showLayer("toolBox");
conditionBrowser();
}

function toggleWizardBox(val){
if(isWizardBoxOpen) minimizeWizardBox();
else maximizeWizardBox();
}
function minimizeWizardBox(){
isWizardBoxOpen=false;
document.getElementById("div_toggleWizardBox").innerHTML="Show Wizard";
document.getElementById("wizardBox").style.display="none";
adjustInfoBoxHeight();
}
function maximizeWizardBox(){
isWizardBoxOpen=true;
document.getElementById("div_toggleWizardBox").innerHTML="Hide Wizard";
document.getElementById("wizardBox").style.display="block";
adjustInfoBoxHeight();
}

function changeMapOptions(){
minimizeToolBox();
minimizeWizardBox();
changeInfoBox(4);
}

function queryThisLayer(toolBoxID,layerID){
changeToolBox(toolBoxID);
document.getElementById("queryTitle").innerHTML=layerID;
}
function identifyThisLayer(toolBoxID,layerID){
changeToolBox(toolBoxID);
document.getElementById("identifyTitle").innerHTML=layerID;
}
function selectThisLayer(toolBoxID,layerID){
changeToolBox(toolBoxID);
document.getElementById("selectTitle").innerHTML=layerID;
}

function openReportToolBox(){
changeToolBox(4);
}

function toggleLayerGroup(item){
	myImage="groupWidget_"+item;
	myGroup="layerGroup_"+item;
	if(document.getElementById(myImage).src.indexOf("minus") > 0 ){
	document.getElementById(myImage).src="../images/widgets/plusBoxWhite.gif";
	document.getElementById(myGroup).style.display="none";
	}
	else{
	document.getElementById(myImage).src="../images/widgets/minusBoxWhite.gif";
	document.getElementById(myGroup).style.display="block";
	}
}