IroCon.util.Dom = function() {
    return {
        createElement : function(tag, id) {
            var obj = null;
            if ((document)&&(document.createElement)) {
                obj = document.createElement(tag);
                if ((obj) && (obj != null)) {
                    obj.id = id;
                    if (document.body.appendChild) {
                        document.body.appendChild(obj);
                    } else if (document.documentElement.appendChild) {
                        document.documentElement.appendChild(obj);
                    } else if (document.appendChild) {
                        document.appendChild(obj);
                    }
                }
            }
            return obj;
        },
        getElement : function(id) {
            return document.getElementById(id);
        },
        getElements : function(selector) {
            var i;
            var s=[];
            var selid="";
            var selclass="";
            var tag=selector;
            var objlist=[];
            if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
                s=selector.split(" ");
                var fs=s[0].split("#");
                if(fs.length==1) return(objlist);
                return(document.getElementById(fs[1]).getElementsByTagName(s[1]));
                }
            if(selector.indexOf("#")>0){ //id selector like "tag#id"
                s=selector.split("#");
                tag=s[0];
                selid=s[1];
                }
            if(selid!=""){
                objlist.push(document.getElementById(selid));
                return(objlist);
                }
            if(selector.indexOf(".")>0){  //class selector like "tag.class"
                s=selector.split(".");
                tag=s[0];
                selclass=s[1];
                }
            var v=document.getElementsByTagName(tag);  // tag selector like "tag"
            if(selclass=="")
                return(v);
            for(i=0;i<v.length;i++){
                if(v[i].className==selclass){
                    objlist.push(v[i]);
                    }
                }
            return(objlist);
        },
        toggle : function(o) {
            if (o.visibility == 'hidden' || o.visibility == '') {
                IroCon.util.Dom.showObject(o);
            } else {
                IroCon.util.Dom.hideObject(o);
            }
        },
        hide : function(o) {
            o.style.visibility = 'hidden';
            o.style.display = 'none';
        },
        show : function(o) {
            o.style.visibility = 'visible';
            o.style.display = 'block';
        },
        move : function(o, b, h, v) {
            var y = o.style;
            
            if (h=="left") {
                y.left = IroCon.util.Dom.getLeftPos(b) + "px";
            } else {
                y.left = (IroCon.util.Dom.getLeftPos(b) + b.clientWidth - o.clientWidth) + "px";
            }
            if (v=="top") {
                y.top = IroCon.util.Dom.getTopPos(b) + "px";
            } else if (v=="bottom")  {
                y.top = ((IroCon.util.Dom.getTopPos(b) + b.clientHeight) - o.clientHeight) + "px";
            } else if (v=="above")  {
                y.top = (IroCon.util.Dom.getTopPos(b) - o.clientHeight) + "px";
            } else if (v=="below")  {
                y.top = (IroCon.util.Dom.getTopPos(b) + b.clientHeight) + "px";
            }
        },
        resize : function(o, b, s, n) {
            var y = o.style;
            
            if (s=="height") {
                if (n) {
                    y.height = (b.clientHeight + n) + 'px';
                } else {
                    y.height = b.clientHeight + 'px';
                }
            } else {
                if (n) {
                    y.width = (b.clientWidth + n) + 'px';
                } else {
                    y.width = relObj.clientWidth + 'px';
                }
            }
        },
        getTopPos : function(o){
            var t=o.offsetTop;
            var p=o.offsetParent;
            while (p!=null){
                t=t+p.offsetTop;
                p=p.offsetParent;
            }
            return t;
        },
        getLeftPos : function(o){
            var t=o.offsetLeft;
            var p=o.offsetParent;
            while (p!=null){
                t=t+p.offsetLeft;
                p=p.offsetParent;
            }
            return t;
        },
        getPosition : function(o){
            return {
                x: IroCon.util.Dom.getLeftPos(o), 
                y: IroCon.util.Dom.getTopPos(o)
            }; 
        },
        getMousePos : function(e){
            if(e.pageX || e.pageY){ 
                return {x:e.pageX, y:e.pageY}; 
            } 
            return { 
                x:e.clientX + document.body.scrollLeft - document.body.clientLeft, 
                y:e.clientY + document.body.scrollTop  - document.body.clientTop 
            };
        },
        getMouseOffset: function(target, e) {
            e = e || window.event;

            var docPos = IroCon.util.Dom.getPosition(target);
            var mousePos = IroCon.util.Dom.getMousePos(e);
            return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
        },
        getOpacity: function(o, n) {
            var v = 0;
            var m = 0;
            if(typeof o.style.opacity != "undefined"){
                m = o.style.opacity;
            } else if(typeof o.style.MozOpacity != "undefined"){
                m = o.style.MozOpacity;
            } else if(typeof o.style.KhtmlOpacity != "undefined"){
                m = o.style.KhtmlOpacity;
            } else if(typeof o.style.filter != "undefined"){
                var regExpGet = /.*opacity=([0-9]+).*/; // IE doesn't seem to understand \d
                var regExpSet = /(.*opacity=)[0-9]+(.*)/;
                var s = o.style.filter;
                m = s.match(regExpGet) ? s.replace(regExpGet, "$1") : 0;
            }
            if (m > 0 && m < 1 ) 
                v = m * 100;
            return v;
        },
        setOpacity : function(o, n) {
	        var y = o.style; 
            y.opacity = (n / 100);
            y.MozOpacity = (n / 100);
            y.KhtmlOpacity = (n / 100);
            y.filter = "alpha(opacity=" + n + ")";
        },
        changeOpacity: function (o, n) {
            var v = IroCon.util.Dom.getOpacity(o);
            IroCon.util.Dom.setOpacity(o, v+n)
        },
        canHaveClass : function(o) {
            return ((o != null) && (o.className != null));
        },
        hasAnyClass : function(o) {
            return (IroCon.util.Dom.canHaveClass(o) && (o.className.length > 0));
        },
        hasClass : function(o, c) {
            return (IroCon.util.Dom.hasAnyClass(o) && (o.className.indexOf(c) > -1));
        },
        addClass : function(o, c) {
            if (IroCon.util.Dom.hasAnyClass(o))
            {
                if (!IroCon.util.Dom.hasClass(o, c))
                {
                    o.className = o.className + " " + c;
                }
            }
            else if (IroCon.util.Dom.canHaveClass(o))
            {
                o.className = c;
            }
        },
        addClassUpward : function(o, sc, c, tc) {
            var op = o;
            while ((op != null) && (!hasClass(op, tc)))
            {
                IroCon.util.Dom.addClass(op, c);
                op = op.parentNode;
            }    
        },
        swapClass : function(o, c, r) {
            if (IroCon.util.Dom.hasAnyClass(o))
            {
                o.className = o.className.replace(new RegExp(c, "gi"), r);
            }
        },
        swapOrAddClass : function(o, c, r) {
            if (IroCon.util.Dom.hasClass(o, c))
            {
                IroCon.util.Dom.swapClass(o, c, r);
            }
            else
            {
                IroCon.util.Dom.addClass(o, c);
            }
        },
        removeClass : function(o, c) {
            IroCon.util.Dom.swapClass(o, c, "");
        },
        removeClassUpward : function(o, sc, c, tc) {
            var op = o;
            while ((op != null) && (!IroCon.util.Dom.hasClass(op, tc)))
            {
                IroCon.util.Dom.removeClass(op, c);
                op = op.parentNode;
            }    
        },
        addEvent : function(o, e, f) {
            if (o.addEventListener){
                o.addEventListener(e, f, false);
            } else if (o.attachEvent){
                o.attachEvent('on'+e, f);
            }
        },
        removeEvent : function(o, e, f) {
            if (o.addEventListener) {
                o.removeEventListener(e, f, false);
            } else if (document.attachEvent) {
                o.detachEvent('on'+e, f);
            }
        }
    }
}();
