﻿// JavaScript utilities for ISS Flight


// This sets the height of the element with the given ID

function setElementHeightById(id, height) {
	var element = document.getElementById(id);
	element.height = height + 'px';
	setTimeout("handleOnresize();", 1000);
}

//setTimeout("setElementHeightById('controls', 30);", 5000);

// This sets the width of the element with the given ID
function setElementWidthById(id, width) {
	var element = document.getElementById(id);
	element.width = width + 'px';
	setTimeout("handleOnresize();", 1000);
}

// This returns the height of the element with the given ID
function getElementHeightById(id) {
	var element = document.getElementById(id);
	element = document.body;
//	alert("id " + id + ", element " + element + ", element.height " + element.height);
	return element.height;
}

// This returns the width of the element with the given ID
function getElementWidthById(id) {
	var element = document.getElementById(id);
	return element.width;
}

// This returns the width of the window.
function getWindowWidth() {
	if (document.body.offsetWidth) {
		return document.body.offsetWidth;
	} else if (innerWidth) {
		return innerWidth;
	} else {
		return null;
	}
}

// This returns the height of the window.
function getWindowHeight() {
	if (document.body.offsetHeight) {
		return document.body.offsetHeight;
	} else if (innerHeight) {
		return innerHeight;
	} else {
		return null;
	}
}

var resizeListeners = new Array();

// This adds the element with the given ID to the list of onresize listeners
function addOnresizeListener(id) {
	resizeListeners.push(id);
}

// This should be called by the onresize event; it distributes the event
// to all registered listeners.
function handleOnresize() {
	for (var x = 0; x < resizeListeners.length; x++) {
		var name = resizeListeners[x];
		var element = document.getElementById(name);
		if (element.onresize) {
			element.onresize();
		} else {
//			alert("element " + element 
//					+ ", element.onresize " + element.onresize);
		}
	}
}
