//******************************************************
//  DynEl class which defines methodology for 
//  cross-browser, cross-platform, DHTML
//  Borrowed from: "Javascript: The Definitive Guide" 3rd Ed.
//               Author: David Flanagan
//  Adapted to drop down menus by: Alan Diamond
//  UMDNJ - NJDS - Digital Media Services (http://www.umdnj.edu/dmsweb/
//******************************************************


//******************************************************
//  Initializes the Dynamic element variables, and writes the
//  style sheet to the <HEAD> portion
//******************************************************
function DynEl(window, id, body, left, top, width, height) {
	this.window = window;
	this.id = id;
	this.body = body;
	//Output CSS-P style sheet
	var d = window.document;
	d.writeln('<STYLE TYPE="text/css"> <!--');
	d.write('#' + id + ' {position:absolute;');
	d.writeln('visibility:hidden;');
	if (left) d.writeln('left:' + left + 'px;');
	if (top) d.writeln('top:' + top + 'px;');
	if (width) d.writeln('width:' + width + 'px;');
	if (height) d.writeln('height:' + height + 'px;');
	if (width & height) d.writeln('clip:rect(0px, ' + width + 'px, ' + height + 'px, 0px);');
	d.writeln('font-size: 12px; font-family: "arial", "helvetica", "sans-serif"; font-weight: regular; color: #000000; font-align: center;');
	d.writeln('}');
	d.writeln('--></STYLE>');
}

//Navigator methods:
if (navigator.appName.indexOf("Netscape") != -1) {

	//Output dynamic element to document
	//Must be called before any other method
	DynEl.prototype.output = function() {
		var d = this.window.document;	//shortcut
		//Output element into <DIV> tag
		d.writeln('<DIV ID="' + this.id + '" ALIGN="CENTER" CLASS="menus">');
		d.writeln(this.body);
		d.writeln("</DIV>");
		//Save reference to layer
		this.layer = d[this.id];
	}
	
	//Show, hide, and setBgColor functions
	DynEl.prototype.show = function() {this.layer.visibility = "show"; }
	DynEl.prototype.hide = function() {this.layer.visibility = "hide"; }
	DynEl.prototype.setBgColor = function(color) {this.layer.bgColor = color; }
	
	//Returns information about the element
	DynEl.prototype.getX = function() { return this.layer.left; }
	DynEl.prototype.getY = function() { return this.layer.top; }
	DynEl.prototype.getWidth = function() { return this.layer.width; }
	DynEl.prototype.getHeight = function() { return this.layer.height; }
	
	//Dynamically change contents of element
	DynEl.prototype.setBody = function() {
		for (var i = 0; i < arguments.length; i++)
			this.layer.document.writeln(arguments[i]);
		this.layer.document.close();
	}
	
	//Registers event handler for named event.
	//Passed eventname should be ex.(onmousedown, onkeypress)
	//Passes handler all information (handler should be function you write)
	DynEl.prototype.addEventHandler = function(eventname, handler) {
		this.layer.captureEvents(DynEl._eventmasks[eventname]);
		var dynel = this;
		this.layer[eventname] = function(event) {
			return handler(dynel, event.type, event.x, event.y,
							event.which, event.which,
							((event.modifiers & Event.SHIFT_MASK) != 0),
							((event.modifiers & Event.CTRL_MASK) != 0),
							((event.modifiers & Event.ALT_MASK) != 0));
		}
	}
	
	//Unregisters named event handler
	DynEl.prototype.removeEventHandler = function(eventname) {
		this.layer.releaseEvents(DynEl._eventmasks[eventname]);
		delete this.layer[eventname];
	}
	
	//Array of event names and masks
	DynEl._eventmasks = {
		onabort:Event.ABORT, onblur:Event.BLUR, onchange:Event.CHANGE,
		onclick:Event.CLICK, ondblclick:Event.DBLCLICK,
		ondragdrop:Event.DRAGDROP, onerror:Event.ERROR,
		onfocus:Event.FOCUS, onkeydown:Event.KEYDOWN,
		onkeypress:Event.KEYPRESS, onkeyup:Event.KEYUP, onload:Event.LOAD,
		onmousedown:Event.MOUSEDOWN, onmousemove:Event.MOUSEMOVE,
		onmouseout:Event.MOUSEOUT, onmouseover:Event.MOUSEOVER,
		onmouseup:Event.MOUSEUP, onmove:Event.MOVE, onreset:Event.RESET,
		onresize:Event.RESIZE, onselect:Event.SELECT, onsubmit:Event.SUBMIT,
		onunload:Event.UNLOAD
	};
	
} //Netscape methods

//Internet Explorer methods:
if (navigator.appName.indexOf("Microsoft") != -1) {

	//Output dynamic element to document
	//Must be called before any other method
	DynEl.prototype.output = function() {
		var d = this.window.document;
		//Out element with <DIV> tag
		d.writeln('<DIV ID="' + this.id + '" ALIGN="CENTER" CLASS="menus">');
		d.writeln(this.body);
		d.writeln("</DIV>");
		//Save references to <DIV> tag and STYLE
		this.element = d.all[this.id];
		this.style = this.element.style;
	}
	
	//Show, hide, and setBgColor functions
	DynEl.prototype.show = function() { this.style.visibility = "visible"; }
	DynEl.prototype.hide = function() { this.style.visibility = "hidden"; }
	DynEl.prototype.setBgColor = function(color) { this.style.backgroundColor = color; }

	//Returns information about the element
	DynEl.prototype.getX = function() { return this.style.left; }
	DynEl.prototype.getY = function() { return this.style.top; }
	DynEl.prototype.getWidth = function() { return this.style.width; }
	DynEl.prototype.getHeight = function() { return this.style.height; }
	
	//Change contents of element
	DynEl.prototype.setBody = function() {
		var body = "";
		for (i = 0; i < arguments.length; i++)
			body += arguments[i] + "\n";
		this.element.innerHTML = body;
	}
	
	//Registers event handler for named event.
	//Passed eventname should be ex.(onmousedown, onkeypress)
	//Passes handler all information (handler should be function you write)
	DynEl.prototype.addEventHandler = function(eventname, handler) {
		var dynel = this;
		this.element[eventname] = function() {
			var e = dynel.window.event;
			e.cancelBubble = true;
			return handler(dynel, e.type, e.x, e.y,
							e.button, e.keyCode,
							e.shiftKey, e.ctrlKey, e.altKey);
		}
	}
	
	//Remove an event handler
	DynEl.prototype.removeEventHandler = function(eventname) {
		delete this.element[eventname];
	}
	
} //IE methods

