var Physio = {
  
  user: null,
  cal: null,
  registerValidator: null,
  
	init: function() {
	  Locale.use('de-DE');
    Physio.addEvents();
    //FB.XFBML.parse();
  },
	
	addEvents: function() {
    
    $('closeTheater').addEvent('click', function() {
      $('theater').setStyle('display', 'none');
      document.html.removeClass('theatermode');
    });
    
    if($('action')) {
      var fadeout = new Fx.Morph('action', {
          duration: 'long',
          transition: Fx.Transitions.Sine.easeOut
      });
      fadeout.start({
        'opacity': 1,
      }).chain(function() {
        this.start.delay(1000, this, {
          'opacity': 0,
          'height': 0,
          'padding': 0
        });
      });
    }
    
		switch(window.location.pathname.split("/")[1]) {
		  case "registration":
        Physio.addFormValidation();
		    break;
		  case "termin":
		    therapist = (new URI()).get('fragment');
		    if(therapist == '') {
		      therapist = 'brost';
        }
		    Physio.cal = new Calendar($("calendar"), therapist);
		    $("chooseTherapist").getChildren().each(function(therapist) {
		      therapist.addEvent("click", function(e) {
		        Physio.cal = new Calendar($("calendar"), e.target.href.split("#")[1]);
          });
        });
		    break;
		  case "verification":
		    
		    break;
		  case "techniken":
		    $$(".technics-box").each(function(box) {
		      box.fade('out');
		      box.setStyle("display", "none");
        });
        $("technics-list").getChildren().each(function(technik) {
          technik.addEvent("click", function(e) {
            Physio.chooseTech(e.target.href.split("#")[1]);
          });
        });
        var choosen = window.location.hash.substr(1);
        if($(choosen)) {
          Physio.chooseTech(choosen);
        } else {
          Physio.chooseTech('manuelle');
        }
		    break;
		  case "ueber":
		    new Fx.Accordion($('acc-uli'), '#acc-uli h2', '#acc-uli .acc-content');
		    new Fx.Accordion($('acc-moni'), '#acc-moni h2', '#acc-moni .acc-content');
		    break;
    }
	},
	
	loginFormResponse: function(response) {
		response = JSON.decode(response);
		if(response.error) {
			$("headerLogin").highlight("#f00");
		} else {
		  $('loginMail').set('value', '');
		  $('loginPw').set('value', '');
		  Physio.user = response.user;
		  $('loggedInMail').set('text', Physio.user.mail);
		  $("loginFormDiv").fade("out");
		  $("loggedInDiv").fade("in");
    }
	},
	
	logout: function() {
		new Request.JSON({url: "/requests/user.php", onSuccess: Physio.logoutResponse}).post({"action": "logout"});	
		Physio.user = null;
		$("loginDiv").fade("in");
    $("loggedInDiv").fade("out");
    $('loggedInMail').set('text', '');
	},
	
	addFormValidation: function() {
    Physio.registerValidator = new Form.Validator.Inline($('registerForm'), {
      onFormValidate: Physio.submitRegisterForm,
      onElementPass: Physio.passRegisterElement,
      onElementFail: Physio.failRegisterElement,
      errorPrefix: ''
    });
    
    Physio.registerValidator.add('containAlpha', {
      test: function(element) {
        return element.get('value').length != 0 ? element.get('value').match(/[a-zA-Z]+/g) : true;
      },
      
      errorMsg: 'Das Passwort muss mindestens einen Buchstaben enthalten.'
    });
    
    Physio.registerValidator.add('containNum', {
      test: function(element) {
        return element.get('value').length != 0 ? element.get('value').match(/\d+/g) : true;
      },
      
      errorMsg: 'Das Passwort muss mindestens eine Ziffer enthalten.'
    });
    
    Physio.registerValidator.add('containSpecial', {
      test: function(element) {
        return element.get('value').length != 0 ? element.get('value').match(/[!"#\$%&'\(\)\*\+,-.\/:;<=>\?@\[\\\]\^_`\{|\}~\.)]+/g) : true;
      },
      
      errorMsg: 'Das Passwort muss mindestens ein Sonderzeichen enthalten. (Satzzeichen wie: !"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~.)'
    });
    
    Physio.registerValidator.add('validate-alpha', {
      test: function(element) {
        return Form.Validator.getValidator("IsEmpty").test(element) || (/^[a-zäöüA-ZÄÖÜß]+$/).test(element.get("value"));
      },
      
      errorMsg: 'Geben Sie in diesem Eingabefeld bitte nur Buchstaben ein. Leerzeichen und andere Zeichen sind nicht erlaubt.'
    });
    
    new Tips('.formRow', {
      className: 'tooltip',
      fixed: true,
      offset: {
        x: 0,
        y: 0
      }
    });
  },
  
  passRegisterElement: function(element, t) {
    element.getParent().setStyle('color', '#000');
  },
  
  failRegisterElement: function(element) {
    element.getParent().setStyle('color', '#ab111c');
    element.setStyle('background-color', '#ffcccc');
  },
	
	validateRegisterForm: function(e) {
    e.stop();
    Physio.registerValidator.validate();
  },
	
	chooseTech: function(tech) {
    $$(".technics-box").each(function(box) {
      box.setStyle("display", "none");
      box.fade('out');
    });
    $("technics-list").getChildren().each(function(t) {
      t.getFirst().setStyle('color', '#F58020');
    });
    $('technics-list').getElement('a[href$='+tech+']').setStyle('color', '#ab111c');
    var box = $($('technics-list').getElement('a[href$='+tech+']').href.split("#")[1]);
    box.setStyle('display', 'block');
    box.fade('in');
  },
  
  chooseTherapist: function(therapist) {
    console.log(therapist);
  }
}

window.addEvent("domready", function() {
	Physio.init();
});
