var DOMHelper = function() {
}

DOMHelper.prototype = {
    'getTitle':function() {
        return document.title;
    },
    'getBody':function() {
        return document.getElementsByTagName('body')[0];
    },
    'replaceText':function(element, text) {
        if( typeof(element) != null ) {
            element.innerHTML = text;
        }
    },
    '$':function($id) {
        return document.getElementById($id);
    },
    'append':function(element, child) {
        element.appendChild(child);
    },
    'remove':function($element, $child) {
        $element.removeChild($child);
    },
    'create':function(strElement) {
        var element = null;
        var tagMatches = strElement.match(/<([^< ]+)([^<]*)\/?>/);
        if( tagMatches.length > 1 ) {
            var tagName = tagMatches[1];
            var attributes = this.getAttributesFromString( tagMatches[2] );
            var text = this.getTextNodeFromElementString(strElement);
            var element = document.createElement(tagName);
            for(attributeName in attributes) {
                element.setAttribute(attributeName, attributes[attributeName]);
            }
            if(text) {
                if (tagName != 'input') {
                    this.append(element,text);
                }
            }
        }
        return element;
    },
    'getAttributesFromString':function(strAttributes) {
        //Clear slashes of self-closing tags and left/right trim.
        var attributeMatches = strAttributes.replace(/[\/]+/ig, '').replace(/^\s+|\s+$/g, '');
        var attributeMatches = attributeMatches.match(/[^= ]+="[^"]+"/ig);
        var attributes = {};
        if( attributeMatches ) {
            for( var i=0; i < attributeMatches.length; i++ ) {
                var attribute = attributeMatches[i].split('=');
                attributes[attribute[0]] = attribute[1].replace(/"/ig, ''); 
            }
        }
        return attributes;
    },
    'getTextNodeFromElementString':function( $strElement ) {
        var $textNode = null;
        var $textMatches = $strElement.match(/>([^\<]+)</i);
        if( $textMatches ) {
            if(   $textMatches.length > 1 ) {
                $textNode = document.createTextNode($textMatches[1]);
            } 
        }
        return $textNode;
    },
    'addEvent':function( $element, $type, $callback, $useCapture ) {
        // Cross browser event handling by Scott Andrew
        if( $element.addEventListener ) {		
            $element.addEventListener( $type, $callback, $useCapture );
        } else if ( $element.attachEvent ) {
            var $r = $element.attachEvent('on'+$type, $callback);
            return $r;
        } else {
            alert($element);	
            $element['on'+$type] = $callback;
        }
    },
    'hide':function( $element ) {
        $classes = $element.getAttribute('class');
        $classes = $classes ? $classes + ' hide' : 'hide';  
        $element.setAttribute('class', $classes);
    },
    'preventDefault':function(event) {
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }
}
