window.addEvent('domready', function() {
  // switch css-styles to js mode - nice and clean non-destructive hack
  document.body.className = "jsact";
  
  var teasers = $$('.teaser');
  for ($i=0; $i<teasers.length; $i++) {
    teasers[$i].parentNode.onclick = function() {
      toggle(this, 'act');
    };
  }
  
  if ($('fce_blattnav')) {
   var leaves = $$('#fce_blattnav .blatt');
   for (var i=0; i<leaves.length; i++) {
     leaves[i].onmouseover = function() { 
       holdSlideshow();
       makeThisAct($(this), 'blatt', 'blatt-act');
      };
     leaves[i].onmouseout = function() { runSlideshow(4)};
   }
   makeFirstAct($('fce_blattnav'));
   runSlideshow(4);
  }

  if ($('content')) {
	// attach onclick event listener to all .item(s)
	$$('#content .toggle_item').each(function(item) {
		item.addEvent('click', function(event) {
			var target = $(event.target);
			// check that target is as expected
			if(target.hasClass('toggle')) {
				this.toggleClass('active');
				target.toggleClass('active');
				target.hasClass('active') ? target.innerHTML = 'verstecken…' : target.innerHTML = 'mehr…';
			}
		})
	});
  }
  
  if ($('request')) {
  	var defaultmsg = "Telefonnummer oder E-Mail";
  	$('request-button').getElement('input').set('opacity', '0.6').set('disabled', 'disabled');
    $('request').getElement('.personal-data').addClass('field-content').set('value', defaultmsg);
    $('request').getElement('.personal-data').addEvent('click', function(){
      (this).set('value', '').removeClass('field-content');
    });
    $('request').getElement('.personal-data').addEvent('focus', function(){
		$('request-button').getElement('input').set('opacity', '1');
    });
    $('request-button').addEvent('mouseover', function(){
    	if ($('request').getElement('.personal-data').get('value').toString() != defaultmsg) {
    	    $('request-button').getElement('input').erase('disabled');
    	}
    });
  }
});

function toggle(el, className) {
  el = $(el);
  var cur = el.parentNode.firstChild;
  do {
    if(cur == el && cur.className != className) {
      cur.className = className;
    } else {
      cur.className = "";
    }
  } while (cur = cur.nextSibling);
}

function wrap ($target, $tagName, $attributes) {  
  // use only 'clean' attribute objects
  // right now it's too troublesome to filter out unsettable attributes
  // unable to just point to an element with the right values (although that would be desirable)
  var $wrap = document.createElement ($tagName);
  for ($att in $attributes) 
    set_attribute ($wrap, $att, $attributes[$att]);
  $target.parentNode.insertBefore ($wrap, $target);
  $wrap.appendChild ($target);
  return $wrap;
}

function set_attribute ($target, $name, $value) {
  if (typeof $value == 'object')
    for ($att in $value) 
      set_attribute ($target[$name], $att, $value[$att]);
  else
    $target.setAttribute ($name,$value);
}

Element.implement ({
  wrap: function ($tagName, $attributes) {
    return wrap (this, $tagName, $attributes);
  }
})