var bdone = true;
var t;
var filters_configured = false;

function filter_products(src)
{
	if(!filters_configured)
		return false;
	bdone = false;
	show_hourglass();
	switch(src)
	{
		case 'brands':
			$('dh-hourglass-message').innerHTML = 'Filtering by brand';
			pl_filter_by_brand();
			break;
		case 'catno':
			pl_filter_by_page_no();
			break;
		case 'price':
			pl_filter_by_price();
			break;
	}
	bdone = true;
}

// create the AJAX call to filter products by brand
function pl_filter_by_brand()
{
	var brand_id = $('dh-brandlist').options[$('dh-brandlist').options.selectedIndex].value;
	if(brand_id == '0')
	{
		pl_show_all();
	}
	else if(brand_id =='')
	{
		return;
	}
	else
	{
		var url = '_get_prods.asp';
		var pars = 'brand=' + $('dh-brandlist').options[$('dh-brandlist').options.selectedIndex].value;
		objAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: pl_update_prods });
	}
}

// show or hide thumbnails based upon the value of the brands drop down
function pl_update_prods(xml)
{
	var data = xml.responseXML.documentElement;
	try
	{
		var err = data.getElementsByTagName('error')[0].firstChild.data;
	}
	catch(ex)
	{

		alert(ex);
	}
	
	if(err == 'NONE')
	{
		var prods = data.getElementsByTagName('prod_id');
		var prodivs = $$('div#dh-prodlist-middle .dh-thumb');
		var prodid;
		var bfound = false;
		for(var i = 0; i < prodivs.length; i++)
		{
			prodid = prodivs[i].getAttribute('id');
			prodid = prodid.replace('p', '');
			bfound = false;
			for(var j = 0; j < prods.length; j++)
			{
				if(prodid == prods[j].firstChild.data)
					bfound = true;				
			}
			if(!bfound)
				$(('p' + prodid)).style.display = 'none';
			else
				$(('p' + prodid)).style.display = 'block';
		}
	}
}


function pl_filter_by_price()
{
	var pf = $('dh-pricefilter').options[$('dh-pricefilter').options.selectedIndex].value;
	if(pf == '0')
	{
		pl_show_all();
	}
	else if(pf != 'x')
	{
		var price_bands = pf.split('-');
		var prodivs = $$('div#dh-prodlist-middle .dh-thumb');
		var prod_price;
		for(var i = 0; i < prodivs.length; i++)
		{
			// get the product id from the id attribute of this thumbnail's div
			prodid = prodivs[i].getAttribute('id');
			prodid = prodid.replace('p', '');
			prod_price = Number($(('price' + prodid)).innerHTML);
			if((Number(prod_price) >= Number(price_bands[0])) && (Number(prod_price) <= Number(price_bands[1])))
				$(('p' + prodid)).style.display = 'block';
			else
				$(('p' + prodid)).style.display = 'none';
		}
	}
}

// filters the page by catalogue page number
function pl_filter_by_page_no()
{
	var pagenum = $('dh-pagenumfilter').options[$('dh-pagenumfilter').options.selectedIndex].value;
	if(pagenum == '0')
	{
		pl_show_all();
	}
	else if(pagenum != 'x')
	{
		var prodid;
		var prodpagenum;
		var prodivs = $$('div#dh-prodlist-middle .dh-thumb');
		for(var i = 0; i < prodivs.length; i++)
		{
			// get the product id from the id attribute of this thumbnail's div
			prodid = prodivs[i].getAttribute('id');
			prodid = prodid.replace('p', '');
			prodpagenum = Number($(('page' + prodid)).innerHTML);
			if(pagenum == prodpagenum)
				$(('p' + prodid)).style.display = 'block';
			else
				$(('p' + prodid)).style.display = 'none';
		}

	}
}

// resets the thumbnail display and shows all of them
function pl_show_all()
{
	var prodid;
	var prodivs = $$('div#dh-prodlist-middle .dh-thumb');
	for(var i = 0; i < prodivs.length; i++)
	{
		prodid = prodivs[i].getAttribute('id');
		$(prodid).style.display = 'block';
	}
	
	if($('dh-pricefilter'))
		$('dh-pricefilter').options.selectedIndex = 0;
	if($('dh-brandlist'))
		$('dh-brandlist').options.selectedIndex = 0;
	if($('dh-pagenumfilter'))
		$('dh-pagenumfilter').options.selectedIndex = 0;


}


function pl_configure_filters()
{

	// configure the price points
	var topprice = Math.round($('dh-top-price').innerHTML);
	var divisor = Math.round(Math.round(topprice / 5) / 10) * 10;
	divisor <= 0 ? divisor = 1 : divisor = divisor;
	// var to = divisor;
	var to = 0;
	var from = '0';
	var pf = $('dh-pricefilter');
	var o;


	do
	{
		to += divisor;
		o = document.createElement('option');
		o.text = from + ' to ' + to + ' (GBP)';
		o.value = from  + '-' + to;
		try
		{
			pf.add(o,null); // standards compliant
		}
		 catch(ex)
		{
			pf.add(o); // IE only
		}
		from = to;
	} while(to <= topprice)

	// configure the catalogue page numbers
	var pf = $('dh-pagenumfilter');
	var tmp;
	var numpagenos = 0
	var bfound = false;
	var pagenoarray = new Array();
	var catnos = $$('div#dh-prodlist-middle .dh-cat-page-nums');
	
	// loop through all the catalogue numbers on this page and insert each unique one into an array
	for(var i = 0; i < catnos.length; i++)
	{
		tmp = catnos[i].innerHTML;
		for(var j = 0; j < pagenoarray.length; j++)
		{
			if(tmp == pagenoarray[j])
			{
				bfound = true;
				break;
			}
		}

		if(!bfound)
		{
			if (Number(tmp))
			{
				pagenoarray[numpagenos] = Number(tmp);
				numpagenos++;
			}
		}
		bfound = false;
	}

	
	// sort the array into numerical order
	pagenoarray.sort(sortNumber);

	// create a drop down list of unique catalogue numbers
	for(var i = 0; i < pagenoarray.length; i++)
	{
		o = document.createElement('option');
		o.text = pagenoarray[i];
		o.value = pagenoarray[i];
		try
		{
			pf.add(o,null); // standards compliant
		}
		 catch(ex)
		{
			pf.add(o); // IE only
		}

	}
	filters_configured = true;
}


// "Hourglass" (galloping horse) functions

function check_hourglass()
{
	if(bdone)
	{
		$('dh-prodlist-middle').style.display = '';
		$('dh-hourglass').style.display = 'none';
		clearTimeout(t);
	}				
}

function show_hourglass()
{
	$('dh-prodlist-middle').style.display = 'none';
	$('dh-hourglass').style.display = '';
	t = setTimeout(check_hourglass, 500);
}

function sortNumber(a,b) { return a - b }