$(document).ready(function() {
	// Shopping cart
	$('#cart .body').toggle();
	$('#cart-btn a').attr('href', '#');
	$('#cart-btn a').click(function(){
		$(this).toggleClass('open');
		$('#cart .body').toggle();
	});

	// Chapters nav
	$(".chapter-nav li:has(ul) > a").after('<span class="tgl">&nbsp; &nbsp;</span>');
	$(".sub-chapter li:has(ul) > a").after('<span class="tgl">&nbsp; &nbsp;</span>');
	$('li.opened > .tgl').remove();
	$('li.active > .tgl').remove();
	$('.chapter-nav .tgl').click(function(){
		$(this).parent().toggleClass('open');
	});
	$('.sub-chapter .tgl').parent().addClass('js');
	$('.sub-chapter .tgl').click(function(){
		$(this).parent().toggleClass('open');
	});

});

$(document).ready(function(){

	$('a.start-in-popup').nyroModal(
		{
			type:'swf',
			width:'400',
			height:'400'
		}
	);

	PostcodeAanvullen('data_NL_FormTextbox_13', null, null, 'data_NL_FormTextbox_14');

});

function PostcodeAanvullen(postcodeVeld, huisnummerVeld, straatVeld, plaatsVeld) {

	this.postcodeVeld        = postcodeVeld;
	this.huisnummerVeld     = huisnummerVeld;
	this.straatVeld              = straatVeld;
	this.plaatsVeld             = plaatsVeld;

	if(!document.getElementById(this.postcodeVeld)){
		return false;
	}

	document.getElementById(this.postcodeVeld).PostcodeAanvullen = this;

	if(document.getElementById(this.huisnummerVeld)){
		document.getElementById(this.huisnummerVeld).PostcodeAanvullen = this;
	}

	$('#'+this.postcodeVeld+', #'+this.huisnummerVeld).change(function(event){
		var obj = event.target.PostcodeAanvullen;
		return $.get("/postcode/", {zipcode: $('#'+obj.postcodeVeld).val(), housenumber: $('#'+obj.huisnummerVeld).val()}, obj.update, "json");
	});

	this.update = function(data) {

		if(typeof(data.results) == 'object' && typeof(data.results[0]) == 'object') {
		   if(typeof(data.results[0].street) == 'string') {
			   $('#'+straatVeld).val(data.results[0].street);
		   }

		   if(typeof(data.results[0].city) == 'string') {
			   $('#'+plaatsVeld).val(data.results[0].city);
		   }
		}
	}
}


function initGroups(){
	$('#infoOrder .group-item').hide();
	//$('#infoOrder .open .group-item').show();
}

$(document).ready(function(){
	$('#infoOrder .toggle, #infoOrder h3').click(function(){
		var p = $(this).parent();
		var isOpen = p.hasClass('open');
		if(isOpen){
			$('.toggle', p).html('(Openen)');
			$('.group-item', p).hide();
			p.removeClass('open');
		} else {
			$('.toggle', p).html('(Sluiten)');
			$('.group-item', p).show();
			p.addClass('open');
		}

		return false;
	});
});

