//misc objects
//a simple encapsulation object
//used to query widths and heights
function cDomObject( sId ){
	this.hElement = document.getElementById( sId )	
	this.hStyle = this.hElement.style
}

cDomObject.prototype.getWidth = function( ){
	return  cDomObject.getWidth( this.hElement )
}

cDomObject.getWidth = function( hElement ){
	hElement = cDomObject.$( hElement )
	var nWidth = cDomObject.getStyleValue( hElement, 'width' )
	if( isNaN( nWidth ) || ( nWidth == null ) ){
		nWidth = hElement.clientWidth ? hElement.clientWidth : hElement.offsetWidth
	}
	//alert( nWidth )
	return nWidth
}

cDomObject.prototype.getHeight = function( ){
	return  cDomObject.getHeight( this.hElement )
}

cDomObject.getHeight = function( hElement ){
	hElement = cDomObject.$( hElement )
	var nHeight = cDomObject.getStyleValue( hElement, 'height' )
	if( isNaN( nHeight ) || nHeight == null ){
		nHeight = hElement.clientHeight ? hElement.clientHeight : hElement.offsetHeight
	}
	return nHeight
}

cDomObject.getLeft = function( hElement ){
	hElement = cDomObject.$( hElement )
	var nLeft = cDomObject.getStyleValue( hElement, 'left' )
	if( isNaN( nLeft ) || ( nLeft == null ) ){
		nLeft = parseInt( hElement.offsetLeft )
	}	
	return nLeft
}

cDomObject.prototype.getTop = function( ){
	return cDomObject.getTop( this.hElement )
}

cDomObject.getTop = function( hElement ){
	hElement = cDomObject.$( hElement )
	var nTop = cDomObject.getStyleValue( hElement, 'top' )
	if( isNaN( nTop ) || ( nTop == null ) ){
		nTop = parseInt( hElement.offsetTop )
	}
	return nTop
}

//this was grabbed from YUI
cDomObject.getXY = function( hElement ){
	// has to be part of document to have pageXY
	if ( hElement.offsetParent === null || cDomObject.getStyle( hElement, 'display' ) == 'none' ) {
		return false;
	}
	var hParentNode = null;
	var pos = [];
	var hBox;

	if ( hElement.getBoundingClientRect ) { // IE
		hBox = hElement.getBoundingClientRect();
		var scrollTop = Math.max( document.documentElement.scrollTop, document.body.scrollTop );
		var scrollLeft = Math.max( document.documentElement.scrollLeft, document.body.scrollLeft );
		return [hBox.left + scrollLeft, hBox.top + scrollTop];
	} else { // safari, opera, & gecko
		pos = [ hElement.offsetLeft, hElement.offsetTop ];
		hParentNode = hElement.offsetParent;
		if ( hParentNode != hElement ) {
			while ( hParentNode ) {
				pos[0] += hParentNode.offsetLeft;
				pos[1] += hParentNode.offsetTop;
				hParentNode = hParentNode.offsetParent;
			}
		}
		if ( navigator.userAgent.toLowerCase().indexOf( 'webkit' ) && cDomObject.getStyle( hElement, 'position' ) == 'absolute' ) { // safari doubles in some cases
			pos[0] -= document.body.offsetLeft;
			pos[1] -= document.body.offsetTop;
		}
	}
	if ( hElement.parentNode ) { 
		hParentNode = hElement.parentNode; 
	} else { 
		hParentNode = null; 
	}
	while ( hParentNode && hParentNode.tagName.toLowerCase() != 'body' && hParentNode.tagName.toLowerCase() != 'html' ){ // account for any scrolled ancestors
		if ( cDomObject.getStyle( hParentNode, 'display' ) != 'inline' ) { // work around opera inline scrollLeft/Top bug
		  pos[0] -= hParentNode.scrollLeft;
		  pos[1] -= hParentNode.scrollTop;
		}

		if ( hParentNode.parentNode ) { 
			hParentNode = hParentNode.parentNode; 
		} else { 
			hParentNode = null; 
		}
	}
	return pos;
}

cDomObject.Borders = { 'l':'borderLeftWidth', 'r':'borderRightWidth', 't':'borderTopWidth', 'b':'borderBottomWidth' }
cDomObject.getBorderWidth = function( hElement, sBorder ){
	var nR = 0
	for( var hI in cDomObject.Borders ){
		if( sBorder.indexOf( hI ) >=0 ){
			nR += cDomObject.getStyleValue( hElement, cDomObject.Borders[ hI ] );
		}
	}
	return nR;
}

cDomObject.Paddings = { 'l':'paddingLeft', 'r':'paddingRight', 't':'paddingTop', 'b':'paddingBottom' }
cDomObject.getPaddingWidth = function( hElement, sPadding ){
	var nR = 0
	for( var hI in cDomObject.Paddings ){
		if( sPadding.indexOf( hI ) >=0 ){
			nR += cDomObject.getStyleValue( hElement, cDomObject.Paddings[ hI ] );
		}
	}
	return nR;
}

//are we using the border box
cDomObject.isBorderBox = function( hElement ){
	var nua = navigator.userAgent.toLowerCase();
	var bSafari = nua.indexOf( 'webkit' ) >=0;
	var bGecko = nua.indexOf( 'gecko' ) >= 0;
	var bIE7 = nua.indexOf( 'msie 7' ) >= 0;
	return ( ( document.all && !bIE7 && false ) || 
			 ( bGecko && cDomObject.getStyle( hElement, 'MozBoxSizing' ) == 'border-box' ) ||
			 ( !bSafari && cDomObject.getStyle( hElement, 'boxSizing' ) == 'border-box' ) );
	/*
        return((b.isIE && !b.isIE7) || (b.isIE7 && !strict && el.style.boxSizing != 'content-box') || 
           (b.isGecko && YAHOO.util.Dom.getStyle(el, "-moz-box-sizing") == 'border-box') || 
           (!b.isSafari && YAHOO.util.Dom.getStyle(el, "box-sizing") == 'border-box'));  
	*/
}

// used to get the absolute position of an relativeli position element
// by accumulating the offset parameters
// example
// cDomObject.getOffsetParam( hElement,'offsetLeft' )
cDomObject.getOffsetParam = function( hElement, sParam, hLimitParent ){
	hElement = cDomObject.$( hElement )
	var nRes = 0
	if( typeof hLimitParent == 'undefined' || hLimitParent == null ){
		hLimitParent = document.body.parentElement
	}
	while( hElement != hLimitParent ){
		nRes += hElement[ sParam ] ? hElement[ sParam ] : 0
		if( !hElement.offsetParent ) { break }
		hElement = hElement.offsetParent
	}
	return nRes
}

// used to get the absolute position of an relativeli position element
// by accumulating the scroll offset parameters
// example
// cDomObject.getScrollOffset( hElement,'Left' )

cDomObject.getScrollOffset = function( hElement, sParam, hLimitParent  ){
	hElement = cDomObject.$( hElement )
	var nRes = 0
	var nOff
	if( hLimitParent == null ){
		hLimitParent = document.body.parentElement
	}
	while( hElement != hLimitParent ){
		nOff = hElement[ 'scroll' + sParam ]
		if( nOff == hElement[ 'offset' + sParam ] ){
			nOff = 0
		}
		nRes +=  nOff
		if( !hElement.offsetParent ) { break }
		hElement = hElement.parentNode
	}
	return nRes
}

//
cDomObject.getStyle = function( hElement, sStyleSelector ){
	hElement = cDomObject.$( hElement )
	if( hElement.style && typeof( hElement.style[ sStyleSelector ] ) != 'undefined' && hElement.style[ sStyleSelector ] != '' ){
		return hElement.style[ sStyleSelector ]
	} else if( hElement.currentStyle ){
		if( typeof( hElement.currentStyle[ sStyleSelector ] ) != 'undefined' ){
			return hElement.currentStyle[ sStyleSelector ]
		}
	} else if( hElement.ownerDocument && hElement.ownerDocument.defaultView && hElement.ownerDocument.defaultView.getComputedStyle ){
		var hStyle = hElement.ownerDocument.defaultView.getComputedStyle( hElement, null )
		if( hStyle ){
			if( typeof( hStyle[ sStyleSelector ] ) != 'undefined' ){
				return hStyle[ sStyleSelector ] 
			}
		}
	}
	return null
}

cDomObject.getStyleValue = function( hElement, sStyleSelector ){
	var sValue = cDomObject.getStyle( hElement, sStyleSelector )
	var nValue = parseInt( sValue )
	if( !isNaN( nValue ) ){
		return nValue
	} else {
		return null
	}
}

cDomObject.setStyleValue = function( hElement, sStyleSelector, sStyleValue ){
	hElement = cDomObject.$( hElement )
	hElement.style[ sStyleSelector ] = sStyleValue;
}

cDomObject.setAbsolutePositioned = function( hElement, zIndex ){
	cDomObject.setStyleValue( hElement, 'position', 'absolute' );
	if( zIndex ){
		cDomObject.setStyleValue( hElement, 'z-index', zIndex );
	}
}
	
cDomObject.setRelativePositioned = function( hElement, zIndex ){
	cDomObject.setStyleValue( hElement, 'position', 'relative' );
	if( zIndex ){
		cDomObject.setStyleValue( hElement, 'z-index', zIndex );
	}
}

//
cDomObject.$ = function( hElement ){
	if( typeof( hElement ) == 'string' ){
		return document.getElementById( hElement )
	} else {
		return hElement
	}
}
