/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Konstantin Jagello | http://javascript-array.com/ */

var TimeOut         = 300;
var currentLayer    = null;
var currentitem     = null;
var currentLayerNum = 0;
var noClose         = 0;
var closeTimer      = null;

function mopen(n) {
	var l = document.getElementById("menu"+n);
	var mm = document.getElementById("mmenu"+n);
	
	if(l) {
		mcancelclosetime();
		l.style.visibility = 'visible';
		if(currentLayer && (currentLayerNum != n)) {
			currentLayer.style.visibility = 'hidden';
		}
		currentLayer = l;
		currentitem = mm;
		currentLayerNum = n;			
	} else if(currentLayer) {
		currentLayer.style.visibility = 'hidden';
		currentLayerNum = 0;
		currentitem = null;
		currentLayer = null;
 	}
}

function mclosetime() {
	closeTimer = window.setTimeout(mclose, TimeOut);
}

function mcancelclosetime() {
	if(closeTimer) {
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}

function mclose() {
	if(currentLayer && noClose!=1)	 {
		currentLayer.style.visibility = 'hidden';
		currentLayerNum = 0;
		currentLayer = null;
		currentitem = null;
	} else {
		noClose = 0;
	}
	currentLayer = null;
	currentitem = null;
}

document.onclick = mclose; 

/*function setTopOver(subSrc) {
	var tempId = subSrc.substring(subSrc.lastIndexOf('/') + 1, subSrc.length);
	var tempId = tempId.substring(0, tempId.indexOf('_'));
	var topId = tempId.replace('Sub', '');
	var topObj = document.getElementById(topId);
	var tsrc = topObj.getAttribute('src');
	var ftype = tsrc.substring(tsrc.lastIndexOf('.'), tsrc.length);
	var tmainStr = tsrc.replace('_up', '');
	var tmainStr = tmainStr.replace('_selected', '');
	var toversrc = tmainStr.replace(ftype, '_over'+ftype);
	topObj.setAttribute('src', toversrc);
}
*/
function getTopLevelMenu(subSrc) {
	var tempId = subSrc.substring(subSrc.lastIndexOf('/') + 1, subSrc.length);
	var tempId = tempId.substring(0, tempId.indexOf('_'));
	var id =  tempId.replace('Sub', '');
	return document.getElementById(id);
}

function setTopOver(subSrc) {
	var topObj = getTopLevelMenu(subSrc);
	var tsrc = topObj.getAttribute('src');
	var ftype = tsrc.substring(tsrc.lastIndexOf('.'), tsrc.length);
	var tmainStr = tsrc.replace('_up', '');
	var tmainStr = tmainStr.replace('_selected', '');
	var toversrc = tmainStr.replace(ftype, '_over'+ftype);
	topObj.setAttribute('src', toversrc);
}

function setTopOut(subSrc, selected) {
	var topObj = getTopLevelMenu(subSrc);
	var tsrc = topObj.getAttribute('src');
	var ftype = tsrc.substring(tsrc.lastIndexOf('.'), tsrc.length);
	var toutsrc;
	if(selected.getAttribute('id') != topObj.getAttribute('id')) {
		toutsrc = tsrc.replace('_over'+ftype, '_up'+ftype);
	} else {
		toutsrc = tsrc.replace('_over'+ftype, '_selected'+ftype);
	}
	topObj.setAttribute('src', toutsrc);
}

function initRollovers() {
	if (!document.getElementById) return
	
	var aOverPreLoad = new Array();
	var aDownPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	var sSelectedMenu;

	for (var i = 0; i < aImages.length; i++) {
		var src = aImages[i].getAttribute('src');
		if(src.indexOf('_selected') > 0) {
			sSelectedMenu = aImages[i];
		}
	}
	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var mainStr = src.replace('_up', '');
			var mainStr = mainStr.replace('_selected', '');
			var oversrc = mainStr.replace(ftype, '_over'+ftype);
			var downsrc = mainStr.replace(ftype, '_selected'+ftype);

			aImages[i].setAttribute('oversrc', oversrc);
			aImages[i].setAttribute('downsrc', downsrc);
			
			aOverPreLoad[i] = new Image();
			aOverPreLoad[i].src = oversrc;
			
			aDownPreLoad[i] = new Image();
			aDownPreLoad[i].src = downsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('oversrc'));
				
				if(this.getAttribute('src').indexOf('Sub') > 0) {
					setTopOver(sTempSrc);
				}
			}	
/*			
			aImages[i].onmousedown = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('downsrc'));
			}	
*/			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_over'+ftype, ftype);
				this.setAttribute('src', sTempSrc);

				if(this.getAttribute('src').indexOf('Sub') > 0) {
					setTopOut(sTempSrc, sSelectedMenu);
				}
			}
		}
	}
}
