	
	function displayBasket()
	{
			if ($('#carouselcontainer').length > 0) {
				$('#carouselcontainer').hide();
			}
			$('div#basketbox').show('slow');
			$('#homesearchinput').hide();
			$('div#basketboxbody').load("templates/shop/basket.php");	
	}
	
	function hideBasket()
	{
			if ($('#carouselcontainer').length > 0) {
				$('#carouselcontainer').show();
				$('#homesearchinput').show();
			}
			$('div#basketbox').hide('slow');
	}
			
	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}

	}

	function siteSearch()
	{
		var searchstring = $('#homesearchinput').val();
		document.location = "index.php?link=boeken&search="+searchstring;
	}


	function checkFastSearch()
	{
		if  ( $('#selectsearch').attr('name') != "" ) {
				var tableobject = $('#productlist');	
				var tablelabelobject = $('img#productlistlabel');
				$('#selectsearch').attr('name', "");
				$.post("templates/generated/products.php",  { cat: "" , subcat: "" , selectthema: "", selectage: "", selectreeks: "", selectsearch: $('#selectsearch').val()} , 
						function(data){
							tableobject.html(data);
							tablelabelobject.attr('src', 'images/products/result.jpg');
				});
		}
	}
	
	function initBookTriggers()
	{
			$("a.fancybox").fancybox({'frameWidth': 640,
					  'frameHeight': 480,
					  'hideOnContentClick': true
				  }); 
			$('a.fancyRecensie').fancybox();
			$('a.fancyMerit').fancybox();
			
				
	}

	function selectBookEverywhere(id)
	{
		$.fancybox.close();
		document.location = "index.php?link=boeken&product="+id+"&cat=&subcat=";
	}

	function selectBook(id)
	{
		$('div#bookView').load("templates/generated/product.php?product="+id , function(e){
				initBookTriggers();	
		});
	}

	$( function() {
		var email = $("#popup_email");
		var allFields = $([]).add(email);
		var flagNewsletterAdded = false;
		
		initBookTriggers();
		
		$('div#basketbox').hide();
		
		
		$('a.microsite').fancybox({
			type: 'iframe',
			padding: 5,
			width: 913,
			height: 810,
			scrolling: 'auto',
			onStart: function() {
				if ($('#carouselcontainer').length > 0) {
					$('#carouselcontainer').hide();	
				}
			},
			onClosed: function() {
				if ($('#carouselcontainer').length > 0) {
					$('#carouselcontainer').show();	
				}	
			}
			
		});
		
		
		$('a.xxmicrosite').click(function(e) {
			var theUrl = $(this).attr('href');
			var theName = $(this).attr('name');
			var theWidth = 903+5;//$(this).attr('preferred_width');
			var theHeight = 800+5;//$(this).attr('preferred_height');
			popupwindow = window.open(theUrl, theName, 'location=0,status=0,scrollbars=1,menubar=0,directories=0,resizable=0,width='+theWidth+',height='+theHeight);	
			return false;
		});
				
		
		$('a.action_newsletter').click(function(e) {
			$('div#dialog_newsletter').dialog('open');
		});
		
		$('a.action_basket').click(function(e) {
			displayBasket()
		});
	
		$('div#productinfofooter').live('click', function(e) {
			var productid = $(this).attr('name');
			$('div#basketbox').show('slow');
			$('div#basketboxbody').load("templates/shop/basket.php",{ action: "add", parm1: productid}, displayBasket());
		});
		
		$("button#dosearch").click(function(e) {
			showProducts();
		 });
	
		$("a.clickBook").live("click", function(e) {
			var id = $(this).attr('id');
			selectBook(id);
		
			e.preventDefault();
		});
		
		$('span.action_orderproduct').live("click", function(e) {
				var productid = $(this).attr('name');
				$('div#basketbox').show('slow');
				$('div#basketboxbody').load("templates/shop/basket.php",{ action: "add", parm1: productid}, displayBasket());
			});
		
		$(".select_book").change(function(e) {
			showProducts();
		 });
	 
		$('input#selectsearch').keypress(function(e){
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13){
				showProducts();
			}
		});
	
		$('#homesearchinput').keydown(function(event) {
  			if (event.keyCode == '13') {
  			   event.preventDefault();
			   siteSearch();
  			}
		});
		
		$('#homesearchbutton').click(function(e) {
			siteSearch();
		});
	
		$('div#basketboxclose').click(function(e) {
			 hideBasket();									   
		});
		
		checkFastSearch();
	
	   $('div#msg_newsletter').dialog({ 
		  autoOpen: flagNewsletterAdded,
		  modal: false
		  });
		
		$('a.fancyRecensie').fancybox();
		$('a.fancyMerit').fancybox();
		
		$('div#dialog_newsletter').dialog({ 
					autoOpen: false,
					modal: true,
					buttons: {
							'Inschrijven': function() {
								var bValid = true;
								allFields.removeClass('ui-state-error');
								
								$('#emailaddress').val(email.val());
								//bValid = bValid && checkLength(email,"email",6,180);
								
								$(this).dialog('close');
								$('#form_newsletter').submit();
							
							},
						  'Annuleer': function() {
							  $(this).dialog('close');
						  }
						},
				open: function() {
						$('#carouselcontainer').hide();
						},
				close: function() {
						allFields.val('').removeClass('ui-state-error');
						$('#carouselcontainer').show();
						  }

					
					});
		
	});
