function showNewCategories(cid, show_id){
    value=document.getElementById(cid).value;
    
    if(value == 'new_cat'){
        show(show_id+'1');
        show(show_id+'2');
    } else {
       hide(show_id+'1');
       hide(show_id+'2');
    }
}

/*
 * accpetImages is called when the use clicks 'use selection' on the imgmanager page.
 * this simply updates updates the images data, by calling delete_images, and add_images.
 */

var img_selected = -1;
var img_list = new Array();
var caption_lookup = {};


function acceptImagesAndCaptions(list){
	if(list.length == 0){ g('move_buttons').style.display = 'none'; }
	else { g('move_buttons').style.display = 'block'; }
	g('upload_images').value = list.join(",");
	var img_cont = g('upload_images_container');

	img_cont.innerHTML = "";

		delete_images(list);

	for(var i=0; i< list.length; i++){
		add_images(list);
	}
	
	output_images();
}

/*
 * This removes any images that aren't in the selection
 */
function delete_images(list){
	var backup = new Array();
	for(var i =0; i < list.length; i++){
		for(var j=0; i < img_list.length; i++){
			if(list[i] == img_list[j].image){
				var image_details = {pos:backup.length,
						     image:list[i],
						     caption:img_list[j].caption};
				backup.push(image_details);
			}
		}
	}
	img_list = backup;
}

/*
 * TODO: The complexity of the function can be replaced, by a much simpiler one, due to me adding
 * caption lookup.
 * this goes through and moves the images around depending on the new order, and adds
 * any new ones in.
 */
function add_images(list){
	var seen = false;
	for(var i =0; i < list.length; i++){
		seen = false;
		for(var j =0; j < img_list.length; j++){
			if(img_list[j].image == list[i] && img_list[j].pos == i){
				seen = true;
				break;
			}
		}
		if(!seen){
			//var image_details = {pos:}
			var len = img_list.length;
			var caption ='';
			if(caption_lookup[list[i]]){
				caption = caption_lookup[list[i]];
			}
			var image_details = {pos:i, image:list[i],caption:caption};
			if(i == len){
				img_list.push(image_details);
				break;
			}
			var backup = new Array();
			for(var k = i; k < len; k++){
				img_list[k].pos += 1;
				backup.push(img_list[k]);
			}
			
			img_list[i] = image_details;
			i++;
			//test: do length-1, then do a push for the last one.
			for(var k = 0; k < backup.length; k++){
				img_list[i] = backup[k];
			}
			
		}
	}
}

/*
 * output_images
 * This simply creates all the html elements and sticks them on the page.
 * then it calls set_captions.
 */
function output_images(){
	var container = g('upload_images_container');
	container.innerHTML = "";

	var table = c('table', {cellspacing:'0',cellpadding:'0'});

	for(var i = 0; i < img_list.length; i++){
		var img = '<img src="'+the_base_path+'/getimage.php?id='+ escape(img_list[i].image) +'&x=80&y=80" />';

		var textarea = '<textarea onfocus="change_sel('+img_list[i].pos+')" onblur="get_caption(\''+img_list[i].image+'\')" cols="15" rows="4" name="'+img_list[i].image+'_c" id="'+img_list[i].image+'_c">'+img_list[i].caption+'</textarea>';
		if(i == img_selected){
			var radio = "<input type='radio' id='radio_"+img_list[i].pos+"' checked='true' onclick='change_sel("+img_list[i].pos+")' name='img_selecter' value='img_sel"+img_list[i].pos+"' />";
		} else {
			var radio = "<input type='radio' id='radio_"+img_list[i].pos+"' onclick='change_sel("+img_list[i].pos+")' name='img_selecter' value='img_sel"+img_list[i].pos+"' />";
		}
		var td = c('td', '', radio);
		var td2 = c('td', '', img);
		var td3 =  c('td', '', textarea);
		var children = new Array();
		children.push(td);
		children.push(td2);
		children.push(td3);

		var tr = c('tr', '', children);
		ac(table, tr);
		
	}
	ac(container, table);
	var images_string = "";
	for(var i =0; i < img_list.length; i++){
		if(i != 0){
			images_string += ",";
		}
		images_string += ''+img_list[i].image;
	}
	g('upload_images').value = images_string;
	set_captions();

}

/*
 * set_captions
 * set captions simply places all the captions in the one string
 * delimeted by =+=, and then puts them in the hidden field 'image_captions'
 */
function set_captions(){
	var captions = '';
	for(var i=0 ; i < img_list.length; i++){
		if(i != 0){
			captions += "=+=";
		}
		captions += ''+img_list[i].caption;
	}
	g('images_captions').value = captions;

}

/*
 * get_caption
 * get_caption does a lookup on the image, and finds the caption
 * that belongs to it.
 * If an image is removed while using the add images, and then then
 * placed back in, the caption is preserved.
 */
function get_caption(img_altered){
	var caption = g(img_altered+'_c').value;
	caption_lookup[img_altered] = caption;
	for(var i = 0; i < img_list.length; i++){
		if(img_list[i].image == img_altered){
			img_list[i].caption = caption;
			set_captions();
			return;
		}
	}
}

/*
 * init_images
 * Initialise the images data, setting the captions.
 * then calls output_images to output the html.
 */
/*
function init_images(){
	var images = g('upload_images').value;
	if(images == '') return;
	var imgs = images.split(",");
	g('move_buttons').style.display = 'block';
	var captions = new Array();
	var caption_string = g('images_captions').value;
	captions = caption_string.split('=+=');

	for(var i =0; i< imgs.length; i++){
//alert(i);
		var cap = '';
		if(captions[i]){
			cap = captions[i];
		}
		var image_details = {pos:i,image:imgs[i],caption:cap};
		caption_lookup[imgs[i]] = cap;
		img_list.push(image_details);

	}
	output_images();
}
*/
/*
 * change_sel
 * updates the currently selected image
 */
/*
function change_sel(img_index){
	g('radio_'+img_index).checked='true';
	img_selected = img_index;
}

/*
 * mv_image_up
 * moves the image up on place, called when the UP button is pressed.
 */
/*
function mv_image_up(){
	if(img_selected == -1) return;
	if(img_selected == 0) return;

	for(var i=0; i < img_list.length; i++){
		get_caption(img_list[i].image);
	}

	var backup = img_list[img_selected-1];
	img_list[img_selected-1] = img_list[img_selected];
	img_list[img_selected-1].pos -= 1;
	img_list[img_selected] = backup;
	//Hack to stop it treating pos as a string, if I use +.
	img_list[img_selected].pos -= (-1);

	img_selected -= 1;
	output_images();
}
*/
/*
 * mv_image_down
 * moves the image one place down, called when the DN button is pressed
 */
/*
function mv_image_down(){
	if(img_selected == -1) return;
	if(img_selected == (img_list.length-1)) return;

	for(var i=0; i < img_list.length; i++){
		get_caption(img_list[i].image);
	}

	var backup = img_list[img_selected+1];

	img_list[img_selected+1] = img_list[img_selected];
	//Hack to stop it treating pos as a string, if I use +.
	img_list[img_selected+1].pos -= (-1);
	img_list[img_selected] = backup;
	img_list[img_selected].pos -= 1;
	img_selected += 1;
	output_images();
}*/

