//
// DX0 Generic JavaScript
// by Jonathan Gillette
// $Id$
//

//
// Sniffing class ( class doesn't clutter the global namespace )
//
function browserIDClass () {
	var browser = navigator.userAgent;
	this.browser = browser;
	this.ie4 = browser.indexOf( "MSIE 4" ) + 1;
	this.ie5 = browser.indexOf( "MSIE 5" ) + 1;
	this.ie6 = browser.indexOf( "MSIE 6" ) + 1;
	this.op4 = browser.indexOf( "Opera 4" ) + 1;
	this.ns4 = browser.indexOf( "Mozilla/4" ) + 1;
	if ( this.op4 || this.ie4 || this.ie5 || this.ie6 ) this.ns4 = 0;
	this.ns6 = browser.indexOf( "Mozilla/5" ) + 1;

	//
	// Break into categories: non-DHTML/DHTML, IE4+/DOM
	//   -- when IE6, Opera, further Browsers come in, then we can
	//      just add them to the categories, or make new categories.
	//
	this.dhtml = 0;
	if ( this.ie4 || this.ie5 || this.ie6 || this.ns4 || this.ns6 || this.op4 )
		this.dhtml = 1;
	this.ie4up = ( this.ie4 || this.ie5 || this.ie6 ) ? 1 : 0;
	this.dom = ( this.op4 || this.ns6 ) ? 1 : 0;
	this.document = ( this.ns4 ) ? 'document.' : '';
	this.style = ( this.ie4up ) ? '.style' : '';
	this.child = ( this.ns4 ) ? '' : '.document';
	this.show = ( this.ns4 ) ? 'show' : 'visible';
	this.hide = ( this.ns4 ) ? 'hide' : 'hidden';
}

browserID = new browserIDClass();


function dx0browserWidth() {  
	return ( browserID.ie4up ? "document.body.clientWidth" : "window.innerWidth" );
}

function dx0browserHeight() {
	return ( browserID.ie4up ? "document.body.clientHeight" : "window.innerHeight" );
}

document.dxelements = [];

//
// baseObject Class
// - name : actual ID of the DIV this baseObject is to reference
// - parent : JavaScript baseObject which references this elements parent
//
function baseObject (name, parent)
{
	this.divId = name;
	this.nestDiv = '';
	this.linkObj = '';
	this.bounds = [];
	this.children = [];
	this.ps = '';
	this.mouseDownFlag = 0;
	if ( parent ) {
		this.parent = parent;
		this.parent.children[this.parent.children.length] = this;
	}

	if ( !browserID.dom ) {
		if ( browserID.ns4 ) { if ( this.parent ) this.nestDiv = this.parent.nestDiv + browserID.document + this.parent.divId + '.'; }
		this.prop = eval( this.nestDiv + browserID.document + this.divId );
		this.style = eval( this.nestDiv + browserID.document + this.divId + browserID.style );
	}
	else
	{	
		this.prop = document.getElementById(this.divId);
		this.style = this.prop.style;
	}
	this.divX = ( browserID.ns4 ? parseInt(this.prop.left) : this.prop.offsetLeft );
	this.divY = ( browserID.ns4 ? parseInt(this.prop.top) : this.prop.offsetTop );
	this.divWidth = this.getWidth();
	if ( browserID.dom ) {
		if (this.parent) {
			this.divX -= this.parent.divX;
			this.divY -= this.parent.divY;
		}
	}
	this.begX = this.divX;
	this.begY = this.divY;
	this.prop.obj = this;

	// scroll attribs
	this.scrollIter = 0;
	this.scrollRateNow = 0;
	this.scrollTop = 0;
	this.scrollLeft = 0;
	this.scrollHeight = 300;	// just a default..
	this.scrollWidth = this.getWidth();
	this.thumbMover = dx0nothing;
	this.hThumbDiv = null;
	this.vThumbDiv = null;
	this.timeout = null;
	document.dxelements[this.divId] = this;
}
		
baseObject.prototype.show = function () { this.style.visibility = browserID.show; }
baseObject.prototype.hide = function () { this.style.visibility = browserID.hide; }
baseObject.prototype.moveBy = function (xLoc, yLoc) { this.divX += xLoc; this.divY += yLoc; this.applyCoords(); }
baseObject.prototype.moveTo = function (xLoc, yLoc) {
	if (xLoc != null) this.divX = xLoc;
	if (yLoc != null) this.divY = yLoc;
	this.applyCoords();
}
baseObject.prototype.absMoveTo = function (xLoc, yLoc) {
	if (xLoc != null) this.divX = xLoc - this.parent.getLeft();
	if (yLoc != null) this.divY = yLoc - this.parent.getTop();
	this.applyCoords();
}
baseObject.prototype.applyCoords = function () { this.style.left = this.divX; this.style.top = this.divY;
	if (this.linkObj)	this.linkObj.moveTo(this.divX, this.divY);
}
baseObject.prototype.moveFixedTo = function (xLoc, yLoc) { this.begX = xLoc; this.begY = yLoc; this.moveTo(xLoc, yLoc); }

//
// My OLD PHP CODE to do change the operator for NS6
//
//<? if ($GLOBALS['DX0_NS4'] || $GLOBALS['DX0_IE4UP']) $oper = '+=';
//   else if ($GLOBALS['DX0_NS6']) $oper = '='; ?>
//

baseObject.prototype.getLeft = function ()
{
	tempX = this.divX;
	if (this.parent)
		tempX += this.parent.getLeft();
	return tempX;
}

baseObject.prototype.getTop = function ()
{
	tempY = this.divY;
	if (this.parent)
		tempY += this.parent.getTop();
	return tempY;
}

baseObject.prototype.getAbsLeft = function ()
{
	tempX = ( browserID.ns4 ? parseInt(this.prop.pageX) : this.prop.offsetLeft );
	tempX += this.getParentOffset( "Left" );
	return ( browserID.ns6 ) ? tempX + 8 : tempX;
}

baseObject.prototype.getAbsTop = function ()
{
	tempY = ( browserID.ns4 ? parseInt(this.prop.pageY) : this.prop.offsetTop );
	tempY += this.getParentOffset( "Top" );
	return ( browserID.ns6 ) ? tempY + 2 : tempY;
}

baseObject.prototype.getParentOffset = function ( axisName ) {
	xPos = 0;
	if ( browserID.ns4 ) {
		if ( this.parent )
			xPos = eval( "this.parent.get" + axisName + "()" );
	} else {
		tempEl = this.prop.offsetParent;
		while (tempEl != null) {
			xPos += eval( "tempEl.offset" + axisName );
			tempEl = tempEl.offsetParent;
		}
	}
    return xPos;
}

baseObject.prototype.getWidth = function () { return ( browserID.ns4 ? this.prop.document.width : this.prop.offsetWidth ); }
baseObject.prototype.getHeight = function () { return ( browserID.ns4 ? this.prop.document.height : this.prop.offsetHeight ) + 20; }
baseObject.prototype.resizeTo = function (w, h) {
	if ( browserID.ns4 ) {
		this.prop.resizeTo(w, h);
	} else { 
		this.style.width = w;
		this.style.height = h;
	}
}

baseObject.prototype.clip = function (x0, y0, x1, y1)
{
	if ( browserID.ns4 ) {
		this.style.clip.top = y0;
		this.style.clip.right = x1;
		this.style.clip.bottom = y1;
		this.style.clip.left = x0;
	} else {
		this.style.clip = 'rect(' + y0 + ' ' + x1 + ' ' + y1 + ' ' + x0 + ')';
	}
	if (this.linkObj) 	this.linkObj.clip(x0, y0, x1, y1);
}

baseObject.prototype.write = function (HTMLstr)
{
	if ( browserID.ns4 ) {
		this.prop.document.open();
		this.prop.document.write(HTMLstr);
		this.prop.document.close();
	} else if ( browserID.ie4up ) {
    	this.prop.innerHTML = HTMLstr;
	} else if ( browserID.dom ) { 
	    var eleRange = this.prop.ownerDocument.createRange();
		eleRange.selectNodeContents(this.prop);
		eleRange.deleteContents();
		var cFrag = eleRange.createContextualFragment(HTMLstr);
		this.prop.appendChild(cFrag);
	}
}

function dx0getMouseTarget( e ) {
	if ( browserID.ns4 ) {
		return e.target.obj;
	} else {
		return event.srcElement.obj;
	}
}

var dx0m_x = dx0m_y = 0;
var targetEle = null;
baseObject.prototype.setMouseListener = function (funct) {
	var mouseEvts = ['down', 'up'];
	this.mouseListener = eval(funct);
	for (i = 0; i < mouseEvts.length; i++) {
		funcDef = 'ms0 = function (e) { ';
		funcDef += 'if (this.obj) { targetEle = this.obj } ';
		funcDef += 'else { targetEle = dx0getMouseTarget(e); } ';
               	funcDef += 'if ( targetEle != null ) { targetEle.mouseListener(\"' + mouseEvts[i].toUpperCase() + '\"); } '
		funcDef += 'return false; }';
		eval(funcDef);
		if ( browserID.dom ) { 
			this.prop.addEventListener('mouse' + mouseEvts[i], ms0, false); 
		} else { 
			eval('this.prop.onmouse' + mouseEvts[i] + ' = ms0;'); 
		} 
	}
	if ( browserID.ns4 ) { 
		this.prop.captureEvents( Event.MOUSEOUT | Event.MOUSEOVER);
	} 
	if ( document.dx0mouseSetup == null )
		dx0setupDocumentMice();
}

function dx0setupDocumentMice() {
	/* -- possible onmouseup funct -- *
	document.onmouseup = function (e) {
		if ( targetEle != null )
			targetEle.mouseListener("UP");
		if ( browserID.ns4 )
			return window.routeEvent(e);
		else	return false;
	}
	*/
	document.onmousemove = function (e) {
		if ( browserID.ns4 || browserID.ns6 ) {
			dx0m_x = e.pageX;
			dx0m_y = e.pageY;
		} else {
			dx0m_x = event.clientX + document.body.scrollLeft;
			dx0m_y = event.clientY + document.body.scrollTop;
		}
		if ( targetEle != null && targetEle.mouseListener != null )
			targetEle.mouseListener("MOVE");
		if ( browserID.ns4 )
			return window.routeEvent(e);
		else	return false;
	}
	if ( browserID.ns4 )
		document.captureEvents( Event.MOUSEMOVE );
}

baseObject.prototype.setMouseSensor = function () {
	this.prop.onmouseover = function () { this.obj.mouseListener( "OVER" ); return false; }
}

dx0trickleEvent = function ( children ) {
	if ( children != null && children.length > 0 ) {
		for ( i in children ) {
			if ( children[i].setMouseListener != null )
				return true;
			else if ( dx0trickleEvent( children ) )
				return true;
		}
	}
	return false;
}

document.dx0timeObj = [];
stNo = 0;
baseObject.prototype.setTimeout = function (EVALstr, time) {
	this.clearTimeout();
	var trip = stNo++;
	document.dx0timeObj[trip] = this;
	this.st = setTimeout("baseObjectRunTimeout(" + trip + ", \"" + EVALstr + "\")", time);
}

baseObject.prototype.clearTimeout = function () {
	if (this.st) clearTimeout(this.st);
}

function baseObjectRunTimeout (toNum, method) {
	if (!document.dx0timeObj[toNum]) return;
	eval("document.dx0timeObj[toNum]." + method);
	delete(document.dx0timeObj[toNum]);
}


// vert ( 0 = horizontal, 1 = vertical )
baseObject.prototype.scroll = function (vert, sign)
{
  if (this.scrollingRateNow != sign) this.scrollingRateNow = sign; this.scrollIter = 0;
  if (sign == 0) {this.clearTimeout();}
  else {
    this.scrollIter++;
    if (this.scrollIter < this.threshold) Nsign = sign;
    else Nsign = sign * 2;
    newCoord = ((!vert) ? this.scrollLeft : this.scrollTop) + (Nsign * this.rate);
    dirWord = (!vert) ? "Width" : "Height";
    propWord = (!vert) ? "Left" : "Top";
    testVal = ((Nsign > 0) 
       ? "newCoord + this.scroll" + dirWord + " < this.get" + dirWord + "()"
       : "newCoord >= 0");
    if (!eval(testVal)) 
	newCoord = (Nsign > 0)
		? this.getHeight() - this.scrollHeight
		: 0;
    eval("this.scroll" + propWord + " = newCoord;");
    this.scrollClip();
    this.thumbMover(this.scrollLeft / this.getWidth(), this.scrollTop / ( this.getHeight() - this.scrollHeight ) );
    this.setTimeout("scroll(" + vert + ", " + sign + ")", 50);
  }
}

baseObject.prototype.scrollReset = function() {
  this.scrollLeft = 0;
  this.scrollTop = 0;
  this.scrollClip();
  this.thumbMover( 0, 0 );
}

baseObject.prototype.scrollClip = function() {
  this.clip( this.scrollLeft, this.scrollTop, this.scrollLeft + this.scrollWidth,
    this.scrollTop + this.scrollHeight );
  this.moveTo( this.begX - this.scrollLeft, this.begY - this.scrollTop );
}


baseObject.prototype.runPs = baseObjectRunPs;
function baseObjectRunPs() {
	if (!(r = this.ps)) return;
	this.ps = "";
	eval(r);
}

function dx0nothing () { ; }

//
// Relative element writer
//
function writeRelativeEle( name, content ) {
	if ( browserID.ns4 ) {
		document.write( "<ILAYER NAME=\"" + name + "\" VISIBILITY=\"HIDDEN\">\n" );
		if ( content )
			document.write( content );
		document.write( "</ILAYER>\n" );
	} else {
		document.write( "<DIV ID=\"" + name + "\">\n" );
		if ( content )
			document.write( content );
		document.write( "</DIV>\n" );
	}
}

function dumpAlert( obj ) {
	str = "";
	for ( i in obj )
		str += i + ": " + obj[i] + "\n";
	alert(str);
}
	
dx0basicLoadFlag = 1;
