(function($) {
    var submit_form = true;

    var fill_blank_slugs = function () {
        var inputs = $('#section_set-group input[type=text]'),
        regex_name = /[0-9]+-name$/;
        inputs.each(function(index, el){
            var match_name = regex_name.exec(el.id);
            if (match_name) {
                var slug_id = el.id.replace('name', 'slug');
                if (el.value != "" && document.getElementById(slug_id).value == "") {
                    var slug_name = URLify(document.getElementById(el.id).value, 50);
                    var e = document.getElementById(slug_id);
                    e.value = slug_name;
                }
            }
        });
    };

    var check_slugs = function () {
        var inputs = $('#section_set-group input[type=text]'),
        regex_name = /[0-9]+-name$/,
        slug_names = {};
        submit_form = true;
        inputs.each(function(index, el){
            var match_name = regex_name.exec(el.id);
            if (match_name) {
                var slug_id = el.id.replace('name', 'slug');
                var slug_name = document.getElementById(slug_id).value;
                var slug_names_closure = slug_names;
                if (slug_names_closure[slug_name]) {
                    slug_names_closure[slug_name] += 1;
                    $('#'+slug_id).addClass('slug_error');
                    submit_form = false;
                }
                else {
                    slug_names_closure[slug_name] = 1;
                    $('#'+slug_id).removeClass('slug_error');
                }
            }
        });
    };

    var create_keyup_events = function () {
        var inputs = $('#section_set-group input[type=text]'),
        regex_name = /[0-9]+-name$/;
        regex_slug = /[0-9]+-slug$/;

        inputs.each(function(index, el){
            var match_name = regex_name.exec(el.id);
            var match_slug = regex_slug.exec(el.id);
            if (match_name) {
                var slug_id = el.id.replace('name', 'slug');
                $(el).keyup(function() {
                    var slug_id_closure = slug_id;
                    var slug_name = URLify(document.getElementById(el.id).value, 50);
                    var e = document.getElementById(slug_id_closure);
                    
                    e.value = slug_name;
                    check_slugs();
                });
            }
            else if (match_slug) {
                var slug_id = el.id.replace('name', 'slug');
                $(el).keyup(function() {
                    check_slugs();
                });
            }
        });
    };


    $(document).ready(function(){
        fill_blank_slugs();
        check_slugs();

        var form = $('form:first');
        form.submit(function() {
            if (submit_form) {
                return true;
            }
            else {
                alert("Form contains errors, check slugs are unique.");
                return false;
            }
        });

        create_keyup_events();
    });

    $(window).bind('load', function() {
        $('tr.add-row a').click(function(){
            create_keyup_events();
        });
    });

})(django.jQuery);
