/**
 *	@author		Alvaro Redondo
 *	@version	07/08/2006
 */

function scrollViewportTopDown() {
	var i = document.documentElement.scrollTop;
	do {
		i++;
		document.documentElement.scrollTop = i;
	} while (document.documentElement.scrollTop == i);
	return true;
}

function makeLayerVisibleInViewport(id) {
	var layer = document.getElementById(id);
	if (!layer) {
		return false;
	}
	var top;
	if (findPosY) {
		top = findPosY(layer);
	} else if (Position && Position.cumulativeOffset) {
		top = parseInt(Position.cumulativeOffset(layer)[1]);
	}
	if (document.documentElement.scrollTop > top) {
		do {
			document.documentElement.scrollTop--;
		} while (document.documentElement.scrollTop > top);
	} else {
		while ((document.documentElement.scrollTop + document.documentElement.clientHeight) < (top + layer.offsetHeight)) {
			document.documentElement.scrollTop++;
		}
	}
	return true;
}

/**
 *	NOTA: Esta función sólo es válida si la desviación de la capa con respecto a su padre
 *	es despreciable.
 */
function centerLayerInViewport(layerId, scrollTop) {
	var element = document.getElementById(layerId);
	if (element) {
		if (!scrollTop) {
			scrollTop = document.documentElement.scrollTop;
		}
		element.style.left = parseInt((document.documentElement.clientWidth - element.offsetWidth) / 2) + 'px';
		element.style.top = parseInt((document.documentElement.clientHeight - element.offsetHeight) / 2)
				+ scrollTop + 'px';
	}
}

function getPageWidth() {
	var xScroll;
	if (!((window.innerHeight && window.scrollMaxY) || (document.body.scrollWidth > document.body.offsetWidth))) {
		xScroll = document.body.scrollWidth;
	} else {
		xScroll = document.body.offsetWidth;
	}

	var windowWidth;
	if (self.innerWidth) {
		windowWidth = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
	}

	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return pageWidth;
}

function getPageHeight() {
	var yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		yScroll = document.body.scrollHeight;
	} else {
		yScroll = document.body.offsetHeight;
	}

	var windowHeight;
	if (self.innerHeight) {
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowHeight = document.body.clientHeight;
	}

	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	return pageHeight;
}

