var Mojosearch = {};

Mojosearch.check = function () {
	if ( ! jQuery) {
		setTimeout('Mojosearch.check()', 10);
	} else {
		Mojosearch.init();
	}
}

Mojosearch.init = function () {
//	jQuery.noConflict();

	jQuery('form#forgotten').hide();
	
	jQuery( 'a#forgotten_password' ).live( 'click', function(e) {
		
		e.preventDefault();
		
		jQuery('#mojo_login_error').text('');
		jQuery('#mojo_forgotten_error').text('');

		jQuery('form#login').hide();
		jQuery('form#forgotten').show();

		return false;
	});
	
	jQuery( 'a#login' ).live( 'click', function(e) {
		
		e.preventDefault();
		
		jQuery('#mojo_login_error').text('');
		jQuery('#mojo_forgotten_error').text('');
		
		jQuery('form#forgotten').hide();
		jQuery('form#login').show();

		return false;
	});
	
	// Submit forgotten password form.
	jQuery('form#login').submit(function(e) {
		e.preventDefault();
		
		jQuery('#mojo_login_error').text('');
		jQuery('#mojo_forgotten_error').text('');
						
		jQuery.post( 
			jQuery('form#login').attr('action') + '/login/process_ajax',
			jQuery('form#login').serialize(), 
			function(response){
				if ( response.login_status == 'success' ) {					
										
					window.location.href = window.location.href;
					window.location.reload(true);
					
				} else {
					jQuery('#mojo_login_error').text( response.message );
				}
			}, 'json' 
		);
		return false;
	});
	
	// Submit forgotten password form.
	jQuery('form#forgotten').submit(function(e) {
		e.preventDefault();
		
		jQuery('#mojo_login_error').text('');
		jQuery('#mojo_forgotten_error').text('');
						
		jQuery.post( 
			jQuery('form#forgotten').attr('action') + '/admin/addons/mojologin/forgotten_password_ajax', 
			jQuery('form#forgotten').serialize(), 
			function(response){
				if ( response.login_status == 'success' ) {					
					jQuery('#mojo_forgotten_success').text( response.message );
				} else {
					jQuery('#mojo_forgotten_error').text( response.message );
				}
			}, 'json' 
		);
		return false;
	});
}

setTimeout('Mojosearch.check()', 10);


// The approach above is derived from Dan Horrigan's "Equipment" addon for MojoMotor.
// https://github.com/dhorrigan/mojo-equipment
