/**
 * @author Johan
 */
var fbRegex = /^[a-z0-9_.\- ]+$/i;
function fb_timedRefresh(timeoutPeriod){
    setTimeout("location.reload(true);", timeoutPeriod);
}

function fb_checkAll() {
	var isChecked = jQuery("#fb-check-all:checked").is(":checked");
	jQuery('input[name="fb-check-file-names"]').each(function() {
		this.checked = isChecked;
	});			
}

function fb_processBackup() {
	var backupId = jQuery("#fb-backup-id").val();
	if (backupId == 0) {
		alert("Please choose back-up name");
		return;
	}
	var checkEverything = jQuery("#fb-check-everything:checked").is(":checked");
	if (!checkEverything) {
		var items = jQuery('input[name="fb-check-file-names"]:checked');
		var n = items.length;
		if (n == 0) {
			return;
		}
		var files = new Array();
		for (var i = 0; i < n; i++) {
			files.push(jQuery(items[i]).val());
		}
		var data = {
			post_action: "process-backup",
			backup_id: backupId,
			check_everything: checkEverything,
			files: files
		};
	} else {
		var data = {
			post_action: "process-backup",
			backup_id: backupId,
			check_everything: checkEverything
		};
	}
	jQuery("#fb-btn-backup-file").attr("disabled", "disabled");
	jQuery("#fb-create-backup").show();
	jQuery.post(fbAjax.fbAjaxActionUrl, data, function(response){
		jQuery("#fb-btn-backup-file").removeAttr("disabled");
		jQuery("#fb-create-backup").hide();
		jQuery("#fb-div-backup-result").show();
		jQuery("#fb-div-backup-result").html('<span class="fb-span-backup-result">'+response+'</span>');
		fb_timedRefresh(2000);
	});
}

function fb_loadSourceFiles() {
	var data = {post_action: "file-list-view-ajax"};	
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-file-list").html(response);
	});
}

function fb_loadArchiveFiles() {
	jQuery("#fb-backup-name").focus();
	var data = {post_action: "archive-list-view-ajax"};
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-list").html(response);
	});
}

function fb_addArchive() {
	
	var backupName = jQuery.trim(jQuery("#fb-backup-name").val());
	if (backupName == "") {
		return;
	}
	if (!fbRegex.test(backupName)) {
		alert("Archive name should not contain wild characters.");
		return;
	}
	jQuery("#fb-processing").show();
	var data = {post_action: "add-archive", backup_name: backupName};
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-list").html(response);
		jQuery("#fb-processing").hide();
		fb_clearThis('fb-div-archive-contents');
	});
}

function fb_updateArchiveName(id, name) {
	var backupName = prompt("Please edit name here: ", name);
	if (backupName == null || name == "") {
		return;
	}
	if (!fbRegex.test(backupName)) {
		return;
	}
	jQuery("#fb-processing").show();
	var data = {
			post_action: "update-archive-name",
			id: id,
			backup_name: backupName
			};
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-list").html(response);
		jQuery("#fb-processing").hide();
		fb_clearThis('fb-div-archive-contents');
	});
}

function fb_deleteArchive(id) {
	if (!confirm("Are you sure you want to delete this archive?")) {
		return;
	}
	jQuery("#fb-processing").show();
	var data = { post_action: "delete-archive", id: id };
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-list").html(response);
		jQuery("#fb-processing").hide();
		fb_clearThis('fb-div-archive-contents');
	});
}

function fb_viewSuccessFiles(id) {
	jQuery("#fb-div-archive-contents").html("loading...");
	var data = { post_action: "success-files-view", id: id };
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-contents").html(response);
	});
}

function fb_viewFailedFiles(id) {
	jQuery("#fb-div-archive-contents").html("loading...");
	var data = { post_action: "failed-files-view", id: id };
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-contents").html(response);
	});
}

function fb_viewQueuedFiles(id) {
	jQuery("#fb-div-archive-contents").html("loading...");
	var data = { post_action: "queued-files-view", id: id };
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-div-archive-contents").html(response);
	});
}

function fb_clearThis(id) {
	jQuery("#"+id).html("");
}

function fb_removeContentFile(id) {
	if (!confirm("Are you sure you want to delete this file?")) {
		return;
	}
	jQuery("#fb-div-archive-contents").prepend('<span id="fb-span-loader" class="fb-span-archive-text">processing...</span>');
	var data = { post_action: "remove-content-file", id: id };
	jQuery.post(fbAjax.fbAjaxActionUrl , data, function(response){
		jQuery("#fb-span-loader").remove();
		jQuery("#fb-div-archive-contents").html(response);
	});
}
