
function Tagcloud(idx) {
  this.id = undefined;
  this.keywords = [];
  this.drawContainerIdx = undefined;
  this.saveElemIdx = undefined;
  this.mode = 'edit'; //edit,detial
  
  this._setID(idx);
}

// #################################

Tagcloud.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('Tagcloud->_setID: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('Tagcloud->_setID: Argument str ist nicht vom Typ String!');
  }
  this.id = str;
}

// #################################

Tagcloud.prototype.setDrawContainerIdx = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('Tagcloud->_setDrawContainer: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('Tagcloud->_setDrawContainer: Argument str ist nicht vom Typ String!');
  }
  this.drawContainerIdx = str;
}

// #################################

Tagcloud.prototype.setKeywords = function(str){
  if (arguments.length != 1) {
    focus();
    throw new Error('Tagcloud->setKeywords: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('Tagcloud->setKeywords: Argument str ist nicht vom Typ String!');
  }
  str = str.toString();
  if (! str.length) {
    return;
  }
  str = str.replace(/^\[/,'');
  str = str.replace(/\]$/,'');
  var row = str.split(/\]\[/gi);
  for (var i=0; i<row.length; i++) {
    var col = row[i].split(/\,/gi);
    if (! col.length) {
      continue;
    }
    var key = TagcloudKeyword.createInstance();
    if (col[0] && col[0].length) {
      key.setKeyword(col[0]);   
    }
    if (col[1] && col[1].length) {
      key.setSize(col[1]);  
    }
    if (col[2] && col[2].length) {                                  
      key.setURL(col[2]);  
    }   
    if (col[3] && col[3].length) {  
      key.setCmsID(col[3]);  
    }
    if (col[4] && col[4].length) {                         
      key.setTarget(col[4]); 
    }
    this.addKeyword(key);
  }
  this.drawKeywords();
}

// #################################

Tagcloud.prototype.addKeyword = function(obj){
  if (arguments.length != 1) {
    focus();
    throw new Error('Tagcloud->addKeyword: Falsche Anzahl von Argumenten!');
  }
  if (! (obj instanceof TagcloudKeyword)) {
    focus();
    throw new Error('Tagcloud->addKeyword: Argument obj ist keine Instance von TagcloudKeyword!');
  }
  this.keywords.push(obj);
}

// #################################

Tagcloud.prototype.drawKeywords = function () {
  var elem = document.getElementById(this.drawContainerIdx);
  if (! elem) {
    focus();
    throw new Error('Tagcloud->drawKeywords: Es existiert keine HTML-Element mit id='+ this.drawContainerIdx +'!');
    return;
  }
  var html = '';
  for (var i=0; i<this.keywords.length; i++) {
      var key = this.keywords[i];
      var target = '_self';
      if (key.target) {
        target = key.target;
      }
      var url = 'javascript:void(0);';
      if (key.url)   {
        url = key.url;
      } else if (key.cmsID) {
        url = Tagcloud.cmsURLPrefix + '' + key.cmsID;
      }
      var size = '3';
      if (key.size) {
        size = key.size;
      }
      html += '<a href="'+ url  +'" target="'+ target +'" class="tag-cloud-link"><span class="tag-cloud-'+ size  +'">'+ key.keyword  +'</span></a> \n';
  }
  elem.innerHTML = html;
}

// #################################

Tagcloud.cmsURLPrefix = '';
Tagcloud._increment = [];
Tagcloud._autoIdxPrefix = '_Tagcloud_';
Tagcloud._registerInstance = {};
Tagcloud._registerInstanceLength = [];

// #################################

Tagcloud.getInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error('Tagcloud.getInstance: Falsche Anzahl von Argumenten!');
  }
  if (! (Tagcloud._registerInstance[id])){
    focus();
    throw new Error('Tagcloud.getInstance: Es ist keine Tagcloud.Instance mit id=' + id + ' registriert!');
  }
  return Tagcloud._registerInstance[id];
}

// #################################

Tagcloud.createInstance = function(id) {
  if (!arguments.length) {
    id = Tagcloud._autoIdxPrefix + Tagcloud._increment.length;
    Tagcloud._increment.push(1);
  }
  if (! (Tagcloud._registerInstance[id])){
    Tagcloud._registerInstance[id] = new Tagcloud(id);
    Tagcloud._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error('Tagcloud.createInstance: ID schon vorhanden!');
  }
  return Tagcloud.getInstance(id);
}

// #################################
// #################################
// #################################

function TagcloudKeyword(idx) {
  this.id = undefined;
  this.keyword = undefined;
  this.url = undefined;
  this.cmsID = undefined;
  this.target = '_self';
  this.size = '3';  
  this._setID(idx);
}

// #################################

TagcloudKeyword.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->_setID: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->_setID: Argument str ist nicht vom Typ String!');
  }
  this.id = str;
}

// #################################

TagcloudKeyword.prototype.setKeyword = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->setKeyword: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->setKeyword: Argument str ist nicht vom Typ String!');
  }
  this.keyword = str;
}

// #################################

TagcloudKeyword.prototype.setSize = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->setSize: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->setSize: Argument str ist nicht vom Typ String!');
  }
  this.size = str;
}

// #################################

TagcloudKeyword.prototype.setCmsID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->setCmsID: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->setCmsID: Argument str ist nicht vom Typ String!');
  }
  this.cmsID = str;
}

// #################################

TagcloudKeyword.prototype.setURL = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->setURL: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->setURL: Argument str ist nicht vom Typ String!');
  }
  this.url = str;
}

// #################################

TagcloudKeyword.prototype.setTarget = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('TagcloudKeyword->setTarget: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('TagcloudKeyword->setTarget: Argument str ist nicht vom Typ String!');
  }
  this.target = str;
}

// #################################

TagcloudKeyword._increment = [];
TagcloudKeyword._autoIdxPrefix = '_TagcloudKeyword_';
TagcloudKeyword._registerInstance = {};
TagcloudKeyword._registerInstanceLength = [];

// #################################

TagcloudKeyword.deleteInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error('TagcloudKeyword.deleteInstance: Falsche Anzahl von Argumenten!');
  }
  if (! (TagcloudKeyword._registerInstance[id])){
    focus();
    throw new Error('TagcloudKeyword.deleteInstance: Es ist keine TagcloudKeyword.Instance mit id=' + id + ' registriert!');
  }
  delete TagcloudKeyword._registerInstance[id];
}

// #################################

TagcloudKeyword.getInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error('TagcloudKeyword.getInstance: Falsche Anzahl von Argumenten!');
  }
  if (! (TagcloudKeyword._registerInstance[id])){
    focus();
    throw new Error('TagcloudKeyword.getInstance: Es ist keine TagcloudKeyword.Instance mit id=' + id + ' registriert!');
  }
  return TagcloudKeyword._registerInstance[id];
}

// #################################

TagcloudKeyword.createInstance = function(id) {
  if (!arguments.length) {
    id = TagcloudKeyword._autoIdxPrefix + TagcloudKeyword._increment.length;
    TagcloudKeyword._increment.push(1);
  }
  if (! (TagcloudKeyword._registerInstance[id])){
    TagcloudKeyword._registerInstance[id] = new TagcloudKeyword(id);
    TagcloudKeyword._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error('TagcloudKeyword.createInstance: ID schon vorhanden!');
  }
  return TagcloudKeyword.getInstance(id);
}
