/*
 * Author      : Zeeshan Muhammad
 * Contact     : Web: www.nfwebsolutions.com - Email: enquiries@nfwebsolutions.com - Voicemail/fax: +044 (0)709 2300 838
 * Version     : 1.00
 * Created     : 06/08/2006
 * Last updated: 28/08/2006
 *
 * CONTENTS
 * -------------------
 * ==VSCROLL
 * ==METHODS
 * ==over
 *    ==item GETELEMENTPOSY
 *    ==item GETWINDOWHEIGHT
 *    ==item GETELEMENTHEIGHT
 * ==back
 * ==SETSIZES
 * ==WINDOW.ONRESIZE
 * ==WINDOW.ONLOAD
 *
 */

/* ==VSCROLL
--------------------------------------------------------------------- */
function vScroll() {

	var ul = document.getElementsByTagName('UL')[0];
	var firstContainer = document.createElement("DIV");
	var lastContainer = document.createElement("DIV");

	// IE6 has issues with obj.setAttribute('class', 'exampleClass')
	firstContainer.className = 'first';
	lastContainer.className = 'last';
	ul.parentNode.insertBefore(firstContainer, ul);
	ul.parentNode.appendChild(lastContainer, ul);

	var wrapper = document.getElementById('wrapper');
	var liHeight = getElementHeight(ul.firstChild.nextSibling);

	var remainingSpace;
	setSizes();
	var ulOriginalTop = ((getElementPosY(ul) - remainingSpace) + liHeight/2);
	var timer = window.setInterval(scrollList, 120);

	function scrollList() {
		var ulTop = getElementPosY(ul) - remainingSpace;
		if ((ulTop - 2) <= (ulOriginalTop - liHeight)) {
			ul.style.top = ulOriginalTop + 'px';
			ul.appendChild(ul.getElementsByTagName('LI')[0]);
		} else {
			ul.style.top = (ulTop - 2) + 'px';
		}

	// end scroll
	}


	/* ==METHODS - GETELEMENTPOSY
	--------------------------------------------------------------------- */
	function getElementPosY(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;

	// end getElementPosY
	}


	/* ==METHODS - GETWINDOWHEIGHT
	--------------------------------------------------------------------- */
	function getWindowHeight() {
		var windowHeight = 0;
		if (typeof(window.innerHeight) == 'number') {
			windowHeight = window.innerHeight;
		}
		else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
			else {
				if (document.body && document.body.clientHeight) {
					windowHeight = document.body.clientHeight;
				}
			}
		}

		return windowHeight;

	// end getWindowHeight
	}


	/* ==METHODS - GETELEMENTHEIGHT
	--------------------------------------------------------------------- */
	function getElementHeight(obj) {
		if (obj.offsetHeight) {
			return obj.offsetHeight;
		} else {
			return obj.clientHeight;
		}

	// end getElementHeight
	}


	/* ==WINDOW.ONRESIZE
	--------------------------------------------------------------------- */
	window.onresize = function() {
		setSizes();

	// end onresize
	}


	/* ==SETSIZES
	--------------------------------------------------------------------- */
	function setSizes() {

		// Safari and Konqueror have issues with remainingSpace
		if (navigator.appVersion.match(/Konqueror|Safari|KHTML/)) {
			remainingSpace = 0;
		} else {
			remainingSpace = Math.ceil((getWindowHeight() - wrapper.offsetHeight)/2);
			remainingSpace = (remainingSpace <= 0) ? 0 : remainingSpace;
		}

		wrapper.style.marginTop = remainingSpace + 'px';

	// end setSizes
	}

// END vScroll
};

// addEvent(window,'load',vScroll) would be surplus in this single-page site
window.onload = vScroll;