jQuery(document).ready(function(){      
    
    var j = jQuery;
    
    // validate signup form on keyup and submit
    j("#new-dedication-form").validate({
        ignore: [],
        messages: {
            friend_ids: "Please enter at least one member."
        }
    });
    
    var onAutocompleteSelect = function(value, data) {
        j('#selection').html('<img src="\/global\/flags\/small\/' + data + '.png" alt="" \/> ' + value);
        alert(data);
    }

    var options = {
        minLength: 1,
        serviceUrl: ajaxurl,
        width: 300,
        delimiter: /(,|;)\s*/,
        onSelect: ia_on_autocomplete_select,
        deferRequestBy: 0, //miliseconds
        params: {
            action: 'bp_dedication_autocomplete_ajax_handler'
        },
        noCache: true //set to true, to disable caching
    };    
    
    j("#dedicate_to_input").autocomplete(options);
    
    j("#dedicate_to_input").change(function(){
        if(j('#dedicate_to_input').val() == '') {
            j('#friend_ids').val('');
        }
    });
    
    var url = '';

    j('#video_url').change(function(){
        // Check for empty input field
        if(j('#video_url').val() != ''){
            // Get youtube video's thumbnail url
            // using jYoutube jQuery plugin
            url = j.jYoutube(j('#video_url').val());
            
            // Now append this image to <div id="thumbs">
            j('#thumbs').html(j('<img src="'+url+'" />'));
        }
    });
    
    j("#user-list li a.remove").live("click", function() {
        var friend_id = j(this).prop('id');

        friend_id = friend_id.split('-');
        friend_id = friend_id[1];

        j.post( ajaxurl, {
            action: 'bp_dedication_add_user_ajax_handler',
            'friend_action': 'remove',
            'cookie': encodeURIComponent(document.cookie),
            //'_wpnonce': j("input#_wpnonce_invite_uninvite_user").val(),
            'friend_id': friend_id
        },
        function(response)
        {
            friendIDs = j('#friend_ids').val().split(',');
            friendIDs.splice(j.inArray(friend_id, friendIDs),1);            
            j('#friend_ids').val(friendIDs.toString());
            
            j('#user-list li#uid-' + friend_id).remove();
        });

        return false;
    });    
    
});

function ia_on_autocomplete_select( value, data ) {
    var j = jQuery;
    var friendIDs;
	
    // Check the right checkbox
    //j('#invite-anyone-member-list input#f-' + data).prop('checked',true);
	
    //j('#dedicate_to_input').addClass('loading');

    j.post( ajaxurl, {
        action: 'bp_dedication_add_user_ajax_handler',
        'friend_action': 'add',
        'cookie': encodeURIComponent(document.cookie),
        //'_wpnonce': j("input#_wpnonce_invite_uninvite_user").val(),
        'friend_id': data
    },
    function(response)
    {        
        friendIDs = j('#friend_ids').val().split(',');
        friendIDs.push(data);
        j('#friend_ids').val(friendIDs.toString());

        /*j('#user-list').html('');
        j('#user-list').append(response);*/
        //j('#dedicate_to_input').removeClass('loading');
    });
		
    // Remove the value from the send-to-input box
    // j('#dedicate_to_input').val('');
}