window.addEvent('domready', function() {
  $each($$('.flowplayer'),function(obj,ind){          
    flowplayer(obj, "/scripts/flowplayer-3.2.7.swf");
  });
  new FaceTip($$('.glossar'));
  if ($('uebersicht')){
    $('uebersicht').getElements('h1').setStyle('margin-bottom',4);
    $('uebersicht').getElements('h1').setStyle('margin-top',4); 
  }
  if ($('toggle')){
    $('containerContent').getElements('span.toggle').setStyle('display','none');
    $('containerContent').getElements('h1').addClass('closed');
    $('containerContent').getElements('h1').setStyle('margin-bottom',4);
    $('containerContent').getElements('h1').setStyle('margin-top',4);
    $('containerContent').getElements('h1').addClass('hborder');
    if ($('containerContent').getElement('span.toggle')) $('containerContent').getElement('span.toggle').setStyle('display','block');
    if ($('containerContent').getElement('h1')) $('containerContent').getElement('h1').addClass('opened');
    if ($('containerContent').getElement('h1')) $('containerContent').getElement('h1').removeClass('closed');
    
    $('containerContent').getElements('h1').addEvent('click',function(ev){
      if (ev.target.hasClass('opened')){
        ev.target.removeClass('opened');
        ev.target.addClass('closed');
        ev.target.getNext().setStyle('display','none');
      } else {
        ev.target.removeClass('closed');
        ev.target.addClass('opened');
        ev.target.getNext().setStyle('display','block');
      }
    });
  }
  if ($('anfrage')){
        submit=new Element('input', {'class': 'submit','type': 'button', 'value': 'Anfrage absenden'}).inject($('anfrage'));        
        submit.addEvent('click', function(){        
          submit.setProperty('disabled','true');
          submit.set('value', 'Daten werden gesendet ...');
          
          jsonRequest=new Request.JSON({encoding:'utf-8',noCache:true,url: '/assets/json/mailer.php',onSuccess: function(responseJSON, responseText){
            submit.removeProperty('disabled');
            //alert('send');
            if (responseJSON.error){
              $each(responseJSON.error,function(obj,ind){          
                $(obj).addClass('error');
                //alert(obj);
              });
            }
            if (responseJSON.success){
              $each(responseJSON.success,function(obj,ind){
                $(obj).removeClass('error');
                //alert(obj);
              });
            }
            submit.set('value', 'Anfrage absenden');
            if (responseJSON.error){
              //alert(responseJSON.error);
            } else {
              
              submit.setProperty('disabled','true');
              submit.set('value','Anfrage absenden');              
              thanx=new Element('div', {'html': '<br/><b>Vielen Dank f&uuml;r Ihre Anfrage</b>'}).inject($('containerContent'));
              $('anfrage').destroy();              
            }
          }}).send({method: 'post',data: $('anfrage').toJSON()}); 
        });
   }
});

Element.implement({
    toJSON: function(){
        var json = {};
        this.getElements('input, select, textarea', true).each(function(el){
            if (!el.name || el.disabled || el.type == 'submit' || el.type == 'reset' || el.type == 'file') return;
            var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
                return opt.value;
            }) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
            $splat(value).each(function(val){
                if (typeof val != 'undefined') {
                    json[el.name] = val;
                }
            });
        });
        return json;
    }
});
