//myDom framework @ Silantiev Alexandr (www.shaolin.ru)
(function() {
  function _$(els) {
    this.elements = [];
    for (var i=0; i<els.length; i++) {
      var element = els[i];
      if (typeof element == 'string') element = document.getElementById(element);
      if (element === null) continue;
      this.elements.push(element);
    }
    return this;
  }

  _$.prototype = {
    each: function(fn) {
      for ( var i = 0, len = this.elements.length; i<len; ++i ) fn.call(this, this.elements[i]);
      return this;
    },
    event: function(type, fn) {
      var listen = function(el) {
        if (window.addEventListener) el.addEventListener(type, fn, false);
        else if (window.attachEvent) el.attachEvent('on'+type, function() { fn.call(el, window.event); });
      };
      this.each(function(el) { listen(el); });
      return this;
    },
    eventoff: function(type, fn) {
      var listen = function(el) {
        if (window.removeEventListener) el.removeEventListener(type, fn, false);
        else if (window.detachEvent) el.detachEvent('on'+type, function() { fn.call(el, window.event); });
      };
      this.each(function(el) { listen(el); });
      return this;
    },
    setCss: function(o) {
      var that = this;
      this.each(function(el) {
        for (var prop in o) {
          that.setStyle(prop, o[prop]);
        }
      });
      return this;
    },
    setStyle: function(prop, val) {
      this.each(function(el) {
        el.style[prop] = val;
      });
      return this;
    },
    removeStyle: function(prop) {
      this.each(function(el) {
        el.style[prop] = '';
      });
      return this;
    },
    addClass: function(className) {
      this.each(function(el) {
        el.className += (el.className.length ? " " : "") + className;
      });
      return this;
	},
	replClass: function(className) {
      this.each(function(el) {
        el.className = className;
      });
      return this;
	},
	removeClass: function(className) {
	  var pattern = new RegExp(('(^|\\s)' + className + '(\\s|$)'), 'i');
      this.each(function(el) {
        el.className = el.className.replace(pattern, '');
      });
      return this;
	},
	toggleClass: function(className) {
	  var that = this;
	  this.each(function(el) {
		that.hasClass(className) ? that.removeClass(className) : that.addClass(className);
	  });
      return this;
    },
	addHTML: function(html) {
      this.each(function(el) {
        el.innerHTML += html;
      });
      return this;
    },
	replHTML: function(html) {
      this.each(function(el) {
        el.innerHTML = html;
      });
      return this;
    },
	setDisplay: function(flag) {
	  var flag = flag || 'on'; 
	  this.each(function(el) {
        if ( flag == 'off' ) el.style.display = 'none';
        else el.style.display = 'block';
	  });
      return this;
    },
	setVisible: function(flag) {
	  var flag = flag || 'on';
	  this.each(function(el) {
        if ( flag == 'off' ) el.style.visibility = 'hidden';
		else el.style.visibility = 'visible';
	  });
      return this;
    },
    toggleDisplay: function() {
	  this.each(function(el) {
        if ( el.style.display != 'none' ) el.style.display = 'none';
        else el.style.display = 'block';
	  });
      return this;
    },	
    toggleVisible: function() {
	  this.each(function(el) {
        if ( el.style.visibility != 'hidden' ) el.style.visibility = 'hidden';
        else el.style.visibility = 'visible';
	  });
      return this;
    }
  };
  window.$ = function() {
    return new _$(arguments);
  }
})();

