function DomTools() {
    
}

DomTools.isIE = function () {
    /*@cc_on @*/
    /*@if (@_jscript_version >= 1)
        return true;
    @end @*/
    
    return false;
}

DomTools.getHead = function() {
    var doc = document;
    
    if (arguments.length > 1) {
        doc = arguments[1];
    }
    
    var nl = doc.getElementsByTagName('head');
    
    if (nl.length < 1) {
        return null;
    }
    
    return nl[0];
}

DomTools.getBody = function() {
    var doc = document;
    
    if (arguments.length > 1) {
        doc = arguments[1];
    }
    
    var nl = doc.getElementsByTagName('body');
    
    if (nl.length < 1) {
        return document.body || null;
    }
    
    return nl[0];
}

DomTools.getElementsByClassName = function (className) {
    var children = document.getElementsByTagName('*') || document.all;
    var elements = new Array();
    
    var child = null;
    
    for (var i = 0; i < children.length; i++) {
        child = children[i];
        
        if (child.className.findWord(className) != true) {
            continue;
        }
        
        elements.push(child);
        
    }

    return elements;
}

DomTools.getActualStyle = function(node, property) {
    var result = null;
    var tmp = null;
    
    if (node.currentStyle) {
        result = node.currentStyle[property];
    
    } else if (document.defaultView
            && document.defaultView.getComputedStyle) {
        
        tmp = document.defaultView.getComputedStyle(node, null);
        
        if (tmp != null) {
            result = document.defaultView.getComputedStyle(node, null).getPropertyValue(property);
        }
        
    } else if (window.getComputedStyle) {
        result = window.getComputedStyle(node, null).getPropertyValue(property);
        
    }

    if ((result == null)
            && (node.style.getPropertyValue)) {
        result = node.style.getPropertyValue(property);
        
    } else if ((result == null)
            && (node.style)) {
        eval('result = node.style.' + property + ';');
    }
    
    if (result == null) {
        result = '';
    }
    
    return result;
}

DomTools.addEventHelpers = function (obj) {
    if (obj.appendNewEvent) {
        return true;
    }
    
    obj.appendNewEvent = function(eventWithoutOn, functionDef) {
            if (this.addEventListener) {
                this.addEventListener(eventWithoutOn, functionDef, false);
                return true;
            }
            
            if (this.attachEvent) {
                this.attachEvent("on" + eventWithoutOn, functionDef);
                return true;
            }
            
            return false;
        }
    
    return true;
}


DomTools.addHelpers = function (node) {
    if (!node.nodeName) {
        return;
    }
    
    
    if (!node.hasDomToolsFeatures) {
        node.hasDomToolsFeatures = function () {
            return true;
        }
        
    } else {
        return;
        
    }
    
    
    if (!node.rugama) {
        node.rugama = new TreeMap();
    }
    
    if (!node.setFloatStyle) {
        node.setFloatStyle = function (floatValue) {
            if (!this.style) {
                return false;
            }
            
            var stl = this.style;
            
            if (stl.styleFloat) {
                this.style.styleFloat = floatValue;
                return true;
                
            } else if (stl.cssFloat) {
                this.style.cssFloat = floatValue;
                return true;
                
            }
            
            return false;
        }
        
    }
    
    if (!node.setOpacityStyle) {
        node.setOpacityStyle = function (opacityValue) {
            if (!this.style) {
                return false;
            }
            
            opacityValue = Number.parse.integer(opacityValue, 1);
            
            if (opacityValue < 0) {
                opacityValue = 0;
            }
            
            if (DomTools.isIE() == true) {
                this.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacityValue + ');'
                
                return true;
                
            }
            
            this.style.opacity = (opacityValue / 100);
            
            return true;
        }
    }
    
    if (!node.appendText) {
        node.appendText = function(text) {
            this.appendChild(document.createTextNode(text));
        }
    }
    
    if (!node.appendBreak) {
        node.appendBreak = function() {
            this.appendChild(document.createElement('br'));
        }
    }
    
    if (!node.appendLineBreak) {
        node.appendLineBreak = function() {
            this.appendChild(document.createElement('hr'));
        }
    }
    
    if (!node.getActualStyle) {
        node.getActualStyle = function (styleProp) {
            return DomTools.getActualStyle(this, styleProp);
        }
        
        
        node.getActualStyle = function (property) {
            var result = null;
            var tmp = null;
            
            if (DomTools.isIE() == true) {
                property = property.normalizeCodeVariable();
            }
            
            if (this.currentStyle) {
                result = this.currentStyle[property];
            
            } else if ((document.defaultView)
                    && (document.defaultView.getComputedStyle)) {
                
                tmp = document.defaultView.getComputedStyle(this, null);
                
                if (tmp != null) {
                    result = tmp.getPropertyValue(property);
                }
                
            } else if (window.getComputedStyle) {
                tmp = window.getComputedStyle(this, null);
                
                if (tmp != null) {
                    result = tmp.getPropertyValue(property);
                }
                
            }
            
            if ((result == null)
                    && (this.style)
                    && (this.style.getPropertyValue)) {
                result = this.style.getPropertyValue(property);
                
            } else if ((result == null)
                    && (this.style)) {
                eval('result = this.style.' + property + ';');
            }
            
            if (result == null) {
                result = '';
            }
            
            return result;
        }
    }
    
    DomTools.addEventHelpers(node);
    
    if (!node.getElementsByClassName) {
        node.getElementsByClassName = function (className) {
            var sz = this.childNodes.length;
            var i = 0;
            
            var sz2 = 0;
            var j = 0;
            
            var childResult = null;
            var elements = new Array();
            
            var child = null;
            
            for (i = 0; i < sz; i++) {
                child = this.childNodes[i];
                
                if (child.nodeType != 1) {
                    continue;
                }
                
                DomTools.addHelpers(child);
                
                if (child.className.findWord(className) == true) {
                    elements.push(child);
                }
                
                childResult = child.getElementsByClassName(className);
                
                sz2 = childResult.length;
                
                if (sz2 < 1) {
                    continue;
                }
                
                for (j = 0; j < sz2; j++) {
                    elements.push(childResult[j]);
                    
                }
                
                
            }
            
            return elements;
        }
    }
    
    
    if (!node.removeAllChildren) {
        node.removeAllChildren = function() {
            var start = this.childNodes.length - 1;
            
            var i = 0;
            
            for (i = start; i >= 0; i--) {
                this.removeChild(this.childNodes[i]);
                
            }
            
        }
    }
    
    
}

DomTools.create = function(nodeName) {
    var doc = document;
    
    if (arguments.length > 1) {
        doc = arguments[1];
    }
    
    
    var result = doc.createElement(nodeName);
    DomTools.addHelpers(result);
    
    return result;
}

DomTools.createFakeEvent = function (command, target) {
    var fakeEvent = new Object();
    
    fakeEvent.command = command;
    
    fakeEvent.target = target;
    
    fakeEvent.stopPropagation = false;
    
    fakeEvent.returnValue = true;
    
    fakeEvent.getTarget = function () {
        return this.target;
    }
    
    fakeEvent.dispose = function (returnValue) {
        this.stopPropagation = true;
        
        this.returnValue = (returnValue === true) ? true : false;
        
    }
    
    fakeEvent.getReturn = function () {
        return this.returnValue;
    }
    
    
    fakeEvent.isDisposed = function () {
        return this.stopPropagation;
    }
    
    return fakeEvent;
}

DomTools.createEvent = function(e) {
    var ev = (!e) ? window.event : e;
    
    ev.getTarget = function() {
        var result = (this.target) ? this.target : this.srcElement;
        
        // defeat Safari bug
        if (result.nodeType == 3) {
            result = result.parentNode;
        }
        
        return result;
    }
    
    ev.isLeftClick = function () {
        return (((this.which) && (this.which == 1))
            || ((this.button) && (this.button == 1)));
    }

    ev.getPointerX = function () {
        return this.pageX
            || (this.clientX
                + (document.documentElement.scrollLeft
                    || document.body.scrollLeft));
    }
    
    ev.getPointerY = function () {
        return this.pageY
            || (this.clientY
                + (document.documentElement.scrollTop
                    || document.body.scrollTop));
    }
    
    ev.dispose = function () {
        this.cancelBubble = true;
        
        if (this.returnValue) {
            this.returnValue = false;
        }
        
        if (this.preventDefault) {
            this.preventDefault(); 
            this.stopPropagation();
        }
        
        return true;
    }
    
    return ev;
}

DomTools.isElement = function (element) {
    if (element == null) {
        return false;
    }
    
    if ((!element.nodeType)
            || (element.nodeType != 1)) {
        return false;
    }
    
    return true;
}

DomTools.insertStyleSheet = function (cssUrl) {
    var head = DomTools.getHead();
    
    if (head == null) {
        document.write('<link type="text/css" media="all" rel="stylesheet" href="' + cssUrl + '" />');
        return true;
    }
    
    var newLink = DomTools.create('link');
    newLink.rel = "stylesheet";
    newLink.media = "all";
    newLink.type = "text/css";
    newLink.href = cssUrl;
    
    head.appendChild(newLink);
    
    return true;
}

DomTools.insertScript = function (jsUrl) {
    var head = DomTools.getHead();
    
    if (head == null) {
        document.write('<script type="text/javascript" src="' + jsUrl + '" ><'+'/'+'sc'+'ript>');
        return true;
    }
    
    var newScript = DomTools.create('script');
    newScript.type = "text/javascript";
    newScript.src = jsUrl;
    
    head.appendChild(newScript);
    
    return true;
}


DomTools.inspect = function(object) {
    var props = [];
    
    var showFunctions = true;
    
    if (arguments.length > 1) {
        showFunctions = (arguments[1] == true) ? true : false;
    }
    
    for (var k in object) {
        var v = object[k];
        
        if ((typeof v == 'function')
                && (showFunctions == false)) {
            continue;
        }
        
        props.push(k + ': "' + v + '"');
        
    }
    
    return '{ ' + props.join("\n") + ' }';
}

DomTools.printObject = function(obj) {
    var showFunctions = true;
    
    if (arguments.length > 1) {
        showFunctions = (arguments[1] == true) ? true : false;
    }
    
    DomTools.msg(DomTools.inspect(obj, showFunctions));
}

DomTools.msg = function(text) {
    if (!this.counter) {
        this.counter = 0;
    }
    
    var msg = document.getElementById('DomTools:msg:ui:container');
    
    if (!msg) {
        msg = DomTools.create('pre');
        msg.style.textAlign = 'left';
        msg.id = 'DomTools:msg:ui:container';
        DomTools.getBody().appendChild(msg);
        
        
    }
    
    msg.appendText(this.counter + " : ");
    
    if (typeof text == 'object') {
        text = DomTools.inspect(text);
    }
    
    msg.appendText(text + "\n");
    msg.appendLineBreak();
    
    this.counter++;
}

DomTools.htmlMsg = function(html) {
    if (!this.counter) {
        this.counter = 0;
    }
    
    var msg = document.getElementById('DomTools:msg:ui:container');
    
    if (!msg) {
        msg = DomTools.create('pre');
        msg.style.textAlign = 'left';
        msg.id = 'DomTools:msg:ui:container';
        DomTools.getBody().appendChild(msg);
        
        
    }
    
    var tmp = DomTools.create('span');
    tmp.innerHTML = html;
    
    msg.appendChild(tmp);
    msg.appendLineBreak();
    
    this.counter++;
}


function $() {
    var result = new Array();
    var element = null;
    var i = 0;
    var sz = arguments.length;
    
    for (i = 0; i < sz; i++) {
        element = arguments[i];
        if (typeof element == 'string') {
            element = document.getElementById(element);
        }
        
        if (arguments.length == 1) {
            return element;
        }

        result.push(element);
    }

    return result;
}

function _$() {
    var result = new Array();
    var element = null;
    var i = 0;
    var sz = arguments.length;
    
    var j = null;
    var sz2 = null;
    
    for (i = 0; i < sz; i++) {
        element = arguments[i];
        if (typeof element != 'string') {
            continue;
        }
        
        element = DomTools.getElementsByClassName(element);
        
        if (sz == 1) {
            return element;
        }
        
        sz2 = element.length;
        
        if (sz2 < 1) {
            continue;
        }
        
        for (j = 0; j < sz2; j++) {
            result.push(element[j]);
        }
        
        
    }

    return result;
}


window.rugama = new TreeMap();

window.isLoaded = false;
window.loadStack = new Array();
window.appendLoadEvent = function (event) {
    if (this.isLoaded == true) {
        event();
        return true;
    }
    
    this.loadStack.push(event);
    return true;
}

//window.isLoaded = true;

window.executeLoadEvents = function () {
    //var sz = this.loadStack.length;
    var i = 0;
    
    
    for (i = 0; i  < this.loadStack.length; i++) {
        this.loadStack[i]();
    }
    
    this.loadStack = new Array();
    
}



DomTools.addEventHelpers(document);
DomTools.addEventHelpers(window);


window.appendNewEvent('load',
    function() {
        window.executeLoadEvents();
        window.isLoaded = true;
        return true
    });


/*
function _initDom(e,el) {
    if (_initDom.intiateOnce) {
        return false;
    };
    
    
    if (el && el.runtimeStyle ) {
        //IE5.0+
        if (document.readyState.toLowerCase() == "complete"
                || document.readyState.toLowerCase() == "interactive") {
            el.runtimeStyle.behavior = "none";
            
        } else {
            return false;
        };
    }
    
    _initDom.intiateOnce = true;
    
    
    window.executeLoadEvents();
    window.isLoaded = true;
    return true
}

if (DomTools.isIE()) {
    if (!!document.styleSheets[0]) {
        //html{behavior:expression(void(DOMLoad(null,this)));}
        document.styleSheets[0].addRule('HTML', 'behavior:expression(void(_initDom(null,this)))');
        
    } else {
        document.createStyleSheet("javascript:'HTML:behavior:expression(void(_initDom(null,this)))'");
        
    }
    
} else if (document.getBoxObjectFor && window.addEventListener) {
    document.addEventListener("DOMContentLoaded", _initDom, null);
    
    
} else {
    window.appendNewEvent('load',
        function() {
            window.executeLoadEvents();
            window.isLoaded = true;
            return true
        });
    
    
}
*/



