function search_sort(my_items)
{
	document.location.hash = 'itemsTop';
	var my_search_box = document.getElementById("searchInputText");
	my_matches = new Array();
	for(i=0; i < my_items.length; i++)
	{
		if(my_items[i].name.toLowerCase().indexOf(my_search_box.value.toLowerCase()) != -1)
		{
			//my_items[i].show_sort_category = true;
			my_matches.push(my_items[i]);
		}
	}
	my_search_box.value = '';
	return my_matches;
}

function sub_nav()
{
	output = '';
	for(i=0; i < categories_select.length; i++)
	{
		if(categories_select[i] != my_current_category)
		{
			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: show_my_items(\'' + categories_select[i] + '\');">' +
					  '<div id="unav' + i + '" class="sub_nav_button">' +					  
					  categories_select[i] + '</div></div>';
		} else {
			output += '<div class="nav_btn_container"><div class="sub_nav_button_over">' + categories_select[i] + '</div></div>';
		}
	}
	output += '<div style="font-size: 1px; clear: both;">&nbsp;</div>'
	document.getElementById("sub_nav").innerHTML = output;
}

function view_screen(screen_name, this_item_id)
{		
	currently_viewing = this_item_id;
	screen_window = document.open('item_screen_window.html?' + screen_name, 'screenshots', 'toolbar=0, width=382, height=420, resizable=0, scrollbars=0, status=0, menubar=no, location=no');
}
function show_my_items(to_show)
{
	my_current_category = new String(to_show);
	if(to_show != 'itemSearch')
	{
		to_show = to_show.toLowerCase();
		to_show = to_show.replace(/\./i, '');
		to_show = to_show.replace(/polearm/, 'pole');
		to_show = to_show.replace(/misc/, 'etc');
		to_show = to_show.replace(/fist/, 'dualfist');
		
		show_items = sort_pages(my_items, to_show);
	} else {
		var my_search_box = document.getElementById("searchInputText");
		if(!my_search_box.value)
		{
			document.getElementById("item_container").innerHTML = '<br /><strong>Error:</strong> Please enter an item name in the search box.';
			sub_nav();
			return;
		}
		show_items = search_sort(my_items);
		if(show_items.length == 0)
		{
			document.getElementById("item_container").innerHTML = '<br />There are no items in this category matching your search criteria.';
			sub_nav();
			return;
		}
	}
	show_items.sort(compare_items);
	// header
	output = '<table width="100%" cellpadding="0" cellspacing="0" border="0">' +
			 '<tr id="item_table_header">' +
			 '<td class="item_table_header">&nbsp;</td>';
	for(i=0; i < my_header.length; i++)
	{
		var sort_indicator = (my_sort_index == (i+1)) ? '&nbsp;<span style="font-size: 10px; font-weight: normal;">' + ((greater_than == 1) ? '+' : '-') + '</span>' : '';
		output += '<td class="item_table_header"><a href="javascript: prev_sort_index = ' + my_sort_index + '; my_sort_index=' + (i+1) + '; flip_sort(); show_my_items(\'' + my_current_category + '\');">' + my_header[i] +  sort_indicator + '</a></td>';
	}
	output += '<td width="15" class="item_table_header">&nbsp;</td></tr>';
	
	for(i=0; i < show_items.length; i++)
	{
		this_row_data = show_items[i].row_data();
		
		is_sealed = ((this_row_data[1].toLowerCase()).indexOf('sealed') >=0 && selected_category != 'accessories') ? true : false;
		
		if(!is_sealed)
		{
			show_items[i].set_icon();
			output += '<tr id="itemrow' + show_items[i].id + '">' +
					  '<td class="item_table_cell" width="40"><a name="item' + i + '" /><img src="' + this_row_data[0].src + '" border="0" class="row_icon" onerror="this.src=\'../../images/item_icons/noicon.jpg\';"></td>';
				  
			for(j=1; j < this_row_data.length; j++)
			{
				if(j==1 && show_screens && show_items[i].screenshot == 1)
				{
					this_title = '<a class="itemTitleLink" href="javascript: view_item(\'' + i + '\');">' + this_row_data[j].replace(/<(.*)>/g, '($1)') + '</a>';
				} else {
					this_title = this_row_data[j].replace(/<(.*)>/g, '($1)');
				}
				
				this_title = this_title.replace(/--(.*)--/, '<span style="font-size: 11px; font-weight: normal; color: #CCCCCC;"><nobr> ($1)</nobr></span>');
				
				if(show_items[i].show_sort_category)
				{
					//this_title += '<br /><nobr><span style="color: #2e4759; font-weight: normal; font-size: 10px;">[' + show_items[i].type + ']</span></nobr>';
				}
				
				this_style = (j != 1) ? 'style="text-align: center;";' : '';
				output += '<td class="item_table_cell"' + this_style + '>' + ((j == 1) ? '<b>' + this_title + '</b>' :  this_row_data[j]) + '</td>';
			}
			
			output += '<td class="item_table_cell">' + ((my_recipes[show_items[i].id]) ? '<a href="javascript: show_rec_box(\'' + show_items[i].id + '\');"><img src="../../images/book_icon.jpg" border="0" alt="View/Hide Recipe" title="View/Hide Recipe"></a>' : '&nbsp;') + '</td></tr>';
			// recipe box...
			if((my_recipes[show_items[i].id]))
			{
				output += '<tr id="rec' + show_items[i].id + '" style="display: none;"><td colspan="12"><div id="rec_cont' + show_items[i].id + '" class="rec_box">';
				output += '.';
				output += '</div></td></tr>';
			}
		}
	}
	output += '</table>';
	
	document.getElementById("item_container").innerHTML = output;
	sub_nav();
}
function is_numeric(s_text)
{
	var valid_chars = "0123456789.";
	for (i = 0; i < s_text.length; i++) 
	{  
		if (valid_chars.indexOf(s_text.charAt(i)) == -1) 
		{
			return false;
		}
	}
	return true;
}
function flip_sort()
{
	if(prev_sort_index == my_sort_index && greater_than == 1)
	{
		greater_than = -1;
		less_than = 1;
	} else {
		greater_than = 1;
		less_than = -1;
	}
}
	
function compare_items(a, b)
{
	// testing...
	var a_row_data = a.row_data();
	var b_row_data = b.row_data();
	
	if(is_numeric(a_row_data[my_sort_index]) || is_numeric(b_row_data[my_sort_index]))
	{
		a_row_data[my_sort_index] = (is_numeric(a_row_data[my_sort_index])) ? a_row_data[my_sort_index] : 0;
		b_row_data[my_sort_index] = (is_numeric(b_row_data[my_sort_index])) ? b_row_data[my_sort_index] : 0;
		
		if((a_row_data[my_sort_index] - b_row_data[my_sort_index]) < 0) return greater_than;
		if((a_row_data[my_sort_index] - b_row_data[my_sort_index]) > 0) return less_than;
		return 0;
	} else {
		// put S grade on top...
		if(a_row_data[my_sort_index].toLowerCase() == 's')
		{
			a_row_data[my_sort_index] = '0';
		}
		if(b_row_data[my_sort_index].toLowerCase() == 's')
		{
			b_row_data[my_sort_index] = '0';
		}
		
		if(a_row_data[my_sort_index].toLowerCase() > b_row_data[my_sort_index].toLowerCase()) return greater_than;
		if(a_row_data[my_sort_index].toLowerCase() < b_row_data[my_sort_index].toLowerCase()) return less_than;
		return 0;
	}
}
function show_rec_box(rec_box_id)
{
	var rec_name = 'rec' + rec_box_id;
	var item_row_name = 'itemrow' + rec_box_id;
	var rec_container_name = 'rec_cont' + rec_box_id;
	
	if(document.getElementById(rec_container_name).innerHTML = '.')
	{
		var output = '';
		for(var j=0; j < my_recipes[rec_box_id].length; j++)
		{
			for(var k=0; k < my_recipes[rec_box_id][j].ingredients.length; k++)
			{
				 output += get_sub_recipes(my_recipes[rec_box_id][j].ingredients[k], 1);
			}
			output += (my_recipes[rec_box_id].length > 1 && j <  (my_recipes[rec_box_id].length - 1)) ? '<p style="border-bottom: solid 1px #2E4759; margin-bottom: 10px; font-size: 10px;">&nbsp;</p>' : '';
		}
		document.getElementById(rec_container_name).innerHTML = output;
	}
	
	if(document.getElementById(rec_name).style.display == "")
	{
		document.getElementById(rec_name).style.display = "none";
		document.getElementById(item_row_name).style.background = "";
		
	} else {
		document.getElementById(rec_name).style.display = "";
		document.getElementById(item_row_name).style.background = "#2E4759";
	}

}
function get_sub_recipes(this_recipe, this_scale)
{
	if(!my_recipes[this_recipe.id])
	{
		return '<p>(' + (this_scale * this_recipe.qty) + ') ' + this_recipe.name + '</p>';
	} else {
		var 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 l=0; l <  my_recipes[this_recipe.id].length; l++)
		{
			for(var m=0; m < my_recipes[this_recipe.id][l].ingredients.length; m++)
			{
				output += get_sub_recipes(my_recipes[this_recipe.id][l].ingredients[m], Math.ceil((this_scale * this_recipe.qty)/my_recipes[this_recipe.id][l].qty));
			}
		}
		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 = "-";
	}		
}
function close_item(id)
{
	document.getElementById('item_container').innerHTML = prev_content;
	document.location.hash = 'item' + id;
}
var zI = 1;
var my_current_category;
var my_sort_index = 2;
var prev_sort_index = 2;
var greater_than = 1;
var less_than = -1;
var prev_content = '';
var show_items = new Array();

show_my_items(categories_select[0]);
set_my_ss_stats();

