function saveNameAndSubmit() {
    if ($('#first_run_name').val() !== '') {
        $('#hidden_profile_name').val($('#first_run_name').val());
        return true;
    }
    else {
        alert("Please enter a name.");
        return false;
    }
}

function updateProfileName() {
    if ($('#name').val() !== '') {
        $('#profile_name').html($('#name').val() + ' ');
        $('#hidden_profile_name').val($('#name').val());
        $('#change_name_popup').jqmHide();
        }
    else {
        alert("Enter the full name you'd like to use on the site.");
    }
}

function showNameChange(evt) {
    $('#updated_name').val($('#profile_name').html());
    $('#name_change_input').show();
}

function showFirstRunPopup() {
    $("#enter_name_popup").jqm({modal: true, onTop:true}); 
    $("#enter_name_popup").jqmShow();
}

function showSecondRunPopup() {
    $("#initial_help_popup").jqm({modal: true, onTop:true}); 
    $("#initial_help_popup").jqmShow();
    $("#initial_help_popup").jqmAddClose("#initial_help_close_button");
}

function showBioEditor() {
	$("#bio_ctl_txt").html("hide text editor");
    $("#bio_help_txt").html("enter any information about yourself and your background you'd like to share. your bio will be saved when you save your profile");
    $("#bio_control").unbind();
    $("#bio_control").bind('click', function() { hideBioEditor(); });
    $('#bio').show('slow');
    $("#bio_ctl_img").attr("src", "/images/shrink.gif");
    $("#bio_ctl_img").attr("alt", "Image button to click to hide text editor.");
    $("#bio_control").attr("title", "click to hide text editor");
    $("#bio_control").tooltip({ showURL: false, delay: 0 });
}

function hideBioEditor() {
    $("#bio_ctl_txt").html("show text editor");
    $("#bio_help_txt").html("describe yourself and your background");
    $("#bio_control").unbind(); 
    $("#bio_control").bind('click', function() { showBioEditor(); }); 
    $('#bio').hide('slow');
    $("#bio_ctl_img").attr("src", "/images/grow.gif");
    $("#bio_ctl_img").attr("alt", "Image button to click to show text editor.");
    $("#bio_control").attr("title", "click to show text editor");
    $("#bio_control").tooltip({ showURL: false, delay: 0 });
}

$(document).ready(function() {
    $('#update_name').bind('click', updateProfileName);
    $('#cancel_update_name').bind('click', function() {$('#name_change_input').hide()});
    $('#change_name_trigger').bind('click', showNameChange); 
    $('#basic_tags').jqm({modal: true, trigger: '#basic_tags_control'});
    $('#basic_tags').jqmAddClose('#basic_tags_close_dialog'); 
    $('#deep_tags').jqm({modal: true, trigger: '#deep_tags_control'});
    $('#deep_tags').jqmAddClose('#deep_tags_close_dialog');
    $('#new_work').jqmAddTrigger('.new_work_trigger'); 
    $(".rte-zone").rte("/css/rte.css", "/images/");   
    $('#profile_pic_upload').jqm({modal: true, trigger: '#profile_pic_upload_control'});
    $('#profile_pic_upload').jqmAddClose('#profile_pic_upload_close_dialog');
    $('#change_image_trigger').bind('click', function() {$('#image_change_input').toggle()});
    $('#initial_save_button').bind('click', saveNameAndSubmit);
    $('#bio_control').bind('click', function() {
 		showBioEditor();   	
    });
    $('form').submit( function() {
        $('input[type=submit]', this).attr('disabled', 'disabled');
        showUploadProgress();
    });
        $("#profile_tab").addClass("here");
    $('#change_name_popup').jqm({modal: true, onTop:true});
    $('#change_name_popup').jqmAddClose('#change_name_popup_close_dialog');
    $('#change_name_popup').jqmHide();
    $("#update_name_button").click(function() {updateProfileName()});
    $('#name_countdown').html("512");   
    $('#name').keydown(function(e) {
        if (e.keyCode == 13) {
            $("#update_name_button").click();
            return false;
        }
        limitText(document.forms.name_update.name, $('#name_countdown'), 512);
    }); 
    $('#name').keyup(function() {
        limitText(document.forms.name_update.name, $('#name_countdown'), 512);
    });
    
});

