function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 


$(document).ready(function(){
        
    $("#discover_nav_link").toggle(
        
        function() {
            $("#discover_nav_item").addClass("clicked");
            $("#discover_menu").show();
            
            $(document).one("click", function() {
                $("#discover_nav_item").removeClass("clicked");
                $("#discover_menu").hide(); 
            });
        },
        
        function() {
            $("#discover_nav_item").removeClass("clicked");
            $("#discover_menu").hide();
        }
        
    );
    
    $("#login_nav_link").click(function(e) {
        e.preventDefault();
        $("#login_menu").toggle();
        $("#login_nav_item").addClass('clicked')
    });
    
    $("#login_menu").mouseup(function(){
        return false;
    });
    
    $(document).mouseup(function(e){
        if($(e.target).parent('#login_nav_link').length == 0) {
            $("#login_nav_item").removeClass('clicked');
            $("#login_menu").hide();
        }
    });
  
    $("#logged_in_user_link").toggle(
        
        function() {
            $("#logged_in_user_options").addClass("clicked");
            $("#logged_in_user_menu").show();
            
            $(document).one("click", function() {
                $("#logged_in_user_options").removeClass("clicked");
                $("#logged_in_user_menu").hide();
            });
        },
        
        function() {
            $("#logged_in_user_options").removeClass("clicked");
            $("#logged_in_user_menu").hide();
        }
    );
    
    $("#save_basic_info").click(function() {
        
        $.post('/profiles/save/basic_info/',
            
            {
                'first_name'    : $("#id_first_name").val(),
                'last_name'     : $("#id_last_name").val(),
                'date_of_birth' : $("#id_date_of_birth").val(),
                'city'          : $("#id_city").val(),
                'state'         : $("#id_state").val(),
                'postal_code'   : $("#id_postal_code").val()
            },
            
            function(data) {
                
                if(data.success == true) {
                    $("#save_basic_info").after('Saved!');
                } else {
                    $("#save_basic_info").after('Save Failed!');
                }
                
            }, 'json' // close callback
            
            
        ); // close post
        
    }); // close save_basic_info click
    
    $("#save_physical_attributes").click(function() {
        
        $.post('/profiles/save/physical_attributes/',
            {
                'height'        : $("#id_height").val(),
                'weight'        : $("#id_weight").val(),
                'eye_color'     : $("#id_eye_color").val(),
                'skin_color'    : $("#id_skin_color").val(),
                'hair_length'   : $("#id_hair_length").val(),
                'hair_color'    : $("#id_hair_color").val(),
                'ethnicity'     : $("#id_ethnicity").val()
            },
            
            function(data) {
                
                if(data.success == true) {
                    $("#save_physical_attributes").after('Saved!');
                } else {
                    $("#save_physical_attributes").after('Save Failed!');
                }
            
            }, 'json' // close callback
            
        ); // close post
        
    }); // close save_physical_attributes click
    
    $("#save_musician_profile").click(function() {
        
        $.post('/profiles/save/musician_profile/',
            {
                'stage_name'        : $("#id_stage_name").val(),
                'use_stage_name'    : $("#id_use_stage_name").is(':checked'),
                'genre'             : $("#id_genre").val(),
                'role'              : $("#id_role").val()
            },
            function(data) {
                
                if(data.success == true) {
                    $("#save_musician_profile").after('Saved!');
                } else {
                    $("#save_musician_profile").after('Save Failed!');
                }
                
            }, 'json' // close callback
    
        ); // close post
    
    }); // close save musician profile click
    
    $("#save_modeling_profile").click(function() {
       
       $.post('/profiles/save/modeling_profile/',
        
        {
            'categories'    : $("#id_categories").val()
        },
        
        function(data) {
            
        }, 'json' // close callback
        
       ); // close post
        
    }); // close save modeling profile click
    
    // contact form is ajaxified
    $('#site_feedback_form').ajaxForm({
        dataType: 'json',
        clearForm: true,
        success: function(json) {
            $(this).html('<span class="feedback_sent">Thank you for your thoughts.</span>');
        }
    });
    
}); // close document ready