// JavaScript Document
$(document).ready(function() {
	var t = '';
	$("#sortbox").click(function() {
		$("#dropdown").slideToggle();
	});
	$("#logo").click(function() {
		window.location.href = "./";
	});
	$("#subgroep_list ul li").click(function() {
		var url = $(this).find('a:first').attr('href');
		if (url) window.location.href = url;
	});
	$("#teaserimg li").click(function() {
		window.location.href = "./?nr=" + $(this).attr("id");
	});
	$("p.btt_command").click(function() {
		if ($(this).attr('id')) {
			clearTimeout(t);
			$.post("core/actions.php", {
				a: 'add_item',
				nr: $(this).attr('id')
			}, function() {
				$.loadcart();
			});
			$.showupdate();
		}
	});
	$("span.btt_command_detail").click(function() {
		$.post("core/actions.php", {
			a: 'add_item',
			nr: $(this).attr('id'),
			qty: $("input[class=aantal]").val(),
			maat: $("select[name=maat]").val(),
			bestelinfo: $("textarea[name=bestelinfo]").val()
		}, function() {
			if ($("span.btt_command_detail").hasClass('redirect')) {
				window.location.href = '?show=cart';
			} else {
				$.loadcart();
			}
		});
		if (!$("span.btt_command_detail").hasClass('redirect')) {
			$.showupdate();
		};
	});
	$(".cart_qty input").blur(function() {
		$.post("core/actions.php", {
			a: "update",
			nr: $(this).attr('nr'),
			qty: $(this).val()
		}, function() {
			window.location.href = "?show=cart";
		});
	});
	$(".cart_qty input").keypress(function(e) {
		if (e.keyCode == 13) {
			$(this).blur();
		}
	});
	$(".cart_delitem").click(function() {
		$.post("core/actions.php", {
			a: "update",
			nr: $(this).attr('nr'),
			qty: 0
		}, function() {
			window.location.href = "?show=cart";
		});
	});
	$("#empty_cart").click(function() {
		$.post("core/actions.php", {
			a: "empty"
		}, function() {
			window.location.href = "?show=cart";
		});
	});
	$.loadcart = function() {
		$("p.cartbox").load("core/actions.php", {
			a: 'loadcart'
		});
	}
	$.showupdate = function() {
		$("div.added").slideDown();
		t = setTimeout(function() {
			$("div.added").slideUp('def')
		}, 3000);
	}
	$("#register").click(function() {
		var block = false;
		$("form[name=frm_register] input").each(function() {
			if (!$(this).val() && $(this).hasClass('required')) {
				$(this).addClass('open');
				block = true;
			} else {
				$(this).removeClass('open')
			}
		});
		if (!(block)) {
			$("form[name=frm_register]").submit();
		}
	});
	$("#opslaan").click(function() {
		var block = false;
		$("form[name=frm_gegevens] input").each(function() {
			if (!$(this).val() && $(this).hasClass('required')) {
				$(this).addClass('open');
				block = true;
			} else {
				$(this).removeClass('open')
			}
		});
		if (!(block)) {
			$("form[name=frm_gegevens]").submit();
		}
	});
	$("#ex_login").click(function() {
		var block = false;
		$("form[name=frm_ex_login] input").each(function() {
			if (!$(this).val() && $(this).hasClass('required')) {
				$(this).addClass('open');
				block = true;
			} else {
				$(this).removeClass('open')
			}
		});
		if (!(block)) {
			$("form[name=frm_ex_login]").submit();
		}
	});
	$.loadcart();
	$.update_price = function(add) {
		var amount = parseFloat($("input[name=cart_total]").val().replace(",", "")) + Number(add)
		$("span.amount").html("&euro; " + numberWithCommas(amount.toFixed(2)));
	}
	function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
	$("#browse.shipping #rcol input").click(function() {
		var add = 0;
		$("#browse.shipping #rcol input:checked").each(function() {
			add += Number($(this).attr('price'));
		});
		$.update_price(add);
	});
	$("span.bekijk a").click(function() {
		$("div.cart_wrap").fadeToggle();
	});
	$("#process").click(function() {
		//form check
		if ($("input[name=akk_lev_voorw]")) {
			if (!$("input[name=akk_lev_voorw]").is(':checked')) {
				alert("Gelieve akkoord te gaan met onze voorwaarden");
				return false;
			}
		}
		$("input[name=info]").val($("textarea[name=order_options]").val());
		$("form[name=process]").submit();
	});
	$("div.bttsearch").click(function() {
		$("form[name=frm_search]").submit();
	});
	$(".altboxdetail").click(function() {
		$('body').append("<div id='overlay'></div>");
		$("#overlay").fadeIn('fast');
		$('body').append("<div id='imagebox_wrap'></div>");
		$("#imagebox_wrap").load("core/actions.php", {
			a: "imagebox",
			nr: $("input[name=nr]").val()
		}, function() {
			$("#wf_lightbox").fadeIn();
		});
	});
	$(".print_order").click(function() {
		$('body').append("<div id='overlay'></div>");
		$("#overlay").fadeIn('fast');
		$('body').append("<div id='modal'></div>");
		$("#modal").load("core/actions.php", {
			a: "print_order",
			nr: $(this).html()
		});
	});
	$("div.altbox_light img").live('click', function() {
		//alert($(this).attr('src'));
		//alert($("#mainimg").attr('style'));
		var src = $(this).attr('src');
		$("#mainimg").fadeOut('fast', function() {
			$("div.closeimgbox").fadeOut('fast');
			var img = new Image();
			img.onload = function() {
				//alert(this.width + 'x' + this.height);
				var w = this.width >= 400 ? this.width : 400;
				var h = this.height;
				$("#wf_lightbox").animate({
					width: w + 10,
					marginLeft: "-" + (w + 8) / 2
				}, 500, function() {
					$("div.closeimgbox").fadeIn('fast');
				});
			}
			img.src = src.replace("W=100&H=90", "W=700&H=350");
			$("#mainimg").css('background-image', 'url("' + src.replace("W=100&H=90", "W=700&H=350") + '")').fadeIn('fast');
		})
	})
	$("div.closeimgbox").live('click', function() {
		$("#wf_lightbox").fadeOut();
		$("#imagebox_wrap").html('');
		$("#overlay").fadeOut('fast');
	});
	$("#btt_adv_search").click(function() {
		var sList = "{";
		$("form[name=frm_adv_search] input").each(function() {
			if ($(this).val()) {
				sList += $(this).attr('name') + "|" + $(this).val() + ",";
			}
		});
		sList += "}";
		window.location.href = "?s=" + sList;
		//$("form[name=frm_adv_search]").submit();			
	});
	$("form[name=frm_adv_search] input").keypress(function(e) {
		if (e.keyCode == 13) {
			$("#btt_adv_search").click();
		}
	});
	$("ul.tabbox li").click(function() {
		$(this).addClass('active').siblings().removeClass('active');
		var alt = $(this).attr('alt');
		$('.infobox').fadeOut('fast');
		t = setTimeout(function() {
			$("#" + alt).fadeIn('def');
		}, 300);
	});
	$("#btt_lost_pw").click(function() {
		$('#box_login').fadeOut('fast');
		t = setTimeout(function() {
			$("#box_lost_pw").fadeIn('def');
		}, 300);
	});
	$("#lost_send").click(function() {
		if ($.validate('frm_lost_pw')) {
			$("p.lost_error").fadeOut();
			$("p.lost_succes").fadeOut();
			$.post("core/actions.php", {
				a: "lost_send",
				email: $("#loginbox input[name=lost_email]").val()
			}, function(data) {
				if (data == "error") {
					//error
					$("p.lost_error").fadeIn();
				} else {
					//success	
					$("p.lost_succes").fadeIn();
					t = setTimeout(function() {
						$('#box_login').fadeIn('fast');
						$("#box_lost_pw").fadeOut('fast');
					}, 2000);
				}
			});
		}
	});
	$("p.aflever_invullen").click(function() {
		$("#shippingbox").slideToggle();
	});
	$("#apply_coupon_code").click(function() {
		if ($.validate('process')) {
			$.post("core/actions.php", {
				a: "apply_coupon_code",
				code: $("input[name=coupon_code]").val()
			}, function(data) {
				if(data){
				   $("#coupon_status").html(data);
			   }else{
			      location.reload();
			   }
			});
		}
	});
	jQuery.validate = function(form) {
		var block = false;
		$("form[name=" + form + "] input").each(function() {
			if (!$(this).val() && $(this).hasClass('required')) {
				$(this).addClass('open');
				block = true;
			} else {
				$(this).removeClass('open')
			}
		});
		if (!(block)) {
			return true;
		} else {
			return false;
		}
	}
	if ($("#css_preview")) {
		t = setTimeout(function() {
			$('#css_preview').slideToggle();
		}, 1000);
	}
	$("#pop_close").live('click', function() {
		$("#modal").fadeOut();
		$("#modal").html('');
		$("#overlay").fadeOut("fast");
		$("#modal").remove();
	});
	$("span.track_start").click(function(){
	   $("textarea[name=itemCodes]").html($(this).attr('id'));
	   $("form[name=searchForm]").submit();
	   alert($(this).attr(id));
	});
	//merk toggle
	$("ul.menu li.merk").hide();
	$(".groeptoggle li").click(function(){
	   if($(this).hasClass('merk')){
	      $("ul.menu li.merk").show();
	      $("ul.menu:not(.level0) li:not(.merk)").hide();
	   }else{
	      $("ul.menu li.merk").hide();
	      $("ul.menu:not(.level0) li:not(.merk)").show();
	   }   
	});	
});

