
var undefined;

function Notepad(id) {
  this.id = undefined;
  this.noteList = [];
  this.lang = "en";
  this.noteListName = 'myNotepad';
  this.urlprefix = '';
  this._setId(id);
  this._loadNoteList();
}

//**************************************************************************

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

//**************************************************************************

Notepad.prototype._loadNoteList = function() {
  if (! (Tools && Tools.setCookieValue)) {
    focus();
    throw new Error("Notepad->loadNoteList: Bitte das Modul Tools laden!");
  }
  var name = this.noteListName;  
  var val = Tools.getCookieValue(name);
  //alert(val);
  if (! val.length) {
    return;
  }
  var arr = val.split(/\,/);
  var list = [];
  for (var i=0; i<arr.length; i++) {
    var peer = arr[i].split(/\:/);
    var item = NotepadItem.createInstance();
    item.setContent(peer[0]);  
    var d = peer[1];  
    if (! d) {
      d = Notepad._createSetDate();
    }
    item.setSetDate(d);
    list.push(item);
  }
  this.noteList = list;
  return true;
}                       

//**************************************************************************

Notepad.prototype._noteListToDBString = function() {
  var str = '';
  for (var i=0; i<this.noteList.length; i++) {
    var item = this.noteList[i];
    var cont = item.content;
    var setDate = item.setDate;
    str += cont + ':' + setDate;
    if (i < this.noteList.length -1) {
     str += ',';
    }
  }
  return str;
}                      

//**************************************************************************

Notepad.prototype._createSetDate = function() {
  var date = new Date();
  var y = date.getFullYear();
  var m = date.getMonth();
  m++;
  if (m < 10) {
    m = '0' + m;
  }
  var d = date.getDate();
  if (d < 10) {
    d = '0' + d;
  }
  var str = y + '' + m + '' + d;
  return str;
}                                 

//**************************************************************************

Notepad.prototype._noteListHasValue = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Notepad->_noteListHasValue: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Notepad->_noteListHasValue: Argument str ist nicht vom Typ String!");
  }
  for (var i=0; i<this.noteList.length; i++) {
    var item = this.noteList[i];
    if (str != item.content) {
      continue;
    }
    return true;
  }
  return false;
}                   

//**************************************************************************

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

//**************************************************************************

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

//**************************************************************************

Notepad.prototype.addNoteList = function(str) {
  if (arguments.length < 1) {
    focus();
    throw new Error("Notepad->addNoteList: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Notepad->addNoteList: Argument str ist nicht vom Typ String!");
  }
  if (! str.length) {
    return;
  }
  if (this._noteListHasValue(str)) {
    Notepad.displayAlert('list_has_value');
    return;
  }
  var item = NotepadItem.createInstance();
  item.setContent(str);                                              
  item.setSetDate(Notepad._createSetDate());
  this.noteList.push(item);
  this.saveNoteList();
  this.setPreviewPages();
  Notepad.displayAlert('has_added');
}                                   

//**************************************************************************

Notepad.prototype.deleteNoteList = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Notepad->deleteNoteList: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Notepad->deleteNoteList: Argument str ist nicht vom Typ String!");
  }
  if (! str.length) {
    return;
  }
  var idx;
  for (var i=0; i<this.noteList.length; i++) {
    var item = this.noteList[i];    
    if (item.content  != str) {
      continue;
    }    
    var del = this.noteList.splice(i, (i+1));
    delete del;
    break;
  }
  this.saveNoteList();
  this.setPreviewPages();
  this.showNotelist();
}                                     

//**************************************************************************

Notepad.prototype.showNotelist = function() {
  if (! this.urlprefix) {
    return;
  }
  var url = this.urlprefix;
  var templ = 'notelist_de';
  if (this.lang == 'en') {
    templ = 'notelist_en';
  }
  if (this.lang == 'fr') {
    templ = 'notelist_fr';
  }
  if (this.lang == 'it') {
    templ = 'notelist_it';
  }
  url += '&_sprache=' + templ;
  url += '&_sortierung=titel_asc';
  if (! this.noteList.length) {
    url += '&_status=neu';
  }
  for (var i=0;i<this.noteList.length;i++) {
    var item = this.noteList[i];
    url += '&idartikel=';
    url += item.content;
  }
  window.location = url;
}                             

//**************************************************************************

Notepad.prototype.setPreviewPages = function() {
  var elem = document.getElementById('__NOTELIST_PREVIEW_PAGES__');
  if (! elem) {
    return;
  }
  var post_fix = 'Seiten';
  if (this.lang != 'de') {
    post_fix = 'pages';
  }
  elem.innerHTML = this.noteList.length + ' ' + post_fix;
}              

//**************************************************************************

Notepad.prototype.saveNoteList = function() {
  if (! (Tools && Tools.setCookieValue)) {
    focus();
    throw new Error("Notepad->saveNoteList: Bitte das Modul Tools laden!");
  }
  var name = this.noteListName;  
  var val = this._noteListToDBString(); 
  var date = new Date();
  var days365 = (1000 * 60 * 60 * 24 * 365);
  date = new Date(new Date().setTime(date.getTime() + days365));
  //alert(date);
  // mit Datum in einem Jahr
  Tools.setCookieValue(val, name, date);
} 

//**************************************************************************

Notepad._increment = [];
Notepad._registerInstance = {};
Notepad._registerInstanceLength = [];         

//**************************************************************************

Notepad._createSetDate = function() {
  var date = new Date();
  var y = date.getFullYear();
  var m = date.getMonth();
  m++;
  if (m < 10) {
    m = '0' + m;
  }
  var d = date.getDate();
  if (d < 10) {
    d = '0' + d;
  }
  var str = y + '' + m + '' + d;
  return str;
}     

//**************************************************************************
        
Notepad.getLangTranslation = function(str, lang){
  if (arguments.length < 1) {
    focus();
    throw new Error("ShoppingBasket->getLangTranslation: Falsche Anzahl von Argumenten!");
  }  
  if (! lang) {
    lang = 'en';
  }
  var item = {};
  item['en'] = {};
  item['fr'] = {};
  item['de'] = {};
  item['it'] = {};                       
  
  item['en']['list_has_value'] = 'You added this article to your Notelist before.';
  item['fr']['list_has_value'] = 'You added this article to your Notelist before.';    
  item['de']['list_has_value'] = 'Dieser Artikel befindet sich bereits auf Ihrem Merkzettel';
  item['it']['list_has_value'] = 'You added this article to your Notelist before.';                          
  
  item['en']['has_added'] = 'The article has been added to your Notelist.';
  item['fr']['has_added'] = 'The article has been added to your Notelist.';    
  item['de']['has_added'] = 'Dieser Artikel befindet sich bereits auf Ihrem Merkzettel';
  item['it']['has_added'] = 'The article has been added to your Notelist.';                        
  
  item['en']['system_alert_button'] = 'Close';
  item['fr']['system_alert_button'] = 'Close';    
  item['de']['system_alert_button'] = 'Hinweis schließen';
  item['it']['system_alert_button'] = 'Close';                                       
  
  item['en']['item_deleted'] = 'The Article has been deleted from your Notelist';
  item['fr']['item_deleted'] = 'The Article has been deleted from your Notelist';    
  item['de']['item_deleted'] = 'Der Artikel wurde von Ihrem Merkzettel entfernt.';
  item['it']['item_deleted'] = 'The Article has been deleted from your Notelist';   
  
  if (! (item[lang] && item[lang][str])) {
    return '';
  }
  return item[lang][str];
}   

//**************************************************************************

Notepad.displayAlert = function(err){
  var err_str = 'Die Aktion wurd durchgeführt ';
  var txt = Notepad.getLangTranslation(err, this.lang);
  if (txt.length) {
    err_str = txt;
  } else if (err.length) {
    err_str = err;
  }
  var elem = document.getElementById('__GLOBAL_SYSTEM_ALERT_ELEMENT__');
  if (! elem) {
    elem = document.createElement('div');
    elem.setAttribute('id','__GLOBAL_SYSTEM_ALERT_ELEMENT__');
    if (IE6 || IE7) {
      elem.setAttribute('className','gloabl-system-alert');
    } else {
      elem.setAttribute('class','gloabl-system-alert');
    }
    elem.setAttribute('style','visibility: hidden;');
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(elem);
  }
  elem.style.display = 'block';
  var html = '<div class="gloabl-system-alert-inner">';
  html += '<div class="gloabl-system-alert-inner-text">';
  html += err_str;
  html += '<div class="float-aufheben"><br /></div>';
  html += '</div>';
  html += '<div class="gloabl-system-alert-inner-button">';
  html += '<a href="javascript: void(0);" onclick="Notepad.hideAlert();">';
  html += Notepad.getLangTranslation('system_alert_button', this.lang);
  html += '</a>';
  html += '<div class="float-aufheben"><br /></div>';
  html += '</div>';
  html += '<div class="float-aufheben"><br /></div>';
  html += '</div>';
  elem.innerHTML = html;  
  var top = 0;
  top = Tools.getScreenHeight();
  top = top - Tools.getElementHeight(elem);
  top = top/2;
  top += Tools.getScrollPositionTop();
  top - 100;
  if (navigator.userAgent.toString().match(/Opera/)) {
      top -= 150;
  }
  elem.style.top = top + 'px';
  var left = parseFloat(1095 / 2);
  left -= parseFloat(Tools.getElementWidth(elem)/2);
  elem.style.left = left + 'px';
  var h = Tools.getElementHeight(elem);
  var w = Tools.getElementWidth(elem);
  elem.style.width = '1px';
  elem.style.height = '1px';
  elem.style.visibility = 'visible';
  Notepad.slideAlert(elem , w, h);
}        

//**************************************************************************

Notepad.slideAlert = function(elem, w, h){
  var fast = 0;
  var diff_h = 20;
  var diff_w = 20;
  if (Notepad.timeout) {
    window.clearTimeout(Notepad.timeout);
  }
  if (! elem) {
    return;
  }
  var eH = Tools.getElementHeight(elem);
  var eW = Tools.getElementWidth(elem);
  if (eH >= h && eW >= w) {
    return;
  }   
  if (eH < h) {
    if (h - eH < diff_h) {
      diff_h = parseFloat(h - eH);
    }
    elem.style.height = parseFloat(parseFloat(elem.style.height) + diff_h) + 'px';
  }      
  if (eW < w) {
    if (w - eW < diff_w) {
      diff_w = parseFloat(w - eW);
    }
    elem.style.width = parseFloat(parseFloat(elem.style.width) + diff_w) + 'px';
  }
  var func = function () {
    Notepad.slideAlert(elem, w, h);
  }
  Notepad.timeout = window.setTimeout(func, fast);
}  

//**************************************************************************

Notepad.hideAlert = function(){
  var elem = document.getElementById('__GLOBAL_SYSTEM_ALERT_ELEMENT__');
  if (! elem) {
    return;
  }
  elem.style.display = 'none';
}        

//**************************************************************************

Notepad.getSetDate = function(str) {  
  var date = '';
  var item = NotepadItem.getInstanceByContent(str);
  if (item) {
    date = item.setDate;
    date = date.substring(6,8) + '.' +date.substring(4,6)+ '.' + date.substring(0,4);
  }
  return date; 
}

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

//**************************************************************************

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

//**************************************************************************
//**************************************************************************
//**************************************************************************
//**************************************************************************
//**************************************************************************

function NotepadItem(id) {
  this.id = undefined;
  this.content = '';
  this.setDate = '';
  this._setId(id);
}

//**************************************************************************

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

//**************************************************************************

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

//**************************************************************************

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

//**************************************************************************

NotepadItem._increment = [];
NotepadItem._registerInstance = {};
NotepadItem._registerInstanceLength = [];  

//**************************************************************************

NotepadItem.getInstanceByContent = function(str) {
  if (arguments.length!=1) {
    throw new Error("NotepadItem.getInstanceByContent: Falsche Anzahl von Argumenten!");
  }    
  for (var i in NotepadItem._registerInstance) {
    if (NotepadItem._registerInstance[i].content != str) {
      continue;
    }
    return NotepadItem._registerInstance[i];
  }
  return undefined;
}  

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

//**************************************************************************

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

//**************************************************************************
