function KrebsLocalBase() {
  var __impressum_moved = 0;

  this.onLoad = function() {
    this.moveImpressum();

    this.rubrik = new KrebsLocalBaseRubrik();

    // text-focus effecte für eingabe boxen
    $$('input.search_input').each(function(node) {
      var value = node.value;

      // formular wird nur ausgeloest, wenn auch was eigenes
      // eingetragen wurde! ansonsten hinting an den nutzer.
      Event.observe(node.form, 'submit', function(e){
        if (node.value == value) {
          Event.stop(e);

          node.value = '';
          node.focus();
        }
      }); 

      Event.observe(node, 'focus', function(){ 
        node.removeClassName('blurred');  

        if (node.value == value) node.value = ''; 
      });
      
      Event.observe(node, 'blur', function(){ 
        node.addClassName('blurred'); 

        if (!node.value) node.value = value;  
      });
    });
  };

  // ------------------------------------------------------------------

  this.moveImpressum = function(_force) {
    if (__impressum_moved && !_force) return;

    var o_impr = document.getElementById('impressum');

    if (!o_impr) return;

    // erstmal ausblenden, aber hoehe erhalten
    o_impr.style.visibility = 'hidden';

    var o_col1 = document.getElementById('col1');
    var o_col2 = document.getElementById('col2');

    var c1h = simple_get_height(o_col1);
    var c2h = simple_get_height(o_col2);

    c1h += 5;

    if (c1h > c2h){
      o_impr.style.bottom = (c2h - c1h) + 'px';
    }

    // und jetzt wieder sichtbar machen
    o_impr.style.visibility = 'visible'; 

    __impressum_moved = 1;
  };

  var simple_get_height = function(_node) {   // reduced copy from protoype.js
    var display = _node.style.display;

    if (display != 'none' && display != null) // Safari bug
      return _node.offsetHeight;

    // All *Width and *Height properties give 0 on elements with display none,
    // so enable the element temporarily

    var els = _node.style;
    var originalVisibility = els.visibility;
    var originalPosition   = els.position;
    var originalDisplay    = els.display;

    els.visibility = 'hidden';
    els.position   = 'absolute';
    els.display    = 'block';

    var originalHeight = _node.clientHeight;

    els.visibility = originalVisibility;
    els.position   = originalPosition;
    els.display    = originalDisplay;

    return originalHeight;
  };

  // ------------------------------------------------------------------

  /* this.makeWide = function() {
    var link = document.createElement('link');

    link.setAttribute('href', '/.files/ntp/css/wide.css');
    link.setAttribute('rel', 'stylesheet');
    link.setAttribute('type', 'text/css');

    document.getElementsByTagName('head')[0].appendChild(link);
  }; */

  // ------------------------------------------------------------------

  this.getMediaData = function(_media, _id) {
    // zuerst suchen, ob es eine explizite variable gibt
    if (typeof(window['KrebsLocal'+_media+'Data_'+_id]) == 'object') {
      return window['KrebsLocal'+_media+'Data_'+_id];

    // jetzt versuchen auf einen globalen data-hash zuzugreifen
    }else if (typeof(window['KrebsLocal'+_media+'Data']) == 'object') {
      return window['KrebsLocal'+_media+'Data'][_id];
    }

    return;
  };

  // ------------------------------------------------------------------
 
  this.setCookie = function(_name, _value, _days) {
    var expires = "";

    if (_days) {
      var date = new Date();          
          date.setTime(date.getTime()+(Math.floor(_days*24*60*60*1000)));

      expires = "; expires="+date.toGMTString();
    }

    document.cookie = _name + "=" + _value + expires + "; path=/";
  };

  this.getCookie = function(_name) {
    _name += "=";

    var cookies = document.cookie.split(';');

    for (var i=0; i < cookies.length; i++) {
      var cookie = cookies[i];

      // remove leading whitespace ?
      while (cookie.charAt(0) == ' ') cookie = cookie.substring(1, cookie.length);

      if (cookie.indexOf(_name) == 0) return cookie.substring(_name.length, cookie.length);
    }

    return null;
  };

  this.removeCookie = function(_name) {
    this.setCookie(_name, '', -1);
  };

  // ------------------------------------------------------------------

  this.fireEvent = function (_node, _event){
    if (document.createEventObject){	// dispatch for IE
      var e = document.createEventObject();

      return _node.fireEvent('on'+_event, e)
    
    }else{  // dispatch for firefox + others
      var e = document.createEvent("HTMLEvents");

      e.initEvent(_event, true, true ); // event type,bubbling,cancelable

      return !_node.dispatchEvent(e);
    }
  };

  // ------------------------------------------------------------------

  this.makeTouchScrollable = function(_node) {
    var pageY = 0;
    var moved = 0;

    Event.observe(_node, 'touchstart', function(event) {
      event.preventDefault();

      pageY = 0;
      moved = 0;
    });

    Event.observe(_node, 'touchmove', function(event) {
      event.preventDefault();

      var lastY = pageY;
      var touch = event.touches[0];

      pageY = touch.pageY;
 
      // abgang, wenn wir noch kein lastY haben
      if (!lastY) return;

      var down = pageY - lastY;

      _node.scrollTop -= down;

      // vermerken, wieviel bewegung es gab
      moved += Math.abs(down);
    });

    Event.observe(_node, 'touchend', function(event) {
      event.preventDefault();

      if (moved < 5) KrebsLocalBase.fireEvent(event.target, 'click');
    });
    
  };
};

// ------------------------------------------------------------------

function KrebsLocalBaseRubrik() {
  // initialisierung
  var __default  = null;
  var __selected = null;

  $('ntl_filter').setOpacity(0.5);

  $('ntl_top_content').select('div.rubrik').each( function(rubrik) {
    rubrik.observe('mouseover', function() { select(rubrik); });

    if (rubrik.hasClassName('rubrik_sel')){
      __default  = rubrik;
      __selected = rubrik;
    }
  });

  var select = function(_node){
    if (!_node.hasClassName('rubrik')) _node = _node.parentNode;

    if (_node === __selected) return;

    // layout aendern 
    turnOff(__selected);
    turnOn(_node);

    // mouse beobachten, wenn alternative rubrik ausgeklappt
    if (_node !== __default) document.onmousemove = mouseMoved; 

    // und natuerlich merken, was wir gerade anzeigen
    __selected = _node;
  };

  var restore = function() {
    // ueberwachung wieder abschalten
    document.onmousemove = '';

    // layout zueruecksetzen
    turnOff(__selected);
    turnOn (__default);

    __selected = __default;
  };

  var turnOn = function(_node) {
    // rubrik tab farblich verändern
    _node.addClassName('rubrik_sel');
    _node.addClassName('bl_red');

    var links = $(_node.id+'_links');

    if (links) {
      links.show();

      if (_node !== __default){
        $('ntl_filter').show();
      }
    }
  };

  var turnOff = function(_node){
    _node.removeClassName('rubrik_sel');
    _node.removeClassName('bl_red');

    var links = $(_node.id+'_links');

    if (links) links.hide();

    $('ntl_filter').hide();
  };

  // ------------------------------------------------------------------
  // mouse listener
  // ------------------------------------------------------------------

  var mouseMoved = function(event){
    // this is my own evil explorer hack
    if (!event){ event = window.event; }

    var mleft = Event.pointerX(event);
    var mtop  = Event.pointerY(event);

    var p1 = Position.page($('ntl_top_content'));
    var p2 = Position.page($('ntl_bot_content'));

    var dim = $('ntl_top_content').getDimensions();

    var x1 = p1[0] - 10;
    var y1 = p1[1] - 10;

    var x2 = p2[0] + dim.width + 10;
    var y2 = p2[1] + dim.height + 10;

    if ((mleft < x1) || (mleft > x2) ||
        (mtop  < y1) || (mtop  > y2)){

      restore();
    }
  };
};

// --------------------------------------------------------------------
// --------------------------------------------------------------------
// --------------------------------------------------------------------

function KrebsLocalBaseAnimation() {

  this.animate = function() {
    var duration = arguments[0];

    if (!duration) return;

    var obj   = [];
    var after = [];

    var code = "";

    for (var i = 1; i < arguments.length; i++) {
      var arg = arguments[i];
      var pos = i - 1;

      if (arg.call) {
        code += 'obj['+pos+']' + createCode(arg, pos)+"\n";
        obj.push(arg.call);

      }else if(arg.node) {
        code += 'obj['+pos+']' + createCode(arg, pos)+"\n";
        obj.push(arg.node);
      }

      if (arg.after) after.push(arg.after);
    }

    if (obj.length < 1) return;

    eval("var call = function(obj,v){"+code+"}");

    var start = (new Date()).getTime();

    var pe = new PeriodicalExecuter(function(pe){
      var elapsed = (new Date()).getTime() - start;

      if (elapsed > duration) {
        pe.stop();

        call(obj, 1);

        for( var i = 0; i < after.length; i++) {
          after[i]();
        };        

      }else{
        call(obj, (elapsed/duration));
      }

    }, 0.001);

    return pe;
  };

  var createCode = function(_arg, _pos) {
    var dist = _arg.to - _arg.from;
    var code = _arg.from + ' + (' + dist +' * v)';

    if (_arg.round) code = 'Math.floor('+code+')';

    code = '('+code+')';

    if (_arg.call) return code + ';';

    if (_arg.prefix)  code = "'" + _arg.prefix + "' + " + code;
    if (_arg.postfix) code += " + '" + _arg.postfix + "'"; 
    
    return '.style["'+_arg.style+'"] = ' + code + ';';
  };
};

// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------

var KrebsLocalBaseAnimation = new KrebsLocalBaseAnimation();

var KrebsLocalBase = new KrebsLocalBase();

Event.observe(document, "dom:loaded", function(){ KrebsLocalBase.onLoad(); } );

