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

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

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

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

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

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

ArticleList.prototype.setArticleListItems = function(str){
  if (arguments.length != 1) {
    focus();
    throw new Error('ArticleList->setArticleListItems: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {
    focus();
    throw new Error('ArticleList->setArticleListItems: 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 article = ArticleListItem.createInstance();
    if (col[0] && col[0].length) {
      article.setTitle(col[0]);   
    }
    if (col[1] && col[1].length) {                                  
      article.setURL(col[1]);  
    }   
    if (col[2] && col[2].length) {                   
      article.setCmsID(col[2]);  
    }
    if (col[3] && col[3].length) {                         
      article.setTarget(col[3]); 
    }
    this.addArticleListItem(article);
  }
  this.drawKeywords();
}

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

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

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

ArticleList.prototype.drawKeywords = function () {
  var elem = document.getElementById(this.drawContainerIdx);
  if (! elem) {
    focus();
    throw new Error('ArticleList->drawKeywords: Es existiert keine HTML-Element mit id='+ this.drawContainerIdx +'!');
    return;
  }
  var html = '';
  html += '<ul>';
  for (var i=0; i<this.article.length; i++) {
      html += '<li>';
      var article = this.article[i];
      var target = '_self';
      if (article.target) {
        target = article.target;
      }
      var url = 'javascript:void(0);';
      if (article.url)   {
        url = article.url;
      } else if (article.cmsID) {
        url = ArticleList.cmsURLPrefix + '' + article.cmsID;
      }
      html += '<a href="'+ url  +'" target="'+ target +'">'+ article.title  +'</a> \n';
      html += '</li>';
  }
  html += '</ul>';
  elem.innerHTML = html;
}

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

ArticleList.cmsURLPrefix = '';
ArticleList._increment = [];
ArticleList._autoIdxPrefix = '_ArticleList_';
ArticleList._registerInstance = {};
ArticleList._registerInstanceLength = [];

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

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

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

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

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

function ArticleListItem(idx) {
  this.id = undefined;
  this.title = undefined;
  this.url = undefined;
  this.cmsID = undefined;
  this.target = '_self';
  this._setID(idx);
}

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

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

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

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

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

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

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

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

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

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

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

ArticleListItem._increment = [];
ArticleListItem._autoIdxPrefix = '_ArticleListItem_';
ArticleListItem._registerInstance = {};
ArticleListItem._registerInstanceLength = [];

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

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

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

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

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

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