$(document).ready(function() {

	if ($("a[rel^='prettyPhoto']").length > 0) {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			showTitle:false,
			//animationSpeed: 'normal', /* fast/slow/normal */
			//padding: 40, /* padding for each side of the picture */
			//opacity: 0.35, /* Value betwee 0 and 1 */
			//showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
			counter_separator_label: '/' /* The separator for the gallery counter 1 "of" 2 */
		});
	}

	//fade items on left side of screen
	homepageItemCounter = $('.homepageItem').length;
	if (homepageItemCounter > 1) {
		$('.homepageItem').hide();
		$('.homepageItem').first().show();
		setTimeout('showHideItems(\'.homepageItem\')',showHideTimeout);
	}

	//header flash
	handleBackground();

	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();

	//check all formfields on blur (leaving field)
	if ($("form").length > 0) {
		$("form input, form select").blur(function() {
			$.checkField($(this));
		});

		//check form on submit
		$('form').submit(function() {
			return $.checkForm($(this));
		});
	}


});

$.checkField = function(field)
{
	if (field.hasClass('isReq') && field.val() == '') {
		field.css("border","1px solid red");
		$('#lb_'+field.attr('id')).css('color','red');
		return false;
	} else if ((field.hasClass('email') || field.attr('name') == 'email') && !$.emailValidation(field.val())) {
		field.css("border","1px solid red");
		$('#lb_'+field.attr('id')).css('color','red');
		return false;
	} else {
		field.css("border","1px solid #B1B1B1");
		$('#lb_'+field.attr('id')).css('color','#404040');
	}
	return true;
};

$.emailValidation = function(value) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(value) == false) {
		return false;
	}
	return true;
};


$.checkForm = function(currentForm)
{
	var errorFree = true;
	currentForm.find('textarea, input, select').each(function()	{
		if (!$.checkField($(this))) {
			errorFree = false;
		}
	});
	return errorFree;
};



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();
}

function handleBackground()
{
	if ($("div#content").length > 0)	{

		var cId = '1';
		//decide witch page where on
		if ($("input.cbackScroll").length > 0)	{
			var pId = $("input.cbackScroll").val();
			if (pId != '')	{
				cId = pId;
			}
		}

		$("div#fotofader").ccmsFadeBackground({
			animationSpeed: 1000,
			structuurid: cId,
			ajaxGetImagesScript: '/javascript/ajaxRequests/getImgUl.php',
			timeOut: 5000,
			showBreakOption: false
		});
	}
}
