Function.EnsureNamespace('Inrix');

Inrix.Dom=new function(){
    var _ie=navigator.userAgent.indexOf('MSIE ')>-1;
    var _elts=[];
    var _defaultMap;
    var _leakList=[
        'appendChild','cloneNode',
        'insertAdjacentElement','insertAdjacentHTML','insertBefore',
        'onclick','onerror','onload','onselectstart','onmousedown',
        'onmouseover','onmouseout','ondblclick'
    ];
    
    this.add=addElement;
    this.addClass=addClass;
    this.clearContent=clearContent;
    this.create=create;
    this.disableSelect=disableSelect;
    this.enableSelect=enableSelect;
    this.encode=encodeHTML;
    this.first=firstChild;
    this.focus=focusElement;
    this.get=getElement;
    this.getAttribute=getAttribute;
    this.getScreenSize=getScreenSize;
    this.getCurrentStyle=getCurrentStyle;
    this.getOffset=getOffset;
    this.getScrollOffset=getScrollOffset;
    this.hasClass=hasClass;
    this.hide=hideElement;
    this.last=lastChild;
    this.next=nextSibling;
    this.previous=previousSibling;
    this.purge=purgeLeaks;
    this.nextSibling=nextSibling;
    this.remove=removeElement;
    this.removeAttributes=removeAttributes;
    this.removeClass=removeClass;
    this.scrollTo=scrollToElement;
    this.select=selectNodes;
    this.selectSingle=selectSingleNode;
    this.setAttributes=setAttributes;
    this.setContent=setContent;
    this.show=showElement;
    
    function addClass(elt,className){
        if(!elt||hasClass(elt,className))return;
        elt.className=[elt.className,className].slice(!elt.className).join(' ');
    }
    
    function addElement(elt,parentNode,index){
        if(!elt||!parentNode){
//            debugger;
            return;
        }
        if(arguments.length<3)return parentNode.appendChild(elt);
        return parentNode.insertBefore(elt,parentNode.childNodes[index]||null);
    }
    
    function clearContent(elt){
        if(!elt||!elt.nodeType)return;
        while(elt.childNodes.length)removeElement(elt.childNodes[0]);
    }
    
    function create(parentNode,properties,tagName,nameSpace){
        var elt;
        if(nameSpace&&document.createElementNS)elt=document.createElementNS(nameSpace,tagName||"div");
        else elt=document.createElement(tagName||"div");
        Object.Set(elt,properties);
        if(parentNode)parentNode.appendChild(elt);
        return elt;
    }

    function disableSelect(elt){
        elt.onselectstart = Inrix.Events.Void;
        elt.onselect = Inrix.Events.Void;
        elt.unselectable = "on";
        elt.style.MozUserSelect = "-moz-none";
    }

    function enableSelect(elt){
        elt.onselectstart = Inrix.Events.CancelBubble;
        elt.onselect = Inrix.Events.CancelBubble;
        elt.unselectable = "off";
        elt.style.MozUserSelect = "text";
    }
    
    function encodeHTML(text){
        if(!text||!text.toString)return '';
        return text.toString().replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\r\n|\r|\n/g,'<br />').replace(/"/g,'&quot;');
    }

    function firstChild(elt,nodeType){
        if(!elt)return null;
        if(!nodeType)nodeType=1;
        var ret=elt.firstChild;
        while(ret&&ret.nodeType!=nodeType)ret=ret.nextSibling;
        return ret;
    }
    
    function focusElement(elt,selectContents){
        if(elt){
            try{
                elt.focus();
                if(selectContents&&elt.select)elt.select();
                return true;
            }catch(e){}
        }
        return false;
    }
    
    function getElement(id){
        return document.getElementById(id);
    }
    
    function getAttribute(elt,name){
        var ret=null;
        if(elt){
            if(_ie){
                if(elt.attributes&&elt.attributes[name])ret=elt.attributes[name].nodeValue;
            }else {
                if(elt.getAttribute)ret=elt.getAttribute(name);
            }
        }
        return ret;
    }
    
    function getScreenSize(){
        if(typeof(document)=='undefined')return {width:0,height:0};
        return {
            width:document.documentElement.clientWidth,
            height:document.documentElement.clientHeight||document.documentElement.offsetHeight
        };
    }

    function getCurrentStyle(elt,key) {
        if(elt){
            if(elt.currentStyle)return elt.currentStyle[key];
            if(document.defaultView)return document.defaultView.getComputedStyle(elt,null)[key];
        }
        return "";
    }
    
    function getOffset(elt,limit,breakForOverflow,container){
        var offset={x:0,y:0};
        while(elt&&elt!=limit&&elt!=document&&elt.nodeType!=11){
            if(breakForOverflow){
                var pos=getCurrentStyle(elt,"position");
                var overflow=getCurrentStyle(elt,"overflow");
                if(pos!="static"||overflow!="visible")break;
            }
            offset.x+=elt.offsetLeft;
            offset.y+=elt.offsetTop;
            elt=elt.offsetParent||elt.parentNode;
        }
        if(container)container.offsetParent=elt;        
        return offset;
    }
    
    function getScrollOffset(elt){
        return {
            x:elt?elt.scrollLeft:document.documentElement.scrollLeft||document.body.scrollLeft,
            y:elt?elt.scrollTop:document.documentElement.scrollTop||document.body.scrollTop
        }
    }
    
    function hasClass(elt,className){
        if(!elt)return false;
        var classReg=new RegExp(String.Format("\\b{0}\\b",className));
        return classReg.test(elt.className);
    }
    
    function hideElement(elt){
        if(!elt)return;
        elt.style.display='none';
    }
    
    function lastChild(elt,nodeType){
        if(!elt)return null;
        if(!nodeType)nodeType=1;
        var ret=elt.lastChild;
        while(ret&&ret.nodeType!=nodeType)ret=ret.previousSibling;
        return ret;
    }

    function nextSibling(elt,nodeType){
        if(!elt)return null;
        if(!nodeType)nodeType=1;
        var ret=elt.nextSibling;
        while(ret&&ret.nodeType!=nodeType)ret=ret.nextSibling;
        return ret;
    }

    function previousSibling(elt,nodeType){
        if(!elt)return null;
        if(!nodeType)nodeType=1;
        var ret=elt.previousSibling;
        while(ret&&ret.nodeType!=nodeType)ret=ret.previousSibling;
        return ret;
    }
    
    function purgeLeaks(elt,knownLeaks,purgeChildren){
        if(!elt||!elt.nodeType)return;
        clearList(elt,_leakList);
        clearList(elt,knownLeaks,true);
        if(purgeChildren){
            var length=elt.childNodes.length;
            for(var i=0;i<length;i++){
                purgeLeaks(elt.childNodes[i],knownLeaks,purgeChildren);
            }
        }
    }
    
    function removeElement(elt,knownLeaks,debug){
        if(!elt||!elt.nodeType)return;
        while(elt.childNodes.length)removeElement(elt.childNodes[0],knownLeaks,debug);
        if(elt.nodeType==1&&!elt.xml){
            purgeLeaks(elt,knownLeaks,false);
//            if(debug){
//                var map={parentNode:1,nextSibling:1,previousSibling:1,currentStyle:1,document:1,parentTextEdit:1,parentElement:1,runtimeStyle:1,filters:1,style:1,behaviorUrns:1,all:1,offsetParent:1,children:1,ownerDocument:1,childNodes:1,attributes:1};
//                for(var x in elt)try{if(!map[x]&&elt[x]&&typeof(elt[x])==='object')alert(x)}catch(e){};
//            }
        }
        if(elt.parentNode)elt.parentNode.removeChild(elt);
    }
    
    function clearList(elt,list,cust){
        if(!elt||!list)return;
        for(var i=0;i<list.length;i++){
            if(!elt[list[i]])continue;
            if(elt[list[i]].nodeType)removeElement(elt[list[i]]);
            elt[list[i]]=null;
            elt.removeAttribute(list[i]);
        }
    }

    function removeAttributes(elt,attributes){
        if(!elt||!elt.removeAttribute)return;
        if(typeof(attributes)!=Array)attributes=[attributes];
        for(var i=0;i<attributes.length;i++)elt.removeAttribute(attributes[i]);
    }
    
    function removeClass(elt,className){
        if(!elt||!hasClass(elt,className))return;
        var newClass=elt.className.replace(new RegExp(['\\s?\\b','\\b'].join(className),'g'),'');
        elt.className=newClass;
        if(!newClass)elt.removeAttribute("class");
    }
    
    function scrollToElement(elt,top){
        if(!elt||!elt.scrollIntoView)return;
        elt.scrollIntoView(!!top);
    }
    
    function setAttributes(elt,attributes,nameSpace){
        if(!elt)return;
        if(nameSpace&&elt.setAttributeNS){
            for(var x in attributes)elt.setAttributeNS(nameSpace||null,x,attributes[x]);
        }else{
            for(var x in attributes)elt.setAttribute(x,attributes[x]);
        }
    }
    
    function selectAncestor(base,matches){
        if(!base||!matches)return null;
        if(matches.constructor!=Array)matches=[matches];
        var elt=base;
        while(elt=elt.parentNode){
            for(var i=0;i<matches.length;i++){
                if(Object.Matches(elt,matches[i]))return elt;
            }
        }
        return null;
    }
    
    function selectSingleNode(base,matches,ancestor){
        if(ancestor)return selectAncestor(base,matches);
        var ret=selectNodes(base,matches,1);
        if(!ret)return null;
        for(var i=0;i<ret.length;i++){
            if(ret[i].constructor==Array){
                if(ret[i].length)return ret[i][0];
            }
            return ret[i];
        }
    }
    
    function selectNodes(base,matches,limit){
        if(!matches)return [];
        var multiMatch=(matches.constructor==Array);
        if(!multiMatch)matches=[matches];
        var matchCount=0;
        return findMatches(base||document.body,[])||[];
        function findMatches(elt,ret){
            if(elt&&elt.childNodes){
                for(var i=0;i<elt.childNodes.length;i++){
                    for(var j=0;j<matches.length;j++){
                        if(Object.Matches(elt.childNodes[i],matches[j])){
                            if(!ret[j])ret[j]=[];
                            ret[j].push(elt.childNodes[i]);
                            if(++matchCount==limit)break;
                        }
                    }
                    if(matchCount==limit)break;
                    findMatches(elt.childNodes[i],ret);
                }
            }
            return multiMatch?ret:ret[0];
        }
    }
    
    function setContent(elt,contents){
        if(!elt||!elt.nodeType)return;
        clearContent(elt);
        elt.innerHTML=contents;
    }
    
    function showElement(elt,auto){
        if(!elt)return;
        elt.style.display=auto?'':'block';
    }


}

