helper = {
  loader: function(action) {
    if(action == 'show')
      $('div#loader').fadeIn('fast');
    else
      $('div#loader').fadeOut('fast');
  },
  
  checkForm: function(path, form, callback) {

    var form = $(form);
    $('input[type="submit"]', form).click(function(e) {
      e.preventDefault();
      $.post(path, form.serialize(), function(data) {
      
        if(data.href) {
          $('div.message', form).html(data.msg.html).hide().slideDown(function() {
            document.location.href = data.href;
          });
        } else if(data.msg.type == 'highlight') {
          $('div.message', form).html(data.msg.html).hide().slideDown();
        } else if(data.msg == '') {
          // do nothing
        } else {
          $('div.message', form).html(data.msg.html).hide().slideDown().delay(3000).slideUp();
        }
        
        if(callback)
          callback(data);
        
      }, 'json');
    });
  }
}

contact = {
  __init: function() {
    helper.checkForm('/info/contact', 'form#contact', function(data) {
      if(data.msg.type == 'highlight')
        $('form#contact input[type="submit"]').fadeOut();
    });
  }
}

join = {
  __init: function() {
    helper.checkForm('/user/join', 'form#join', function(data) {
      if(data.msg == '')
        join.showConfirm();
    });
  },
  
  showConfirm: function() {
    $.post('/user/join/confirm', $('form#join').serialize(), function(data) {
      $(data).dialog({
        title     : 'Daten überprüfen',
        resizable : false,
        modal     : true,
        width     : 314,
        show      : "fade",
        hide      : "fade",
        buttons   : {
          'Jetzt registrieren': function() {
            join.showFinish();
            $(this).dialog('close');
          },
          'Daten anpassen': function() {
            $(this).dialog('close');
          }
        }
      });
    });
  },
  
  showFinish: function() {
    $.post('/user/join/finish', $('form#join').serialize(), function(data) {
      $('div#join').fadeOut(function() {
        $('div#join').html(data).fadeIn();
      });
    });
  }
}

login = {
  __init: function() {
    
    helper.checkForm('/login', 'form#loginForm', function(data) {
      if(data.agb == 0)
        login.showAgb();
    });
  }, 
  
  showAgb: function() {
    $('div#agb').dialog({
      title     : 'Neue AGB akzeptieren',
      resizable : false,
      modal     : true,
      width     : 400,
      show      : "fade",
      hide      : "fade",
      buttons   : {
        'Ich akzeptiere die neuen AGB': function() {
          $.post('/login', { agb: 1, email: $('form#loginForm input[name="email"]').val() }, function(data) {
            $.post('/login', $('form#loginForm').serialize(), function(data) {
              if(data.href) {
                $('form#loginForm div.message').html(data.msg.html).hide().slideDown(function() {
                  document.location.href = data.href;
                });
              }
            }, 'json');
          });
          $(this).dialog('close');
        },
        'Abbrechen': function() {
          $(this).dialog('close');
        }
      }
    });
  }
}

lostpassword = {
  __init: function() {
    helper.checkForm('/user/lostpassword', 'form#lostpassword', function(data) {
      if(data.msg.type == 'highlight')
        $('form#lostpassword input[type="submit"]').fadeOut();
    });
  }
}

changeProfile = {
  __init: function() {
    helper.checkForm('/user/settings', 'form#changeProfile');
  }
}

changePassword = {
  __init: function() {
    helper.checkForm('/user/settings', 'form#changePassword');
  }
}

// events/subscribe/
subscribe = {
  __init: function(event_id, paypal) {
    subscribe.bezahlChange();
    subscribe.profilDialog();
    helper.checkForm('/events/subscribe/id:'+event_id, 'form#subscribe');
  },
  
  bezahlChange: function() {
    $('div#radio').buttonset();
    $('div.paymentInfo div.paypal').hide();
    
    $('input[name="payment"]').click(function() {
      var id = $(this).attr('id');
      $('div.paymentInfo > div').hide();
      $('div.'+id).show();
    });
  },
  
  profilDialog: function() {
    var dialog = $('<div class="ui-helper-hidden"></div>').appendTo('div#subscribe');
    dialog.load('/user/settings', {}, function (responseText, textStatus, XMLHttpRequest) {});
      
    $('a.profilDialog').click(function(e) {
      e.preventDefault();
      
      dialog.dialog({
        title     : 'Profil anpassen',
        resizable : false,
        modal     : true,
        width     : 535,
        show      : "fade",
        hide      : "fade",
        open      : function(event, ui) {
          $dialog = $(event.target);
          // Ausgabe anpassen
          $('div', $dialog).removeClass('frame').removeClass('ui-widget').removeClass('ui-widget-content').removeClass('ui-corner-all');
          $('h2, form#changePassword, p', $dialog).hide();
          $('input[type="submit"]').button();
        },
        close     : function() {
          $('div#userdata').fadeOut().load(window.location.pathname+' div#userdata', function() {
            $(this).fadeIn();
          });
        }
      });
    });
  },
  
  paypalRegist: function(event_id) {
    $.post('/events/subscribe/id:'+event_id, {
      action: 'paypalRegist',
    }, function(data) {
       if(data.href) {
          $('div.paypal div.paypalMessage').html(data.msg.html).hide().slideDown(function() {
            document.location.href = data.href;
          });
        }
    }, 'json');
  }
}

$(document).ready(function() {

  $('input[type="submit"], input[type="button"], a.button').button();

  $('#flashMessages').fadeTo(0,0.5).animate({opacity: '+=0'},2500, function () {
    $(this).fadeOut();
  });
  
  $('input.datepicker').datepicker();
});


