function supportInputFields() {
	var values = new Array();
	var inputs = $(":input");
	
	inputs.each(function(i,e) {
		values[i] = e.value;
		if($(e).hasClass("clear")) {
			$(e).focus(function() {			
				if(e.value == values[i]) {				
					e.value = '';
				}
			});
			$(e).blur(function() {
				if(e.value == '') {
					e.value = values[i];
				}
			});
		} else if($(e).is(".password.text")) {
			$(e).focus(function() {		
				$(this).addClass('hide');
				$(this).parent("form").find(".password.input").removeClass('hide');
				$(".password.input",$(this).parent("form")).focus();					
			});			
		} else if($(e).is(".password.input")) {
			$(e).blur(function() {
				if(e.value == '') {
					$(this).addClass('hide');
					$(this).parent("form").find(".password.text").removeClass('hide');
				}
			});
		}
	});
}

$(function() {
	$(".carousel").jCarouselLite({
		btnNext: "div.next",
		btnPrev: "div.prev",
		visible: 4
	});	

	/* bovenplaat */
	$.slideShow = function(e) {
 	    var $active = $(e).find('.beeld.active');
		
	    if ( $active.length == 0 ) $active = $(e).find('.beeld:last');
	    	
	    var $next =  $active.next().length ? $active.next() : $(e).find('.beeld:first');
		
	    $active.addClass('last-active');
	
	    $next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            $active.removeClass('active last-active');
	    });	   		    	
	}

	if($('#header .bg .beeld').length > 1) {
		var fade = setInterval( "$.slideShow('div#header .bg')", 5000 );
	}

	$(".clickable").click( function() {					
		document.location.href=$('a:eq(0)',this).attr('href');
		return false;
	});
	
	$("h2.voorwaarden").toggle(
		function(){
			$("div.voorwaarden").show();
		},
		function() {
			$("div.voowaarden").hide();
		}
	);

	
	/* hoofdmenu */
	$('.hoofdmenu').each(function(i,el){		
		$(this).mouseover(function() {
			waitMenu = setTimeout(function() {
				$(el).siblings().each(function(i, e) {
					$('#submenu' + (i+1)).hide();
					$('#headermenu').attr('src',baseUrl + 'img/headermenu.png');
				});
				$('#submenu' + (i+1)).show();
				$('#headermenu').attr('src',baseUrl + 'img/headermenu'+(i+1)+'.png');
			},100);
		}).mouseout(function(e) {
			if (waitMenu != 'undefined'){clearTimeout(waitMenu);}
			var x = (e.pageX - $('#header').position().left);
			var y = (e.pageY - $('#header .bottom').position().top);
			// Fix voor IE i.c.m. imagemap
			if (y<83) {
				$('#submenu' + (i+1)).hide();
				$('#headermenu').attr('src',baseUrl + 'img/headermenu.png');
			}
		});
	});
	
	$('.submenu').each(function(i,e){		
		$('.content',e).hide();
		$('.content.is_active',e).show();
		$(this).mouseover(function() {
			$('#submenu' + (i+1)).show();
			$('#headermenu').attr('src',baseUrl + 'img/headermenu'+(i+1)+'.png');
		}).mouseout(function() {
			$('#submenu' + (i+1)).hide();
			$('#headermenu').attr('src',baseUrl + 'img/headermenu.png');
		});
		$('.tab',this).each(function(j,el){
			$(this).click(function(){
				$('.tab',e).removeClass('is_active');
				$('.content',e).hide();
				$('.tab',e).eq(j).addClass('is_active');
				$('.content',e).eq(j).show();
			});			
		});
	});

	$("#content .links.categorie a.doelgroep").click(function(event){
		event.preventDefault();

		$("#content .links.categorie .categorieen").toggleClass('inactive active');
		$("#content .links.categorie a.doelgroep img").each(function (i ,e) {
			var src = $(this).attr('src');
			if ($(this).hasClass('active') === true) {
				src = src.replace('_active','_inactive');
			} else if ($(this).hasClass('inactive') === true) {
				src = src.replace('_inactive','_active');				
			}
			$(this).attr('src', src);
		}).toggleClass('active inactive');
				
		$('#content .rechts .categorie').toggleClass('inactive active');
			
		return false;
	});
	
	$(".merken h2").toggle(
		function(){
			if($('.merken .content').html().length == 0) {
				$.ajax({
					url: "shop/ajaxgetmerken/",
					data: {categorie: $('.merken input[name="categorie"]').val()},
					success: function(result) {
						$('.merken .content').html(result);
						$('.merken .populair').hide();
						$('.merken .content').show();						
					}
				});
				return false;
			} else {
				$('.merken .populair').hide();
				$('.merken .content').show();				
			}
		},function() {
			$('.merken .content').hide();
			$('.merken .populair').show();
		}
	);
	
	$.setDoelgroep = function(doelgroep) {
		$.ajax({
			url: "shop/ajaxsetdoelgroep/",
			data: 'doelgroep='+doelgroep
		});
	};
	

	$('form#retouren button:submit').click(function(e) {
		e.stopPropagation();
		// selecteer waardes
		
		var selectedItems = new Array();
		$("input[@name='retouren[]']:checked").each(function() {selectedItems.push($(this).val());});
		$.ajax({
			url: "mijn/retourneren/",
			data: {zending_ids: selectedItems},
			success: function(data) {				
				$.popup.show(data);
			}
		});
		return false;
	});

	
	/* zijkantjes H2 */
	$('h2').each(function() {
		$(this).append('<div class="titel_links"></div><div class="titel_rechts"></div>');
	});

	/* in/uitklappen selectboxes */
	$('.selectbox').click(function() {
		if (!$(this).hasClass('open')) {
			$('.selectbox.open').removeClass('open');
		}
		$(this).toggleClass('open');
	});
	
	/* aanmelden nieuwsbrief */
	$('form.nieuwsbrief_aanmelden a').click(function() {
		$('form.nieuwsbrief_aanmelden').submit();
		return false;
	});
	
	/* vergroten thumbnails op detailpagina */
	$('.details_links .thumbs div').click(function() {
		$('#afbeelding_groot').attr('src', $('img', this).attr('src').replace('64/64','372/372'));
		$('a.afbeelding').attr('href', $('img', this).attr('src').replace('64/64','800/800'));
		$('.details_links .innerzoom img').attr('src', $('img', this).attr('src').replace('64/64','3720/3720'));
	});
	
	/* filters */
	$('.filter .filtertje select').change(function() {
		$(this.form).submit();
	});
	
	/* lightbox */
	$('a.afbeelding').lightBox();
	$('img.zoomicon').click(function() {
		$(this).siblings('a.afbeelding').click();
	});
	
	/* zoom */
	var afbeeldingVerhouding = 1;
	$('.details_links .zoom').hide();
	$('.details_links .afbeelding').mousemove(function(e) {
		var x = ((e.pageX - $(this.parentNode).position().left) * -1 * afbeeldingVerhouding) + ($(this).width()/2);
		var y = ((e.pageY - $(this.parentNode).position().top) * -1 * afbeeldingVerhouding) + ($(this).height()/2);
		$('.details_links .innerzoom img').css('left', x + 'px').css('top', y + 'px');
	}).mouseover(function() {
		$('.details_links .zoom').show();
		afbeeldingVerhouding = $('.details_links .innerzoom img').width() / $('.details_links .afbeelding img').width();
	}).mouseout(function() {
		$('.details_links .zoom').hide();
	});
		
	/* validatie artikel-bestel */
	$('form#bestel').submit(function() {		
		if($('.maten input[name="artikelnr"]:checked').val()==undefined) {
			$('p.error').show();
			return false;
		} else {
			return true;
		}
	});
	
	/* wijzigen aantal in winkelmandje */
	$('.winkelmandje .item .aantal select').change(function() {
		$(this.form).submit();
	});
		
	// Valideer emailadres
	$('input#email').change(function() {
		var email = this.value;
		$.ajax({
			cache: false,
			url: location.href,
			data: { email: email, gegevens: 1 },
			dataType: 'json',
			type: 'POST',
			success: function(data, textStatus, XMLHttpRequest) {
				$('tr#emailrow .error').remove();
				if (data.emailError) {
					$('tr#emailrow').append('<td class="error">' + data.emailErrorText + '</td>');
				}
			}
		});
	});
	
	// Voorbereiden 'autoclear' velden
	$('input.autoclear').each(function() {
		this.defaultValue = this.value;
	});
	// focus 'autoclear' veld
	$('input.autoclear').focus(function() {
		if (this.value==this.defaultValue) {
			this.value = '';
		}
	});
	// blur 'autoclear' veld
	$('input.autoclear').blur(function() {
		if (this.value=='') {
			this.value = this.defaultValue;
		}
	});
	
	//SUPPORT
	supportInputFields();

	// kleur-afbeelding valign
	$('.details_rechts .kleuren img').each(function() {
		if (this.complete) {
			if ($(this).height() < 89) {
				$(this).css('margin-top', (((89-$(this).height())/2)+1) + 'px');
			}
		} else {
			$(this).load(function() {
				if ($(this).height() < 89) {
					$(this).css('margin-top', (((89-$(this).height())/2)+1) + 'px');
				}
			});
		}
	});

	// thumbnail valign + onclick
	$('.details_links .thumbs div img').each(function() {
		if (this.complete) {
			if ($(this).height() < 64) {
				$(this).css('margin-top', ((64-$(this).height())/2) + 'px');
			}
		} else {
			$(this).load(function() {
				if ($(this).height() < 64) {
					$(this).css('margin-top', ((64-$(this).height())/2) + 'px');
				}
			});
		}
	}).click(function() {
		var p = this.parentNode;
		while (p.nodeName!='DIV') {
			p = p.parentNode;
		}
		$(p).click();
	});
		
	// Retour ruilen voor artikel
	if ($('select[name^=refundmethode]').val() == 'ruilen') {
		$('tr.ruilartikel').show();
	} else {
		$('tr.ruilartikel').hide();
	}
	$('select[name^=refundmethode]').change(function() {
		if ($(this).val() == 'ruilen') {
			$('tr.ruilartikel').show();
		} else {
			$('tr.ruilartikel').hide();
		}
	});	
});
