//// ------------------------------------------------------------------
////   ENVISION SYSTEMS DHTML LIBRARY Written by William Jamieson
//// ------------------------------------------------------------------
//***************************************************************
// REQUIRES dhtmlMicroAPIv1.00.js
//***************************************************************

// Change Object Parameters
function changeObjectClass(objectID, newClassName){
	returnDOMObject(objectID).className = newClassName;
}

function changeObjectStyle(objectID, newStyleSpecifications){
	returnDOMObject(objectID).style = newStyleSpecifications;
}

function getObjectContents(objectID){
	return returnDOMObject(objectID).innerHTML;
}
function changeObjectContents(objectID, newContents){
	returnDOMObject(objectID).innerHTML = newContents;
}

function changeObjectBackgroundColor(objectID, newBackgroundColor){
	returnDOMObjectStyle(objectID).backgroundColor=newBackgroundColor;
}

// Visibility
function toggleObjectDisplay(objectID){
	// onClick="toggleObjectDisplay('SubMenu1');"
    if (DOMID || DOMAll){
        myDOMStyle = returnDOMObjectStyle(objectID);
        if(myDOMStyle.display == 'block'){
        	myDOMStyle.display = 'none';
        }else{
        	myDOMStyle.display = 'block';
        }
	}
    return;
 }	
function getObjectDisplay(objectID){
	// onClick="toggleObjectDisplay('SubMenu1');"
    if (DOMID || DOMAll){
        myDOMStyle = returnDOMObjectStyle(objectID);
        if(myDOMStyle.display == 'block'){
        	return 'block';
        }else{
        	return 'none';
        }
	}
 }	

function setObjectVisibility(objectID, newVisibilityState){
	// Options for newVisibilityState:
	// 1. 'hidden'
	// 2. 'visible'
    returnDOMObjectStyle(objectID).visibility = newVisibilityState;
}

function setObjectDisplayState(objectID, newDisplayState){
	// Options for newDisplayState:
	// 1. 'none'
	// 2. 'block'
	// 3. 'inline'
    returnDOMObjectStyle(objectID).display = newDisplayState;
}

function toggleObjectVisibility(objectID) {
    var myDOMStyle = returnDOMObjectStyle(objectID);
    currentVisibilityState = myDOMStyle.visibility;
	if(currentVisibilityState == 'hidden' || currentVisibilityState == 'hide'){
		myDOMStyle.visibility = 'visible';
	}else{
		if(currentVisibilityState == 'visible' || currentVisibilityState=='show'){
			myDOMStyle.visibility = 'hidden'; 
		}else{
			myDOMStyle.visibility = 'visible';
		}
	}
}

// 2D Positioning (Horizontal/Vertical Positioning)
function setObjectPosition (objectID, newX, newY){
	// Moves an object to an absolute position
	// onClick="setObjectPosition('myobject',250, 250);"
    setObjectXPosition (objectID, newX);
    setObjectYPosition (objectID, newY);
}
function setObjectXPosition (objectID, newX){
	// Moves an object to an absolute X position
	// onClick="setObjectXPosition('myobject',250);"
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle.left = newX + "px";
}
function setObjectYPosition (objectID, newY){
	// Moves an object to an absolute Y position
	// onClick="setObjectYPosition('myobject',250);"
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle.top = newY + "px";
}

function moveObject (objectID, offsetX, offsetY){
	// Moves an object to a position that is relative to its current position
	// onClick="MoveObject ('myobject',50, -50);"
    var myDOM = returnDOMObject(objectID);
    var myDOMStyle = returnDOMObjectStyle(objectID);
    if (typeof(myDOMStyle.pixelLeft)=='number') {
        myDOMStyle.pixelLeft += offsetX;
        myDOMStyle.pixelTop += offsetY;
	}else{
        if (myDOM.offsetLeft != null) {
			var currentLeft = myDOM.offsetLeft;
            var currentTop = myDOM.offsetTop;
            myDOMStyle.left = currentLeft + offsetX;
            myDOMStyle.top = currentTop + offsetY;
		}else{
			myDOM.moveBy(offsetX,offsetY);
		}
	}
}

//3D Positioning (Z Positioning)
var lastObjectID = null;
var lastObjectZIndex = 0;

function setZIndexOfObject(objectID, newLayer){
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle.zIndex = newLayer;
  }

function getZIndexOfObject(objectID){
    var myDOMStyle = returnDOMObjectStyle(objectID);
    if (myDOMStyle.zIndex != null){
		return myDOMStyle.zIndex;
	}
    return (null);
  }

function bringObjectToFront(objectID){
	// onClick="bringObjectToFront('myobject1');"
    if (lastObjectID != null){
		setZIndexOfObject(lastObjectID, lastObjectZIndex);
     }
    lastObjectID = objectID;
    lastObjectZIndex = getZIndexOfObject(objectID);
    setZIndexOfObject(objectID,999);
  }


// Object Scrolling
function scrollPageTo(scrollPageToX, scrollPageToY){
	// Scrolls to an absolute position
	if (browserIsIE){
		getBodyObject().scrollLeft = scrollPageToX;
		getBodyObject().scrollTop = scrollPageToY;
		return;
	}else{
		scrollTo(scrollPageToX, scrollPageToY);
		return;
	}
}

function scrollPageBy(scrollPageByX, scrollPageByY){
	// Scrolls to a position that is relative to its current position
	var currentXPosition = getHorizontalScrollPosition();
	var currentYPosition = getVerticalScrollPosition();
	var newXPosition = currentXPosition + scrollPageByX;
	var newYPosition = currentYPosition + scrollPageByY;
	if (browserIsIE){
		getBodyObject().scrollLeft = newXPosition;
		getBodyObject().scrollTop = newYPosition;
		return;
	}else{
		scrollTo(newXPosition, newYPosition);
		return;
	}
}

function getHorizontalScrollPosition(){
    if (getBodyObject().scrollLeft!=null){
		return getBodyObject().scrollLeft;
    }
    if (window.pageXOffset!=null){
		return window.pageXOffset;
	}
    return 0;
  }
function getVerticalScrollPosition(){
    if (getBodyObject().scrollTop!=null){
		return getBodyObject().scrollTop;
    }
    if (window.pageYOffset!=null){
		return window.pageYOffset;
	}
    return 0;
  }

function currentScrollPosition(){
	var currentXPosition = getHorizontalScrollPosition();
	var currentYPosition = getVerticalScrollPosition();
	var currentScrollPosition = new Array(currentXPosition,currentYPosition);
	return currentScrollPosition;
}

// Adjusting the stylesheet
function addCSSRule(styleSheetObjectID,newSelectorName,newSelectorDefinition){
	// onClick="addCSSRule('body','background-color : #FF9999;');"
	if (browserIsIE){
		var theStyleSheet = eval('document.styleSheets.' + styleSheetObjectID);
		theStyleSheet.addRule(newSelectorName,newSelectorDefinition);
	}        
}

function changeClassDefinition(objectID,nameOfStyle,newStyleDefinition){
	// onClick="changeClassDefinition('myobject1','fontSize','24px');"
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle [nameOfStyle] = newStyleDefinition;
  }


// Get Object Dimensions
function getObjectLeft(objectID){
    var myDOM = returnDOMObject(objectID);
	var objectLeft = 0;
	if (myDOM.offsetParent){
		while (myDOM.offsetParent){
			objectLeft += myDOM.offsetLeft;
			myDOM = myDOM.offsetParent;
		}
	}else if (myDOM.x){
		objectLeft += myDOM.x;
	}
	return objectLeft;
}
function getObjectRight(objectID){
    return getObjectLeft(objectID) + getObjectWidth(objectID);
}
function getObjectTop(objectID){
	myDOM=returnDOMObject(objectID);
	var objectTop = 0;
	if (myDOM.offsetParent){
		while (myDOM.offsetParent){
			objectTop += myDOM.offsetTop;
			myDOM = myDOM.offsetParent;
		}
	}else if (myDOM.y){
		objectTop += myDOM.y;
	}
	return objectTop;
}
function getObjectBottom(objectID){
    return getObjectTop(objectID) + getObjectHeight(objectID);
}

function getObjectWidth(objectID){
    var myDOM = returnDOMObject(objectID);
    if (typeof(myDOM.offsetWidth)=='number') return myDOM.offsetWidth;
    else if (myDOM.clip && typeof(myDOM.clip.width)=='number') return myDOM.clip.width;
    return (null);
}
function getObjectHeight(objectID){
    var myDOM = returnDOMObject(objectID);
    if (typeof(myDOM.offsetHeight)=='number') return myDOM.offsetHeight;
    else if (myDOM.clip && typeof(myDOM.clip.height)=='number') return myDOM.clip.height;
    return (null);
}
function setObjectSize(objectID, newWidth, newHeight){
	// Resize an object to a specified Width and Height
	// onClick="setObjectSize('myobject',250, 250);"
    setObjectWidth (objectID, newWidth);
    setObjectHeight (objectID, newHeight);
}
function setObjectWidth(objectID, newWidth){
	// Resize an object to a specified Width
	// onClick="setObjectWidth('myobject',250);"
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle.width = newWidth + "px";
}
function setObjectHeight(objectID, newHeight){
	// Resize an object to a specified Height
	// onClick="setObjectHeight('myobject',250);"
    var myDOMStyle = returnDOMObjectStyle(objectID);
    myDOMStyle.height = newHeight + "px";
}



function showDimensions(objectID){
    var currentWidth = getObjectWidth(objectID);
    var currentHeight = getObjectHeight(objectID);
    var currentLeft = getObjectLeft(objectID);
    var currentRight = getObjectRight(objectID);
    var currentTop = getObjectTop(objectID);
    var currentBottom = getObjectBottom(objectID);
    alert('Width = ' + currentWidth + 
    	  '\nHeight = ' + currentHeight + 
		  '\nLeft = ' + currentLeft + 
		  '\nRight = ' + currentRight + 
		  '\nTop = ' + currentTop + 
		  '\nBottom = ' + currentBottom
	);
}

function getBodyObject(){
	if(document.compatMode && document.compatMode=="CSS1Compat"){
		return document.documentElement;
	}else{
		return document.body;
	}
}

function getCoordinatesRelativeToDocument(e){
	if(!e){
		e = window.event;
	}
	// GET THE CURRENT POSTION OF THE CURSOR RELATIVE TO DOCUMENT (NOT WINDOW)
	// NOTE: Netscape 6 and Explorer define clientX/Y as the mouse coordinates relative to the 
	//       window.  Opera, Konqueror and iCab define clientX/Y as the mouse coordinates relative 
	//       to the document, not to the window.
	if(!e || (typeof(e.pageX)!='number' && typeof(e.clientX )!='number')){
		cursorPositionX = 0;
		cursorPositionY = 0;
	}else if(typeof(e.pageX)=='number' || typeof(e.pageY)=='number'){
		// NOTE: Netscape uses pageX/Y to get the cursors relative position to the document and
		//       clientX/Y to  get the cursors relative position to the window.  This conditional
		//       gives us the correct mouse coordinates for Netscape 4 and 6 and sometimes by iCab.
		cursorPositionX = e.pageX;
		cursorPositionY = e.pageY;
	}else{
		// NOTE: If clientX/Y contain the coordinates relative to the document, we read them out 
		//       and are ready. (Opera,iCab and Konqueror). InScript is the scripting language 
		//       used by iCab.
		//		 If clientX/Y contain the coordinates relative to the window, we read them out, 
		//       add the scrolling offset of the page and are ready. (IE)
		cursorPositionX = e.clientX;
		cursorPositionY = e.clientY;
		if(!((window.navigator.userAgent.indexOf('Opera')+1) || (window.ScriptEngine && ScriptEngine().indexOf('InScript')+1) || window.navigator.vendor == 'KDE' )){
			// NOT: Browser should be Internet Explorer
			cursorPositionX = cursorPositionX + getHorizontalScrollPosition();
			cursorPositionY = cursorPositionY + getVerticalScrollPosition();
		}
	}
	return [cursorPositionX, cursorPositionY];
}
function getCoordinatesRelativeToWindow(e){
	if(!e){
		e = window.event;
	}
	// GET THE CURRENT POSTION OF THE CURSOR RELATIVE TO WINDOW (NOT DOCUMENT)
	// NOTE: Netscape 6 and Explorer define clientX/Y as the mouse coordinates relative to the 
	//       window.  Opera, Konqueror and iCab define clientX/Y as the mouse coordinates relative 
	//       to the document, not to the window.
	if(!e || typeof(e.clientX)!='number'){
		cursorPositionX = 0;
		cursorPositionY = 0;
	}else{
		// NOTE: If clientX/Y contain the coordinates relative to the document, we read them out 
		//       and are ready. (Opera,iCab and Konqueror). InScript is the scripting language 
		//       used by iCab.
		//		 If clientX/Y contain the coordinates relative to the window, we read them out, 
		//       add the scrolling offset of the page and are ready. (IE)
		cursorPositionX = e.clientX;
		cursorPositionY = e.clientY;
		if(!((window.navigator.userAgent.indexOf('Opera')+1) || (window.ScriptEngine && ScriptEngine().indexOf('InScript')+1) || window.navigator.vendor == 'KDE' )){
			// NOT: Browser should be Internet Explorer
			cursorPositionX = cursorPositionX + getHorizontalScrollPosition();
			cursorPositionY = cursorPositionY + getVerticalScrollPosition();
		}
	}
	return [cursorPositionX, cursorPositionY];
}

function getFrameSize(){
	// GET THE CURRENT SIZE OF THE INNER WINDOW/FRAME BOUNDARY
	if(typeof(self.innerWidth)=='number'){
		// Mozilla
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}else if(typeof(getBodyObject().clientWidth)=='number'){
		frameWidth = getBodyObject().clientWidth;
		frameHeight = getBodyObject().clientHeight;
	}
	return [frameWidth, frameHeight];
}

function getFrameBoundaryPositions(){
	// GET THE DOCUMENT POSTIONS OF ALL INNER WINDOW/FRAME BOUNDARIES
	frameWidthHeight = getFrameSize();
	frameWidth = frameWidthHeight[0];
	frameHeight = frameWidthHeight[1];

	leftEdge = getHorizontalScrollPosition();
	rightEdge = leftEdge + frameWidth;
	topEdge = getVerticalScrollPosition();
	bottomEdge = topEdge + frameHeight;

	return [leftEdge, rightEdge, topEdge, bottomEdge];
}

