$(document).ready(function() {
	if ($('.letter').length != 0) {
		$('.letter').hide();
		$('.abc').click(function() {
			var abcLetter = $(this).attr('id').split('_');
			if ($('#abcLetter_'+abcLetter[1]).css('display') == 'none') {
				$('#abcLetter_'+abcLetter[1]).slideDown();
			} else {
				$('#abcLetter_'+abcLetter[1]).slideUp();
			}
		});
	}


	/* zebra bandjes op product detail pagina */
	$("div.relevante_projecten div:odd").addClass("project_oneven");
	$("div.relevante_projecten div:even").addClass("project_even");
	$("div.relevante_projecten div:first").removeClass("project_even").addClass("maatwerk_header");

	addFormAction();


});


function showResultsPerPage(results)
{
	$('#productsPerpage').val(results);
	$('#theFilterForm').submit();
}

function submitFilterForm()
{
	$('#page').val('1');
	$('#theFilterForm').submit();
}

//add action to filterform
function addFormAction()
{
	$('#theFilterForm').submit(function() {
		//display loadingdiv
		$("#loadingFilter").css('opacity', '0.5');
		$("#loadingFilter").css('display','block');
		$("#loadingProducts").css('opacity', '0.5');
		$("#loadingProducts").css('display','block');
		var inputs = $("#theFilterForm :input");
		var filterUrl = '';
		//create filterAjaxurl
		inputs.each(function(i){
			if ($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true) {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			} else if ($(this).attr('type') == 'text' || $(this).attr('type') == 'hidden') {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			}
 		});

 		//process ajaxrequest
		$.ajax({
			url: "/filterProducts.php",
			type: "POST",
			data: filterUrl,
			dataType: "html",
			//cache: false,
			success: function(responseHtml) {
				var $holder = $('<div/>').html(responseHtml);
				//update filterDiv
				$('#productFilter').html($('#productFilter', $holder).html());
				//update productresultdiv
				$('.content_bb').html($('#productsFound', $holder).html());

				$("#loadingFilter").css('display','none');
				$("#loadingProducts").css('display','none');
				addFormAction();
			}
		});
		return false;
	});
}

function showLoginError(elementName){

	var element = document.getElementById(elementName);

	$.modal($(element),{

    	minHeight:17,
		containerCss:{backgroundColor:"#fff",height:130,padding:0,width:322},

		onOpen: function(dialog)
		{
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.slideDown('normal', function () {
					dialog.data.fadeIn('normal');

					element.style.visibility = 'visible';


				});
			});
		},
		onClose: function(dialog)
		{
			dialog.overlay.fadeOut('fast', function () {
				dialog.container.fadeOut('fast', function () {
					dialog.data.fadeOut('fast', function () {

						$.modal.close(); // must call this!
						$("#klantLoginFoutMeldingen").remove(); // and this too!

					});
				});
			});
		}

	});
}

function showResultPage(number)
{
	$('#page').val(number);
	$('#theFilterForm').submit();
}

function uncheckFilter(id)
{
	$('#' + id + 'Fieldset > input').attr('checked', false);
	$('#theFilterForm').submit();
}