/**
/**
 *  @author     Fabian Beiner <fbe(at)DERPUNKT(dot)de>
 *  @copyright  Fabian Beiner / DER PUNKT gmbh, Karlsruhe, Germany
 *  @license    http://creativecommons.org/licenses/by-sa/3.0/de/
 *  @date       13th January 2009
 *  @link       http://derpunkt.de
 *
 *  This file contains some JavaScript functions that I'm using.
 */

var stopMe = 0;

window.addEvent('load', function() {
/**
 *  Internet Explorer 6 sometimes has problems with the foot...
 *  After the page is loaded, I'll push the CSS settings *again* to
 *  the div#foot element.
 */
  $('foot').setStyles({
    'position': 'absolute',
    'bottom': '20px',
    'left': '0'
  });
});

window.addEvent('load',
function() {

/**
 *  Searchfield.
 */
  if ($('searchfield') != null) {
    $('searchfield').addEvent('focus',
    function() {
      this.setProperty('class', 'inputfocus');
      if (this.value == this.defaultValue) this.value = '';
    });
    $('searchfield').addEvent('blur',
    function() {
      this.removeClass('inputfocus');
      if (!this.value) this.value = this.defaultValue;
    });
  }

/**
 *  target="_blank" replacement.
 */
  $$('a.new-window').each(function(link) {
    link.addEvent('click',
    function() {
      window.open(this.href);
      return false;
    });
  });

/**
 *  Small function for jumping to the top of the current page.
 */
  if ($('functions_top') != null) {
    $('functions_top').addEvent('click',
    function(event) {
      event.stop();
      window.scrollTo(0, 0);
    });
  }

/**
 *  Small function for printing the current page.
 */
  if ($('functions_print') != null) {
    $('functions_print').addEvent('click',
    function(event) {
      event.stop();
      window.print();
    });
  }

/**
 *  Self-Made Accordion.
 */
  var list = $$('#collapse li div.collapse');
  var headings = $$('#collapse li div.toggler');
  var collapsibles = new Array();
  var current = null;

  function resetHeadings(except) {
    headings.each(function(heading) {
      if(heading.get('id') == 'toggler-last')  {
        $('collapse').setStyle('border-bottom', '1px #e5e5e5 solid');
      }
//      alert(heading.get('class'));
      if(heading.hasClass('toggler_open')){
        heading.removeClass('toggler_open');
      }
//      if (heading != except) {
        heading.setStyle('border-top', '1px #e5e5e5 solid');
//      }
    });
  }

  function setHeadings() {
    headings.each(function(heading) {
      heading.setStyle('border-top', '1px #fc3 solid');
      $('collapse').setStyle('border-bottom', '1px #fc3 solid');
    });
    current = null;
  }

  headings.each(function(heading, i) {
    var collapsible = new Fx.Slide(list[i], {
      duration: 100,
      transition: Fx.Transitions.linear
    });
    collapsibles[i] = collapsible;
    heading.addEvent('click',
    function(e) {
      e.stop();
      resetHeadings(this);
      if (this.hasClass('toggler_open')) {
        current = this;
        this.removeClass('toggler_open');
      } else {
        if(current != this)  {
          current = this;
          this.addClass('toggler_open');
          if (this.get('id') != 'toggler-last') {
            this.getParent().getNext().getFirst().setStyle('border-top', '1px #fc3 solid');
          } else {
            $('collapse').setStyle('border-bottom', '1px #fc3 solid');
          }
        }
        else  {
          current = null;
        }
      }
      collapsible.toggle();
      for (var j = 0; j < collapsibles.length; j++) {
        if (j != i) collapsibles[j].slideOut();
      }
    });
    heading.addEvent('mouseover',
    function(e) {
      e.stop();
      this.setStyle('text-decoration', 'underline');
    });
    heading.addEvent('mouseout',
    function(e) {
      e.stop();
      this.setStyle('text-decoration', 'none');
    });
    collapsible.hide();
  });

  if ($('collapse') != null) {
    $('collapse-all').setStyle('display', 'none');
    $('collapse-all').addEvent('click',
    function(e) {
      e.stop();
      resetHeadings();
      this.setStyle('display', 'none');
      $('expand-all').setStyle('display', 'inline');
      headings.each(function(heading, i) {
        collapsibles[i].hide();
      });
    });

    $('expand-all').addEvent('click',
    function(e) {
      e.stop();
      setHeadings();
      this.setStyle('display', 'none');
      $('collapse-all').setStyle('display', 'inline');
      headings.each(function(heading, i) {
        collapsibles[i].show();
      });
      $('wrapper').addClass('wrapper');
    });
  }
});
