// Adapted from DHTML3API.js custom API for cross-platform
// object positioning by Danny Goodman (http://dannyg.com).
// Release 3.0. Supports NN, IE, and W3C DOMs.

var DHTMLAPI = {
    browserClass : new Object(),
    
    init : function () {
        this.browserClass.isCSS = ((document.body && document.body.style) ? true : false);
        this.browserClass.isW3C = ((this.browserClass.isCSS && document.getElementById) ? true : false);
        this.browserClass.isIE4 = ((this.browserClass.isCSS && document.all) ? true : false);
        this.browserClass.isNN4 = ((document.layers) ? true : false);
        this.browserClass.isIECSSCompat = ((document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false);
    },

    // Seek nested NN4 layer from string name
    seekLayer : function (doc, name) {
        var elem;
        for (var i = 0; i < doc.layers.length; i++) {
            if (doc.layers[i].name = name) {
                elem = doc.layers[i];
                break;
            }
            // dive into nested layers if necessary
            if (doc.layers[i].document.layers.length > 0) {
                elem = this.seekLayer(doc.layers[i].document, name);
                if (elem) {break;}
            }
        }
        return elem;
    },
    
    // Convert element name string or object reference
    // into a valid element object reference
    getRawObject : function (elemRef) {
        var elem;
        if (typeof elemRef == "string") {
            if (this.browserClass.isW3C) {
                elem = document.getElementById(elemRef);
            } else if (this.browserClass.isIE4) {
                elem = document.all(elemRef);
            } else if (this.browserClass.isNN4) {
                elem = this.seekLayer(document, elemRef);
            }

        } else {
            // pass through objects reference
            elem = elemRef;
        }
        return elem;
    },
    
    // Convert element name string or object reference
    // into a valid style (or NN4 layer) object reference
    getStyleObject : function (elemRef) {
        var elem = this.getRawObject(elemRef);
        if (elem && this.browserClass.isCSS) {
            elem = elem.style
        }
        return elem;
    },

    // Flow an element at a specific pixel coordinate
    flowTo : function (elemRef, x, y) {
        var elem = this.getStyleObject(elemRef);
        if (elem) {
            var offsetX = this.getScrollPositionLeft()+x;
            var offsetY = this.getScrollPositionTop()+y;
            var deltaX = parseInt((offsetX-this.getElementLeft(elemRef))/2);
            var deltaY = parseInt((offsetY-this.getElementTop(elemRef))/2);
            this.moveBy(elemRef,deltaX,deltaY);
            if (deltaX || deltaY) {
                setTimeout("DHTMLAPI.flowTo('"+elemRef+"',"+x+","+y+")",50);
            }
        }
    },

    // Position an element at a specific pixel coordinate
    moveTo : function (elemRef, x, y) {
        var elem = this.getStyleObject(elemRef);
        if (elem) {
            if (this.browserClass.isCSS) {
                // equalize incorrect numeric value type
                var units = (typeof elem.left == "string") ? "px" : 0;
                elem.left = x + units;
                elem.top = y + units;
            } else if (this.browserClass.isNN4) {
                elem.moveTo(x,y);
            }
        }
    },
    
    // Move an element by x and/or y pixels
    moveBy : function (elemRef, deltaX, deltaY) {
        var elem = this.getStyleObject(elemRef);
        if (elem) {
            if (this.browserClass.isCSS) {
                // equalize incorrect numeric value type
                var units = (typeof elem.left == "string") ? "px" : 0;
                if (!isNaN(this.getElementLeft(elemRef))) {
                    elem.left = this.getElementLeft(elemRef) + deltaX + units;
                    elem.top = this.getElementTop(elemRef) + deltaY + units;
                }
            } else if (this.browserClass.isNN4) {
                elem.moveBy(deltaX, deltaY);
            }
        }
    },
    
    // Set the z-order of an object
    setZIndex : function (obj, zOrder) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            elem.zIndex = zOrder;
        }
    },
    
    // Set the background color of an object
    setBGColor : function (obj, color) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            if (this.browserClass.isCSS) {
                elem.backgroundColor = color;
            } else if (this.browserClass.isNN4) {
                elem.bgColor = color;
            }
        }
    },
    
    // Set the visibility of an object to visible
    show : function (obj) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            elem.visibility = "visible";
        }
    },
    
    // Set the visibility of an object to hidden
    hide : function (obj) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            elem.visibility = "hidden";
        }
    },

    // Set the display attribute of an object
    display : function (obj,attr) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            elem.display = attr;
        }
    },

    // Toggle two elements display attribute
    toggle : function (obj0,obj1,attr) {
        var elem0 = this.getStyleObject(obj0);
        var elem1 = this.getStyleObject(obj1);
        if (elem0 && elem1) {
            elem0.display = "none";
            elem1.display = attr;
        }
    },

    // Set mouse-over attributes of an object
    MO : function (obj, bgcolor, border, color, cursor, weight) {
        var elem = this.getStyleObject(obj);
        if (elem) {
            if (bgcolor.length) {
                elem.background = bgcolor;
            }
            if (border.length) {
                elem.border = border;
            }
            if (color.length) {
                elem.color = color;
            }
            if (cursor.length) {
                elem.cursor = cursor;
            }
            if (weight.length) {
                elem.fontWeight = weight;
            }
        }
    },

    // return computed value for an element's style property
    getComputedStyle : function (elemRef, CSSStyleProp) {
        var elem = this.getRawObject(elemRef);
        var styleValue, camel;
        if (elem) {
            if (document.defaultView && document.defaultView.getComputedStyle) {
                // W3C COM version
                var compStyle = document.defaultView.getComputedStyle(elem, "");
                styleValue = compStyle.getPropertyValue(CSSStyleProp);
            } else if (elem.currentStyle) {
                // make IE style property camleCase name from CSS version
                var IEStyleProp = CSSStyleProp;
                var re = /-\D/;
                while (re.test(IEStyleProp)) {
                    camel = IEStyleProp.match(re)[0].charAt(1).toUpperCase();
                    IEStyleProp = IEStyleProp.replace(re, camel)
                }
                styleValue = elem.currentStyle[IEStyleProp];
            }
        }
        return (styleValue) ? styleValue : null;
    },
    
    // Retrieve the x coordinate of a positionable object
    getElementLeft : function (elemRef) {
        var elem = this.getRawObject(elemRef);
        var result = null;
        if (this.browserClass.isCSS || this.browserClass.isW3C) {
            result = parseInt(this.getComputedStyle(elem, "left"));
        } else if (this.browserClass.isNN4) {
            result = elem.left;
        }
        return result;
    },
    
    // Retrieve the y coordinate of a positionable object
    getElementTop : function (elemRef) {
        var elem = this.getRawObject(elemRef);
        var result = null;
        if (this.browserClass.isCSS || this.browserClass.isW3C) {
            result = parseInt(this.getComputedStyle(elem, "top"));
        } else if (this.browserClass.isNN4) {
            result = elem.top;
        }
        return result;
    },
    
    // Retrieve  the rendered width of an element
    getElementWidth : function (elemRef) {
        var elem = this.getRawObject(elemRef);
        var result = null;
        if (elem) {
            if (elem.offsetWidth) {
                if (elem.scrollWidth && (elem.offsetWidth != elem.scrollWidth)) {
                    result = elem.scrollWidth;
                } else {
                    result = elem.offsetWidth;
                }
            } else if (elem.clip && elem.clip.width) {
                // Netscape 4 positioned elements
                result = elem.clip.width;
            }
        }
        return result;
    },
    
    // Retrieve the rendered height of an element
    getElementHeight : function (elemRef) {
        var elem = this.getRawObject(elemRef);
        var result = null;
        if (elem) {
            if (elem.offsetHeight) {
                result = elem.scrollHeight;
            } else if (elem.clip && elem.clip.height) {
                result = elem.clip.height;
            }
        }
        return result;
    },
    
    // Return the available content width space in browser window
    getInsideWindowWidth : function () {
        if (window.innerWidth) {
            return window.innerWidth;
        } else if (this.browserClass.isIECSSCompat) {
            // measure the html lelement's clientWidth
            return document.body.parentElement.clientWidth;
        } else if (document.body && document.body.clientWidth) {
            return document.body.clientWidth;
        }
        return null;
    },
    
    // Return the available content height space in browser window
    getInsideWindowHeight : function () {
        if (window.innerHeight) {
            return window.innerHeight;
        } else if (this.browserClass.isIECSSCompat) {
            // measure the html element's clientHeight
            return document.body.parentElement.clientHeight;

        } else if (document.body && document.body.clientHeight) {
            return document.body.clientHeight;
        }
        return null;
    },

    // Return the left-most viewable scroll position (x coordinate)
    getScrollPositionLeft : function () {
        if (window.scrollX >= 0) {
            return window.scrollX;
        } else if (this.browserClass.isIECSSCompat) {
            return document.body.parentElement.scrollLeft;
        } else if (document.body && document.body.scrollLeft) {
            return document.body.scrollLeft;
        }
        return 0;
    },

    // Return the top-most viewable scroll position (y coordinate)
    getScrollPositionTop : function () {
        if (window.scrollY >= 0) {
            return window.scrollY;
        } else if (this.browserClass.isIECSSCompat) {
            return document.body.parentElement.scrollTop;
        } else if (document.body && document.body.scrollTop) {
            return document.body.scrollTop;
        }
        return 0;
    }

}
addOnLoadEvent(function() {DHTMLAPI.init()});

//
//
//
var SCROLL_EVENT = {
    init : function () {
        window.onscroll = this.increment;
        document.body.onscroll = this.increment;
    },
    increment : function () {
        SCROLL_QUEUE.events += 1;
        setTimeout("SCROLL_EVENT.decrement()",250);
    },

    decrement : function () {
        SCROLL_QUEUE.events -= 1;
        this.position();
    },
    position : function () {
        if (SCROLL_QUEUE.events <= 0) {
            var i;
            for (i=0;i<SCROLL_QUEUE.floats.length;i++) {
                DHTMLAPI.flowTo(SCROLL_QUEUE.floats[i][0],
                                SCROLL_QUEUE.floats[i][1],
                                SCROLL_QUEUE.floats[i][2]);
            }
        }
    }
}
var SCROLL_QUEUE = {
    events : 0,
    floats : Array (),
    register : function (name,posx,posy) {
        this.floats[this.floats.length]=[name,posx,posy];
    }
}
addOnLoadEvent(function() {SCROLL_EVENT.init()});

