function findBaseName(url) {
   return url.replace(/^.*\\/, '');
}

function filterSubCats(catid,subcatid) {
	addremove = 'add';
	if (subcatid==0) {
		subcats = new Array();
		$('.products .product').show();
		$('.subcats a').removeClass('act');
		$('.subcats #subcat_0').addClass('act');
	} else {
		if ($('.subcats #subcat_'+subcatid).attr('class')=='act') {
			for (var i=0;i<subcats.length;i++) {
				if (subcats[i]==subcatid) subcats.splice(i,1);
			}
			addremove = 'remove';
		} else subcats.push(subcatid);
		
		$.ajax({ 
			type	: "POST", 
			url		: "/services/filterCategory.php", 
			data	: "catid="+catid+"&subcatid="+subcats.join('_'),
			dataType: "json",
			success : function(res) {
				$('.products .product').hide();
				$.each(res.products, function(i,row){			
					$('.products #product_'+row.id).show();
				});
				
				if (addremove=='add') {
					$('.subcats #subcat_0').removeClass('act');
					$('.subcats #subcat_'+subcatid).addClass('act');
				} else if (addremove=='remove') {
					$('.subcats #subcat_'+subcatid).removeClass('act');
					if ($('.subcats a.act').length==0) filterSubCats(catid,0);
				}
			}
		});
	}
}

function searchContent(val) {
	found = false;
	$('.searchfrm .progress').html("<img src='/images/ajax-loader.gif' style='width:16px;height:16px'>").show().css('cursor','auto');
	$('.searchfrm input').css('padding-right','27px').css('width',257);
	$.ajax({
		type: "POST",
		url: "/services/searchContent.php",
		data: "val="+val,
		dataType: "json",
		success: function(res){
			if (val!='') $('.searchfrm .searchbtn').attr('href',res.searchPage);
			else $('.searchfrm .searchbtn').attr('href','javascript:void(0);');
			if (res.products.length>0) {
				$('.searchresults .searchCnt').html('');
				
				$.each(res.products, function(i,row){
					html = "<a href='"+row.urladdress+"' title=\""+row.title+"\" class='prodbtn'>";
						html += "<span class='img'>";
						if (row.image!='') html += "<img src='"+row.image+"'>";
						html += "</span>";
						html += "<span class='desc'>";
							html += "<font class='greentxt title'>"+row.title+"</font>";
							html += "<font class='greentxt'>Inhoud</font> "+row.content+"<br>";
							html += "<font class='orangetxt'>&euro; "+row.price+"</font><br>";
						html += "</span>";
					html += "</a>";
					
					$('.searchresults .searchCnt').append(html);
				});
				if (res.totalResults>3) {
					$('.searchresults .bottomBar').css('height',34).css('background-position','0 0').css('font-size','11px').css('line-height','14px').find('span').css('border-top','1px solid #e39e5d');
					$('.searchresults .bottomBar a font').html(res.totalResults);
					$('.searchresults .bottomBar a').attr('href',res.searchPage).show();
				} else $('.searchresults .bottomBar').css('height',4).css('background-position','0 -30px').css('font-size','0px').css('line-height',0).find('span').css('border-top',0);
					
				$('.searchresults .searchCnt').append("<div class='regulator'></div>");
				found = true;
			}
			
			if (found) {
				$('.searchfrm .progress').html("<img src='/images/buttons/btn_close.png' class='closebtn' style='padding:2px 5px'>").show().css('cursor','pointer');
				$(".closebtn").click(function() {
					$('.searchfrm input').val('Zoek een product ...');
					$(this).parent().hide();
					$('.searchresults').hide();
					$('.searchfrm .progress').html('').hide();
					$('.searchfrm input').css('padding-right','5px').css('width',279);
					$('.container .menu .searchfrm input').autocomplete('destroy');
					$('.container .menu .searchfrm input').autocomplete({
						minLength: 0,
						source:function(request,response) {
							searchVal = request.term;
							if (searchVal.length==0) {
								$('.searchfrm input').val('Zoek een product ...');
								$('.searchfrm .progress').html('').hide();
								$('.searchfrm input').css('padding-right','5px').css('width',279);
								$('.searchresults').hide();
								$('.container .menu .searchfrm input').autocomplete('destroy');
								$('.container .menu .searchfrm input').autocomplete({
									minLength: 0,
									source:function(request,response) { searchContent(request.term); }
								});
							} else if (searchVal.length >=1) searchContent(request.term);
						}

					});
				});
				$('.searchresults').show();
			} else {
				$('.searchfrm .progress').html('').hide();
				$('.searchfrm input').css('padding-right','5px').css('width',279);
				$('.searchresults').hide();
			}
		}
	});
}
