var my_ss_stats = new Array();
var currently_viewing = 0;
var my_header = new Array();
var id_to_ingredients = new Array();
var id_to_qty = new Array();
var zI = 1;
var recipes_to_cats = new Array();
var cur_descriptor = '';
var max_level = 9;

function sort_by_skill()
{
	my_items.sort(compare_recipes);
	
	for(var i=0; i < my_items.length; i++)
	{
		skill_levels[my_items[i].skill].push(my_items[i]);
	}
	for(var i=1; i <= max_level; i++)
	{
		skill_levels[i].sort(compare_recipes);
	}
}
function compare_recipes(a, b)
{
	if(a.name.toLowerCase() > b.name.toLowerCase()) return 1;
	if(a.name.toLowerCase() < b.name.toLowerCase()) return -1;
	return 0;
}
function set_my_ss_stats()
{
	for(var i=0; i < my_items.length; i++)
	{
		my_ss_stats[my_items[i].id] = my_items[i].row_data();
		id_to_ingredients[my_items[i].id] = my_items[i].ingredients;
		id_to_qty[my_items[i].id] = my_items[i].qty;
		if(!recipes_to_cats[my_items[i].cat_index])
		{
			recipes_to_cats[my_items[i].cat_index] = new Array();
		}
		recipes_to_cats[my_items[i].cat_index].push(my_items[i]);
	}
}
function split_ingredients(raw)
{
	my_ingredients = new Array();
	tmp_ingredients = raw.split('^');
	for(var i=0; i < tmp_ingredients.length; i++)
	{
		tmp_stats = tmp_ingredients[i].split("|");
		my_ingredients.push(new Ingredient(tmp_stats[0], tmp_stats[1], tmp_stats[2]));
	}
	return my_ingredients;
}
function first_to_upper(string)
{
	end_string = string.substring(1);
	begin_string = (string.substring(0, 1)).toUpperCase();
	return new String(begin_string + end_string);
}
function view_item_stats(this_item_id, this_item_heading)
{		
	currently_viewing = this_item_id;
	my_header = this_item_heading.split(',');
	screen_window = document.open('recipe_stats_window.html', 'itemStats', 'toolbar=0, width=423, height=63, resizable=0, scrollbars=0, status=0, menubar=no, location=no');
}
function write_recipes(skill_level, in_cat_index)
{
	category_select(in_cat_index);
	skill_level_select(skill_level, in_cat_index);
	zI = 1;
	var found = 0;
	var output = '';
	for(var i=0; i < recipes_to_cats[in_cat_index].length; i++)
	{
		if(recipes_to_cats[in_cat_index][i].skill == skill_level)
		{
			found++;
			
			var cur_recipe = recipes_to_cats[in_cat_index][i];
			//cur_recipe.name = cur_recipe.name.replace('<', '&lt;').replace('>', '&gt;');
			//this_title = (cur_recipe.screen == 'true') ? '<a href="javascript: view_screen(\'' + cur_recipe.descriptor + '\', \'' + cur_recipe.id + '\', \'' + cur_recipe.heading + '\');">' + cur_recipe.name + '</a>' :
			this_title = (cur_recipe.screen == 'true') ? '' + cur_recipe.name + '' :
						 ((cur_recipe.id && cur_recipe.row_data().length > 0) ? '<a href="javascript: view_item_stats(\'' + cur_recipe.id + '\', \'' + cur_recipe.heading + '\');">' + cur_recipe.name + '</a>' : cur_recipe.name);
			output += '<p style="margin-top: 0px; text-align: left;"><img src="/templates/Underworld/data/images/arsenal/reciple/step.jpg"></p>';
			output += '<div class="recipeContainer">';
			output += '<div class="headerContainer"><p class="iconContainer"><img class="item_icon" align="absmiddle" src="/templates/Underworld/data/images/arsenal/reciple/icons/item_' + cur_recipe.id + '.jpg" onerror="this.src=\'/images/item_icons/noicon.jpg\';"></p><p class="recipeHeader">' + this_title + ' (' + cur_recipe.qty + ')</p>' +
					  '<p class="stats">' +
					  'MP: ' + cur_recipe.mp + '</p></div>';
			
			output += '<div style="font-size: 5px; clear: both;">&nbsp;</div>';
			output += '<div class="ingredientsContainer">';
			for(var j = 0; j < cur_recipe.ingredients.length; j++)
			{
				output += get_sub_recipes(cur_recipe.ingredients[j], 1);
			}
			output += '</div></div>';
		}
	}
	output += (found == 0) ? '<p style="font-weight: bold; padding-top: 30px;">Keine Rezepte gefunden.</p>' : '';
	
	document.getElementById("recipes").innerHTML = output;
}
function category_select(my_index)
{
	var tab_etc_class = "";
	var tab_accessories_class = "";
	var tab_weapons_class = "";
	var tab_armor_class = "";
	var tab_common_class = "";
	
	switch(my_index)
	{
		case '4': tab_common_class = "_down";
		break;
		
		case '3': tab_etc_class = "_down";
		break;
		
		case '2': tab_accessories_class = "_down";
		break;
		
		case '1': tab_armor_class = "_down";
		break;
		
		default: tab_weapons_class = "_down";
		break;
	}
	var output = '<a href="javascript: write_recipes(\'1\', \'0\');"><img src="/templates/Underworld/data/images/arsenal/reciple/weapons_tab' + tab_weapons_class + '.jpg" border="0"></a>' +
				 '<a href="javascript: write_recipes(\'1\', \'1\');"><img src="/templates/Underworld/data/images/arsenal/reciple/armor_tab' + tab_armor_class + '.jpg" border="0"></a>' +
				 '<a href="javascript: write_recipes(\'1\', \'2\');"><img src="/templates/Underworld/data/images/arsenal/reciple/accessories_tab' + tab_accessories_class + '.jpg" border="0"></a>' +
				 '<a href="javascript: write_recipes(\'1\', \'3\');"><img src="/templates/Underworld/data/images/arsenal/reciple/etc_tab' + tab_etc_class + '.jpg" border="0"></a>' +
				 '<a href="javascript: write_recipes(\'1\', \'4\');"><img src="/templates/Underworld/data/images/arsenal/reciple/common_tab' + tab_common_class + '.jpg" border="0"></a>';
	document.getElementById("cat_nav").innerHTML = output;
}
function skill_level_select(skill_level, my_index)
{
	var output = '<div id="sub_nav_level">Level:</div>';
	for(var i=1; i <= max_level; i++)
	{
		if(i != skill_level)
		{
			output += '<div class="nav_btn_container" ' +
					  'onMouseOver="document.getElementById(\'unav' + i + '\').className=\'sub_nav_button_over\'; style.cursor=\'Pointer\';" ' +
					  'onMouseOut="document.getElementById(\'unav' + i + '\').className=\'sub_nav_button\';"' +
					  'onClick="javascript: write_recipes(\'' + i + '\', \'' + my_index + '\');">' +
					  '<div id="unav' + i + '" class="sub_nav_button">' +					  
					  '' + i + '</div></div>';
		} else {
			output += '<div class="nav_btn_container"><div class="sub_nav_button_over">' + i + '</div></div>';
		}
	}
	output += '<div style="font-size: 1px; clear: both;">&nbsp;</div>'
	document.getElementById("sub_nav").innerHTML = output;
}
function get_sub_recipes(this_recipe, this_scale)
{
	if(!id_to_ingredients[this_recipe.id])
	{
		return '<p>(' + (this_scale * this_recipe.qty) + ') ' + this_recipe.name + '</p>';
	} else {
		output = '<p>(' + (this_scale * this_recipe.qty) + ') <a class="smallLink" href="javascript: show_subs(' + zI + ');">' + this_recipe.name + ' <span style="font-size: 10px;">[<span id="pm'  + zI + '">+</span>]</span></a></p>' +
				 '<div id="sub' + zI + '" style="padding-left: 20px; padding-bottom: 3px; display: none;">';
			
		zI++;
		for(var m=0; m < id_to_ingredients[this_recipe.id].length; m++)
		{
			output += get_sub_recipes(id_to_ingredients[this_recipe.id][m], Math.ceil((this_scale * this_recipe.qty)/id_to_qty[this_recipe.id]));
		}
		output += '</div>';
		
		return output;
	}
}

function show_subs(recipe_id)
{
	if(document.getElementById("sub" + recipe_id).style.display == "block")
	{
		document.getElementById("sub" + recipe_id).style.display = "none";
		document.getElementById("pm" + recipe_id).innerHTML = "+";
	} else {
		document.getElementById("sub" + recipe_id).style.display = "block";
		document.getElementById("pm" + recipe_id).innerHTML = "-";
	}		
}
