var timeoutID;
var timeoutHideLeftMenu;
var timeouthatImage;

function goLocation() {
	var theArray = eval('window.' + getFront(this.id, '.'));
	var theHref = theArray[getEnd(this.id, '.')][1];
	if (theHref != '') {
		menuTopMouseOut();
		location.href = theHref;
	}
}

function menuTopMouseOver() {
	clearTimeout(timeoutID);
	if ((this.parentNode) && (this.parentNode.className == 'topMenu'))
		this.style.backgroundImage = 'url(Images/Menu17Over.jpg)';
	else
		this.style.backgroundImage = 'url(Images/Menu16Over.jpg)';
	var theChildDiv, theDiv, theID;
	var theChildDivUnderscore, theIDUnderscore;

	theChildDiv = getFront(this.id, '.') + '_' + getEnd(this.id, '.');
	theChildDivUnderscore = theChildDiv + '_';

	for (var i = 0; i < subMenuDivs.length; i++) {
		theID = subMenuDivs[i];
		theIDUnderscore = theID + '_';
		theDiv = getEl(theID);

		if (theChildDivUnderscore.indexOf(theIDUnderscore) == 0) {
			theDiv.style.visibility = 'visible';
		} else {
			theDiv.style.visibility = 'hidden';
		}
	}
}

function menuTopMouseOutDelay() {
	if ((this.parentNode) && (this.parentNode.className == 'topMenu'))
		this.style.backgroundImage = 'url(Images/Menu17.jpg)';
	else
		this.style.backgroundImage = 'url(Images/Menu16.jpg)';
	timeoutID = setTimeout(function() {menuTopMouseOut(); }, 300);
}

function menuTopMouseOut() {
	for (var i = 0; i < subMenuDivs.length; i++) {
		getEl(subMenuDivs[i]).style.visibility = 'hidden';
	}
}

function aBlurListener() {
	var el = ascendDOM(this, 'a');
	if (el == null) return;
	el.blur();
}

function changeColorBar() {
	var red = Math.floor(Math.random() * 255);
	var green = Math.floor(Math.random() * 255);
	var blue = Math.floor(Math.random() * 255);
	var rgbValue = 'rgb(' + red + ',' + green + ',' + blue + ')';
	colorBar.style.backgroundColor = rgbValue;
	getEl('today').style.color = rgbValue;
	getEl('christmas').style.color = rgbValue;
	linkSpot.innerHTML = '';
}

function menuLeftMouseOver() {
	clearTimeout(timeoutHideLeftMenu);
	clearTimeout(timeouthatImage);
	this.style.backgroundImage = 'url(Images/Menu16Over.jpg)';
	hatRight.src = 'Images/hatRightOver.png';
}

function menuLeftMouseOut() {
	this.style.backgroundImage = 'url(Images/Menu16.jpg)';
	timeoutHideLeftMenu = setTimeout(function() {hideLeftMenu(); }, 300);
	timeouthatImage = setTimeout(function() {sethatRightDefaultImage(); }, 300);
}

function hideLeftMenu() {
	a2.firstChild.style.visibility = 'hidden';
}

function sethatRightDefaultImage() {
	hatRight.src = 'Images/hatRight.png';
}

function hatRightMouseOver(e) {
	clearTimeout(timeoutHideLeftMenu);
	clearTimeout(timeouthatImage);
	var e = e || window.event;
	if (e.type == 'mouseover') {
		this.src = 'Images/hatRightOver.png';
		a2.firstChild.style.visibility = 'visible';
	}
	else if (e.type == 'mouseout') {
		this.src = 'Images/hatRight.png';
		timeoutHideLeftMenu = setTimeout(function() {hideLeftMenu(); }, 300);
	}
}

var isFading = false;
var fadeInterval;
var opacStep = isIE ? 6 : 4;
function fadeImageListener(e) {
	var e = e || window.event;
	if (e.type == 'mouseover') {
		if (!isFading) {
			isFading = true;
			fadeInterval = setInterval(fadeImages, 20);
		}
		if (this.state != 'full') {
			this.state = 'showing';
			if (this.fadeImage == 'off') {
				if (this.id == 'hatLeft') {
					this.src = this.getAttribute('imagebasename') + 'Over' + '.png';
				} else {
					var num = (Math.floor(Math.random() * parseInt(this.getAttribute('imagescount'))) + 1);
					num = num < 10 ? '0' + num : num;
					this.src = this.getAttribute('imagebasename') + num + '.png';
				}
				this.fadeImage = 'on';
			}
		}
	} else if (e.type == 'mousedown') {
		this.src = this.getAttribute('imagebasename') + 'Click.png';
		}
	else if (e.type == 'mouseout') {
		if (this.state != 'none')
			this.state = 'hiding';
	}
}

function fadeImages() {
	var isFadingHere = false;
	var allImages = document.getElementsByTagName('img');
	for (var i = 0; i < allImages.length; i++) {
		var theImage = allImages[i];
		if (theImage.className == 'fadeImage') {
			var state = theImage.state;
			if (state == 'full')
				isFadingHere = true;
			if ((state == 'showing') || (state == 'hiding')) {
				isFadingHere = true;
				if (state == 'showing')
					theImage.index += opacStep;
				else if (state == 'hiding')
					theImage.index -= opacStep;

				if (theImage.index > 100) {
					theImage.index = 100;
					theImage.state = 'full';
				} else if (theImage.index < 0) {
					theImage.index = 0;
					theImage.state = 'none';
					theImage.fadeImage = 'off';
				}

				if (theImage.filters) // IE
					theImage.filters.alpha.opacity = theImage.index;
//					theImage.style.filter = 'alpha(opacity=' + theImage.index + ')';
//					bismarck.style.filter = 'alpha(opacity=' + bisOpac + ')';
				else // W3C
					theImage.style.opacity = theImage.index/101;
			}
		}
	}
	isFading = isFadingHere;
	if (!isFading)
		clearTimeout(fadeInterval);
}

function timeouthatLeftFadeImage() { // Called from attribute in a tag
	setTimeout(function() {sethatLeftFadeImage(); }, 40);
}

function sethatLeftFadeImage() {
	getEl('hatLeft').src = 'Images/hatLeftOver.png';
}
