/* Recipe functions */
var debug = false;	// set to true for debugging purposes

function loadRecipeForm (preview_picture) {	
	loadCategoryList();
	loadItems();
	toggleIngredientsOption();
	if(preview_picture) loadPictureOptions();
	document.forms[0].elements[0].focus();
	protectPage();
}

function toggleIngredientsTextbox(id) {
	
	var ingredients = document.getElementById(id);
	var ingredientsOption = document.getElementById("toggle_ingredients");
	var simpleIngredients = (document.getElementsByName("ingredients")).length != 0;
	if(simpleIngredients) {
		loadItems();
		ingredientsOption.innerHTML = "[Switch to simple <span class='access_key'>m</span>ode]";
		var add_fields = document.getElementsByName("add");
		add_fields[1].focus();
	} else {
		ingredients.innerHTML = "<textarea id='simple_ingredients' name='ingredients' accesskey='i'></textarea>";
		ingredientsOption.innerHTML = "[Switch to advanced <span class='access_key'>m</span>ode]";
		var ingredients_textbox = document.getElementById("simple_ingredients");
		ingredients_textbox.focus();
	}
				
}

function toggleIngredientsOption() {

	var ingredientsOption = document.getElementById("toggle_ingredients");
	ingredientsOption.style.display = ingredientsOption.style.display == 'none' ? 'block' : 'none';

}

function confirmToggleIngredients() {
	return confirm("By changing the ingredients mode, you will lose any ingredients you've entered.");
}

function loadPictureOptions() {
	var picture_upload = document.getElementById("picture_upload");
	var remove = document.getElementById("remove");
	var picture_options = document.getElementById("picture_options");
	
	if(picture_upload!=null && picture_options!=null) {
		options_html = "<a id='change_link' onclick='showPictureUpload();'>";
		if(remove) {
			remove.style.display = "none";
			picture_upload.style.display = "none";
			options_html += "[change picture]";
		} else options_html += "[add picture]";
		options_html += "</a>";
		if(remove) options_html += " <a id='remove_link' onclick='toggleRemovePicture();'>[remove this picture]</a>";
		picture_options.innerHTML = options_html;
	}
}

function showPictureUpload() {
	var picture_upload = document.getElementById("picture_upload");
	var picture = document.getElementById("picture");
	picture_upload.style.display = "block";
	picture.focus();
	picture.focus();	// focus() needs to be called twice in order to give focus in Firefox
}

function toggleRemovePicture() {
	var preview = document.getElementById("preview");
	var picture_removed = document.getElementById("picture_removed");
	var remove_picture = document.getElementById("remove_picture");
	var change_link = document.getElementById("change_link");
	var remove_link = document.getElementById("remove_link");
	if(remove_picture.checked) {
		preview.style.display = "block";
		picture_removed.style.display = "none";
		change_link.style.display = "inline";
		remove_picture.checked = false;
		remove_link.innerHTML = "[remove this picture]";
	} else {
		preview.style.display = "none";
		picture_removed.style.display = "block";
		change_link.style.display = "none";
		remove_picture.checked = true;
		remove_link.innerHTML = "[undo remove]";
	}
}

function loadRecipeActions(recipe_id, picture_details) {
	
	// Picture
	var picture = document.getElementById("recipe_picture");
	if(picture) {
		picture.title = "view larger";
		picture.style.cursor = "pointer";
		picture.onclick = function () { viewPicture(picture_details['width'], picture_details['height'], picture_details['path'], picture_details['title'], picture_details['document_title']); }
	}
	
	// Print
	var print_link = document.getElementById("print");
	print_link.href = "#";
	print_link.target = "_self";
	print_link.onclick = function () { openWindow("print_recipe.php?id="+recipe_id,"print_window",800,600); }
	
	// Share
	var share_link = document.getElementById("share");
	share_link.href = "#";
	share_link.target = "_self";
	share_link.onclick = function () { openWindow("share_recipe.php?id="+recipe_id,"share_window",800,450); }
	
	// Open New Window
	var open_new_window = document.getElementById("open_new_window");
	open_new_window.href = "#";
	open_new_window.target = "_self";
	open_new_window.onclick = function () { openWindow("recipe_window.php?id="+recipe_id,"recipe_window",625,400); }
}

function openWindow(url, id, width, height) {

	window.open(url,id,"width="+width+",height="+height+",statusbar=0,scrollbars=1");

}

function loadCategoryList() {
	
	try {
		var cat_list = document.getElementById("add_remove_category_list");
		cat_list.innerHTML = "<select id='add_remove_list' onchange='addRemoveCategory(this);' accesskey='c'></select> <div id='recipe_categories'><input name='categories' type='hidden' value='' /></div>";
		
		var add_remove_list = document.getElementById("add_remove_list");
		add_remove_list.length=0;
		appendOption(add_remove_list, "[add]", "", true);
		
		// Add category groups to the list
		for(var group in category_list) {
			if(category_list[group].length > 0) {
				//category_list[group].sort();
				appendOption(add_remove_list, "--"+group+"--", "", true);
				for(var i in category_list[group]) {
					var category = category_list[group][i];
					if(category != "") {
						if(debug) category = group+":" + i + "|" + category_list[group][i];
						appendOption(add_remove_list, category, group+":" + i, false);
					}
				}
			}
		}
		
		// Put recipe categories that are already assigned at the bottom of the list
		if(categories.length > 0) {
			appendOption(add_remove_list, "", "", true);			// blank line
			appendOption(add_remove_list, "[remove]", "", true);
	//		for(i=0; i<categories.length ; i++) {
			for(i in categories) {
				appendOption(add_remove_list, categories[i], "[remove]:"+ i, false);
			}
		}
		add_remove_list.selectedIndex = 0;
		
		showCategories();
		
	} catch (e) {}
}

function addRemoveCategory(add_remove_list) {
	var index = add_remove_list.selectedIndex;
	var selection = add_remove_list.options[index].value;
	if(selection == "") return;
	
	var parts = selection.split(":");
	var group = parts[0];
	var category_id = parts[1];
	if(group == "[remove]") {
		var categories_index = categories.length - (add_remove_list.length - index);
		removeCategory(add_remove_list, categories_index);
	} else addCategory(add_remove_list, category_id, group);
	//add_remove_list.remove(index);
	loadCategoryList();
}

function addCategory(add_remove_list, new_category_id, category_group){

	// Add category to recipe categories array
	categories.push(category_list[category_group][new_category_id]);
	category_ids.push(new_category_id);
	category_groups.push(category_group);
	showCategories();
	
	// Remove category from category_list
//	for(i=0; category_list[category_group][i]!=new_category_id && i<category_list[category_group].length ;i++);
	if(debug) displayArray(category_list[category_group], "category_list[category_group]");
	category_list[category_group][new_category_id] = "";
//	category_list[category_group].splice(new_category_id, 1);
	if(debug) displayArray(category_list[category_group], "category_list[category_group]");
	
}

function displayArray(array, array_name) {
	if(array_name === undefined) array_name = "array";
	
	var text = "";
	for(var i in array) 
		text += array_name + "[" + i + "] = " + array[i] + "\n";
	text += "length: " + array.length;
	alert(text);	
}

function removeCategory(add_remove_list, index){

	// Remove category from the recipe categories array
	var category = categories.splice(index, 1);
	var old_category_id = category_ids.splice(index, 1);
	var group = category_groups.splice(index, 1);
	showCategories();
	
	// Add category to category_list
	category_list[group][old_category_id] = category;
//	category_list[group].push(category);

}

function showCategories() {
	var recipe_categories = document.getElementById("recipe_categories");
	var html = categories.join(", ") + "<input name='categories' type='hidden' value='" + category_ids.join(",") + "'>"
	if(debug) html += "<br>" + category_ids.join(",");
	recipe_categories.innerHTML = html;
}

function appendOption(select_list, name, value, disable) {

	var new_element = document.createElement("option");
	new_element.text = name;
	new_element.value = value;
	if(disable) {
		new_element.style.color = "#666666";
		new_element.disabled = true;
	}
	select_list.options.add(new_element);
}

function toggleMyUser(checkbox, username) {

	var user_field = document.getElementById("search_user");
	if(checkbox.checked) user_field.value = username;
	else user_field.value = "";

}
function checkIngredients(form) {
	
	var addFields = document.getElementsByName("add");
	var empty = true;
	var submitOK = true;
	
	for(x=0; x<addFields.length ;x++) {
		if(addFields[x].value!="") empty = false;	
	}
	
	if(!empty) {
		submitOK = confirm("You have an ingredient that's not yet added to the recipe. Do you want to submit without adding it?");
	}
	if(!submitOK) {
		addFields[1].focus();
		confirmation = true;
		toggleSubmitButtons(form);
	}
	
	return submitOK;
	
}

/*function show_preview(source_id) {
	var source = document.getElementById(source_id);
	var filename = source.value;
	if(filename!="") {
		var frame_id = "preview_picture";
		var picture_id = "preview";	
		var frame = document.getElementById(frame_id);
		var picture = document.getElementById(picture_id);
		frame.style.display = 'block';
//		filename = filename.replace("\\","/");
		picture.src = "file:///" + filename;
		alert(picture.src);
	} else {
		alert("Please choose a picture");
		source.focus();
	}
}*/