var App = {
  // all pages
  'init': function() {
    if(document.getElementById && document.getElementsByTagName &&
       document.createElement) {
      // hidden by default
      createStyleRule("#right_toggle_img", "display:block;");
    }    
  },
  // fires before onload
  'preLoad': function(func) {
    if(document.getElementById && document.getElementsByTagName &&
       document.createElement) {
      func();
    }
    this._preloadImages();    
  },
  // fires onload
  'postLoad': function(func) {
    Event.observe(window, 'load', function() {
      func();
    });
  },
  '_preloadImages': function() {
    toggleOnImg = new Image();
    toggleOnImg.src = 'img/toggleon.gif';
  },
  /**
   * Creates a Ajax Responder that gives visual feedback about Ajax activity
   * @param id string  Indicator element, that is shown on Ajax activity
   */
  'registerResponder': function(id) {
    Ajax.Responders.register({
      onCreate: function() {
        if(Ajax.activeRequestCount > 0)
          Element.show(id);
      },
      onComplete: function() {
        if(Ajax.activeRequestCount == 0)
          Effect.Fade(id);
      }
    });
  }
}

var Search = {
  blur: function() {
    if($('search_input').style.background != '#eee')
      $('search_input').style.background = '#eee';
    Effect.SwitchOff('feedback');
  },
  focus: function() {
    if($('search_input').style.background != 'white')
      $('search_input').style.background = 'white';
    Element.show('feedback');
  }
}

var NewsPanel = {
  _show: function() {
    $('content').style.marginRight = '200px';
    $('content').style.borderRight = '1px solid gray';
    $('right').style.borderLeft = '1px solid gray';
    $('right_inner').style.display = 'block';
    $('right_toggle_img').src = 'img/toggleoff.gif';
  },
  _hide: function() {
    $('content').style.marginRight = '0';
    $('content').style.borderRight = 'none';
    $('right').style.borderLeft = 'none';
    $('right_inner').style.display = 'none';
    $('right_toggle_img').src = 'img/toggleon.gif';
  },
  toggle: function() {
    if( $('right_inner').style.display == 'none' )
      this._show();
    else
      this._hide();
  }
}

/**
 * @param txtId     string  Id of the text link
 * @param toggleId  string  Id of the toggle element
 * @param hideTxt   string  innerHTML of txtId if toggleId is shown
 */
function txtToggle(txtId, toggleId, hideTxt) {
  // link text set in document
  var showTxt = $(txtId).innerHTML;

  Event.observe(txtId, 'click', function() {
    // toggle element
    if($(toggleId).style.display == 'none' ||
       $(toggleId).style.display == '')
      setElementStyleById(toggleId, 'display', 'block');
    else
      setElementStyleById(toggleId, 'display', 'none');
    
    // set link text
    if($(txtId).innerHTML == showTxt)
      $(txtId).innerHTML = hideTxt;
    else
      $(txtId).innerHTML = showTxt;
  });
}

App.init();
