// Quick Chat 3.50 - power
function quick_chat_delete_messages(chat_id, to_delete_ids){
    var to_delete_room_name = quick_chat_data[chat_id]['room_name'];
    jQuery.post(quick_chat_load_js_vars.ajaxurl, {
        action: 'quick-chat-ajax-delete',
        to_delete_ids: to_delete_ids,
        to_delete_room_name: to_delete_room_name},
        function(data) {
            for (var i=0;typeof(to_delete_ids[i]) != 'undefined';i++){
                jQuery('div[data-quick-chat-id='+chat_id+'] input[type=checkbox][value="'+to_delete_ids[i]+'"]').parents('.quick-chat-history-message-alias-container').remove();
            }
        });
};

function quick_chat_clean_messages(chat_id){
    var to_clean_room_name = quick_chat_data[chat_id]['room_name'];
    var history_container = jQuery('div[data-quick-chat-id='+chat_id+']').find('div.quick-chat-history-container');

    jQuery.post(quick_chat_load_js_vars.ajaxurl, {
        action: 'quick-chat-ajax-clean',
        to_clean_room_name: to_clean_room_name},
        function(data) {
            for(var i=(jQuery(history_container).children().size()-quick_chat_js_vars.clean_target); i>0; i--){
                jQuery(history_container).find(":first").remove();
            }
        });
};

function quick_chat_ban_users (chat_id, to_ban_ips){
    var this_chat = jQuery.find('div[class=quick-chat-container][data-quick-chat-id="'+chat_id+'"]');
    jQuery.post(quick_chat_load_js_vars.ajaxurl, {
        action: 'quick-chat-ajax-ban',
        to_ban_ips: to_ban_ips},
        function(data) {;
            jQuery(this_chat).find('.quick-chat-users-container input[type=checkbox]').each(function(){
                jQuery(this).attr('checked', false);
            });
        });
};

function quick_chat_transcript(chat_id){
    var room_name = quick_chat_data[chat_id]['room_name'];
    jQuery.post(quick_chat_load_js_vars.ajaxurl, {
        action: 'quick-chat-ajax-transcript',
        room_name: room_name},
        function(data) {
            window.open(quick_chat_stripslashes(data.quick_chat_transcript_url));
        });
};

jQuery("div.quick-chat-delete-link a").bind('click', function(e) {
    e.preventDefault();

    var chat_id = jQuery(this).parents('.quick-chat-container').attr('data-quick-chat-id');
    var this_element = jQuery(this);

    var to_delete_ids = [];
    jQuery(this).parents('.quick-chat-container').find('.quick-chat-history-container input[type=checkbox]:checked').each(function(){
        to_delete_ids.push(jQuery(this).val());
    });

    jQuery(this).fadeTo(100, 0, function() {
        if(to_delete_ids == ""){
            alert(quick_chat_js_vars.delete_what_s);
        } else{
            if (confirm(quick_chat_js_vars.delete_confirm_s)){
                quick_chat_delete_messages(chat_id, to_delete_ids);
            }
        }
        jQuery(this_element).fadeTo(100, 1);
    });
});

jQuery("div.quick-chat-clean-link a").bind('click', function(e) {
    e.preventDefault();

    var chat_id = jQuery(this).parents('.quick-chat-container').attr('data-quick-chat-id');
    var this_element = jQuery(this);

    jQuery(this).fadeTo(100, 0, function() {
        if (confirm(quick_chat_js_vars.clean_confirm_s.replace('%s',quick_chat_js_vars.clean_target))){
            quick_chat_clean_messages(chat_id);
        }
        jQuery(this_element).fadeTo(100, 1);
    });
});

jQuery("div.quick-chat-ban-link a").bind('click', function(e) {
    e.preventDefault();

    var chat_id = jQuery(this).parents('.quick-chat-container').attr('data-quick-chat-id');
    var this_element = jQuery(this);

    var to_ban_ips = [];
    jQuery(this).parents('.quick-chat-container').find('.quick-chat-users-container input[type=checkbox]:checked').each(function(){
        to_ban_ips.push(jQuery(this).val());
    });

    jQuery(this).fadeTo(100, 0, function() {
        if(to_ban_ips == ""){
            alert(quick_chat_js_vars.ban_who_s);
        } else{
            if (confirm(quick_chat_js_vars.ban_confirm_s)){
                quick_chat_ban_users(chat_id, to_ban_ips);
            }
        }
        jQuery(this_element).fadeTo(100, 1);
    });
});

jQuery("div.quick-chat-transcript-link a").live('click', function(e) {
    e.preventDefault();

    var chat_id = jQuery(this).parents('.quick-chat-container').attr('data-quick-chat-id');
    var this_element = jQuery(this);

    jQuery(this).fadeTo(100, 0, function() {
        quick_chat_transcript(chat_id);
        jQuery(this_element).fadeTo(100, 1);
    });
});

var quick_chat_toggle = false;
jQuery("div.quick-chat-select-all-link a").bind('click', function(e){
    e.preventDefault();
    var this_element = jQuery(this);

    jQuery(this).fadeTo(100, 0, function() {
        jQuery(this_element).parents('.quick-chat-container').find('.quick-chat-history-container input[type=checkbox]').attr('checked',!quick_chat_toggle);
        quick_chat_toggle = !quick_chat_toggle;
        jQuery(this_element).fadeTo(100, 1);
    });
});