(function() {
  function _$(els) {
    this.elements = [];
    for (var i = 0, len = els.length; i < len; ++i) {
      var element = els[i];
      if (typeof element == 'string') {
        element = document.getElementById(element);
		if (element == null) {
		  element = document.createElement(els[i]);
		}
      }
      this.elements.push(element);
    }
  }
  _$.prototype = {
    each: function(fn) {
      for ( var i = 0, len = this.elements.length; i < len; ++i ) {
        fn.call(this, this.elements[i]);
      }
      return this;
    },
    setStyle: function(prop, val) {
      this.each(function(el) {
        el.style[prop] = val;
      });
      return this;
    },
    show: function() {
      var that = this;
      this.each(function(el) {
        that.setStyle('display', 'block');
      });
      return this;
    },
    hide: function() {
      var that = this;
      this.each(function(el) {
        that.setStyle('display', 'none');
      });
      return this;
    },

    addEvent: function(type, fn) {
      var add = function(el) {
        if (window.addEventListener) {
          el.addEventListener(type, fn, false);
        } 
        else if (window.attachEvent) {
          el.attachEvent('on'+type, fn);
        }
      };
      this.each(function(el) {
        add(el);
      });
      return this;
    },
	
	appendChild: function(child) {
	    this.elements[0].appendChild(child.elements[0]);
		return this;
	},
	removeChild: function(child) {
	    this.elements[0].removeChild(child.elements[0]);
		return this;
	},
    lastChild: function() {
	    return $(this.elements[0].lastChild);
	},
	
	setInnerHtml: function(text) {
	  this.each(function(el) {
	    el.innerHTML = text;
	  });
	  return this;
	},
	
	value: function() {
	  return this.elements[0].value;
	},
	
	setValue: function(text) {
	  this.elements[0].value = text;
	  return this;
	}
  };
  window.$ = function() {
    return new _$(arguments);
  };

  window.addEvent = function(el, type, fn) {
    if (window.addEventListener) {
      el.addEventListener(type, fn, false);
    } else if (window.attachEvent) {
      el.attachEvent('on'+type, fn);
    }
  };

})();

