/**
 * Jquery
 */
	$(document).ready(function() {
		
		/**
		 * Check for iPad device
		 */
			var ua = navigator.userAgent;
			var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
		
		/**
		 * Marquee
		 */
			$('div.menu marquee').marquee('pointer').mouseover(function () {
				$(this).trigger('stop');
			}).mouseout(function () {
				$(this).trigger('start');
			}).mousemove(function (event) {
				if ($(this).data('drag') == true) {
					this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
				}
			}).mousedown(function (event) {
				$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
			}).mouseup(function () {
				$(this).data('drag', false);
			});
			
		/**
		 * Prijs update
		 */
		 	$('.productPrijs').change(function() {
				
				// Ophalen item
				$item = $(this);
				
				// Naar winkelwagen sturen
				$.ajax({
					url			: 'winkelwagen.php',
					type		: 'POST',
					dataType	: 'html',
					data		: 'aantal=' + $item.val() + '&naam=' + $item.attr("name") + '&prijs=' + $item.attr("title") + '',
					async		: false,
					success		: function(data) {
						
						// Totaal prijs terug koppeling
						var data = jQuery.parseJSON(data);
						$('.totaalPrijs').text(data.totaalPrijs);
						
						// Aantal overschrijven met naar boven afgeronden getallen
						$item.val(data.aantal);
						
					}
				});
				
			});
		
		/**
		 * Bestelling afhalen / ophalen update
		 */
			$('.bestelling').change(function() {

				$item = $(this);
				$.ajax({
					url			: 'winkelwagen.php',
					type		: 'POST',
					dataType	: 'html',
					data		: 'bestelling=' + $item.val() + '',
					async		: false,
					success		: function(data) {
						
						// Totaal prijs terug koppeling
						var data = jQuery.parseJSON(data);
						$('.totaalPrijs').text(data.totaalPrijs);
						
					}
				});
				
			});
			
		/**
		 * Docking bar
		 * Scroll the message box to the bottom offset of browser's scrool bar
		 */
			if(isiPad){
				$(window).scroll(function()
				{
					$('#dock').css({bottom: -$(window).scrollTop()-15+"px" });
				});
			}
			
		/**
		 * Omschrijving
		 */
			// Onzichtbaar maken
			$menu = $(".productHome div span.groot");
			$menu.hide();
			
			// Klik 
			$(".productHome div").click(function () {
			
				$klein = $("span.klein", this) 
				$groot = $("span.groot", this) 
				
				if ($klein.is(":hidden")) 
				{
					$klein.show();
					$groot.hide();
				} 
				
				else 
				{
					$klein.hide();
					$groot.show();				
				}
				
				$(".column").css("height", "");
				equalHeight($(".column"));
				
			});
			
		/**
		 * Facnybox
		 */
			$("a.overlay").fancybox({
				'height'			: 600,
				'width'				: 750,
				'autoDimensions' 	: false
			});
			
			$("#overlay").fancybox({
				'height'			: 600,
				'width'				: 750,
				'autoDimensions' 	: false
			});
			
		/**
		 * Column hoogte
		 */
			 function equalHeight(group) {
				hoogste = 0;
				group.each(function() {
					thisHeight = $(this).height();
						if(thisHeight > hoogste) {
							hoogste = thisHeight;
							col = $(this).attr("id");
						}
					});
					
				// Fix voor iPad
				if(isiPad) {
					hoogste = hoogste + 650;
				}
				
				// Gelijk stellen aan de hoogte
				group.height(hoogste);
				$("#" + col  + " li:last-child").removeClass().addClass("noBorderBottom");
			}
			if($('.column').length) equalHeight($(".column"));
		
		/**
		 * Fix voor Chrome
		 */
			setTimeout(function() { 
				if($(".column").length){
					$("#col1 li.first .productHome div").trigger('click');
					$("#col1 li.first .productHome div").trigger('click');
					equalHeight($(".column"));
				}
			}, 3000);
			
		/**
		 * 800x600 geschikt maken
		 */
		 	function resizer(){
				if($(window).width() < 1024){
					$('.delete').hide();
				} else {
					$('.delete').show();
				}
			}
			$(window).resize(function() {
				resizer();
			});
			resizer();
			
	
	});
