var Login = function () {
    
    return {
        //main function to initiate the module
        init: function () {
        	
           $('.login-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                }
	            },

	            messages: {
	                username: {
	                    required: lang_login_username_is_required
	                },
	                password: {
	                    required: lang_login_password_is_required
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   
					$('.alert-error span', $('.login-form')).html(lang_login_enter_your_username_and_password);
	                $('.alert-error', $('.login-form')).show();
	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
					var action = $('#login-form').attr('action'),
						$button = $('#login-btn'),
						$username = $('#username'),
						$password = $('#password'),
						$captcha = $('#captcha-form'),
						$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
				
					$button.attr('disabled','disabled');
					
					$.post(action, {
							username: $username.val(),
							password: $password.val(),
							captcha: $captcha.val(),
							csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
						},
						function(data){
							$button.removeAttr('disabled');
							
							if (data == 'success') {
								window.location.href = base_url + "home/";
							}
							else {
								$('.alert-error span', $('.login-form')).html(data);
								$('.alert-error', $('.login-form')).show();
								
								$captcha.val('');
								var obj = $('#captcha');
								var tmp = obj.attr('src').split('?');
								obj.attr('src', tmp[0] + '?' + Math.random());
							}
						}
					);
	            }
	        });

	        $('.login-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.login-form').validate().form()) {
	                    var action = $('#login-form').attr('action'),
							$button = $('#login-btn'),
							$username = $('#username'),
							$password = $('#password'),
							$captcha = $('#captcha-form'),
							$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
					
						$button.attr('disabled','disabled');
						
						$.post(action, {
								username: $username.val(),
								password: $password.val(),
								captcha: $captcha.val(),
								csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
							},
							function(data){
								$button.removeAttr('disabled');
								
								if (data == 'success') {
									window.location.href = base_url + "home/";
								}
								else {
									$('.alert-error span', $('.login-form')).html(data);
									$('.alert-error', $('.login-form')).show();
									
									$captcha.val('');
									var obj = $('#captcha');
									var tmp = obj.attr('src').split('?');
									obj.attr('src', tmp[0] + '?' + Math.random());
								}
							}
						);
	                }
	                return false;
	            }
	        });

	        $('.forget-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            ignore: "",
	            rules: {
	                email: {
	                    required: true,
	                    email: true
	                }
	            },

	            messages: {
	                email: {
	                    required: lang_forgot_password_email_is_required
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   

	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	                var action = $('#forget-form').attr('action'),
						$button = $('#submit-btn'),
						$email = $('#email'),
						$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
				
					$button.attr('disabled','disabled');
					
					$.post(action, {
							email: $email.val(),
							csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
						},
						function(data){
							$button.removeAttr('disabled');
							
							var tmp = data.split('|');
							if (tmp[0] == '1') {
								$email.val('');
								$('.alert-success span', $('.forget-form')).html(tmp[1]);
								$('.alert-error', $('.forget-form')).hide();
								$('.alert-success', $('.forget-form')).show();
							}
							else {
								$email.val('');
								$('.alert-error span', $('.forget-form')).html(tmp[1]);
								$('.alert-success', $('.forget-form')).hide();
								$('.alert-error', $('.forget-form')).show();
							}
						}
					);
	            }
	        });

	        $('.forget-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.forget-form').validate().form()) {
	                    var action = $('#forget-form').attr('action'),
							$button = $('#submit-btn'),
							$email = $('#email'),
							$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
					
						$button.attr('disabled','disabled');
						
						$.post(action, {
								email: $email.val(),
								csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
							},
							function(data){
								$button.removeAttr('disabled');
								
								var tmp = data.split('|');
								if (tmp[0] == '1') {
									$email.val('');
									$('.alert-success span', $('.forget-form')).html(tmp[1]);
									$('.alert-error', $('.forget-form')).hide();
									$('.alert-success', $('.forget-form')).show();
								}
								else {
									$email.val('');
									$('.alert-error span', $('.forget-form')).html(tmp[1]);
									$('.alert-success', $('.forget-form')).hide();
									$('.alert-error', $('.forget-form')).show();
								}
							}
						);
	                }
	                return false;
	            }
	        });

	        jQuery('#forget-password').click(function () {
	            jQuery('.login-form').hide();
	            jQuery('.forget-form').show();
	        });

	        jQuery('#back-btn').click(function () {
	            jQuery('.login-form').show();
	            jQuery('.forget-form').hide();
	        });
			
			$('.reset-password-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            ignore: "",
	            rules: {
	                password: {
	                    required: true
	                },
					confirmPassword: {
	                    required: true
	                }
	            },

	            messages: {
	                password: {
	                    required: lang_forgot_password_password_is_required
	                },
					confirmPassword: {
	                    required: lang_forgot_password_confirm_password_is_required
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   

	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	                var action = $('#reset-password-form').attr('action'),
						$button = $('#submit-btn'),
						$password = $('#password'),
						$confirmPassword = $('#confirmPassword'),
						$email = $('#email'),
						$reset_password_key = $('#reset_password_key'),
						$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
				
					$button.attr('disabled','disabled');
					
					$.post(action, {
							password: $password.val(),
							confirmPassword: $confirmPassword.val(),
							email: $email.val(),
							reset_password_key: $reset_password_key.val(),
							csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
						},
						function(data){
							$button.removeAttr('disabled');
							
							var tmp = data.split('|');
							if (tmp[0] == '1') {
								$password.val('');
								$confirmPassword.val('');
								$('.alert-success span', $('.reset-password-form')).html(tmp[1]);
								$('.alert-error', $('.reset-password-form')).hide();
								$('.alert-success', $('.reset-password-form')).show();
							}
							else {
								$password.val('');
								$confirmPassword.val('');
								$('.alert-error span', $('.reset-password-form')).html(tmp[1]);
								$('.alert-success', $('.reset-password-form')).hide();
								$('.alert-error', $('.reset-password-form')).show();
							}
						}
					);
	            }
	        });

	        $('.reset-password-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.reset-password-form').validate().form()) {
	                    var action = $('#reset-password-form').attr('action'),
							$button = $('#submit-btn'),
							$password = $('#password'),
							$confirmPassword = $('#confirmPassword'),
							$email = $('#email'),
							$reset_password_key = $('#reset_password_key'),
							$csrf_metabets_ad_token = $('#csrf_metabets_ad_token');
					
						$button.attr('disabled','disabled');
						
						$.post(action, {
								password: $password.val(),
								confirmPassword: $confirmPassword.val(),
								email: $email.val(),
								reset_password_key: $reset_password_key.val(),
								csrf_metabets_ad_token: $csrf_metabets_ad_token.val()
							},
							function(data){
								$button.removeAttr('disabled');
								
								var tmp = data.split('|');
								if (tmp[0] == '1') {
									$password.val('');
									$confirmPassword.val('');
									$('.alert-success span', $('.reset-password-form')).html(tmp[1]);
									$('.alert-error', $('.reset-password-form')).hide();
									$('.alert-success', $('.reset-password-form')).show();
								}
								else {
									$password.val('');
									$confirmPassword.val('');
									$('.alert-error span', $('.reset-password-form')).html(tmp[1]);
									$('.alert-success', $('.reset-password-form')).hide();
									$('.alert-error', $('.reset-password-form')).show();
								}
							}
						);
	                }
	                return false;
	            }
	        });

	        $.backstretch([
		        base_url + "assets/image/bg/1.jpg",
		         base_url + "assets/image/bg/2.jpg",
		         base_url + "assets/image/bg/3.jpg",
		         base_url + "assets/image/bg/4.jpg"
		        ], {
		          fade: 1000,
		          duration: 8000
		      });
        }

    };

}();