//-----------------------------------------------------------------------------------
// UniDom basic functions - DOM type detection and finding of DOM address.
// This file contains code created by Mihails Bogdanovs.
// Licence: (CC) Creative Commons. See also: http://creativecommons.org/
//-----------------------------------------------------------------------------------
var isDHTML = 0;
var isLayers = 0;
var isAll = 0;
var isID = 0;

if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
        browserVersion = parseInt(navigator.appVersion);
        if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
        else {
        if (document.all) {isAll = 1; isDHTML = 1;}
}}

//----- Inner function - not to be used outside of this module -----
function findDOMinLayersDeeper( objectID, searchRoot ) {
        var curobj;
        var to_see = new Array();

        to_see.push( searchRoot );
        while ( to_see.length > 0 )
        {
                curobj = to_see.pop();
                if ( curobj.layers ) { // ------------
                        if ( curobj.layers[objectID] ) {
                                return curobj.layers[objectID];
                        }
                }
                else { // no layers under curobj -----
                        continue;
                }
                // there are layers under curobj -----

                for ( var i = curobj.layers.length - 1; i >= 0; i-- ) {
                        testObj = curobj.layers[i];
                        to_see.push( testObj );
                }
        }
        return null;
}

//----- Inner function - not to be used outside of this module -----
function findDOMinLayers( objectID ) {
        if (!isLayers) { return null; }
        return findDOMinLayersDeeper( objectID, document );
}

function findDOM(objectID,withStyle) {
        if (withStyle == 1) {
                if (isID) { return (document.getElementById(objectID).style) ; }
                else {
                        if (isAll) { return (document.all[objectID].style); }
                else {
                        if (isLayers) { return findDOMinLayers( objectID ); }
                };}
        }
        else {
                if (isID) { return (document.getElementById(objectID)) ; }
                else {
                        if (isAll) { return (document.all[objectID]); }
                else {
                        if (isLayers) { return findDOMinLayers( objectID ); }
                };}
        }
        return null;
}

//-----------------------------------------------------------------------------------
// UniDom - unified and simplified DOM for most browsers.
//-----------------------------------------------------------------------------------
//----- Finding object's properties: width -----
function UniDom_getWidth()
{
        if ( this.dom_addr.offsetWidth )
                return this.dom_addr.offsetWidth;
        if ( this.dom_addr.clip.width )
                return this.dom_addr.clip.width;
        return null;
}

//----- Finding object's properties: height -----
function UniDom_getHeight()
{
        if ( this.dom_addr.offsetHeight )
                return this.dom_addr.offsetHeight;
        if ( this.dom_addr.clip.height )
                return this.dom_addr.clip.height;
        return null;
}

//----- Finding object's properties: left -----
function UniDom_getLeft()
{
        if (this.dom_style.left)
                return parseInt( this.dom_style.left );
        if (this.dom_style.pixelLeft)
                return parseInt( this.dom_style.pixelLeft );
        if (this.dom_addr.offsetLeft)
                return parseInt( this.dom_addr.offsetLeft );
        return null;
}

//----- Finding object's properties: top -----
function UniDom_getTop()
{
        if (this.dom_style.top)
                return parseInt( this.dom_style.top );
        if (this.dom_style.pixelTop)
                return parseInt( this.dom_style.pixelTop );
        if (this.dom_addr.offsetTop)
                return parseInt( this.dom_addr.offsetTop );
        return null;
}

//----- Finding object's properties: right -----
function UniDom_getRight()
{
        var domStyle = this.dom_style;
        var dom      = this.dom_addr;

        if (dom.left)
                return (domStyle.left + domStyle.clip.width);
        if (domStyle.pixelLeft)
                return (domStyle.pixelLeft + dom.offsetWidth);
        if (dom.offsetLeft)
                return (dom.offsetLeft + dom.offsetWidth);
        return null;
}

//----- Finding object's properties: bottom -----
function UniDom_getBottom()
{
        var domStyle = this.dom_style;
        var dom      = this.dom_addr;

        if (domStyle.top)
                return (domStyle.top + domStyle.clip.height);
        if (domStyle.pixelTop)
                return (domStyle.pixelTop + dom.offsetHeight);
        if (dom.offsetTop)
                return (dom.offsetTop + dom.offsetHeight);
        return null;
}

//----- Finding object's properties: z-index -----
function UniDom_getZIndex()
{
        var domStyle = this.dom_style;
        if ( domStyle.zIndex != null )
                return domStyle.zIndex;
        return null;
}

//----- Finding object's properties: visibility (1=visible; 0=not) -----
function UniDom_isVisible()
{
        if ( isDHTML ) {
                vis1 = this.dom_style.visibility;
                if (( vis1 == 'show') || ( vis1 == 'visible')) {
                        return 1;
                }
                return 0;
        }
        return null;
}

//----- Finding object's properties: value -----
function UniDom_getValue()
{
        if ( isDHTML ) {
                return this.dom_addr.value;
        }
        return null;
}

//----- Finding object's properties: value -----
function UniDom_isChecked()
{
        if ( isDHTML ) {
                return this.dom_addr.checked;
        }
        return null;
}

//--------------------------------------
//----- Changing object properties -----
//--------------------------------------
function UniDom_setZIndex( layerNum )
{
        if ( isDHTML ) {
                this.dom_style.zIndex = layerNum;
        }
}

//----- Setting of visibility (1=visible; 0=not) -----
function UniDom_setVisibility( value )
{
        if ( isDHTML ) {
                if (( value == 'hide') || ( value == 'hidden') || ( (value+'') == '0' )) {
                        val1 = 'hidden';
                }
                else {
                        val1 = 'visible';
                }

                this.dom_style.visibility = val1;
        }
}

//----- Setting of display (1=visible; 0=not) -----
function UniDom_setDisplay( value )
{
        if ( isDHTML ) {
                if (( value+'' == 'none') || ( (value+'') == '0' )) {
                        val1 = 'none';
                }
                else {
                        val1 = 'inline';
                }

                this.dom_style.display = val1;
        }
}

//----- Setting of visibility (1=visible; 0=not) -----
function UniDom_toggleVisibility()
{
        if ( isDHTML ) {
                this.setVisibility( this.isVisible() ? 0 : 1 );
        }
}

//----- Moves object (top left corner) to (x, y) -----
function UniDom_moveTo( x, y )
{
        if ( isDHTML ) {
                this.dom_style.left = x + "px";
                this.dom_style.top  = y + "px";
        }
}

//----- Moves object (top left corner) by (deltaX, deltaY) -----
function UniDom_moveBy( deltaX, deltaY ) {
        var domStyle = this.dom_style;
        var dom      = this.dom_addr;

        if ( !isDHTML ) { return; }

        if (domStyle.pixelLeft) {
                domStyle.pixelLeft += deltaX;
                domStyle.pixelTop  += deltaY;
        }
        else if (dom.offsetLeft != null) {
                var plusLeft = parseInt( dom.offsetLeft );
                var plusTop  = parseInt( dom.offsetTop );

                domStyle.left = (deltaX + plusLeft) + "px";
                domStyle.top  = (deltaY + plusTop) + "px";
        }
        else dom.moveBy(deltaX,deltaY);
}

//----- Changes object style -----
function UniDom_changeStyle( styleName, newVal ) {
        if ( isDHTML && !isLayers ) {
                this.dom_style [styleName] = newVal;
        }
}

//----- Changes object class -----
function UniDom_setClass( newClass ) {
        if ( isDHTML && !isLayers ) {
                this.dom_addr.className = newClass;
        }
}

// -----
function UniDom_setClip(clipTop, clipRight, clipBottom, clipLeft) {
	if ( isDHTML && !isLayers ) {
		var dom = this.dom_style;
		if (dom.clip.left) {
			dom.clip.top = clipTop;
			dom.clip.right = clipRight;
			dom.clip.bottom = clipBottom;
			dom.clip.left = clipLeft;
		}
		dom.clip = 'rect(' + clipTop + ' ' + clipRight + ' ' + clipBottom + ' ' + clipLeft +')';
	}
}

//----- Returns true, if the object was found -----
function UniDom_found() {
        if ( isDHTML ) {
                return (this.dom_addr == null) ? false : true ;
        }
	return false;
}

//----- UniDom Constructor ----------------------------
function UniDom( objId )
{
        //----- Attributes: -----
        this.id  = objId;
        this.dom_addr = findDOM( objId, 0 );
        if ( isLayers ) {
                this.dom_style = this.dom_addr; // for time economy
        }
        else {
                this.dom_style = findDOM( objId, 1 );
        }

        //----- Methods: -----
	this.found     = UniDom_found;

        this.getWidth  = UniDom_getWidth;
        this.getHeight = UniDom_getHeight;
        this.getLeft   = UniDom_getLeft;
        this.getTop    = UniDom_getTop;
        this.getRight  = UniDom_getRight;
        this.getBottom = UniDom_getBottom;
        this.getZIndex = UniDom_getZIndex;
        this.getValue  = UniDom_getValue;

        this.isVisible = UniDom_isVisible;
        this.isChecked = UniDom_isChecked;

        this.setZIndex        = UniDom_setZIndex;
        this.setVisibility    = UniDom_setVisibility;
        this.toggleVisibility = UniDom_toggleVisibility;
        this.setDisplay       = UniDom_setDisplay;
		this.setClip          = UniDom_setClip;

        this.moveTo = UniDom_moveTo;
        this.moveBy = UniDom_moveBy;

        this.changeStyle = UniDom_changeStyle;
        this.setClass    = UniDom_setClass;
}

//-------------------------------------------------------------------
function UniDomFactory( objId )
{
  return new UniDom( objId )
}

//-------------------------------------------------------------------
function findLivePageWidth() {
  if (window.innerWidth != null)
    return parseInt( window.innerWidth );
  if (document != null && document.body != null && document.body.clientWidth != null)
    return parseInt( document.body.clientWidth );
  return (null);
}


