var talent = {
    profile: {
        events: function(u) {
            this.baseurl = u;
            $("button[rel^=Profile]").click(function(e){
                e.preventDefault();
                $(this).parents("div.box").hide();
                var id = "#"+$(this).attr("rel");
                $(id).show();
                $(window).trigger("resize");
                if(ie) {
                    base.raphael();
                }
            });
            $("button[rel=next]").click(function(e){
                e.preventDefault();
                $(this).parents("div.box").find("button[rel^=Profile].selected").next().click();
            });
            $("button[rel=prev]").click(function(e){
                e.preventDefault();
                $(this).parents("div.box").find("button[rel^=Profile].selected").prev().click();
            });
            this.eduexp.init();
            this.ksa();
            this.certlisc(u);
            setInterval("talent.profile.pingtimer();",900000);
        },
        eduexp: {
            init: function(){
                this.edu_cnt = $("#Education > div").not(".raphael").length;
                this.exp_cnt = $("#Experience > div").not(".raphael").length;
                $("#add_experience, #add_education").click(this.generate);
                $("#Profile1 a[name=removeInputs]").unbind().click(this.remove);
                this.reset();
                $("#Experience div input:text[id^=Occupation]").suggest(talent.profile.baseurl+"OccupationSuggestions");
            },
            generate: function(e){
                var obj = talent.profile.eduexp;
                var div = $(this.parentNode).children("div:not(.raphael):first").clone().insertBefore(this).get(0);
                $(div).find("input, select").val("");
                var n = $(this.parentNode).children("div").not(".raphael").length;
                if($(this).parents().is('#Experience')){
                    $(div).find("p:first strong").text("Experience #"+n);
                    obj.exp_cnt++;
                    $(div).find("input:text[id^=Occupation]").suggest(talent.profile.baseurl+"OccupationSuggestions");
                }
                else {
                    $(div).find("p:first strong").text("Education #"+n);
                    obj.edu_cnt++;
                }
                $(div).find("a[name=removeInputs]").click(obj.remove);
                talent.profile.eduexp.reset();
            },
            reset: function(){
                $("#Experience > div").not(".raphael").each(function(i){
                    $(this).find("p:first strong").text("Experience #"+(i+1));
                    $(this).find("input,select").each(function(){
                        this.name = this.name.replace(/\[\d*\]$/,"["+i+"]");
                    });
                });
                $("#Education > div").not(".raphael").each(function(i){
                    $(this).find("p:first strong").text("Education #"+(i+1));
                    $(this).find("input,select").each(function(){
                        this.name = this.name.replace(/\[\d*\]$/,"["+i+"]");
                    });
                });
                this.edu_cnt == 3 ? $("#add_education").hide() : $("#add_education").show();
                this.exp_cnt == 3 ? $("#add_experience").hide() : $("#add_experience").show();
                if(ie) {
                    base.raphael();
                }
            },
            remove: function(e){
                e.preventDefault();
                var obj = talent.profile.eduexp;
                if($(this).parents().is('#Experience')) {
                    if(obj.exp_cnt == 1){
                        popup({
                            title: "Whoops!",
                            html: "You must have a least one work experience listed in your profile.",
                            error: true
                        });
                    } else {
                        obj.exp_cnt--;
                        $(this).parents("div:first").find("input:first").data("list").remove();
                        $(this).parents("div:first").remove();
                        $("#Experience div input:text[id^=Occupation]").unbind().suggest(talent.profile.baseurl+"OccupationSuggestions");
                        obj.reset();
                    }
                }
                else {
                    if(obj.edu_cnt == 1){
                        popup({
                            title: "Whoops!",
                            html: "You must have a least one education listed in your profile.",
                            error: true
                        });
                    } else {
                        obj.edu_cnt--;
                        $(this).parents("div:first").remove();
                        obj.reset();
                    }
                }
            }
        },
        ksa: function(){
            this.ksa_cnt = $("#Profile3 div.ksa input:checkbox:checked").parent().css({
                color: "#05f",
                fontWeight: "bold"
            }).length;
            $("#Profile3 div.ksa input:checkbox").click(function(e){
                if(this.checked){
                    if(talent.profile.ksa_cnt >= 10){
                        this.checked = false;
                        popup({
                            title: "Whoops!",
                            html: "You can only select ten attributes, please remove a currently selected attribute in order to add this one.",
                            error: true
                        });
                    }
                    else {
                        $(this).parent().css({
                            color: "#05f",
                            fontWeight: "bold"
                        });
                        talent.profile.ksa_cnt++;
                    }
                }
                else{
                    $(this).parent().css({
                        color: "",
                        fontWeight: "normal"
                    });
                    talent.profile.ksa_cnt--;
                }
            });
        },
        certlisc: function(u){
            $("#Profile2 input[id^=Certification]").suggest(u+"CertificationSuggestions",{
                onSelect: function() {}
            });
            $("#Profile2 input[id^=License]").suggest(u+"LicenseSuggestions",{
                onSelect: function() {}
            });
        },
        pingtimer: function(){
            $.get("/Smartt/a/talent/dashboard");
        }

    }
};