var _webapp = {
  isTouchDevice : typeof document.ontouchend != 'undefined',
  stage : $('the-content'),
  appSize : $$('body')[0].getSize(),
  is_loading : true,
  loading_state : null,
  debug : false,
  
  fx_chain : new Chain(),
  fx_spd_swipe : 500,
  fx_spd_fade : 200,
  fx_trans : 'ease-in-out',
  
  fx_moo_trans : Fx.Transitions.Sine.easeOut,
  
  content_objects : new Array(),
  content_empty_obj : {
    'article_id' : 0,
    'clang' : CUR_CLANG,
    'path' : '',
    'href' : '',
    'background-image' : '',
    'body-id' : '',
    'body-class' : '',
    'column1' : '',
    'column2' : '',
    'mainmenu' : '',
    'footer' : ''
  },
  content_cur_obj : null,
  content_new_obj : null,
  json_is_loading : null,
  
  _init : function()
  {
    if(this.debug)
    {
      if(typeof window.console == 'undefined' || typeof window.console.log == 'undefined')
      {
        window.console = {
          log : function(){ return null},
          warn : function(){ return null},
          group : function(){ return null},
          error : function(){ return null},
          trace : function(){ return null},
          groupEnd : function(){ return null}
        };
      }
    }
    
    if(this.stage!=null) // go on...
    {
      // receive the correct click/touch event handler
      if(this.isTouchDevice) {

        // set eventHandler-Names
        this.touchstart = 'touchstart';
        this.touchmove = 'touchmove';
        this.touchend = 'touchend';

        // this.hideURLbar();
        document.ontouchmove = function(e){ if(e.preventDefault!=null) e.preventDefault(); }
        document.body.style.WebkitUserSelect = 'none';
      }
      else
      {
        this.touchstart = 'mousedown';
        this.touchmove = 'mousemove';
        this.touchend = 'mouseup';
      }

      // get the current JSON object set up in the head section via js
      if(typeOf(CUR_OBJ)=='object')
      {
        this.content_objects['article'+CUR_OBJ.href] = this.content_new_obj = CUR_OBJ;
        delete CUR_OBJ;
        
        // fade in navigation
        this._cnt_replace_links($$('body')[0]);
        this.fx_chain.chain(function(){ this._anim_switch_list_items('.rex-navi1>li','on'); }.bind(this));
        
        // insert the new content
        this._cnt_insert_content();
      }
    }
  },
  
  _cnt_updateScrollArea : function(obj)
  {
    if(!this.isTouchDevice) return false;
    if(typeOf(obj) != 'object' && typeOf(obj)!='element') return false;
    if(obj.setStyles)
    {
      if(typeof(_webapp.orientation)=='undefined' && (orientation = window.orientation))
        _webapp.orientation = orientation;
        
      window.addEvent('resize', function(e){
        (function() {
          if(orientation = window.orientation)
          {
            if(orientation != _webapp.orientation)
            {
              _webapp.orientation = orientation;
              this._cnt_updateScrollArea(obj);
            }
          }
        }.pass(obj,this)).delay(1000);
      }.pass(obj,this));
      
      if(orientation = window.orientation)
        obj.orientation = orientation;

      obj.setStyles({
        'top':0,
        'left':0,
        'height':0,
        'overflow':'hidden'
      });
      
      obj.setStyles({
        'height':obj.getScrollSize().y,
        'overflow':'visible'
      });
      
      return true;
    }
    else
      return false;
  },
  
  _check_loading_states : function(iterator,last_states)
  {
    if(this.loading_state==null || this.loading_state=='loaded')
    {
      if(this.debug) console.log('LOADING-STATE: '+this.loading_state);
      return true;
    }
    
    if(iterator==null) iterator=0;
    // alert('LOADING-STATE: '+this.loading_state);
    if(this.debug) console.log('LOADING-STATE ('+iterator+'): '+this.loading_state);
    
    max_log_length = 5; delaytime = 500;
    if(typeOf(last_states)!='array') last_states = [];
    last_states.push(this.loading_state);
    
    diff = true;
    paused = false;
    crashed = false;
    if(last_states.length > max_log_length)
    {
      last_states.splice(0,last_states.length-max_log_length-1);
      diff = false;
      for(counter=0; counter<last_states.length; counter++)
        if(counter>0 && last_states[counter-1] != last_states[counter]) { diff = true; break; }
        else if(counter>=max_log_length) crashed = true;
        else if(counter>=max_log_length/2) paused = true;
    }
    if(this.debug) console.log('TMP '+crashed+' / '+paused);

    
    if(crashed)
    {
      if(this.debug)
      {
        console.group("ERROR");
        console.error('NOTHING CHANGED FOR '+((last_states.length-1) * delaytime)+'ms, calling function chained after '+this.loading_state+'...');
        console.trace();
        console.groupEnd();
      }
      this.do_chain();
    }
    else
    {
      if(paused) {
        if(this.debug) console.warn('EFFECT ('+this.loading_state+') PAUSED inserting loading layer...');
      // this._anim_switch_loader('on');
      }

      (function() { this._check_loading_states(iterator+1,last_states); }.pass([iterator,last_states],this)).delay(delaytime);
    }
  },
  
  
  _debug_log_states : function(iterator,last_states)
  {
/*
    if(this.debug===true)
    {
      if(iterator==null) iterator=0;
      // alert('LOADING-STATE: '+this.loading_state);
      console.log('LOADING-STATE ('+iterator+'): '+this.loading_state);
      
      max_log_length = 5;
      if(typeOf(last_states)!='array') last_states = [];
      last_states.push(this.loading_state);
      
      diff = true;
      if(last_states.length > max_log_length)
      {
        last_states.splice(0,last_states.length-max_log_length-1);
        diff = false;
        for(counter=0; counter<last_states.length; counter++)
          if(counter>0 && last_states[counter-1] != last_states[counter]) { diff = true; break; }
      }
      
      if(diff) (function() { this._debug_log_states(iterator+1,last_states); }.pass([iterator,last_states],this)).delay(500);
      else console.log('NOTHING CHANGED FOR '+(last_states.length-1)+' TIMES, quit...');
    }
*/
  },
  
  _cnt_insert_content : function()
  {
    // this._debug_log_states();
    
    // no new item is loaded? go back to old page
    if(this.content_new_obj == null) this.content_new_obj = this.content_cur_obj
    
    // set up a new language
    if(this.content_new_obj != null)
    {
      if(this.content_new_obj.clang != CUR_CLANG)
      {
        this._languagize_navigation();
        CUR_CLANG = this.content_new_obj.clang;
      }
    }
      
    // update the main navigation elements marked as ACTIVE
    if($$('.rex-navi1 li'))
    {
      $$('.rex-navi1 li').removeClass('rex-active');
      $$('.rex-navi1 li').each(function(item,i){
        
        if(item.get('rel')) href = item.get('rel')
        else if(item.getElement('a')) href = item.getElement('a').get('rel');
        if(aid = this.get_article_id(href))
        {
          aid = aid.split('-');
          aid = parseInt(aid[1]);
          if(this.is_article_in_path(aid))
          {
            item.addClass('rex-active');
          }
        }
      }.bind(this));
    }

    // update language menu
    $$('.nav-lang a').each(function(a,i){
      // alert(i+' ('+this.content_new_obj.article_id+') : '+this.get_article_url(this.content_new_obj.article_id,i));
      a.set('rel',this.get_article_url(this.content_new_obj.article_id,i,true));
    }.bind(this));

    // if the content_new_obj variable is set, execute the page changing effects...
    // then change the background / page style
    this.fx_chain.chain(function(){ this._anim_change_background(); }.bind(this));
    
    // insert the content
    this.fx_chain.chain(function(){ this._cnt_init_content(); }.bind(this));
    
    // then wait until everything is loaded the pages
    this.fx_chain.chain(function(){ this._cnt_all_items_loaded(); }.bind(this));

    // first fadeout the loader layer
    this.fx_chain.chain(function(){ this._anim_switch_loader('off'); }.bind(this));

    // then update scrollarea
    this.fx_chain.chain(function(){
      if(this._cnt_updateScrollArea(this.stage))
        this._nav_add_scrolling(this.stage,this);
      this.do_chain();
    }.bind(this));
    
    // then fadein the pages
    this.fx_chain.chain(function(){ this._anim_show_pages(); }.bind(this));

    // then fadein the single page items
    this.fx_chain.chain(function(){ this._anim_switch_list_items('.wrapper','on'); }.bind(this));

    // then start all media-players
    this.fx_chain.chain(function(){ this._video_init(); }.bind(this));

    // then start all lightboxes
    this.fx_chain.chain(function(){ this._lightbox_init(); }.bind(this));

    // then start all click-slideshows
    this.fx_chain.chain(function(){ __click_slideshow._init(); this.do_chain(); }.bind(this));

    // then start all faq-shows
    this.fx_chain.chain(function(){ __click_faq._init(); this.do_chain(); }.bind(this));

    // then set all variables that indicate loading to false
    this.fx_chain.chain(function(){ this._loader_off(); }.bind(this));


    // start the animation chain - if it is not already running
    if(!this.fx_chain.is_running) { this.do_chain(); }    
  },

  _anim_switch_list_items : function(items,onoff)
  {
    this.loading_state = 'switch_list_items ('+items+')';

    onoff = (typeOf(onoff)=='string' && onoff=='on') || onoff===true ? true : false;
    if(typeOf(items) == 'string') items = $$(items);
    else if(typeOf(items)!='array' && typeOf(items)!='object') items = [];
    // will fade in / fade out the main navigation
    if((num = items.length) > 0)
    { 
      items.each(function(item,i){
        
        (function() {
          item.setStyles({
            'zoom':1,
            '-webkit-backface-visibility' : 'hidden',
            '-webkit-transform' : 'translate3d(0,0,0)'
          });
          this._anim_fade(item,{
            'to' : onoff,
            onComplete : (i<num-1 ? function(){ } : function(){ if(ul = item.getParent('ul') && item.getStyle('opacity').toFloat()<=0) { item.getParent('ul').destroy(); } this.do_chain(); }.pass(item,this))
          })
        }.pass([num,item,i,onoff],this)).delay(i*Math.round(_webapp.fx_spd_fade/2));
      }.bind(this));
      
    }
    else
      this.do_chain();       
  },
  


  _cnt_all_items_loaded : function(iterator)
  {
    this.loading_state = 'cnt_all_items_loaded';
    loaded = true;
    iterator = typeOf(iterator)=='null' ? 0 : iterator.toInt();
    
    if(images = $$('#pages img'))
    {
      for(i=0; i<images.length; i++)
        if(images[i].isloading)
        {
          loaded = false;
          break;
        }
    }
    
    if(loaded || iterator>=20)
      this.do_chain();
    else
      (function(){ this._cnt_all_items_loaded(iterator+1); }.pass(iterator,this)).delay(50);
  },
  
  _cnt_init_content : function()
  {
    this.loading_state = 'cnt_init_content';
    if(this.content_new_obj == null) this.content_new_obj = this.content_cur_obj.clang != this.content_new_obj;
    new_language = false;
    if(this.content_cur_obj != null)
      if(this.content_cur_obj.clang != this.content_new_obj.clang) new_language = true;
    
    for(i=1; i<3; i++)
    {
      // insert COLUMN if not found...
      column = !$('column'+i) ? new Element('div#column'+i).inject(this.stage) : $('column'+i);
      column.empty();
      column.setStyle('opacity','0');
      
      // insert the column content from JSON object
      if(column.get('html')=='' && this.content_new_obj['column'+i]!=null) column.set('html',this.content_new_obj['column'+i]);

      // add ONLOADED on all images found in pages
      if(images = column.getElements('img'))
        images.each(function(img,i){ img.isloading = true; img.addEvent('load',function(){ this.isloading=null; }.bind(img)) }.bind(this));

      // then replace all links via REPLACE_LINKS
      this._cnt_replace_links(column);

      // set all .wrapper items in pages to opacity = 0 (they will be shown later...)
      if(items = this.stage.getElements('.wrapper'))
        items.setStyles({
          'opacity':0
        });
    }
    if(new_language) this._update_languages();
    
    this.do_chain();
  },


  _anim_hide_pages : function()
  {
    // will fade out the pages and remove them from the stage
    if($('column1')) $('column1').setStyle('opacity',0);
    if($('column2')) $('column2').setStyle('opacity',0);
    this.do_chain();

/*
    if($('pages'))
    {
      this._anim_fade($('pages'),{
        to: 0,
        onComplete : function(){
          $('pages').empty();
          _webapp.do_chain();
        }
      });
    }
    else
      this.do_chain();
*/
  },

  _anim_show_pages : function()
  {
    this.loading_state = 'anim_show_pages';
    // will fade in the pages 
    if($('column1')) $('column1').setStyle('opacity',1);
    if($('column2')) $('column2').setStyle('opacity',1);
    this.do_chain();

/*
    if(pages = $('pages'))
    {
      this._anim_fade(pages,{
        to : 1,
        onComplete : function(){ this.do_chain(); }.bind(this)
      });
    }
    else
      this.do_chain();
*/
  },
  
  _anim_switch_loader : function(onoff)
  {
    this.loading_state = 'anim_switch_loader';
    onoff = (typeOf(onoff)=='string' && onoff=='on') || onoff===true ? true : false;

    // will fade in / fade out the loader animation
    if(onoff)
    {
      
      if(typeOf(loaderdiv = $('loading'))!='element')
        loaderdiv = new Element("div#loading").inject($$('body')[0],'bottom').setStyle('opacity',0);
      
      this._anim_fade(loaderdiv,{
        to : 1,
        onComplete : function(){
          this.do_chain();
        }.bind(this)
       }); 
    }
    else
    {
      if(typeOf(loaderdiv = $('loading'))=='element')
      {
        if(loaderdiv.getStyle('opacity').toFloat()>0)
        {
          alert("opacity: "+loaderdiv.getStyle('opacity').toFloat());
          
          this._anim_fade(loaderdiv,{
            to:0,
            onComplete : function() {              
              loaderdiv.destroy();
              this.do_chain();
            }.pass(loaderdiv,this)
          }); 
        }
        else
        {
          loaderdiv.destroy();
          this.do_chain();
        }
      }
      else
        this.do_chain();
    }
  },

  _anim_change_background : function()
  {
    this.loading_state = 'anim_change_background';

    if(this.content_new_obj == null) this.content_new_obj = this.content_cur_obj.clang != this.content_new_obj;

    // change body id
    if(this.content_new_obj['body-id']!=null) $$('body')[0].set('id',this.content_new_obj['body-id']);

    // change body classes
    if(this.content_new_obj['body-class']) $$('body')[0].set('class',this.content_new_obj['body-class']);

    // change background image
    oldimg = this.content_cur_obj ? this.content_cur_obj['background-image'] : ($('bg') ? $('bg').get('src') : '');
    if(this.content_new_obj['background-image'] && this.content_new_obj['background-image'] != oldimg)
    {
      newimgfunc = function(){
        Asset.image(this.content_new_obj['background-image'], {
          id: 'bg',
          onLoad: function(){
            this.setStyle('opacity',0).inject($$('body')[0],'bottom');
            _webapp._anim_fade($('bg'),{to : 1, duration : _webapp.fx_spd_fade*3, onComplete : function(){ this.do_chain(); }.bind(_webapp) });
          }
        });
      }.bind(this);
    
      // fade out old image
      if($('bg'))
      {
        this._anim_fade($('bg'),{
          to : 0,
          duration : _webapp.fx_spd_fade*3,
          onComplete : function(){
            $('bg').destroy();
            newimgfunc();
          }.pass(newimgfunc,this)
        });
      }
      else
      {
        newimgfunc();
      }
    }
    else
      this.do_chain();
  },
  
  _anim_fade : function(container,options)
  {
    if(typeOf(container)=='element')
    {
      // options = { to : TRUE/FALSE / VALUE, onComplete : FUNCTION }
      options = typeOf(options)=='object' ? options : {};
      if(typeOf(options.to)=='string' && options.to=='in') options.to = 1;
      else if(typeOf(options.to)=='number' && options.to>0 && options.to<=1) options.to = options.to;
      else if(options.to===true) options.to = 1;
      else options.to = 0;
      options.inout = options.to < container.getStyle('opacity').toFloat() ? true : false;
      options.onComplete = typeOf(options.onComplete)=='function' ? options.onComplete : function(){};
      
      if(options.duration)
      {
        options.duration = typeOf(options.duration)=='number' ? options.duration : options.duration.toInt();
        if(options.duration<0 || options.duration==NaN) options.duration = _webapp.fx_spd_fade;
      }
      else options.duration = _webapp.fx_spd_fade;
  
      if(Browser.safari)
        this._anim_fade_webkit(container,options);
      else
        this._anim_fade_mootools(container,options);
    }
  },
  
  _anim_fade_mootools : function(container,options)
  {
    if(container.is_fading) container.is_fading.cancel();
    
    if(options.to == container.getStyle('opacity').toFloat())
    {
      options.onComplete();
    }
    else
    {
      container.is_fading = new Fx.Morph(container, {
        duration : options.duration,
        onComplete : function()
        {
          this.is_fading = null;
          this.setStyles({
            'opacity':options.to
          });
          if(options.to>=1)
          {
            this.setStyles({
              '-ms-filter':'none',
              'filter':'none'
            });
          }
          options.onComplete();
        }.pass(options,container)
      })
      .start({'opacity':[container.getStyle('opacity').toFloat(),options.to]});
    }
  },
  
  _anim_fade_webkit : function(container,options)
  {
    if(container.timer) clearTimeout(container.timer);
    
    if(options.to == container.getStyle('opacity').toFloat())
    {
      options.onComplete();
    }
    else
    {
      container.oc = function(){
        if(container.timer) clearTimeout(container.timer);
        container.setStyles({
          '-webkit-backface-visibility': 'hidden',
          '-webkit-transform': 'translate3d(0,0,0)',
          '-webkit-transform':null,
          '-webkit-transition':null,
          'opacity':Math.round(container.getStyle('opacity'))
        });
        container.setStyle('-webkit-transform',null);
        options.onComplete();
        options.onComplete = function(){};
      }.pass([options],container);
      
      (function() {
        this.setStyles({
          '-webkit-backface-visibility': 'hidden',
          '-webkit-transform':'translate3d(0, 0, 0)',
          '-webkit-transition':'opacity '+options.duration+'ms linear, -webkit-transform '+Math.round(options.duration/2)+'ms linear',
          'opacity':(options.inout ? options.to-0.01 : options.to+0.01)
	      });
      }.pass(options,container)).delay(10);
      
      container.timer = (function(){ this.oc(); }.bind(container)).delay(options.duration+20);
    }
  },
  
  _anim_slide : function(container,options)
  {
    if(typeOf(container)=='element')
    {
      // options = { from : ARRAY(X,Y), to : ARRAY(X,Y), onComplete : FUNCTION }
      options = typeOf(options)=='object' ? options : {};
      if(!options.from) options.from = [];
      if(!options.to) options.to = [];
  
      for(var prop in options)
      {
        switch(prop)
        {
          case 'from' : case 'to' :
            if(typeOf(options[prop])=='string') options[prop] = [options[prop].toInt(),container.getStyle('top').toInt()];
            else if(typeOf(options[prop])=='number') options[prop] = [options[prop],container.getStyle('top').toInt()];
            else if(typeOf(options[prop])=='object') options[prop] = [options[prop].x.toInt() || container.getStyle('left').toInt(), options[prop].y.toInt() || container.getStyle('top').toInt()];
            else if(typeOf(options[prop])=='array'){
              px = options[prop][0]==null ? container.getStyle('left') : options[prop][0];
              py = options[prop][1]==null ? container.getStyle('top') : options[prop][1];
              options[prop] = [px.toInt(),py.toInt()];
            }
            else options[prop] = [container.getStyle('left').toInt(),container.getStyle('top').toInt()];
          break;
        }
      }
      if(typeOf(options.to)!='array') options.to = options.from;
  
      if(options.to[0] == options.from[0] && options.to[1] == options.from[1])
      {
        // nothing to move - go on...
        options.onComplete();
      }
      else
      {
        container.setStyle('left',options.from[0]+'px');
        container.setStyle('top',options.from[1]+'px');

        if(Browser.safari)
          this._anim_slide_webkit(container,options);
        else
          this._anim_slide_mootools(container,options);
      }
    }
  },

  _anim_slide_mootools : function(container,options)
  {
    if(container.is_sliding) container.is_sliding.cancel();
    
    container.is_sliding = new Fx.Morph(container, {
      duration : _webapp.fx_spd_swipe,
      transition : _webapp.fx_moo_trans,
      onComplete : function()
      {
        options.onComplete();
      }.pass(options,container)
    })
    .start({
      'left':[options.from[0],options.to[0]],
      'top':[options.from[1],options.to[1]]
    });
  },
  
  _anim_slide_webkit : function(container,options)
  {
    if(container.is_sliding) container.removeEventListener('webkitTransitionEnd', container.slider,false);
    
    (function() {
      this.is_sliding = true;
      this.addEventListener('webkitTransitionEnd', container.slider = function(){
        if(this.is_sliding)
        {
          this.is_sliding = null;
          this.removeEventListener('webkitTransitionEnd', container.slider,false);
          this.setStyles({
            '-webkit-backface-visibility': 'hidden',
            '-webkit-transform': 'translate3d(0,0,0)',
            '-webkit-transition':null,
            '-webkit-transform':null,
            'left' : options.to[0]+'px',
            'top' : options.to[1]+'px'
          });
          options.onComplete();
        }
      }.pass(options,this));

      this.setStyles({
        '-webkit-transition':'-webkit-transform '+_webapp.fx_spd_swipe+'ms '+_webapp.fx_trans,
        '-webkit-transform':'translate3d('+(options.to[0]-options.from[0])+'px,'+(options.to[1]-options.from[1])+'px,0)'
      });

    }.pass(options,container)).delay(10);

  },  

  do_chain : function()
  {
    // will execute the next function in the chain
    this.fx_chain.callChain();
  },

  get_uri_parameters : function(uri)
  {
    // extracts URI parameters from a string or a URL
    uri = typeOf(uri)=='string' ? uri : window.location;
    if(uri.indexOf('?')>-1)
    {
      uri = uri.split('?')[1];
      uri = uri.replace(/&amp;/g,'&');
      if(uri.indexOf('&')>-1)
      {
        uri = uri.split('&');
        ret = {};
        for(i=0; i<uri.length; i++)
        {
          if(uri[i].indexOf('=')>-1)
          {
            uri[i] = uri[i].split('=');
            ret[uri[i][0]] = uri[i][1];
          }
          else
            ret[uri[i]] = true;
        }
        return ret;
      }
    }
    return {};
  },
  
  _video_init : function()
  {
    if($$('.mediaplayer').length>0)
    {
      $$('.mediaplayer').each(function(media,i){
        if(media.getElement('.flashobject'))
        {
          if(media.getElement('.flashobject').get('rel'))
          {
            eval(media.getElement('.flashobject').get('rel'));
            media.getElement('.flashobject').set('rel','');
          }
          media.getElement('.flashobject').setStyle('visibility','visible');
        }
        
        try { media.getElement('object').setStyle('visibility','visible'); } catch(e) {};
      });
    }
  
    this.do_chain();
  },
  
  _lightbox_init : function()
  {
    if($$('.lightbox').length>0)
    {
      $$('.lightbox').each(function(lightbox,i){
        if(links = lightbox.getElements('a'))
        {
          classes = links[0].get('class').split(' ');
          for(i=0; i<classes.length; i++)
          {
            if(classes[i].substr(0,2)=='lb')
            {
              links.each(function(link,i){
                link.removeEvent('click');
                link.set('href',link.get('rel'));
                link.set('rel','');
              })

              new MultiBox(classes[i],{
                path:JSPATH,
                skin:JSSKIN,
                name:classes[i]
              });        
              
              break;
            }
          }
        }
      });
    }
    this.do_chain();
  },

  get_article_id : function(link)
  {
    link = typeOf(link) == 'string' ? link : '';

    if(link.indexOf('index.php')>-1)
    {
      param = this.get_uri_parameters(link);
      
      article_id = param.article_id != null ? param.article_id.toInt() : 0;
      clang = param.clang != null ? param.clang.toInt() : -1;
    }
    else
    { 
      article_id = 0; clang=0;
      if(typeOf(REXPATH)=='object')
      { 
        for(var aid in REXPATH)
        {
          for(var cid in REXPATH[aid])
          {
            if(REXPATH[aid][cid].length < link.length && link.substr(link.length-REXPATH[aid][cid].length) == REXPATH[aid][cid])
            {
              article_id = aid;
              clang = cid;
              break;
            }
          }
          if(article_id > 0 && clang>=0) break;
        }        
      }
    }
    
    if(article_id > 0 && clang>=0) return 'article-'+article_id+'-'+clang;
    else return null; 
  },
  
  get_article_url : function(article_id,clang_id,realurl)
  {
    if(typeOf(article_id)!='number') article_id = parseInt(article_id);
    if(article_id>0)
    {
      if(typeOf(clang_id)=='undefined') clang_id = CUR_CLANG;
      clang_id = parseInt(clang_id);
      if(clang_id>=0)
      {
        if(typeOf(REXPATH)=='object' && realurl!==true)
        {
          if(REXPATH[''+article_id][''+clang_id]) return REXPATH[''+article_id][''+clang_id];
        }
        return 'index.php?article_id='+article_id+'&clang='+clang_id;
      }
      return 'index.php?article_id='+article_id;
    }
    return 'index.php';  
  },
  
  is_article_in_path : function(aid)
  {
    aid = typeOf(aid)=='number' ? aid : parseInt(aid);
    if(aid>0)
    {
      path = this.content_new_obj == null ? this.content_cur_obj.path : this.content_new_obj.path;
      path = path.split('|');
      
      for(i=0; i<path.length; i++)
      {
        if(parseInt(path[i])==aid)
        {
          return true;
        }
      }
    }
    return false;
  },
  
  _nav_history_back : function()
  {
    if(this.history.length > 0 && !this.is_loading)
    {
      newhref = this.history.getLast();
      this._cnt_load(newhref,true);
    }
  },
  
  _nav_extend_history : function(linkhref)
  {
    if(!this.history) this.history = [];
    this.history.push(linkhref);
    // this._anim_switch_back_button('on');
  },
  
  _nav_contract_history : function()
  {
    this.history.pop();
    if(this.history.length<=0)
      this._anim_switch_back_button('off');
  },
  
  _cnt_load : function(link,inhistory)
  {
    link = typeOf(link)=='string' || (typeOf(link)=='object' && typeOf(link = link.get('rel'))=='string') ? link : null;
    // will load a url adding a get parameter to receive a json object from cms
    // if(this.debug) console.log('Old article : '+this.get_article_id(this.content_cur_obj.href)+' / New article : '+this.get_article_id(link));
    if(typeOf(link)=='string' && this.get_article_id(link) != this.get_article_id(this.content_cur_obj.href))
    {
      this.is_loading = true;
      // if(this.debug) console.log('Load '+link);
    
      this.loading_state = 'cnt_load';
      // store the current stage content in the history
      if(inhistory==null) this._nav_extend_history(this.content_cur_obj.href);
      else this._nav_contract_history();
 
      // mark that the fx-chain is running
      this.fx_chain.is_running = true;
 
      // then fadeout the navigation
      this.fx_chain.chain(function(){ this._anim_switch_list_items('.wrapper','off'); }.bind(this));

      // then fadeout the pages
      this.fx_chain.chain(function(){ this._anim_hide_pages(); }.bind(this));

      // then fadein the loader layer
      this.fx_chain.chain(function(){ if(this.json_is_loading) { this._anim_switch_loader('off'); } else this.do_chain(); }.bind(this));

      // if each effect is finished, mark the fx-chain as NOT-RUNNING and continue
      // the chain - if the json-laoder is still loading, it will be empty an nothing will
      // happen, otherwise the other chained effects will be executed
      this.fx_chain.chain(function(){ this.fx_chain.is_running = false; this.do_chain(); }.bind(this));

      // start the animation chain
      this.do_chain();
      this._check_loading_states();

      if(!(this.content_new_obj = this.content_objects[this.get_article_id(link)]))
      {
        this.json_is_loading = true;
        
        // load the new object if it is not already stored in the objects-object
        this.new_href = link;
        href = this.new_href+( this.new_href.indexOf('?')>-1 ? '&' : '?')+'json=1';

        new Request.JSON({url: href,
          onComplete: function(obj){
            if(obj)
            {
              this.content_objects[this.get_article_id(this.new_href)] = this.content_new_obj = obj;
            }
            else
              this.content_new_obj = this.content_cur_obj;

            delete this.new_href;
            
            this._cnt_insert_content();
          }.bind(_webapp)
        }).get();
      }       
      else
        this._cnt_insert_content();
    }    
  },

  _nav_open_link : function(url)
  {
    // this happens, when a link is clicked...
    if(!this.is_loading)
    {
      url = typeOf(url)=='element' ? url.get('rel') : url;
      
      if(typeOf(url)=='string')
      {
        // finally start the loading process
        this._cnt_load(url);
      }
    }
  },
  
  _cnt_replace_links : function(container)
  {
    container = (typeOf(container)=='string' && typeOf(container = $(container))=='element') || typeOf(container)=='element' ? container : null;
    // will alter each link within CONTAINER not to open a new browser window
    // but open by js:window.location.href or by open the link via LOAD_CONTENT
    if(typeOf(container)=='element')
    {
    
      pagelinks = $$('a');
      // attach pagelinks with ajax-load function
      pagelinks.each(function(link)
      {
        if(!link.hasClass('noeffect') && typeOf(linkhref = link.get('href'))=='string')
        {
          if(linkhref.indexOf(window.location.host)>-1)
            linkhref = linkhref.split(window.location.host)[1];
            
          if(linkhref!='')
          {
            if(_webapp.has_event(link,_webapp.touchstart))
            {
            }
            else if(link.get('href').indexOf('files/')>-1) {}
            else if(link.get('rel')!=null && link.get('rel').indexOf('index.php')>-1)
            {
              link.set('href','javascript:void(0)');
              link.touch = new Touch(link);
              link.touch.addEvent('releaseInside', function(){
                _webapp._nav_open_link(this);
                return false;
              }.bind(link));
            }
            else if(linkhref.substr(0,SERVERROOT.length) == SERVERROOT) // else if(link.get('href').indexOf('index.php')>-1)
            {
              link.set('rel',link.get('href'))
              link.set('href','javascript:void(0)');
              link.touch = new Touch(link);
              link.touch.addEvent('releaseInside', function(){
                _webapp._nav_open_link(this);
                return false;
              }.bind(link));
            }
            else
            {
/*
              link.addEvent(_webapp.touchstart, function(){
                window.location = this.get('href');
                return false;
              });
*/
            }
            
            // mainmenu-links: add fadein/out-effects
            if(ul = link.getParent('ul'))
            {
              if(ul.hasClass('rex-navi1'))
              {
                link.touch.addEvent('releaseInside', function(){
                  link.getParent('ul').getElements('ul').each(function(el,i){
                    el.setStyles({
                      'height':'0px',
                      'opacity':0
                    });
                    el.active = false;
                  });
                }.bind(link));
                
                if(ul = link.getParent('li').getElement('ul'))
                {
                  if(!_webapp.isTouchDevice)
                  {
                    link.size = link.getParent('li').getSize().y;
                    
                    link.getParent('li').addEvent('mouseenter',function(){
                      ul = link.getParent('li').getElement('ul');
                      
                      if(ul.effect && ul.effect.cancel) ul.effect.cancel();
                      else
                      {
                        ul.setStyles({
                          'overflow':!Browser.ie ? 'hidden' : 'visible',
                          'height':'1px',
                          'display':'block',
                          'opacity':0,
                          'zoom':1
                        });
                        
                        link.getParent('li').setStyles({
                          'height':(link.size+ul.getScrollSize().y)+'px'
                        });
                      }
  
                      ul.effect = new Fx.Morph(ul,{
                        duration : _webapp.fx_spd_fade,
                        onComplete : function()
                        {
                          this.effect = null;
                        }.bind(ul)
                      })
                      .start({
                        'opacity':[ul.getStyle('opacity'),1],
                        'height':[ul.getHeight().toInt(),ul.getScrollSize().y-20]
                      });
                    }.bind(this));
                  
                    link.getParent('li').addEvent('mouseleave',function(){
                      ul = link.getParent('li').getElement('ul');
                      if(ul.effect && ul.effect.cancel) ul.effect.cancel();

                      link.getParent('li').setStyles({
                        'height':link.size+'px'
                      });
  
                      ul.effect = new Fx.Morph(ul,{
                        duration : _webapp.fx_spd_fade,
                        onComplete : function()
                        {
                          this.effect = null;
                        }.bind(ul)
                      })
                      .start({
                        'opacity':[ul.getStyle('opacity'),0],
                        'height':[ul.getHeight().toInt()-20,0]
                      });
                    }.bind(this));
                  }
                  else
                  {
                    link.touch.removeEvents('releaseInside');
                    link.addClass('noeffect'); //removeEvents(_webapp.touchend); //link.addEvent(_webapp.touchend,function() { return false; });
                    link.getParent('li').addEvent(_webapp.touchend,function(e){
                      
                      ul = link.getParent('li').getElement('ul');
                      if(ul.effect && ul.effect.cancel) ul.effect.cancel();
                      else
                      {
                        if(ul.getStyle('display')!='block') {
                          ul.setStyles({
                            'overflow': 'hidden',
                            'height' : '0px',
                            'display':'block',
                            'font-size':'1.2em',
                            'line-height':'1.2em'
                          });
                        }
                        if(ul.getStyle('height').toInt() > 0 )
                        {
                          if(ul.active)
                          {
                            _webapp._nav_open_link(link);
                            link.getParent('li').removeClass('rex-active').blur();
                          }
                          ul.active = false;
                          openit = false;
                        }
                        else
                        {
                          link.getParent('ul').getElements('ul').each(function(el,i){
                            el.setStyles({
                              'height':'0px',
                              'opacity':0
                            });
                            el.active = false;
                          });
                          ul.active = true;
                          
                          openit = true;
                        }

                        ul.effect = new Fx.Morph(ul,{
                          duration : _webapp.fx_spd_fade,
                          onComplete : function()
                          {
                            this.effect = null;
                          }.bind(ul)
                        })
                        .start({
                          'opacity':[ul.getStyle('opacity'),(openit ? 1 : 0)],
                          'height':[ul.getHeight().toInt(),(openit ? ul.getScrollSize().y-20 : 0)]
                        });
                      }
                    

                      return false;
                    }.bind(this));
                  }
                }
              }
            }
          }
        }
      });
      
      container.getElements('img').each(function(img,i){
        img.set('alt',''); img.set('title','');
      });
    }
    else
      this.do_chain();
  },
  
/*
  _slideshow : function(container)
  {
    container = (typeOf(container)=='string' && typeOf(container = $(container))=='element') || typeOf(container)=='element' ? container : null;
    // a slideshow constructor - will receive all images and videos
    // within CONTAINER and rearrange them to be shown in a slideshow
    //
    // usage:
    // if($chk(new_ss = new this._slideshow(CONTAINER))) CONTAINER.ss = new_ss; delete new_ss; CONTAINER.ss.init();
    
    if(container)
    {
      if(container.hasClass('menu-pages')) items = container.getElements('.menu-page');
      else if(container.hasClass('videos')) items = container.getElements('.video');
      else items = container.getElements('img');

      if(items.length>1)
      {
        return {stage     : container,
                items     : items,
                cur_item  : 0,
                new_item  : 0,
                is_moving : false,
                init      : function() {
                  // this.changeText();

                  this.buttonPrev = new Element('div',{'class':'inactive button-'+(this.direction=='vertical' ? 'up' : 'left')+' nav-button'})
                                    .inject(this.stage,'bottom');
                  this.buttonPrev.touch = new Touch(this.buttonPrev);
                  this.buttonPrev.touch.addEvent('releaseInside', function(){ this.prev(); }.bind(this));
     
                  this.buttonNext = new Element('div',{'class':'button-'+(this.direction=='vertical' ? 'down' : 'right')+' nav-button'})
                                    .inject(this.stage,'bottom');
                  this.buttonNext.touch = new Touch(this.buttonNext);
                  this.buttonNext.touch.addEvent('releaseInside', function(){ this.next(); }.bind(this));
                  _webapp._nav_add_swipe(this.stage,this);

                  _webapp._nav_add_swipe(this.stage,this);

                  // update the text
                  if(field = this.stage.getElement('.index'))
                  {
                    if(!this.defaultText) this.defaultText = field.get('html');
                    field.set('html',this.defaultText+' '+(this.cur_item+1)+' / '+this.items.length);
                  }
                  return this;
                },
                next      : function() { 
                  if(this.cur_item < this.items.length-1)
                    this.show(this.cur_item+1);
                },
                prev      : function() {
                  if(this.cur_item > 0)
                    this.show(this.cur_item-1);
                },
                show  : function(num){
                  num = typeof num != 'undefined' ? num.toInt() : -1;
                  
                  if(!this.is_moving && num>-1 && num < this.items.length && num != this.cur_item)
                  {
                    this.is_moving = true;
                    this.new_item = num;
                          
                    // if the current object is a video or contains videos, pause it/them
                    if(this.items[this.cur_item].nodeName=='video')
                      _webapp._video_playpause(this.items[this.cur_item],false);
                    else if(videos = this.items[this.cur_item].getElements('video'))
                      videos.each(function(){ _webapp._video_playpause(this,false); });
                    
                    tox = this.direction=='vertical' ? null : (this.new_item < this.cur_item ? this.stage.getSize().x+1 : (this.items[this.cur_item].getSize().x*-1)-1);
                    toy = this.direction!='vertical' ? null : (this.new_item > this.cur_item ? (this.stage.getSize().y*-1)-1 : this.stage.getSize().y+1); 

                    // move the current item out of the center
                    _webapp._anim_slide(this.items[this.cur_item], {
                      to: [tox,toy],
                      onComplete : function()
                      {
                        this.setStyle('display','none');
                      }.bind(this.items[this.cur_item])
                    });
  
                    fromx = this.direction=='vertical' ? 0 : (this.new_item > this.cur_item ? this.stage.getSize().x+1 : ((this.stage.getSize().x*-1)-1)-1);
                    fromy = this.direction!='vertical' ? 0 : (this.new_item > this.cur_item ? this.stage.getSize().y+1 : (this.stage.getSize().y*-1)-1); 
                    tox = this.direction=='vertical' ? null : 0;
                    toy = this.direction!='vertical' ? null : 0; 

                    // set the new item to the left/right of the stage
                    this.items[this.new_item].setStyles({
                      top : fromy+'px',
                      left : fromx+'px',
                      display : 'block'
                    });
                    _webapp._anim_slide(this.items[this.new_item], {
                      to: [tox,toy],
                      onComplete : function()
                      { 
                        this.update();
                      }.bind(this)
                    });
                  }
                },
                
                update : function()
                {
                  // when the transition is finished, ubdate variables
                  this.cur_item = this.new_item;
                  this.new_item = null;
                  
                  // update the text
                  if(field = this.stage.getElement('.index'))
                  {
                    if(!this.defaultText) this.defaultText = field.get('html');
                    field.set('html',this.defaultText+' '+(this.cur_item+1)+' / '+this.items.length);
                  }
    
                  // update the buttons
                  if(this.cur_item >= this.items.length-1) this.buttonNext.addClass('inactive'); else this.buttonNext.removeClass('inactive');
                  if(this.cur_item<=0) this.buttonPrev.addClass('inactive'); else this.buttonPrev.removeClass('inactive');
                 
                  // start the video (if one is found)
                  if(this.items[this.cur_item].nodeName=='video')
                    _webapp._video_playpause(this.items[this.cur_item],true);
                  else if(videos = this.items[this.cur_item].getElements('video'))
                    videos.each(function(){ _webapp._video_playpause(this,true); });
                    
                  // save that all moving is done
                  this.is_moving = false;
                }
              };
      }
    }
    return null;
  },
  
  _pageview : function(container)
  {
    container = (typeOf(container)=='string' && (container = $(container))) || typeOf(container)=='element' ? container : null;
    // a page constructor - will receive all pages
    // within CONTAINER and rearrange them to be shown as different pages with skip-buttons etc.
    //
    // usage:
    // if($chk(new_pages = new this._pageview(CONTAINER))) CONTAINER.pages = new_pages.init(); delete new_pages;
    
    if(container)
    { 
      items = container.getElements('.page');
      if(items && items.length>1)
      {
        cur_item = 0; for(i=0; i<items.length; i++) if(items[i].hasClass('active')) { cur_item = i; break; }
        
        return {stage     : container,
                items     : items,
                direction : (tmp = container.getElements('.scroll-vertical')) && tmp.length>0 ? 'vertical' : 'horizontal',
                cur_item  : cur_item,
                new_item  : cur_item,
                is_moving : false,
                init      : function() {
                  this.items.setStyles('display','none');
                  this.items[cur_item].setStyles({
                    'display':'block',
                    'left':'0px'
                  });
                  this.buttonPrev = new Element('div',{'class':'button-'+(this.direction=='vertical' ? 'up' : 'left')+' nav-button'})
                                    .inject(container,'bottom');
                  this.buttonPrev.touch = new Touch(this.buttonPrev);
                  this.buttonPrev.touch.addEvent('releaseInside', function(){ this.prev(); }.bind(this));
     
                  this.buttonNext = new Element('div',{'class':'button-'+(this.direction=='vertical' ? 'down' : 'right')+' nav-button'})
                                    .inject(container,'bottom');
                  this.buttonNext.touch = new Touch(this.buttonNext);
                  this.buttonNext.touch.addEvent('releaseInside', function(){ this.next(); }.bind(this));
                  
                  if(cur_item<=0) this.buttonPrev.addClass('inactive');
                  else if(cur_item>=items.length-1) this.buttonNext.addClass('inactive');
                  
                  _webapp._nav_add_swipe(this.stage,this);
                  return this;
                },
                next      : function() {
                  if(this.cur_item < this.items.length-1)
                    this.show(this.cur_item+1);
                },
                prev      : function() { 
                  if(this.cur_item > 0)
                    this.show(this.cur_item-1);
                },
                show  : function(num){
                  num = typeof num != 'undefined' ? num.toInt() : -1;
                  
                  if(!this.is_moving && num>-1 && num < this.items.length && num != this.cur_item)
                  {
                    this.is_moving = true;
                    this.new_item = num;
                          
                    // if the current item contains videos, pause it/them
                    if(videos = this.items[this.cur_item].getElements('video'))
                      videos.each(function(){ _webapp._video_playpause(this,false); });
                    
                    tox = this.direction=='vertical' ? null : (this.new_item < this.cur_item ? this.stage.getSize().x+1 : (this.items[this.cur_item].getSize().x*-1)-1);
                    toy = this.direction!='vertical' ? null : (this.new_item > this.cur_item ? (this.stage.getSize().y*-1)-1 : this.stage.getSize().y+1); 

                    // move the current item out of the center
                    _webapp._anim_slide(this.items[this.cur_item], {
                      to: [tox,toy],
                      onComplete : function()
                      {
                        this.setStyle('display','none');
                      }.bind(this.items[this.cur_item])
                    });
  
                    fromx = this.direction=='vertical' ? 0 : (this.new_item > this.cur_item ? this.stage.getSize().x+1 : ((this.stage.getSize().x*-1)-1)-1);
                    fromy = this.direction!='vertical' ? 0 : (this.new_item > this.cur_item ? this.stage.getSize().y+1 : (this.stage.getSize().y*-1)-1); 
                    tox = this.direction=='vertical' ? null : 0;
                    toy = this.direction!='vertical' ? null : 0; 

                    // set the new item to the left/right of the stage
                    this.items[this.new_item].setStyles({
                      top : fromy+'px',
                      left : fromx+'px',
                      display : 'block'
                    });
                    _webapp._anim_slide(this.items[this.new_item], {
                      to: [tox,toy],
                      onComplete : function()
                      { 
                        this.update();
                      }.bind(this)
                    });
                  }
                },
                update : function()
                {

                  // when the transition is finished, ubdate variables
                  this.cur_item = this.new_item;
                  this.new_item = null;
                  
                  // update the buttons
                  if(this.cur_item >= this.items.length-1) this.buttonNext.addClass('inactive'); else this.buttonNext.removeClass('inactive');
                  if(this.cur_item<=0) this.buttonPrev.addClass('inactive'); else this.buttonPrev.removeClass('inactive');
                 
                  // start the video (if one is found)
                  if(videos = this.items[this.cur_item].getElements('video'))
                    videos.each(function(){ _webapp._video_playpause(this,true); });
                  
                  // update page classes
                  this.items.removeClass('active');
                  this.items[this.cur_item].addClass('active');
                  
                  // update the headlines
                  _webapp._cnt_header_update_headlines();
                  
                  // update the indexes
                  _webapp._cnt_header_update_index();

                  // save that all moving is done
                  this.is_moving = false;
                }
              };
      }
    }
    return null;
  },
*/
  _nav_add_scrolling : function(obj,base,vertical)
  { 
    base = typeOf(base)!='object' ? this : base;
    vertical = typeOf(vertical)=='horizontal' || typeOf(vertical)==false ? false : true;
    if((obj = typeOf(obj)=='string' ? $(obj) : obj))
    {
      
      obj.removeEvents(this.touchstart);
      obj.removeEvents(this.touchmove);
      obj.removeEvents(this.touchend);

      if(obj.getSize().y <= obj.getParent().getSize().y && obj.getSize().x <= obj.getParent().getSize().x)
        return null;

      obj.addEvent(this.touchstart,function(e){
        if(!_webapp.isTouchDevice) this.started = {x:e.page.x, y:e.page.y};
        else if(e.touches.length==1){
          this.started = {x:e.touches[0].pageX, y:e.touches[0].pageY};
         // if(e.preventDefault!=null) e.preventDefault();3
        }
      });
      obj.addEvent(this.touchmove,function(e){
        if(!_webapp.isTouchDevice) this.ended = {x:e.page.x, y:e.page.y};
        else if(e.touches.length==1){
          this.ended = {x:e.touches[0].pageX, y:e.touches[0].pageY};
          // if(e.preventDefault!=null) e.preventDefault();
        }
      });

      
      obj.addEvent(this.touchend,function(e){
        if(typeOf(this.ended)=='object' && typeOf(this.started)=='object' && (this.ended.x.toInt()>=0 && this.started.x.toInt()>=0 && this.ended.x.toInt() != this.started.x.toInt()))
        {  
          dx = this.ended.x.toInt() - this.started.x.toInt();
          dy = this.ended.y.toInt() - this.started.y.toInt();
          if(base.direction==='vertical' || vertical===true)
          {
            
            if(dy>=-50 && dy<=50) {}
            else {
              step = dy*2;
              
              size = this.getScrollSize().y > this.getSize().y ? this.getScrollSize().y : this.getSize().y;
              this.setStyle('height',size+'px');
// alert(size);
              if(this.getPosition().y+step > 0) to = 0;
              else if(this.getPosition().y+step < this.getParent().getSize().y-size) to = this.getParent().getSize().y-size;
              else to = this.getPosition().y+step;
              
              _webapp._anim_slide(this, {
                to: [this.getPosition().x,to],
                onComplete : function()
                { 
                }.bind(this)
              });

              if(e.preventDefault!=null) e.preventDefault();
            } 
          }
          else
          {
            
            if(dx>=-50 && dx<=50) {}
            else {
              step = dx*2;

              if(this.getPosition().x+step > 0) to = 0;
              else if(this.getPosition().x+step < this.getParent().getSize().x-this.getSize().x) to = this.getParent().getSize().x-this.getSize().x;
              else to = this.getPosition().x+step;
              
              _webapp._anim_slide(this, {
                to: [to,this.getPosition().y],
                onComplete : function()
                { 
                }.bind(this)
              });

              if(e.preventDefault!=null) e.preventDefault();
            } 
          }
          this.started = this.ended = null;
        }      
      }.pass(base,obj));
    }
  },
  
  
  _nav_add_swipe : function(obj,base)
  { 
    base = typeOf(base)!='object' ? this : base;
    if((obj = typeOf(obj)=='string' ? $(obj) : obj) && (base.next!=null && base.prev!=null))
    {         
      obj.removeEvents(this.touchstart);
      obj.removeEvents(this.touchmove);
      obj.removeEvents(this.touchend);

      obj.addEvent(this.touchstart,function(e){
        if(!_webapp.isTouchDevice) this.started = {x:e.page.x, y:e.page.y};
        else if(e.touches.length==1){
          this.started = {x:e.touches[0].pageX, y:e.touches[0].pageY};
          if(e.preventDefault!=null) e.preventDefault();3
        }
      });
      obj.addEvent(this.touchmove,function(e){
        if(!_webapp.isTouchDevice) this.ended = {x:e.page.x, y:e.page.y};
        else if(e.touches.length==1){
          this.ended = {x:e.touches[0].pageX, y:e.touches[0].pageY};
          if(e.preventDefault!=null) e.preventDefault();
        }
      });
      obj.addEvent(this.touchend,function(e){
       if(typeOf(this.ended)=='object' && typeOf(this.started)=='object' && (this.ended.x.toInt()>=0 && this.started.x.toInt()>=0 && this.ended.x.toInt() != this.started.x.toInt()))
        {
          dx = this.ended.x.toInt() - this.started.x.toInt();
          dy = this.ended.y.toInt() - this.started.y.toInt();
          if(base.direction==='vertical')
          {
            if(dy>=-50 && dy<=50) {}
            else {
              base[(dy<0 ? 'next' : 'prev')]();
              if(e.preventDefault!=null) e.preventDefault();
            } 
          }
          else
            if(dx>=-50 && dx<=50) {}
            else { 
              base[(dx<0 ? 'next' : 'prev')]();
              if(e.preventDefault!=null) e.preventDefault();
            } 
          this.started = this.ended = null;
        }      
      }.pass(base,obj));
    }
  },
  
  _loader_off : function()
  {
    this.loading_state = 'loaded';
    this.content_cur_obj = this.content_new_obj;
    this.content_new_obj = null;
    this.is_loading = false;
    this.json_is_loading = false;
    
    // force every item to be shown
    $$('#column1').setStyles({'display':'block','opacity':1});
    $$('#column2').setStyles({'display':'block','opacity':1});
    
    this._anim_switch_loader('off');
/*
    $$('.wrapper').setStyles({
      'display':'block',
      'opacity':1,
      'margin-top':0
    });
*/    
  },
  
  _popup_close : function(options)
  {
    if(this.popup!=null && !this.popup.isrunning)
    {
      options = typeOf(options)=='object' ? options : {};
      oncomplete = typeOf(options.onComplete)=='function' ? options.onComplete : function(){};
      
      this.popup.isrunning = true;
      this.popup.setStyle('opacity',1);
      
      this._anim_fade(this.popup,{
        to : 0,
        onComplete : function()
        {
          this.popup.isrunning = null;
          this.popup.destroy();            
          this.popup = null;
          oncomplete();
        }.pass(oncomplete,this)
      });
    }
  },
  
  _popup_open : function(options)
  {
    // creates a popup window
    // options are: HTML (the dialoge), SIZE (the size of the popup),
    // YESFUNC (function to execute when YES is pressed), NOFUNC (function to execute when NO is pressed)
    options = typeOf(options)=='object' ? options : null;
  
    if(this.popup==null && options!=null)
    {
      options.html = typeOf(options.html) != 'string' &&  typeOf(options.html) != 'element' ? '' : options.html;
      
      // validate parameters
      options.size = ((typeOf(options.size) == 'array' && options.size.length>0) || (typeOf(options.size)=='object' && (options.size.width!=null || options.size.height!=null))) ? options.size : {};
      if(typeOf(options.size)=='array') options.size = {width:size[0] || null, height: size[1] || null};
      options.yesfunc = typeOf(options.yesfunc) == 'function' ? options.yesfunc : null;
      options.nofunc = typeOf(options.nofunc) == 'function' ? options.nofunc : null;
      options.close = options.close === false ? false : true;
      options.optfuncs = typeOf(options.optfuncs) == 'array' ? options.optfuncs : [];
      tmp = [];
      for(i=0; i<options.optfuncs.length; i++)
      {
        if(typeOf(options.optfuncs[i])=='object' && typeOf(options.optfuncs[i].func)=='function')
        {
          tmp.push({
            html : typeOf(options.optfuncs[i].html)=='string' ? options.optfuncs[i].html : '',
            css : typeOf(options.optfuncs[i].css)=='string' ? options.optfuncs[i].css : 'btn-'+i,
            func : options.optfuncs[i].func
          });
        }
      }
      options.optfuncs = tmp;
      
      // set up popoup object
      this.popup = new Element('div#popup').setStyles({'opacity':0,'display':'block'});
      this.popup.isrunning = true;

      // set sizes
      if(options.size.width!=null) this.popup.setStyles({'width':options.size.width.toInt()+'px'});
      if(options.size.height!=null) this.popup.setStyles({'height':options.size.height.toInt()+'px'});
      
      // the window
      dialogue = new Element('div',{'class':'popup-dialogue'});
      
      // the text
      if(typeOf(options.html)=='string')
        text = new Element('div',{'class':'confirm-text'}).set('html',VCENTERHTML.replace('HTMLGOESHERE',options.html)).inject(dialogue);
      else if(typeOf(options.html)=='element')
      {
        text = new Element('div',{'class':'confirm-text'}).set('html',VCENTERHTML.replace('HTMLGOESHERE','<div id="replaceme"></div>')).inject(dialogue);
        options.html.replaces(dialogue.getElement('#replaceme'));
      }

      // the yes-button
      if(typeOf(options.yesfunc)=='function')
      {
        yes = new Element('span',{'class':'confirm-yes'}).set('html',VCENTERHTML.replace('HTMLGOESHERE',this._translate('yes')))
              .inject(dialogue);
        yes.touch = new Touch(yes);
        yes.touch.addEvent('releaseInside',function(e){
                if(typeOf(options.yesfunc)=='function') options.yesfunc();
                else this._popup_close();              
                if(e.preventDefault!=null) e.preventDefault();
              }.pass([options],this));
      }
      
      // the optional functions
      for(i=0; i<options.optfuncs.length; i++)
      {
        tmp = new Element('span',{'class':options.optfuncs[i].css}).set('html',VCENTERHTML.replace('HTMLGOESHERE',options.optfuncs[i].html))
              .inject(dialogue);
        tmp.touch = new Touch(tmp);
        tmp.touch.addEvent('releaseInside',function(e){
                opt.func();
                if(e.preventDefault!=null) e.preventDefault();
              }.pass([opt=options.optfuncs[i]],this));
      }     
     
      // the no-button
      if(options.close)
      {
        no = new Element('span',{'class':'confirm-no'}).set('html',VCENTERHTML.replace('HTMLGOESHERE',this._translate('no')))
              .inject(dialogue);
        no.touch = new Touch(no);
        no.touch.addEvent('releaseInside',function(e){
                if(typeOf(options.nofunc)=='function') options.nofunc();
                else this._popup_close();              
                if(e.preventDefault!=null) e.preventDefault();
              }.pass([options],this));
      }
      
      // inject dialogue into popup
      dialogue.inject(this.popup);

      // finally inject the popup into main page
      // this.popup.inject(this.stage);
      this.popup.inject($$('body')[0]);
      
      this._anim_fade(this.popup,{
        to: 1,
        onComplete : function(){
          this.isrunning = false;
          if(this.getElement('input.first')!=null) this.getElement('input.first').focus();
        }.bind(this.popup)
      });
    }
  },
  
  has_event : function(el,eventType,fn)
  {
    if(typeOf(el)=='element')
    {
  		var myEvents = el.retrieve('events');
  		//can we shoot this down?
  		return myEvents && myEvents[eventType] && (fn == undefined || myEvents[eventType].keys.contains(fn));
    }
  },
  
  _translate : function(word,inserts)
  {
    word = typeOf(word)=='string' ? word : null;
    inserts = typeOf(inserts)=='array' ? inserts : [];
    
    if(word!=null && I18N!=null)
    {
      tobj = this.content_new_obj==null ? this.content_cur_obj : this.content_new_obj==null;
      clang = tobj.clang!=null ? tobj.clang : CUR_CLANG;
      clang = clang==null ? 0 : clang.toInt();

      if(I18N[''+clang]!=null && typeOf(I18N[''+clang][word])=='string')
      {
        word = I18N[''+clang][word];
        
        for(wordi=0; wordi<inserts.length; wordi++)
          word = word.replace("{"+wordi+"}",inserts[wordi]);
        
        word = word.replace(/{([^}]*)}/g,'');
        return word;
      }
    }
    return '[translate:'+word+']';
  },
  
  _update_languages : function() {
//    $('logo').set('rel',REXPATH[START_ARTICLE_ID][this.content_new_obj.clang]); // .set('href','index.php?clang='+this.content_new_obj.clang);
    $('logo').set('rel','index.php?article_id='+START_ARTICLE_ID+'&clang='+this.content_new_obj.clang); // .set('href','index.php?clang='+this.content_new_obj.clang);
    // this._cnt_replace_links($('logo'));
/*
    // the quick-nav-button
    if(this.quicknav.button && this.quicknav.button.get('html') != (tmp = VCENTERHTML.replace('HTMLGOESHERE',this._translate('navigation'))))
      this.quicknav.button.set('html',tmp);

    // the back-button
    if($('back') && $('back').get('html') != (tmp = VCENTERHTML.replace('HTMLGOESHERE',this._translate('back'))))
      $('back').set('html',tmp);
*/
  },
  
  _languagize_navigation : function() {
    // items = $$('.rex-navi1>li').combine($$('.footer>li'));
    this.fx_chain.chain(function(){
      new Fx.Morph($$('#header .rex-navi1')[0], {
        duration: this.fx_spd_fade,
        onComplete : function()
        {
          copyme = new Element('div#copyme')
          .setStyle('display','none')
          .set('html',this.content_new_obj.mainmenu);
          
          if(newel = copyme.getElement('.rex-navi1'))
          {
            newel.setStyles({'opacity':0});
            newel.getElements('li').setStyles({'opacity':1});
            newel.replaces($$('#header .rex-navi1')[0]);
  
            new Fx.Morph($$('#header .rex-navi1')[0], {
              duration: this.fx_spd_fade,
              onComplete : function()
              {
              }.bind(this)
            })
            .start({opacity:[0,1]})
          }
          else
          {
            $$('#header .rex-navi1')[0].empty();
          }
          copyme.destroy();
        }.bind(this)
      })
      .start({opacity:[1,0]});

      new Fx.Morph($$('#footer .footer')[0], {
        duration: this.fx_spd_fade,
        onComplete : function()
        {
          copyme = new Element('div#copyme')
          .setStyle('display','none')
          .set('html',this.content_new_obj.footer);
          
          if(newel = copyme.getElement('.footer'))
          {
            newel.setStyles({'opacity':0});
            newel.getElements('li').setStyles({'opacity':1});
            newel.replaces($$('#footer .footer')[0]);

            new Fx.Morph($$('#footer .footer')[0], {
              duration: this.fx_spd_fade,
              onComplete : function()
              {
              }.bind(this)
            })
            .start({opacity:[0,1]})
          }
          else
          {
            $$('#footer .footer')[0].empty();
          }
          copyme.destroy();
        }.bind(this)
      })
      .start({opacity:[1,0]});
      
      (function(){ this.do_chain()}.bind(this)).delay(this.fx_spd_fade*2);
    }.bind(this));
  }
  
};







var Touch = new Class({
  /* Adds TOUCH handlers to elements
     USAGE :
    el.touch = new Touch(el);
    el.touch.addEvent('start', function(){
      // DO SOMETHING WHEN TOUCH STARTS
    });
    el.touch.addEvent('end', function(){
      // DO SOMETHING WHEN TOUCH ENDS
    });
    el.touch.addEvent('cancel', function(){
      // DO SOMETHING WHEN TOUCH IS CANCELED (E.G. THE FINGER MOVES FROM THE TOUCHED OBJECT)
    });
    el.touch.addEvent('move', function(dx, dy){
      // DO SOMETHING WHEN TOUCH IS MOVED
    }.bind(el));  
    el.touch.addEvent('click', function(){
      // DO SOMETHING WHEN TOUCH IS CANCELED (E.G. THE FINGER MOVES FROM THE TOUCHED OBJECT)
    });
  */
  Implements: Events,
  initialize: function(element){
    this.element = document.id(element);
    this.bound = {
      start: this.start.bind(this),
      move: this.move.bind(this),
      end: this.end.bind(this)
    };
    if (typeof document.ontouchend != 'undefined'){
      this.context = this.element;
      this.startEvent = 'touchstart';
      this.endEvent = 'touchend';
      this.moveEvent = 'touchmove';
    } else {
      this.context = document;
      this.startEvent = 'mousedown';
      this.endEvent = 'mouseup';
      this.moveEvent = 'mousemove';
    }
    this.attach();
  },
  attach: function(){
    this.element.addListener(this.startEvent, this.bound.start);
  },
  detach: function(){
    this.element.removeListener(this.startEvent, this.bound.start);
  },
  start: function(event){
    this.preventDefault(event);
    // this prevents the copy-paste dialog to show up when dragging. it only affects mobile safari.
    document.body.style.WebkitUserSelect = 'none';
    this.hasDragged = false;
    this.releasedInside = true;
    this.context.addListener(this.moveEvent, this.bound.move);
    this.context.addListener(this.endEvent, this.bound.end);
    var page = this.getPage(event);
    this.startX = page.pageX;
    this.startY = page.pageY;
    this.fireEvent('start');
  },
  move: function(event){
    this.preventDefault(event);
    var page = this.getPage(event);
    this.deltaX = page.pageX - this.startX;
    this.deltaY = page.pageY - this.startY;
    this.hasDragged = !(this.deltaX === 0 && this.deltaY === 0);
    this.releasedInside = (this.deltaX >= -50 && this.deltaX <= 50 && this.deltaY >=-50 && this.deltaY <=50);
    if (this.hasDragged) this.fireEvent('move', [this.deltaX, this.deltaY]);
  },
  end: function(event){
    this.preventDefault(event);
    // we re-enable the copy-paste dialog on drag end
    document.body.style.WebkitUserSelect = '';
    this.context.removeListener(this.moveEvent, this.bound.move);
    this.context.removeListener(this.endEvent, this.bound.end);
    if(this.releasedInside) this.fireEvent('releaseInside');
    else this.fireEvent((this.hasDragged) ? 'end' : 'cancel');
  },
  preventDefault: function(event){
    if (event.preventDefault) event.preventDefault();
    else event.returnValue = false;
  },
  getPage: function(event){
    //when on mobile safari, the coordinates information is inside the targetTouches object
    if (event.targetTouches) event = event.targetTouches[0];
    if (event.pageX != null && event.pageY != null) return {pageX: event.pageX, pageY: event.pageY};
    var element = (!document.compatMode || document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;
    return {pageX: event.clientX + element.scrollLeft, pageY: event.clientY + element.scrollTop};
  }
});
Touch.build = "%build%";  



var FontChecker = new Class({
	/* implements */
	Implements: [Options],
	/* options */
	options: {
		fakeFont: '__RUBBISH_FONT__',
		testString: 'abcdefghijklmnopqrstuvwxyz'
	},
	/* initialization */
	initialize: function(options) {
		//set options
		this.setOptions(options);
	},
	/* a method that does whatever you want */
	check: function(desiredFont) {
		/* create a hidden element */
		var element = new Element('span',{
			styles: {
				visibility: 'hidden',
				'font-family': this.options.fakeFont
			},
			html: this.options.testString
		}).inject(document.body);
		/* apply a fake font, get width */
		var width = element.getCoordinates().width;
		/* apply desired font */
		element.setStyle('font-family', desiredFont);
		var new_width = element.getCoordinates().width;
		/* take our temp element out of the DOM */
		element.dispose();
		/* compare widths to see check if font is available */
		return (width !== new_width);
	}
});


var __click_slideshow = {
  transition:  Fx.Transitions.Sine.easeOut,
  duration: 300,
  wait: 3000,
  curitem : null,
  items : $$('.clickslideshow'),
  
  _init : function()
  {
    this.items = $$('.clickslideshow');
    this.curitem = 0;
    
    this.items.each(function(el,i){
//      tmp = new Element('div.counter').set('html',(i+1)+' / '+this.items.length).inject(el);
      tmp = new Element('div.counter').inject(el);
      new Element('a.playpause')
      .set('href','javascript:void(0)')
      .set('html',_webapp._translate('pause'))
      .addEvent('click',function(){
        if(this.timeOut)
        {
          clearTimeout(this.timeOut);
          this.timeOut = null;
          this.items.each(function(link){ link.getElement('.playpause').addClass('paused').set('html',_webapp._translate('play')); });
        }
        else
        {
          this.items.each(function(link){ link.getElement('.playpause').removeClass('paused').set('html',_webapp._translate('pause')); });
          this.next(true);
        }        
      }.bind(this))
      .inject(tmp);


    }.bind(this));
    
   this.timeOut = (function(){ this.next(true); }.bind(this)).delay(this.wait);
  },
  
  show : function(num)
  {
    if(!this.ismoving && num!=this.curitem)
    {
      this.ismoving = true;
      this.newitem = num;
      
      if(this.prevButton)
      {
        if(link.params.num<=0)
          this.prevButton.addClass('inactive');
        else this.prevButton.removeClass('inactive');
      }
      
      if(this.nextButton)
      {
        if(link.params.num>=this.items.length-1)
          this.nextButton.addClass('inactive');
        else this.nextButton.removeClass('inactive');
      }
      
      // hide old content
      if(this.items[this.curitem])
      {
        _webapp._anim_fade(this.items[this.curitem],{
          to : 0,
          duration : this.duration,
          onComplete : function(){
            this.items[this.curitem].setStyles({
              'opacity':0,
              'display':'none'
            });
            
            this.items[this.newitem].setStyles({
              'opacity':0,
              'display':'block'
            });
            
            _webapp._anim_fade(this.items[this.newitem],{
              to : 1,
              duration : this.duration,
              onComplete : function()
              {
                this.curitem = this.newitem;
                this.ismoving = false;
                if(this.items[this.curitem]) this.items[this.curitem].setStyle('filter','none');
              }.bind(this)
            });
          }.bind(this)
        })

/*
        new Fx.Morph(this.items[this.curitem],{
          transition: this.transition,
          duration: this.duration,
          onComplete : function()
          {
            this.items[this.curitem].setStyles({
              'opacity':0,
              'display':'none'
            });
            
            this.items[this.newitem].setStyles({
              'opacity':0,
              'display':'block'
            });
            
            new Fx.Morph(this.items[this.newitem],{
              transition: this.transition,
              duration: this.duration,
              onComplete : function()
              {
                this.curitem = this.newitem;
                this.ismoving = false;
                if(this.items[this.curitem]) this.items[this.curitem].setStyle('filter','none');
              }.bind(this)
            })
            .start({
              'opacity':[this.items[this.curitem].getStyle('opacity'),1]
            });
          }.bind(this)
        })
        .start({
          'opacity':[this.items[this.curitem].getStyle('opacity'),0]
        });
*/
      }
    }
  },
  
  prev : function()
  {
    if(this.curitem>0) this.show(this.curitem-1);
  },
  
  next : function(loop)
  {
    clearTimeout(this.timeOut);
    this.timeOut = (function(){ this.next(true); }.bind(this)).delay(this.wait);
    
    if(this.curitem<this.items.length-1) this.show(this.curitem+1);
    else if(loop===true) this.show(0);
  }
};


var __click_faq = {
  transition:  Fx.Transitions.Sine.easeOut,
  duration: 300,
  items : $$('.faq-item'),
  
  _init : function()
  {
    this.items = $$('.faq-item');

    this.items.each(function(el,i){
      if(el.getElement('.the-text'))
      {
        el.getElement('h3').addClass('clickable');
        el.addEvent(_webapp.touchend,function(){
          if(!this.ismoving)
          {
            txt = this.getElement('.the-text');
            this.ismoving = true;
            
            duration = txt.getSize().y>0 ? 200 : 500;
            
            txt.params = {
              'height':(txt.getSize().y>0 ? 0 : txt.getScrollSize().y),
              'padding-bottom':(txt.getSize().y>0 ? 0 : (Browser.ie ? '25px' : '15px'))
            };
            txt.onComplete = function() {
              this.ismoving = false;
            }.bind(this);
            
            
            new Fx.Morph(txt,{
              duration : duration,
              transition : Fx.Transitions.Sine.easeOut,
              onComplete : function(){
                this.ismoving = false;
              }.bind(this)
            })
            .start(txt.params);
          }
        });
      }
    });
  }
};
window.addEvent('domready',function(){
  _webapp._init();
});
/* when the page is loaded */
window.addEvent('load',function() {
/*
	var fc = new FontChecker();
	if(!fc.check('Trade Gothic W02 Bold Cn 20')) $$('body')[0].addClass('nofontface');
*/
});

