(function($) {

    $(function() {

        var validationPattern = [
            {
                field: $('#signup_username'),
                required: { error: 'screen name is required' },
                minLength: { value: 3, error: 'screen name minimum 3 characters' }
            }, {
                field: $('#signup_password'),
                confirmation: { field: $('#signup_password_bis'), error: 'password does not match' },
                required: { error: 'password is required' },
                minLength: { value: 6, error: 'Password minimum 6 characters' }
            }, {
                field: $('#signup_email'),
                pattern: { value: /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, error: 'Please enter valid email' },
                confirmation: { field: $('#signup_email_bis'), error: 'email does not match' },
                required: { error: 'email is required' }
            }, {
                field: $('#signup_captcha'),
                required: { error: 'Please enter letters from captcha' }
            }, {
                field: $('#signup_tos_agree'),
                checked: { error: 'Please agree to terms of membership' }
            }, {
                field: $('#signup_age_certified'),
                checked: { error: 'You must be 18 or older to join FindFred' }
            }
        ];

        (function(patterns) {
            var check = function(s) {
                if (s.field.val().length) {
                    s.confirmation.label.enable();
                    s.confirmation.field.enable();
                } else {
                    s.confirmation.label.disable();
                    s.confirmation.field.disable(true);
                }
            };
            $.each(patterns, function() {
                var i = this;
                if (!i.confirmation) return;
                i.confirmation.label = $('label[for=' + i.confirmation.field.attr('id') + ']');
                check(i);
                i.field.blur(function() {
                    check(i);
                });
                i.field.keyup(function() {
                    check(i);
                });
            });
        })(validationPattern);
        
        var validationByPattern = function(validationPattern) {
            var valid = true;

            var setErrorFor = function(el, error) {
                valid = false;
                el.parent().parent().find('.error').html(error);
            };

            var checkCondition = function(condition) {
                return $.isFunction(condition) ? condition() : (condition || condition === undefined);
            };

            $.each(validationPattern, function() {

                var i = this;
                var val = i.field.val();

                if(val) {
                    if(i.checked && !i.field.attr('checked') && checkCondition(i.checked.condition))
                        setErrorFor(i.field, i.checked.error);

                    if(i.confirmation && val != i.confirmation.field.val() && checkCondition(i.confirmation.condition))
                        setErrorFor(i.field, i.confirmation.error);

                    if(i.pattern && !i.pattern.value.test(val) && checkCondition(i.pattern.condition))
                        setErrorFor(i.field, i.pattern.error);

                    if(i.exLength && val.length == i.exLength.value && checkCondition(i.exLength.condition))
                        setErrorFor(i.field, i.exLength.error);

                    if(i.maxLength && val.length > i.maxLength.value && checkCondition(i.maxLength.condition))
                        setErrorFor(i.field, i.maxLength.error);

                    if(i.minLength && val.length < i.minLength.value && checkCondition(i.minLength.condition))
                        setErrorFor(i.field, i.minLength.error);
                    
                    if(i.required && !val.length && checkCondition(i.required.condition))
                        setErrorFor(i.field, i.required.error);
                }
            });

            return valid;
        };
   
        $('#signup').submit(function() {
            $('.error').empty();
            return validationByPattern(validationPattern);
        });

        $("input, select")
            .focus(function(){
                $(this).addClass('focused');
            })
            .blur(function(){
                $(this).removeClass('focused');
            });
        
        var $birthday = $('#signup_birthday');
        
     /*   $birthday.focus(function() { $birthday.blur(); }).datepicker({
            dateFormat: 'yy-mm-dd',
            hideIfNoPrevNext: true,
            yearRange: '1900:' + ((new Date()).getFullYear() - 18),
            minDate: '-100y',
            maxDate: '-18y',
            firstDay: 1,
            changeYear: true,
            changeMonth: true,
            showOtherMonths: true,
            dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S']
        });    */



        if(!$birthday.val().length) {
            $birthday.val('1980-01-01');
        }
        

		$.editDayAccordingToMonth($("#birth_month"), $("#birth_day"), $("#birth_year"));
        
         $('.birthday_separate').change(function(value){
           var currSelect = $(this);
           if (currSelect.attr('id') != 'birth_day'){
           		$.editDayAccordingToMonth($("#birth_month"), $("#birth_day"), $("#birth_year"));
           }
           var day =  ($("#birth_day option:selected").val()<10)?("0"+$("#birth_day option:selected").val()):$("#birth_day option:selected").val();
           var month = ($("#birth_month option:selected").val()<10)?("0"+$("#birth_month option:selected").val()):$("#birth_month option:selected").val();
           var year = $("#birth_year option:selected").val();
           $birthday.val(year+"-"+month+"-"+day);
        });
        
    });

})(jQuery);

