/*
 * All functions are Copyright Beetlecraft 2003 unless otherwise stated.
 * All rights reserved. 
 * v1.1
*/

var usra = "not detected yet";
var debugon = 0;
var javascript_disabled=0;
var gEvtId;


function oBrowser () {
	usra=navigator.userAgent.toLowerCase();
	this.is_ns = (usra.indexOf('netscape')>=0)?1:0;
	this.is_ns_safari = (usra.indexOf('safari')>=0)?1:0;
	this.is_ie = (usra.indexOf('msie')>=0)?1:0;
	this.is_opera = (usra.indexOf('opera')>=0)?1:0;
	/* IE on Mac dies */
	this.is_ie_mac = this.is_ie && (usra.indexOf('mac_powerpc')>=0?1:0);

/*
	var is_ie =  (document.all)?true:false;
	var is_ns =  (!document.all)?true:false;
	var is_dom=  (document.getElementById)?true:false;
*/

}

function oNetscape () {
	this.xpos = new Function("e","return e.pageX;");
	this.ypos = new Function("e","return e.pageY;");
}

function oMSExplorer () {
	this.xpos = new Function("e",
		"return window.event.x+document.body.scrollLeft;");
	this.ypos = new Function("e",
		"return window.event.y+document.body.scrollLeft;");
}

/* 
 * oBrowserEvt object
 * *: registers events and calls (currently one) event handlers per event
 *    type
*/
function debug_listtimer() {
	var s="; ";
	for (t in gTimers) {
		s += gTimers[t].id + "(" + gTimers[t].statex + ":" + gTimers[t].statey + ") ";
	}
	return s;
}
function debug_move(x,y) {
	try {
	document.getElementById("test").innerHTML='['+usra+'] '+"x:"+x+"y:"+y+debug_listtimer();
	} catch (er) {alert(er);}
}
function debug_down(x,y) {
	//document.getElementById("test").style.visibility='hidden';
	try {
	document.getElementById("test").innerHTML='['+usra+'] '+"x:"+x+"y:"+y+" down"+debug_listtimer();
	} catch (er) {}
}
function debug_up(x,y) {
	try {
	document.getElementById("test").style.visibility='visible';
	document.getElementById("test").innerHTML='['+usra+'] '+"x:"+x+"y:"+y+" up"+debug_listtimer();
	} catch (er) {}
}
function debug_enter(x,y) {
	try {
	document.getElementById("test").innerHTML='['+usra+'] '+"x:"+x+"y:"+y+" enter"+debug_listtimer();
	document.getElementById("test").style.color="blue";
	} catch (er) {}
}
function debug_leave(x,y) {
	try {
	document.getElementById("test").innerHTML='['+usra+'] '+"x:"+x+"y:"+y+" leave"+debug_listtimer();
	document.getElementById("test").style.color="black";
	} catch (er) {}
}

function oBrowserEvt() {
	this.x=-1;
	this.y=-1;
	this.browser=new oBrowser();
	this.browserInterface=null;

	if (this.browser.is_ns) {
		this.browserInterface=new oNetscape();	
	} else {
		this.browserInterface=new oMSExplorer();	
	}

	if (this.browser.is_ns_safari) {
		document.write("<span id='ignore'></span>");
		safari_redo("document.getElementById('ignore').innerHTML=''");
	}

	/* debug functions */
	if (debugon) {
		this.debug_cb=new Array();
		this.debug_cb['move' ]=debug_move;
		this.debug_cb['down' ]=debug_down;
		this.debug_cb['up'   ]=debug_up;
		this.debug_cb['enter']=debug_enter;
		this.debug_cb['leave']=debug_leave;
	} 
	
	/* proper event handlers -- doing noting */
	this.mouse_cb=new Array();
	this.mouse_cb['move' ]=new Function("x","y","return;");
	this.mouse_cb['down' ]=new Function("x","y","return;");
	this.mouse_cb['up'   ]=new Function("x","y","return;");
	this.mouse_cb['enter']=new Function("x","y","return;");
	this.mouse_cb['leave']=new Function("x","y","return;");

	/*event callback that update this class */
	if (debugon) {
	this._hasevent=new Function("e","type",
			"this.x=this.browserInterface.xpos(e);" +
			"this.y=this.browserInterface.ypos(e);" +
			"this.debug_cb[type](this.x,this.y);" +
			"this.mouse_cb[type](this.x,this.y);");
	} else {
	this._hasevent=new Function("e","type",
			"this.x=this.browserInterface.xpos(e);" +
			"this.y=this.browserInterface.ypos(e);" +
			"this.mouse_cb[type](this.x,this.y);");
	}

	/*universal event handler assigned to document */
	document.onmousemove = new Function("e",
		"gBrowserEvt._hasevent(e,'move')");
	document.onmouseup = new Function("e",
		"gBrowserEvt._hasevent(e,'up')");
	document.onmousedown = new Function("e",
		"gBrowserEvt._hasevent(e,'down')");
	document.onmouseover = new Function("e",
		"gBrowserEvt._hasevent(e,'enter')");
	document.onmouseout = new Function("e",
		"gBrowserEvt._hasevent(e,'leave')");

	this.getx = new Function("return this.x");
	this.gety = new Function("return this.y");

	return this;

}

/*
 * automatic event handler for registered objects
*/
var gIdEvt = new Array();

function idevt_debug_show(self) {
	//self.style.visibility = 'visible';
	self.innerHTML = "visible";
}

function idevtdebug_hide(obj) {
	//self.style.visibility = 'hidden';
	self.innerHTML = "hidden";
}

function idevt_m_over (id) {
	me=gIdEvt[id];
	if (!me) return;	
	if (me.idevtdebug_show) { me.idevtdebug_show(me); }
	try {
		for (what in me.overaction) { 
			if (what&&me.overaction[what]) {
				me.overaction[what](me,me.actor_object[what]); 
			}
		}
	} catch (er)  {
		//alert('Error idevt_m_over: ' + er);
	}
}

function idevt_m_out (id) {
	me=gIdEvt[id];
	if (!me) return;
	if (me.idevtdebug_hide) { me.idevtdebug_hide(me); }
	try {
		for (what in me.outaction) { 
			if (what&&me.outaction[what]) 
				me.outaction[what](me,me.actor_object[what]);
		}
	} catch (er)  {
		//alert('Error idevt_m_out: [' + what + '] '+ er);
	}
}

function idevt_m_down (id) {
	me=gIdEvt[id];
	if (!me) return;
	if (me.idevtdebug_hide) { me.idevtdebug_hide(me); }
	try {
		for (what in me.downaction) { 
		try {
			if (what&&me.downaction[what]) {
			window.status=id+":"+what;
				me.downaction[what](me,me.actor_object[what]);
			}
		} catch (er) {
		alert('Error idevt_m_down/2: [' + id + ']' + what + ':' + er);
		}
		}
	} catch (er)  {
		alert('Error idevt_m_down: [' + id + ']' + what + ':' + er);
	}
}

function idevt_m_up (id) {
	me=gIdEvt[id];
	if (!me) return;
	if (me.idevtdebug_hide) { me.idevtdebug_hide(me); }
	try {
		for (what in me.upaction) { 
			if (what&&me.upaction[what]) 
				me.upaction[what](me,me.actor_object[what]);
		}
	} catch (er)  {
		//alert('Error idevt_m_up: ' + er);
	}
}

function oEvtId (id) {
	gIdEvt[id]=this;
	this.id=id;
	try {
		this.obj=document.getElementById(id);
	} catch (er) { alert("Cannot access: "+id); return;}
	/* sometimes it doesn't get found */
	/* for those element, i.e. a refs, add id */
	try {	
	if (!this.obj) {
		var a = document.getElementsByTagName("A");
		for (i=1;i<a.length;i++ ) {
			if (a[i].name == id) {
				this.obj=i;
			}	
		}
	}
	} catch (er) {alert("Cannot access/2: "+id+":"+er);}

	this.offX=-20;
	this.offY=20;

	try {
	document.getElementById(id).onmouseover=
		new Function("idevt_m_over('"+id+"')");
	document.getElementById(id).onmouseout=
		new Function("idevt_m_out('"+id+"')");
	document.getElementById(id).onmousedown=
		new Function("idevt_m_down('"+id+"')");
	document.getElementById(id).onmouseup=
		new Function("idevt_m_up('"+id+"')");
	} catch (er) {alert("oEvtId: "+er);}

	/* default events */
	this.overaction=new Array();
	this.outaction=new Array();
	this.downaction=new Array();
	this.upaction=new Array();
}

function _actionregister(when,what) {
	if (!gIdEvt[when])  { 
		gIdEvt[when] = new oEvtId(when);
		try {
			gIdEvt[when].sensor_object=
				document.getElementById(when);
		} catch (er) { alert("Cannot access: "+when);}
		gIdEvt[when].actor_object = new Array();
	} 
	var event = gIdEvt[when];

	event.overaction[event.overaction.length] = what.overaction;
	event.outaction[event.outaction.length] = what.outaction;
	event.downaction[event.downaction.length] = what.downaction;
	event.upaction[event.upaction.length] = what.upaction;
	event.actor_object[event.actor_object.length] = what;

}


function _mTimerForLoop(id) {
	var ok=0;
	me = gTimers[id];
	me.isrunning=1;
	me.exec();
	if (me.forward && me.statex+me.stepx<=me.maxx) {
		me.statex+=me.stepx;
		ok++;
	} 
	if (me.forward && me.statey+me.stepy<=me.maxy) {
		me.statey+=me.stepy;
		ok++;
	} 
	if (me.forward==0 && me.statex-me.stepx>=me.minx) {
		me.statex-=me.stepx;
		ok++;
	}
	if (me.forward==0 && me.statey-me.stepy>=me.miny) {
		me.statey-=me.stepy;
		ok++;
	}
	if (ok) {
		me.timer=setTimeout("_mTimerForLoop('"+id+"')",me.delay);
	} else {
		if ((me.forward||me.downward) && me.doreturn) {
			me.forward=0;
			me.downward=0;
			me.timer=setTimeout(
				"_mTimerForLoop('"+id+"')",me.delay);
		} else {
			if (me.forward) {
				me.statex=me.maxx;
				me.statey=me.maxy;
			} else {
				me.statex=me.minx;
				me.statey=me.miny;
			}
			me.isrunning=0;
		} 
	} 
}

function oTimer () {
	this.id = gTimers.length;
	if (gTimers[this.id]) {
		return gTimers[this.id];
	} 
	gTimers[this.id]=this;
	this.minx=0;
	this.maxx=0;
	this.miny=0;
	this.maxy=0;
	this.stepx=0;
	this.stepy=0;
	this.statex=0;
	this.statey=0;
	this.delay=100;
	this.isrunning=0;
	this.doreturn=0;
	this.forward=0;
	this.downward=0;
	this.start=new Function(
		"try { " +
			"if (this.isrunning) {return this;}"+
			"_mTimerForLoop('"+this.id+"');" +
		"} catch (er) {alert('oTimer(): '+er);};"
	);
	this.exec = new Function("return;");
	return this;
}

function oAction () {
	this.id = null;
	this.overaction = new Function("return;");
	this.outaction = new Function("return;");
	this.downaction = new Function("return;");
	this.upaction = new Function("return;");
	return this;
}

function tooltip(when,text) {
	var what=new oAction();
	document.write("<span id='tooltip_"+
		when+"' class=popup>"+text+"</span>");
	try {
		what.tooltip_popup=document.getElementById("tooltip_"+when);
	} catch (er) {alert("Cannot access: tooltip_" + when);}
	what.id="tooltip_"+when;
	what.overaction=new Function("me","what",
                "me.tooltip_popup.style.visibility='visible';"+
                "me.tooltip_popup.style.top=gBrowserEvt.getY()+me.offY;" +
                "me.tooltip_popup.style.left=gBrowserEvt.getX()+me.offX;" );
        what.outaction=new Function("me","what",
                "me.tooltip_popup.style.visibility='hidden';")
	_actionregister(when,what);
}

function slidedampen(left,top,right,bottom,steps,me) {
	var timer=new oTimer();
	timer.minx=left;
	timer.maxx=right;
	timer.miny=top;
	timer.maxy=bottom;
	timer.stepx=(right-left)/steps;
	timer.stepy=(bottom-top)/steps;
	if (timer.stepx<1) { timer.stepx=1;}
	if (timer.stepy<1) { timer.stepy=1;}
	timer.exec=new Function("return;");
	timer.obj=me;
	//alert('new: '+timer.min+'<'+timer.max+':'+timer.step);
	return timer;
}

function anim_slide_timed(when,whatid) {
	if (javascript_disabled) {return;}
	var what=new oAction();
	try {
	what.slide_object=document.getElementById(whatid);
	if (!what.slide_object) {
		alert("Cannot find " + whatid );
		return;
	}
	} catch (er) {alert("Cannot access: " + whatid);}
	try {
	what.origX=what.slide_object.style.left;
	what.origY=what.slide_object.style.top;
	} catch (er) {alert("Problem accessing style for "+what.slide_object);}
	what.offX=10;
	what.offY=5;
	what.id=whatid;
//	what.targetname=whatid;
	what.timer=slidedampen(0,0,what.offX,what.offY,50,what.slide_object);
	what.timer.exec=new Function( 
		"me.obj.style.left=me.statex;me.obj.style.top=me.statey;")
	what.overaction=new Function( "me","what", 
		"try {"+
			"what.timer.forward=1;"+
			"what.timer.downward=1;"+
			"what.timer.doreturn=0;"+
			"what.timer.start();" +
		"} catch (er) {alert('anim_slide_timed(): '+er);}" );
	what.outaction=new Function( "me","what", 
		"try {"+
			"what.timer.doreturn=1;what.timer.start();" +
		"} catch (er) {alert('anim_slide_timed(): '+er);}");
	_actionregister(when,what);
}

function anim_slide_timed_reset(when,whatid) {
	if (javascript_disabled) {return;}
	var me=gEvtId[when];
	if (!me) {
		alert('event not found');
		return;
	}
	for ( var what in me.actor_object ) {
		var w=me.actor_object[what];
		if (w.targetname&&w.targetname.indexOf(whatid)>=0) {
			x=1;
			w.timer.isrunning=0;
			w.timer.statex=0;
			w.timer.statey=0;
			w.timer.exec();
		}
	}
	if (!x) { alert('slide_timerd_reset:target '+when+':'+whatid+':'+'not found');}
}


function anim_slide(when,whatid) {
	if (javascript_disabled) {return;}
	var what=new oAction();
	try {
	what.slide_object=document.getElementById(whatid);
	} catch (er) {alert("Cannot access: "+whatid);}
	what.origX=what.slide_object.style.left;
	what.origY=what.slide_object.style.top;
	what.offX=10;
	what.offY=0;
	what.id=whatid;
	what.overaction=new Function("me","what",
		"what.slide_object.style.left=what.origX+what.offX;" +
		"what.slide_object.style.top=what.origY+what.offY;"
	);
	what.outaction=new Function("me","what",
		"what.slide_object.style.left=0px;" +
		"what.slide_object.style.top=0px" 
	);
	_actionregister(when,what);
}

function anim_display(when,where,text) {
	if (javascript_disabled) {return;}
	var what=new oAction();
	what.id = where;
	try {
	what.show_object = document.getElementById(where);
	} catch (er) {alert("Cannot access: "+where);}
	what.downaction=new Function("me","what",
		"what.show_object.innerHTML=\'"+text+"\';");
	_actionregister(when,what);
}

function anim_pressdown(when,where) {
	if (javascript_disabled) {return;}
	var what=new oAction();
	try {
	what.slide_object=document.getElementById(where);
	} catch (er) {alert("Cannot access: "+where);}
	document.write("<table id='shadow_"+
		when+"' class='shadow'><tr><td></td></tr></table>");
	try {
	what.shadow_popunder=document.getElementById("shadow_"+when);
	} catch (er) {alert("Cannot access: shadow_"+when);}
	what.slide_object.style.zIndex+=100;
	what.shadow_popunder.style.zIndex=what.slide_object.style.zIndex-50;
	what.id2="shadow_"+when;
	what.origX=what.slide_object.style.left;
	what.origY=what.slide_object.style.top;
	what.offX=30;
	what.offY=30;
	what.id=where;
	what.timer=slidedampen( 0,0,what.offX,what.offY,50,what.slide_object);
	what.timer.exec=new Function( 
		"me.obj.style.left=me.statex;me.obj.style.top=me.statey;"+
		"me.shadow_popunder.style.left=-me.statex;"+
		"me.shadow_popunder.style.top=-me.statey");
	what.overaction=new Function( "me","what", 
		"try {"+
			"what.timer.forward=1;"+
			"what.timer.doreturn=0;"+
			"what.timer.start();" +
		"} catch (er) {alert('anim_slide_timed(): '+er);}" );
	what.outaction=new Function( "me","what", 
		"try {"+
			"what.timer.doreturn=1;what.timer.start();" +
		"} catch (er) {alert('anim_slide_timed(): '+er);}");
	_actionregister(when,what);
}

function anim_downshow(when,where,replace) {
	if (javascript_disabled) {return;}
	var what=new oAction();
	try {
	what.target=document.getElementById(where);
	} catch (er) {alert("Cannot access: "+where);}
	what.targetname=where;
	try {
		what.replace=document.getElementById(replace);
		what.replacetext=what.replace.innerHTML;
	} catch (er) {alert(er + " in downshow: Cannot access/1: "+replace);return;}
	//Safari doesn't break this right
	var x=what.replacetext.toLowerCase().indexOf('span');
	if (x>=0) {
		what.replacetext=what.replacetext.substring(0,x-1);
	}
	what.replace.style.visibility="hidden";
	what.id='replace';
//	alert('inserting downshow for '+when);
	what.downaction=new Function("me","what",
		"what.target.innerHTML='';"+
		"what.target.innerHTML=what.replacetext;"
	);
	_actionregister(when,what);
}

function anim_forceshow(when,where,replace) {
	/* IE on Mac may crash around here with Flash enabled*/
	//if (javascript_disabled||gBrowserEvt.browser.is_ie_mac) {return;}
	if (!gIdEvt[when])  { 
		alert('event not found');
		return;
	}
	var me=gIdEvt[when];
	var x=0;
	for ( var what in me.actor_object ) {
		var w=me.actor_object[what];
		if (w.targetname&&w.targetname.indexOf(where)>=0) {
			x=1;
			me.downaction[what](me,w);
		}
	}
	if (!x) { alert('anim_forceshow: target '+when+':'+where+':'+'not found');}
}

function anim_redraw(what) {
	if (javascript_disabled) {return;}
	try {
	var w=document.getElementById(what);
	} catch (er) {alert("Cannot access: "+what);}
	w.style.top='0px';
	w.style.left='0px';
	w.style.display='none';
	try {
	setTimeout("document.getElementById('"+
		what+"').style.display='block'",1);
	} catch (er) {alert("anim_redraw: "+er);}
}

var gTimers = new Array();
var gBrowserEvt = new oBrowserEvt();
