function tabbies(container_id, distinct_class)
{
  this.containingElement = null;
  
  this.bookmarkClass = 'tabs_bm';
  
  this.tabClass = 'tab';
  
  this.activeBmClass = 'vybrano';
  
  this.distinctClass = distinct_class;

  this.bmElements = new Array();
  this.bookmarks = new Array();
  
  this.tabElements = new Array();
  this.tabs = new Array();
  
  tabbies.prototype.initialize = function() {
    if(container_id == undefined) {
      this.containingElement = document;
    } else {
      this.containingElement = document.getElementById(container_id);
    }
    this.bmElements = this.getDescendantsByClass(this.bookmarkClass, this.distinctClass);
    
    this.tabElements = this.getDescendantsByClass(this.tabClass, this.distinctClass);

    this.initializeBookmarks();
    this.switchTo(0);  
  }
  
  tabbies.prototype.getDescendantsByClass = function(class_name, other_class_name) {
    if(this.containingElement == null) return false;
    var elements = [];
    var reg = new RegExp("(^|\\s)" + class_name + "(\\s|$)");
    var all = this.containingElement.getElementsByTagName('*');
    if(other_class_name != undefined) {
      var reg2 = new RegExp("(^|\\s)" + other_class_name + "(\\s|$)");
      for (var i = 0; i < all.length; i++) {
        if (reg.test(all[i].className) && reg2.test(all[i].className)) { elements.push(all[i]); }
      }
    } else {
      for (var i = 0; i < all.length; i++) {
        if (reg.test(all[i].className)) elements.push(all[i]);
      }
    }
    return elements;
  }
  
  tabbies.prototype.initializeBookmarks = function() { 
    for (var i = 0; i < this.bmElements.length; i++) {
      this.addEventListener(this.bmElements[i], "click", this.createSwitchCallback(i), this);
      this.disableSelection(this.bmElements[i]);
    }
  }
  
  tabbies.prototype.createSwitchCallback = function(order) {
    var tabbiesObj = this;
    return function () {
      tabbiesObj.switchTo(order);
    };
  }
  
  tabbies.prototype.switchTo = function(order) {
    if(!this.bmElements.length) return false;
    for (var i = 0; i < this.tabElements.length; i++) {
      if(i == order) {
        this.tabElements[i].style.display = '';
      } else {
        this.tabElements[i].style.display = 'none';
      }
    }
    for (var i = 0; i < this.bmElements.length; i++) {
      this.removeClassName(this.bmElements[i], this.activeBmClass);
    }
    this.addClassName(this.bmElements[order], this.activeBmClass);
  }
  
  tabbies.prototype.addEventListener = function(obj,evt,fnc,scope,useCapture) {
    if (!useCapture) useCapture=false;
    var scoped = scope ? function(e) { fnc.apply(scope, [e]); } : fnc;
  	if (obj.addEventListener){ 
  		obj.addEventListener(evt,scoped,useCapture);
  		return true;
  	} else if (obj.attachEvent) {
      return obj.attachEvent("on"+evt,scoped);
    }
  }
  
  tabbies.prototype.hasClassName = function(element, class_name) {
    return element.className.match(new RegExp('(\\s|^)'+class_name+'(\\s|$)'));
  }
  
  tabbies.prototype.addClassName = function(element, class_name) {
    if(!element.className) {
      element.className = class_name;
    } else if(!this.hasClassName(element, class_name)) {
      element.className = element.className + " " + class_name;
    }
  }
  
  tabbies.prototype.removeClassName = function(element, class_name) {
  	if (this.hasClassName(element, class_name)) {
  		var reg = new RegExp('(\\s|^)'+class_name+'(\\s|$)');
  		element.className = element.className.replace(reg, ' ');
  	}
  }
  
  tabbies.prototype.disableSelection = function(target){
    if(typeof target.onselectstart!="undefined") target.onselectstart = function() { return false; } //IE route
    else if(typeof target.style.MozUserSelect!="undefined") target.style.MozUserSelect="none" //Firefox route
    else target.onmousedown = function() { return false; } //All other route (ie: Opera)
  }
  
  this.initialize();
}