// -----------------------------------------------------------------------------
// Filter Selection
// -----------------------------------------------------------------------------
var selected_filter_options=new Object();
var previous_hash = "";
var blank_filter_hash = "";
var update_interval_id = -1;
var top_level_name  = "";
var category_name  = "";
var page_url = "";

function updateFilterURL(jump_to_first_page)
{
	if(jump_to_first_page === undefined)
		jump_to_first_page = false;

	filter_url = "";
	for(key in selected_filter_options)
	{
		if(filter_url.length > 0)
			filter_url = filter_url + "+";
		filter_url = filter_url + key;
	}
	if(filter_url == "")
		filter_url = blank_filter_hash;
	if(filter_url == "")
		filter_url = "clear";
	window.location.replace("#" + filter_url);
	previous_hash = window.location.hash;

	if(document.getElementById('filteredProducts'))
	{
		if(jump_to_first_page == true && page_url.length > 0 && page_url != '0' && page_url != '1' && page_url != 'all')
		{
			window.location.replace(JURL + top_level_name + '/' + category_name + '/#' + filter_url);
		}
		else
			queryFilteredProducts(page_url, window.location.hash);
	}
}

function updateFilters()
{
	if(window.location.hash != null && window.location.hash != previous_hash)
	{
		if(window.location.hash.indexOf("name") == 1 || window.location.hash.indexOf("video") == 1)
			blank_filter_hash = window.location.hash.replace("#", "");

		chunks = window.location.hash.split("+", 100);
		for(var index=0; index < chunks.length; index++)
		{
			chunks[index] = chunks[index].replace("#", "");
			if(chunks[index].indexOf("more") == 0)
			{
				chunks[index] = chunks[index].replace("more", "");
				if(chunks[index].search(/[^0-9]/) < 0)
					showMoreOptions(chunks[index], true);
			}
			else if(chunks[index].search(/[^0-9]/) < 0)
				selectFilterOption(chunks[index], true);
		}
		updateFilterURL();
		previous_hash = window.location.hash;
	}
}

function initializeFilters(new_top_level_name, new_category_name, new_page_url, product_matched_options)
{
	top_level_name = new_top_level_name;
	category_name = new_category_name;
	page_url = new_page_url;
	previous_hash = "~";
	// If a product page is being displayed without filter information
	//   update the filters based on the selected product.
	if(window.location.hash == "" && product_matched_options.length > 0)
	{
		for(var index in product_matched_options)
			selectFilterOption(product_matched_options[index], true);
	}
	updateFilters();
	if(update_interval_id < 0)
		update_interval_id = setInterval(updateFilters, 1000);
}

function clearFilterSelections()
{
	// First unselect all of the selected options
	for(key in selected_filter_options)
	{
		if(key.indexOf("more") < 0)
		{
			key = key.replace("more", "");
			if(key.search(/[^0-9]/) < 0)
				toggleFilterOption(key, true);
		}
	}
	// Then reverse all of the expanded options
	for(key in selected_filter_options)
	{
		if(key.indexOf("more") == 0)
		{
			key = key.replace("more", "");
			if(key.search(/[^0-9]/) < 0)
				showLessOptions(key, true);
		}
	}
	window.location.replace("#clear");
	delete selected_filter_options;
	selected_filter_options = new Object();
	return false;
}

function showMoreOptions(filter_id, skip_url_update)
{
	var filter = document.getElementById("filter_" + filter_id);
	if(filter != null && filter.childNodes != null)
	{
		for(var index in filter.childNodes)
		{
			if(filter.childNodes[index] != null)
			{
				if(filter.childNodes[index].className == "hidden")
					filter.childNodes[index].className = "extra";
			
				if(filter.childNodes[index].childNodes != null)
				{
					for(var subindex in filter.childNodes[index].childNodes)
					{
						if(filter.childNodes[index].childNodes[subindex] != null &&
						   filter.childNodes[index].childNodes[subindex].className == "hidden")
							filter.childNodes[index].childNodes[subindex].className = "extra";
					}
				}
			}
		}
	}
	selected_filter_options["more" + filter_id] = 1;
	more_link = document.getElementById("more" + filter_id);
	if(more_link != null && more_link.parentNode != null)
		more_link.parentNode.className = "hidden";
	less_link = document.getElementById("less" + filter_id);
	if(less_link != null && less_link.parentNode != null)
		less_link.parentNode.className = "less";

	if(skip_url_update === undefined || skip_url_update != true)
		updateFilterURL();
	return false;
}

function showLessOptions(filter_id, skip_url_update)
{
	var filter = document.getElementById("filter_" + filter_id);
	if(filter != null && filter.childNodes != null)
	{
		for(var index in filter.childNodes)
		{
			if(filter.childNodes[index] != null)
			{
				if(filter.childNodes[index].className == "extra")
					filter.childNodes[index].className = "hidden";
			
				if(filter.childNodes[index].childNodes != null)
				{
					for(var subindex in filter.childNodes[index].childNodes)
					{
						if(filter.childNodes[index].childNodes[subindex] != null &&
						   filter.childNodes[index].childNodes[subindex].className == "extra")
							filter.childNodes[index].childNodes[subindex].className = "hidden";
					}
				}
			}
		}
	}
	delete selected_filter_options["more" + filter_id];
	more_link = document.getElementById("more" + filter_id);
	if(more_link != null && more_link.parentNode != null)
		more_link.parentNode.className = "more";
	less_link = document.getElementById("less" + filter_id);
	if(less_link != null && less_link.parentNode != null)
		less_link.parentNode.className = "hidden";

	if(skip_url_update === undefined || skip_url_update != true)
		updateFilterURL();
	return false;
}

function selectFilterOption(option_id, skip_url_update)
{
	var option = document.getElementById("option_" + option_id);
	if(option != null)
	{
		option.className = "selected";
	}
	// Update the array regardless to handle the case where the left nav bar
	//   is not shown in the printable view.
	selected_filter_options[option_id] = 1;

	if(skip_url_update === undefined || skip_url_update != true)
		updateFilterURL();
	return false;
}

function toggleFilterOption(option_id, skip_url_update)
{
	var option = document.getElementById("option_" + option_id);
	if(option != null)
	{
		if(option.className == "selected")
		{
			option.className = "";
			delete selected_filter_options[option_id];
		}
		else
		{
			option.className = "selected";
			selected_filter_options[option_id] = 1;
		}
	}

	if(skip_url_update === undefined || skip_url_update != true)
		updateFilterURL(true);
	return false;
}

function showPageWithFilter(url, open_new_window)
{
	if(window.location.hash == "#" + blank_filter_hash)
	{
		if(open_new_window === undefined || open_new_window != true)
			window.location.href = url;
		else
			window.open(url);
	}
	else
	{
		if(open_new_window === undefined || open_new_window != true)
			window.location.href = url + window.location.hash;
		else
			window.open(url + window.location.hash);
	}
	return false;
}

function submitPageWithFilter(form_name, url)
{
	var form = document.getElementById(form_name);
	if(form != null)
	{
		if(window.location.hash == "#" + blank_filter_hash)
			form.action = url;
		else
			form.action = url + window.location.hash;
		form.submit();
	}
	return false;
}

function compareProducts(form_name, url)
{
	var products_selected = '';
	var selection_count = 0;

	var form = document.getElementById(form_name);
	if(form != null)
	{
		// Determine which products are selected
		for(var i=0; i<form.product_selected.length; i++)
		{
			if(form.product_selected[i].checked)
			{
				if(products_selected.length > 0)
					products_selected += '+';
				products_selected += form.product_selected[i].value;
				selection_count += 1;
			}
		}
		if(selection_count <= 0)
			alert('Please select up to 3 products for comparison.');
		else if(selection_count > 3)
			alert('Please limit your comparison to 3 products at a time.');
		else
		{
			if(window.location.hash == "#" + blank_filter_hash)
				window.location.href = url + products_selected + '/';
			else
				window.location.href = url + products_selected + '/' + window.location.hash;
		}

	}
	return false;
}



// -----------------------------------------------------------------------------
// Filtered Products Query
// -----------------------------------------------------------------------------
var filteredProductsReq = FilteredProductsAjaxReq();

function FilteredProductsAjaxReq() 
{ 
	var xmlhttp=false; 
	try 
	{ 
		//AJAX object for new versions of IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch(e) 
	{ 
		try 
		{ 
			// AJAX object for old versions of IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(e) { xmlhttp=false; } 
	} 
	if (!xmlhttp && window.XMLHttpRequest) 
	{ 
		// AJAX object for non IE browsers
		xmlhttp=new XMLHttpRequest(); 
	} 
	return xmlhttp; 
}

function queryFilteredProducts(page, filters)
{
	if (filteredProductsReq.readyState == 4 || filteredProductsReq.readyState == 0) {
		filters = filters.replace("#", "");
		url = JURL + top_level_name + '/' + category_name + '/filteredProducts/' + page + '/' + filters + '/';

		filteredProductsReq.open("GET", url, true);
		filteredProductsReq.onreadystatechange = handleFilteredProductsQuery; 
		filteredProductsReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		filteredProductsReq.send('');
	}
}
			
function handleFilteredProductsQuery()
{
	if (filteredProductsReq.readyState == 1)
	{
		document.getElementById('filteredProducts').innerHTML = "<center><b>Retrieving Products...</b></center>";
	}

	if (filteredProductsReq.readyState == 4) {
		document.getElementById('filteredProducts').innerHTML = filteredProductsReq.responseText;
	}
}	
