// add the sliding forgot your password form on the login page
//window.addEvent('domready',function(){ forgotPassword();});

var _createForgotPassword = {};
_createForgotPassword.create = function(){

		var forgotPswd = $('forgotPswd');
		var forgotPswdArea = $('forgotPswdArea');
		var forgotPswdAdopt;

		var forgotPswdInput = new Element ("input",{
							'class' : 'text forgetPswd',
							'type' : 'text',
							'size' : '40'});

		var forgotPswdLabel = new Element ("p", {
			'class' : 'full forgetPswdHdr'
		});

		var forgotPswdSubmit = new Element ("input", {
			'class' : 'button',
			'type' : 'submit',
			'value' : 'Send'
		});

		var forgotPswdConfirmation;
		var forgotPswdConfirmationSlide;

		var response2 = function(){
			forgotPswdConfirmation = new Element ("p", {'class' : 'forgotPswdConfirmation'}).setText("Thanks! Check your email for your new password.");
			forgotPswdArea.adopt(forgotPswdConfirmation);
			forgotPswdConfirmationSlide = new Fx.Slide(forgotPswdConfirmation).hide(); 
			forgotPswdConfirmationSlide.slideIn();
		}

		var forgotPswdForm = new Element ("form", {
			'id' : 'forgotPswdForm',
			events : {
				submit : function ( e ) {
					//forgotField.blur(); // ?
					e = new Event ( e ); 
					e.stop(); // prevent the reload of the page
	
					if((emailAdr = forgotPswdInput.value.replace(/(^\s+|\s+$)/) ).length ) {
	
						forgotPswdFormSlide.slideOut();
						forgotPswdInput.value = "";
						
						$E('.forgetPswdHdr').removeClass("errorMessage");
						
						API.requestPassword ( emailAdr, {
									onComplete : response2,
									onError : response2
								} )
					}// /if emailAdr
	
					else {
						$E('.forgetPswdHdr').addClass("errorMessage");
					}
					
				}// /submit
			
			}/* /events */
			
		}/* /params of new Element */).adopt((forgotPswdLabel).setText("Enter your email and we'll send you a new password.")).adopt(forgotPswdInput).adopt(forgotPswdSubmit);
	

		forgotPswd.addEvent('click', function( e ){
			e = new Event ( e );
			e.stop();
	
			if (forgotPswdConfirmationSlide) forgotPswdConfirmationSlide.slideOut();
			
			if (forgotPswdConfirmation) { // remove confirmation so user repeating cycle doesnt see message again
				forgotPswdConfirmation.getParent().remove(); 
				forgotPswdConfirmation = '';
			};
			
			if(!forgotPswdAdopt) { // create the slide object and hide it, so then we can open it
				forgotPswdAdopt = forgotPswdArea.adopt(forgotPswdForm);
				forgotPswdFormSlide = new Fx.Slide(forgotPswdForm).hide(); 
			}

			forgotPswdFormSlide.toggle(); 
		}); // /forgotPswd.addEvent
};


dojo.provide( 'takkle.view.forgotPassword' );
dojo.declare( 'takkle.view.forgotPassword', takkle, _createForgotPassword);