var filterFunctionality = new Array();
var filterSuitable = new Array();
var filterOther = new Array();
var filterFallingHeight = new Array();

function Init()
{
	if (!Array.prototype.contains)
	{
	    Array.prototype.contains = function(obj)
	    {
		    var len = this.length;
		    for (var i = 0; i < len; i++)
		    {
				if(this[i]===obj){return true;}
		    }
		    return false;
		};
	}
}

function Submit(formID)
{
	document.getElementById(formID).submit();
}

function SetInputText(inputID, val, def)
{
	if (document.getElementById(inputID).value == def)
	{
		document.getElementById(inputID).value = val;
	}
}

function ChangeProductImage(id,crop, lines, productline)
{
	if (crop == null)
	{
		var src = "file.php?FileID=" + id + "&Dimension=565";
	}
	else
	{
		var src = "file.php?FileID=" + id + "&Crop=" + crop + "&Productline=" + productline;
	}
	document.getElementById("product_photo").src = src;
	if (lines)
	{
		document.getElementById("lightbox_photo").href = 'file_large.php?FileID=' + id + "&Dimension=800";
	}
	else
	{
		document.getElementById("lightbox_photo").href = 'file_large.php?FileID=' + id + "&Dimension=800&Colour=1";
	}
	
	if (lines)
	{
		document.getElementById("content_block_lines_small").style.display = "block";
		document.getElementById("content_block_lines_small").style.visibility = "visible";
		document.getElementById("content_block_lines_big").style.display = "block";
		document.getElementById("content_block_lines_big").style.visibility = "visible";
		//document.getElementById("watermark").style.display = "block";
		//document.getElementById("watermark").style.visibility = "visible";
	}
	else
	{
		document.getElementById("content_block_lines_small").style.display = "none";
		document.getElementById("content_block_lines_small").style.visibility = "hidden";
		document.getElementById("content_block_lines_big").style.display = "none";
		document.getElementById("content_block_lines_big").style.visibility = "hidden";
		//document.getElementById("watermark").style.display = "none";
		//document.getElementById("watermark").style.visibility = "hidden";
	}
}

function ClearAndFilter(type)
{
	var i = 0;
	while(document.getElementById("filter_" + type + "_" + i) != null)
	{
		var activeCell = document.getElementById("filter_" + type + "_" + i);

		activeCell.className = "canhover";
		for(k = 0; k < filterOther.length; k++)
		{
			if (filterOther[k] == activeCell.innerHTML)
			{
				filterOther.splice(k,1);
			}	
		}
		i++;
	}
	
	FindProducts();
}

function ToggleFallFilter(id, filter)
{
	var cell = document.getElementById(id);
	
	var data = id.split("_");
	var type = data[1];
	var y = data[2];
	var i = 0;
	
	filterFallingHeight = new Array();
	
	
	while(document.getElementById("filter_" + type + "_" + i) != null)
	{
		var activeCell = document.getElementById("filter_" + type + "_" + i);
		if (i == y)
		{
			if (document.getElementById("filter_" + type + "_" + y).className == "canhover selected")
			{
				document.getElementById("filter_" + type + "_" + y).className = "canhover";
			}
			else
			{
				document.getElementById("filter_" + type + "_" + y).className = "canhover selected";
				
				if (filter == "tot 1.50 m")
				{
					filterFallingHeight.push("tot 1.50 m");
					filterFallingHeight.push("tot 1.00 m");
					filterFallingHeight.push("tot 60 cm");
				}
				else if (filter == "tot 1.00 m")
				{
					filterFallingHeight.push("tot 1.00 m");
					filterFallingHeight.push("tot 60 cm");
				}
				else
				{
					filterFallingHeight.push(filter);
				}
			}
		}
		else
		{
			activeCell.className = "canhover";
		}
		
		i++;
	}
	
	
	
	
	FindProducts();
}

function ToggleAndFilter(id, filter)
{
	var cell = document.getElementById(id);
	
	var data = id.split("_");
	var type = data[1];
	var y = data[2];
	var i = 0;
	while(document.getElementById("filter_" + type + "_" + i) != null)
	{
		var activeCell = document.getElementById("filter_" + type + "_" + i);
		if (i == y)
		{
			
			if (!filterOther.contains(filter))
			{
				document.getElementById("filter_" + type + "_" + i).className = "canhover selected";
				filterOther.push(filter);
			}
			else
			{
				document.getElementById("filter_" + type + "_" + i).className = "canhover";
				for(k = 0; k < filterOther.length; k++)
				{
					if (filterOther[k] == filter) // verwijder het item uit de array
					{
						filterOther.splice(k,1);
					}	
				}
			}
		}
		else
		{
			if(activeCell.innerHTML != "")
			{
				activeCell.className = "canhover";
				
				for(k = 0; k < filterOther.length; k++)
				{
					if (filterOther[k] == activeCell.innerHTML) // verwijder alle andere items uit de array
					{
						filterOther.splice(k,1);
					}	
				}
			}
		}
		i++;
	}
	
	FindProducts();
}

function ToggleOrFilter(id, filter, filterArray)
{
	var cell = document.getElementById(id);
	
	if(cell.className == "canhover")
	{
		cell.className = "selected canhover";
		filterArray.push(filter);
	}
	else
	{
		cell.className = "canhover";
		for(i = 0; i < filterArray.length; i++)
		{
			if (filterArray[i] == filter)
			{
				filterArray.splice(i,1);
			}	
		}
	}
	
	FindProducts();
}

function ClearFilters()
{
	var y = 0;
	
	for (var x = 0; x < cellCount; x++)
	{
		y = 0;
		while(document.getElementById("filter_" + x + "_" + y) != null)
		{
			var cell = document.getElementById("filter_" + x + "_" + y);
			if (cell.innerHTML != "")
			{
				cell.className = "canhover";
			}
			y++;
		}
	}
	
	activeFilter = new Array();
	FindProducts();
}

function RemoveIndices(arr, indices)
{
	for(i = (indices.length - 1); i >= 0; i--)
	{
		
		arr.splice(indices[i], 1);
	}
	
	return arr;
}

function FindProducts()
{
	var validProducts = new Array();
	var invalidIds = new Array();
	
	// copy array
	for(var i = 0; i < products.length; i++)
	{
		validProducts.push(products[i]);
	}
	
	// functionaliteit filter
	if(filterFunctionality.length > 0)
	{
		for(var i = 0; i < validProducts.length; i++)
		{
			// functionality
			//alert(validProducts[i][2]);
			var hasFunctionality = false;
			for(var f = 0; f < filterFunctionality.length; f++)
			{
				if (validProducts[i][1].indexOf(filterFunctionality[f], 0) > -1)
				{
					//alert(validProducts[i][0] + "\r\n" + validProducts[i][2] + "\r\n" + filterFunctionality[f] + "\r\n" + validProducts[i][2].indexOf(filterFunctionality[f], 0));
					hasFunctionality = true;
				}
			}
			
			if (!hasFunctionality)
			{
				invalidIds.push(i);
			}
		}
		validProducts = RemoveIndices(validProducts, invalidIds);
	}
	
	// geschikt voor filter
	var invalidIds = new Array();
	if(filterSuitable.length > 0)
	{
		for(var i = 0; i < validProducts.length; i++)
		{
			// functionality
			//alert(validProducts[i][2]);
			var hasSuitable = false;
			for(var f = 0; f < filterSuitable.length; f++)
			{
				if (validProducts[i][2].indexOf(filterSuitable[f], 0) > -1)
				{
					//alert(validProducts[i][0] + "\r\n" + validProducts[i][2] + "\r\n" + filterFunctionality[f] + "\r\n" + validProducts[i][2].indexOf(filterFunctionality[f], 0));
					hasSuitable = true;
				}
			}
			
			if (!hasSuitable)
			{
				invalidIds.push(i);
			}
		}
		validProducts = RemoveIndices(validProducts, invalidIds);
	}
	
	// valhoogte filter
	var invalidIds = new Array();

	if(filterFallingHeight.length > 0)
	{
		for(var i = 0; i < validProducts.length; i++)
		{
			var index = filterFallingHeight.indexOf(validProducts[i][5], 0);
			
			if(index == -1)
			{
				invalidIds.push(i);
			}
		}
		validProducts = RemoveIndices(validProducts, invalidIds);
	}
	
	// other filter
	var invalidIds = new Array();
	if (filterOther.length > 0)
	{
		for(var i = 0; i < validProducts.length; i++)
		{
			var isValid = true;
			for(var j = 0; j < filterOther.length; j++)
			{
				//alert(validProducts[i][3] + "\r\n" + filterOther[j]);
				if (validProducts[i][3] != filterOther[j] &&
					validProducts[i][4] != filterOther[j] &&
					validProducts[i][5] != filterOther[j] &&
					validProducts[i][6] != filterOther[j])
					
				{
					isValid = false;
				}
			}
			
			if (!isValid)
			{
				invalidIds.push(i);
			}
		}
		
		validProducts = RemoveIndices(validProducts, invalidIds);
	}
	
	// hide all products first
	for(var i = 0; i < products.length; i++)
	{
		var element = document.getElementById("product_" + products[i][0]);
		element.style.visibility = "hidden";
		element.style.display = "none";
	}
	
	
	// display valid products
	for(var i = 0; i < validProducts.length; i++)
	{
		var element = document.getElementById("product_" + validProducts[i][0]);
		element.style.visibility = "visible";
		element.style.display = "block";
	}
	
	document.getElementById("filter_count").innerHTML = validProducts.length;
}

Init();

/*
 * SUCKERFISH JAVASCRIPT
 */
 sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
 
