var bGenRunOnce = false;
var sFis = "012";
var sIds = "";

initGen = function() {

	bGenRunOnce = true;

	var thisPage = getPage();
	var m = ob("menu_" + thisPage)
	if (m) m.style.background="url(img/menuhere.gif)"

	setTimeout("doFades()",6000)
	
	var c = ob("menu_customer");
	addEvent(c,"click",customerLogin);
	
}

function customerLogin(e) {
	
	if (document.all) {
		alert("There is a known bug in some versions of MS Internet Explorer\r\n" +
		 	"that incorrectly shows FTP sites as unavailable. If, after clicking\r\n" +
			"OK below, your browser reports that the page cannot be found,\r\n" +
			"press F5 or select Refresh from the View menu to login.");
	}
	window.location.href="ftp://appliedintelligence.dyndns.org/"
	return cancelThisEvent(e);
}

function doFades() {

	if (sFis.length==10) sFis=sFis.substring(8,10);
	var n = Math.round(9*Math.random());
	while (sFis.indexOf(n)>-1) {
		n = Math.round(9*Math.random());
	}
	sFis += "" + n;

	if (sIds.length==3) sIds=sIds.substring(2,3);
	var d = Math.round(2*Math.random());
	while (sIds.indexOf(d)>-1) {
		d = Math.round(2*Math.random());
	}
	sIds += "" + d;

	crossFade("mh"+d,"img/mh"+n+".jpg",0,100);

	setTimeout("doFades()",8000);

}
	
function getPage() {
	
	var p = window.location.href;
	if (p.substring(p.length-1)=="/") p+="index.htm";
	var pA = p.split("/")
	p = pA[pA.length-1]
	pA = p.split(".")
	return pA[0]
}


function preload(){
	var objImage = new Image();
	
	var aImg=document.getElementsByTagName("img");

	for (var i=0;i<aImg.length;i++) {
		objImage.src = aImg[i].src;
	}
}


function crossFade(sId,sNewSrc,nC,nOpac) {

	var obj = ob(sId);

	if (nC==0) {
		if (document.all) {obj.style.filter = "alpha()"};
		crossFade(sId,sNewSrc,-10,100)
	} else {		
		nOpac += nC;
		if ((nOpac>-1&&nC<0) || (nOpac<101&&nC>0)) {
			if (document.all) {
				obj.filters.alpha.opacity=nOpac;
			} else {
				obj.style.MozOpacity=nOpac/100
			}
			setTimeout("crossFade('" + sId + "','" + sNewSrc + "'," + nC + "," + nOpac + ")",1); 	
		} else if (nC<0) {
			obj.src = sNewSrc;
			crossFade(sId,sNewSrc,10,nOpac);
		}
	}
}

addEvent = function( obj, type, fn ) {

	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		eventCache.add(obj, type, fn);
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		eventCache.add(obj, type, fn);
	} else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

eventCache = function(){	
	var listEvents = [];	
	return {		
		listEvents : listEvents,		
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);	
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};				
				item[0][item[1]] = null;
			};
		}
	};
}();


function cancelThisEvent(e) {

	if (typeof(e)=="undefined" && typeof(window.event) != "undefined") e = window.event;

	if (!e) return false;

	if (typeof( window.event ) != "undefined" ) {
		e.returnValue=false;
		e.cancelBubble=true;
	} 
	if (e.stopPropagation) e.stopPropagation();
	if (e.preventDefault) e.preventDefault();
	
	return false;
}


function getParams() {
	var idx = window.location.href.indexOf('?');
	var params = new Array();
	if (idx != -1) {
		var pairs = window.location.href.substring(idx+1, window.location.href.length).split('&');
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
		}
	}
	return params;
}



function ob(sID) {return document.getElementById(sID);}

window.addEvent(window,'load',function(){if(!bGenRunOnce)initGen();});
window.addEvent(window,'unload',eventCache.flush);
