function ctx_blocks_setnum(num, groups) {
	MAX_CONTENT_BLOCKS = num;
	var blocks = $("ctx_blocks_items");
	var num_blocks = blocks.childNodes.length;
	var panels = $a("div", "rel", "ctx_blocks_select", $("ctx_blocks_items"));
	
	if (num > 0) {
		//restrict container to [num] blocks
		if (num_blocks > num) {
			//too many blocks - remove those over maximum and disable add button
			for (a = num; a < blocks.childNodes.length; a++) {
				blocks.removeChild(blocks.childNodes[a]);
			}
			
			//disable "add" button
			$("btn_ctx_blocks_add").disabled = true;
		} else if (num_blocks == num) {
			//enough blocks - just disable add button
			$("btn_ctx_blocks_add").disabled = true;
		} else {
			//more blocks allowed - enable add button
			$("btn_ctx_blocks_add").disabled = false;
		}
	} else if (num == -1) {
		//unlimited blocks allowed - enable add button
		$("btn_ctx_blocks_add").disabled = false;
	}
	
	//if any groups exist, re-set all currently chosen blocks
	if (groups != null) {
		var select, tpl_name;
		
		for (a = 0; a < panels.length; a++) {
			select = panels[a].childNodes[1];
			
			if (select.selectedIndex != -1)
				tpl_name = select.options[select.selectedIndex].value;
			
			wipe_select(select);
			
			for (b = 0; b < groups.length; b++)
				add_select_value(select, groups[b], groups[b], 1);
			
			//reset selection
			select.selectedIndex = 0;
			
			//attempt to select previous value, if it exists
			if (tpl_name != "")
				select_val(select, tpl_name);
			
			//show panel
			remove_class(panels[a], "hidden");
		}
	} else {
		//hide panels
		for (a = 0; a < panels.length; a++)
			add_class(panels[a], "hidden");
	}
}

function ctx_blocks_add(xmldoc, response, defaults) {
	var panel = $("ctx_blocks_items");
	
	if (xmldoc != null) {
		//check child count and grey out add button if needed
		$("btn_ctx_blocks_add").disabled = (panel.childNodes.length == MAX_CONTENT_BLOCKS);
	} else {
		var select = $("ctx_blocks_define_select");
		
		if (panel.childNodes.length >= MAX_CONTENT_BLOCKS && MAX_CONTENT_BLOCKS != -1) {
			new ctx_alert("You have reached the maximum number of content blocks for this template. No more blocks can be added.");
			return false;
		}
		
		if (select.selectedIndex != -1) {
			//check block doesn't already exist
			var block_checks = $a("input", "rel", "ctx_blocks_item", panel);
			var block_id = select.options[select.selectedIndex].value;
			
			//get template ID from page or selector
			if (defaults) {
				var tpl_id = $("tpl_id").value;
			} else {
				var tpl_id = $("sel_tpl_id").options[$("sel_tpl_id").selectedIndex].value;
			}
			
			for (var a = 0; a < block_checks.length; a++) {
				if (block_id == block_checks[a].value) {
					new ctx_alert("The block you are adding is already in the list. Please choose another block before adding.");
					return false;
				}
			}
			
			var post_data = "a=add_block&id=" + block_id + "&tpl_id=" + tpl_id;
			ajax_send(ctx_blocks_add, arguments, PATH_MODULES + "/ctx_blocks/admin/actions.php", post_data);
		}
	}
}

function ctx_blocks_remove(block_id) {
	var panel = $("ctx_blocks_items");
	
	//remove block definition
	destroy_element('ctx_blocks_item_' + block_id);
	
	//re-enable adding button, if needed
	$("btn_ctx_blocks_add").disabled = (panel.childNodes.length == MAX_CONTENT_BLOCKS);
	
	return false;
}

function ctx_blocks_page_presave(action) {
	//enter order numbers for distinct block elements
	var panel = $("ctx_blocks_define_panel");
	
	if (panel != null) {
		var inputs = $a("input", "rel", "element_order_field", panel);
		enter_element_orders(inputs);
	}
}

if (mod_attach_event) {
	mod_attach_event("page_save_xml", ctx_blocks_page_presave);
}