var TabNavigation = Class.create();
TabNavigation.prototype = {
  tabbox: null,
  tablist: null,
  initialize: function( tabBoxId, tabIdList )
  {
    this.tabbox = $(tabBoxId);
    this.tablist = tabIdList;
  },
  open: function( idToOpen, tabNode ) {
    //erst Anzeige des aktiven Reiters wechseln bevor geprüft wird ob die Box zur Anzeige da ist
    //Grund: wenn Benutzer geblocked, nicht email-verifiziert,etc. ist soll es trotzdem so aussehen als würde der Klick funktionieren
    this.tabbox.cleanWhitespace();
    $A(this.tabbox.childNodes).each(function( li ){
      $(li).removeClassName('tab-active');
    });
    if($(tabNode)) {
      $(tabNode.parentNode).addClassName('tab-active');
      tabNode.blur();
    }

    if(!$(idToOpen)) {
      return;
    }
    $A(this.tablist).each(function( tabbox ){
      if(!$(tabbox)) {
        return;
      }
      $(tabbox).removeClassName('tab-active');
    });
    $(idToOpen).addClassName('tab-active');
    if(typeof $(idToOpen).onopen == 'function') {
      $(idToOpen).onopen();
    }

    if (typeof urchinTracker == 'function') {
      urchinTracker('/tab/'+idToOpen+'/');
    }

    (this.specificCounter || Prototype.emptyFunction)();

    $A(document.getElementsByTagName('iframe')).each(function(item){
      if(! $(item).hasClassName('advertisement')){
        return;
      }
      if(! /\?/.test(item.src)){
        item.src += '?tabreload=1';
        return;
      }
      if(! /tabreload=/.test(item.src)){
        item.src += '&tabreload=1';
        return;
      }
      item.src = item.src.replace(/tabreload=[0-9]+/,'tabreload=' + (new Date).getTime());
    });
  }
}


var PageNavigation = {};
PageNavigation.Base = function() {};
PageNavigation.Base.prototype = {
  pagebox: null,
  pagelist: null,
  activePage: false
}

PageNavigation.Default = Class.create();
PageNavigation.Default.prototype = Object.extend(new PageNavigation.Base(), {
  initialize: function( pageBoxId, pageIdList )
  {
    this.pagebox = $(pageBoxId);
    this.pagebox.cleanWhitespace();
    this.pagelist = pageIdList;
    $A(this.pagebox.childNodes).each((function( li ){
      if($(li).hasClassName('page-active')) {
        this.activePage = $(li);
      }
    }).bind(this));
    if( ! this.activePage ) {
      this.activePage = this.pagebox.firstChild.nextSibling;
    }
  },
  next: function(){
    if(this.activePage.nextSibling == this.pagebox.lastChild) {
      return;
    }
    var opener = $(new Function ('', this.activePage.nextSibling.firstChild.getAttribute('onclick')));
    // para 2: das a innerhalb des lis
    this.open(
    this.activePage.nextSibling.firstChild.getAttribute('rel'),
    this.activePage.nextSibling.firstChild
    );
  },
  prev: function (){
    if(this.activePage.previousSibling == this.pagebox.firstChild) {
      return;
    }
    // para 2: das a innerhalb des lis
    this.open(
    this.activePage.previousSibling.firstChild.getAttribute('rel'),
    this.activePage.previousSibling.firstChild
    );
  },
  open: function( idToOpen, pageNode ) {
    if(!$(idToOpen)) {
      return;
    }
    $A(this.pagebox.childNodes).each(function( li ){
      $(li).removeClassName('page-active');
    });
    if($(pageNode)) {
      $(pageNode.parentNode).addClassName('page-active');
      this.activePage = $(pageNode.parentNode);
      pageNode.blur();
    }
    $A(this.pagelist).each(function( pagebox ){
      if(!$(pagebox)) {
        return;
      }
      $(pagebox).addClassName('hidden');
    });
    $(idToOpen).removeClassName('hidden');
    if(typeof $(idToOpen).onopen == 'function') {
      $(idToOpen).onopen();
    }
    if($('search_results_box')) {
      Element.scrollTo($('search_results_box'));
    }
  }
});
PageNavigation.Custom = Class.create();
PageNavigation.Custom.prototype = Object.extend(new PageNavigation.Base(), {
  initialize: function( pageBoxId )
  {
    this.pagebox = $(pageBoxId);
    this.pagebox.cleanWhitespace();
    $A(this.pagebox.childNodes).each((function( li ){
      if($(li).hasClassName('page-active')) {
        this.activePage = $(li);
      }
    }).bind(this));
    if( ! this.activePage ) {
      this.activePage = this.pagebox.firstChild.nextSibling;
    }
  },
  open: function( url ) {
    window.location.href=url;
  },
  next: function(){
    if(this.activePage.nextSibling == this.pagebox.lastChild) {
      return;
    }
    this.open( this.activePage.nextSibling.firstChild.href );
  },
  prev: function (){
    if(this.activePage.previousSibling == this.pagebox.firstChild) {
      return;
    }
    this.open( this.activePage.previousSibling.firstChild.href );
  }
});

