var registrationFormInputEmptyLabels = {
    'new_user_email': '',
    'new_user_email_confirmation': '',
    '_pwd_new_user_password': '',
    '_pwd_new_user_password_confirmation': '',
    'captcha': ''
};

function initRegistrationFormValidators() {
    if ($('#registration-form').length == 0) return;
    addInputValidator('new_user_email', function(value) {
        $('#new_user_email_confirmation').trigger('validate');
        updateCurrentProgress('new_user_email', false);
        if (value.length && value != registrationFormInputEmptyLabels['new_user_email']) {
            emailPattern = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i);
            if (emailPattern.test(value)) {
                updateCurrentProgress('new_user_email', true);
                return true;
            }
            else {
                updateCurrentProgress('new_user_email', false);
                return "Should be valid e-mail address";
            }
        } else return false;
    }); $('#new_user_email').trigger('validate');
    addInputValidator('new_user_email_confirmation', function(value) {
        updateCurrentProgress('new_user_email_confirmation', false);
        if (value.length && value != registrationFormInputEmptyLabels['new_user_email_confirmation']) {
            emailField = $('#new_user_email');
            if (emailField.length && emailField.val() == value) {
                updateCurrentProgress('new_user_email_confirmation', true);
                return true;
            }
            else {
                updateCurrentProgress('new_user_email_confirmation', false);
                return "Doesn't match confirmation email field"
            }
        } else return false;
    }); $('#new_user_email_confirmation').trigger('validate');
    addInputValidator('new_user_password', function(value) {
        updateCurrentProgress('new_user_password', false);
        $('#new_user_password_confirmation').trigger('validate');
        if (value.length && value != registrationFormInputEmptyLabels['new_user_password']) {
            if (value.length < 4) {
                updateCurrentProgress('new_user_password', true);
                return "Password must be at least 3 letters";
            }
            else {
                updateCurrentProgress('new_user_password', false);
                return true;
            }
        } else return false;
    }); $('#new_user_password').trigger('validate');
    addInputValidator('new_user_password_confirmation', function(value) {
        updateCurrentProgress('new_user_password_confirmation', false);
        if (value.length && value != registrationFormInputEmptyLabels['new_user_password_confirmation']) {
            passwordField = $('#new_user_password');
            if (passwordField.length && passwordField.val() == value) {
                updateCurrentProgress('new_user_password_confirmation', true);
                return true;
            }
            else {
                updateCurrentProgress('new_user_password_confirmation', false);
                return "Doesn't match confirmation password field";
            }
        } else return false;
    }); $('#new_user_password_confirmation').trigger('validate');
    addInputValidator('captcha', function(value) {
        if (value.length) {
            updateCurrentProgress('captcha', true);
            return true;
        }
        else {
            updateCurrentProgress('captcha', false);
            return false;
        }
    }); $('#captcha').trigger('validate');
    addCheckBoxValidator('usa_resident', function(state) {
        // code look strange, huh? believe me - that is made by purpose. =)
        if (state) {
            updateCurrentProgress('usa_resident', true);
            return true;
        }
        else {
            updateCurrentProgress('usa_resident', false);
            return false;
        }
    }); $('#usa_resident').trigger('validate');
    addCheckBoxValidator('years_18_old', function(state) {
        // look up
        if (state) {
            updateCurrentProgress('years_18_old', true);
            return true;
        }
        else {
            updateCurrentProgress('years_18_old', false);
            return false;
        }
    });   $('#years_18_old').trigger('validate');
    addCheckBoxValidator('terms_of_service', function(state) {
        // look up
        if (state) {
            updateCurrentProgress('terms_of_service', true);
            return true;
        }
        else {
            updateCurrentProgress('terms_of_service', false);
            return false;
        }
    }); $('#terms_of_service').trigger('validate');
    addCheckBoxValidator('facebook', function(state) {
        // look up
        if (state) {
            updateCurrentProgress('facebook', true);
            return true;
        }
        else {
            updateCurrentProgress('facebook', false);
            return false;
        }
    }); $('#facebook').trigger('validate');
    for (inputId in registrationFormInputEmptyLabels) {
        if (inputId.substring(0, 5) == "_pwd_")
            addEmptyPasswordLabelHandler(inputId, registrationFormInputEmptyLabels[inputId]);
        else
            addEmptyInputLabelHandler(inputId, registrationFormInputEmptyLabels[inputId]);
    }
}

function addEmptyInputLabelHandler(id, emptyLabel) {
    el = $('#' + id); if (el.length) {
        el.bind('focus', function() {
            if ($(this).val() == emptyLabel) $(this).val("");
        });
        el.bind('blur', function() {
            if ($(this).val().length == 0) $(this).val(emptyLabel);
        });
        if (el.val() == "") el.val(emptyLabel);
    }
}

function addEmptyPasswordLabelHandler(id, emptyLabel) {
    el = $('#' + id); if (el.length) {
        el.bind('focus', function() {
            pwdField = $('#' + id.substring(5));
            if (pwdField.length) {
                $(this).hide();
                pwdField.show().focus();
            }
        });
        pwdField = $('#' + id.substring(5)); if (pwdField.length) {
            pwdField.bind('blur', function() {
                if ($(this).val().length == 0) {
                    $(this).hide();
                    $('#_pwd_' + $(this).attr('id')).show();
                }
            });
        }
        if (pwdField.val().length == 0) el.val(emptyLabel);
        else {
            el.val(emptyLabel).hide();
            pwdField.show();
        }
    }
}

function addInputValidator(id, validator) {
    el = $('#' + id); if (el.length) {
        el.bind('keyup validate', function() {
            validationResult = validator($(this).val());
            if (validationResult === true)
                validateFieldIsCorrect(id);
            else if (validationResult === false)
                validateFieldIsEmpty(id);
            else
                validateFieldHaveErrors(id, validationResult);
        });
        el.bind('blur', function() {
            errDiv = $('#error_' + $(this).attr('id')); if (errDiv.length) {
                if (errDiv.html().length) errDiv.slideDown(300);
                else if (errDiv.is(':visible')) errDiv.slideUp(300);
            }
        });
    }
}

function addCheckBoxValidator(id, validator) {
    el = $('#' + id); if (el.length) {
        el.bind('change validate', function() {
            validationResult = validator($(this).attr('checked'));
            if (validationResult === true)
                validateFieldIsCorrect(id);
            else if (validationResult === false)
                validateFieldIsEmpty(id);
            else
                validateFieldHaveErrors(id, validationResult);
            return false;
        });
    }
}

function validateFieldIsCorrect(id) {
    elCloud = $('#hint_cloud_' + id); if (elCloud.length) {
        elCloud.removeClass('cloud_black').removeClass('cloud_orange').addClass('cloud_yellow');
        updateCurrentProgress(id, true);
    }
    errDiv = $('#error_' + id); if (errDiv.length) {
        if (errDiv.is(':visible'))
            errDiv.slideUp(300, function() {
                $(this).html("");
            });
        else
            errDiv.html("");
    }
    // check if all is correct
    if ($('.cloud_black').length == 0 && $('.cloud_orange').length == 0)
        enableSubmitButton(true);
}

function validateFieldIsEmpty(id) {
    elCloud = $('#hint_cloud_' + id); if (elCloud.length) {
        elCloud.removeClass('cloud_yellow').removeClass('cloud_orange').addClass('cloud_black');
        updateCurrentProgress(id, false);
    }
    errDiv = $('#error_' + id); if (errDiv.length) {
        if (errDiv.is(':visible'))
            errDiv.slideUp(300, function() {
                $(this).html("");
            });
        else
            errDiv.html("");
    }
    enableSubmitButton(false);
}

function validateFieldHaveErrors(id, message) {
    elCloud = $('#hint_cloud_' + id); if (elCloud.length) {
        elCloud.removeClass('cloud_black').removeClass('cloud_yellow').addClass('cloud_orange');
        updateCurrentProgress(id, false);
    }
    errDiv = $('#error_' + id); if (errDiv.length) {
        errDiv.html(message);
    }
    enableSubmitButton(false);
}

function submitRegistrationForm() {
    $('#registration-form').submit();
    return true;
}

function enableSubmitButton(enable) {
    submitButton = $('#acc_next'); if (submitButton.length) {
        if (enable) {// && submitButton.is('.disabled')) {
            //submitButton.removeClass('disabled').addClass('b_register');
            //submitButton.unbind('click');
            submitButton.click(submitRegistrationForm);
        } else if (!enable) {// && submitButton.is('.b_register')) {
            //submitButton.removeClass('b_register').addClass('disabled');
            //submitButton.unbind('click');
            submitButton.click(function() {
                return false;
            });
        }
    }
}

function progressBars() {
    if ($('.reg-process').length > 0 || $('.xt-track').length > 0) {
        $('.reg-process').xtProgressbar({
            maxValue: 100,
            changeCallback: function() {
                if ($(this).find('.text').position().left + $(this).find('.text').width() > $(this).find('.xt-track').width()) {
                    $(this).find('.text').hide();
                    $(this).find('.text').css('right', '10px');
                    $(this).find('.text').show();
                }
            }
        });
    }
}

$(document).ready(function() {
    $("#a_demo_video").click(function() {
        TutorialVideo.setCurrentVideoByName("demo");
        TutorialVideo.showVideo();
        return false;
    });
		
    $('#registration-progress').find('.text').css('right', -($('#registration-progress').find('.text').width() + 10) + 'px');

    Cufon.replace('.cufon');
    Cufon.now();

    enableSubmitButton(false);
    $("input:checkbox").checkize();
    initRegistrationFormValidators();

    setTimeout(progressBars, 100);
});


/*
Alain's javascript functions for miscellaneous things
NOTE: this is here until it can be determined whether to integrate these functions into the main
application javascript file.
*/

/* This is used on users/activate in order to toggle the TRANSPARENCY STATEMENT */
function slidedown_toggle(id) {
    if ($(id).is(":hidden")) {
        $(id).slideDown("slow");
    } else {
        $(id).slideUp("slow");
    }
}
