/*
 * Forwart Design website frontend v1.0 for jQuery JavaScript Library v1.4.0
 *
 * Copyright (C) 2003-2010
 * Peter Arts - Forwart Design
 * http://www.forwart.nl
 *
 * Date: 2010-05-04
 * Revision: 0001
 */

var ajaxUrl = '/ajaxpost';
var modifyUrl = '/aanpassen/';
var finishUrl = '/afronden';
var demoScroll = 1;
$(document).ready(function() {
	$('#content div.category a.scrollernav').click(function(e) { e.preventDefault(); });
	$('#content div.category').mouseenter(function() {
		if (demoScroll == 1) {
			demoScroll = 0;
			// unbind this event; no longer needed
			$('#content div.category').unbind('mouseenter');
		}
	});
	$('#content div.category div.scroller').each(function() {
		$(this).carousel();
	});
	$('#content div.category div.product').mouseenter(function() {
		//$(this).find('div.product-detail').stop().fadeTo(1, 1);
		$(this).find('div.product-detail').show();
	});
	$('#content div.category div.product').mouseleave(function() {
		//$(this).find('div.product-detail').stop().fadeOut(300);
		$(this).find('div.product-detail').hide();
	});
	$('#content div.category div.product a.ingredients').click(function(e) {
		e.preventDefault();
		var product = $(this).parents('div.product');
		if (product.hasClass('active')) {
			// already active for ingredients: ignore
			return;
		}
		product.addClass('active');
		// find category container
		var title = $(this).attr('title');
		var catContainer = $(this).parents('div.category');
		var container = catContainer.find('div.ingredients');
		//catContainer.find('span.delivery').fadeOut();
		if (container.length == 0) {
			// create DOM element and load contents
			catContainer.find('span.delivery').after('<div class="ingredients"></div>');
			container = catContainer.find('div.ingredients');
		}
		
		// DOM element already created, show
		container.hide().html('Ingredi&euml;nten laden...').load($(this).attr('href'), function() {
			$(this).prepend('<h4>' + title + '</h4><a href="#" title="Ingredi&euml;nten sluiten" class="close">sluiten</a>');
			$(this).append('&nbsp; <a href="#" title="Ingredi&euml;nten sluiten" class="close btm">sluiten</a>');
			$(this).slideDown();
			$('#content div.category div.ingredients a.close').click(function(e) {
				e.preventDefault();
				container.slideUp(function() { 
					container.html(''); 
					product.removeClass('active').find('h3').addClass('blink').fadeOut(245, function() { product.find('h3').fadeIn(245, function() { product.find('h3').fadeOut(245, function() { product.find('h3').fadeIn(450, function() { product.find('h3').removeClass('blink'); }); }); }); });
				});
				//catContainer.find('span.delivery').fadeIn();
			});
		});
	});
	if ($('#content div.category:first div.product').length > 3) {
		setTimeout(demoScrollNext, 2000);
	}
	if ($('#content div.category:eq(1) div.product').length > 3) {
		setTimeout(demoScrollPrev, 5000);
	}
	$('a[rel="blank"]').click(function(){
	     this.target = "_blank";
	});
	
	// Order product click
	$('a.order').click(function(e){
		// stop normal link click
		e.preventDefault();
		var productId = $(this).parents('div.contents').find('span.product-id').html();
     	var target = $(this);
		$.post(
			ajaxUrl,
			{ action: 'add-product', productid: productId },
			function(modifyid){
				if (modifyid > 0) {
					if (target.hasClass('custom')) {
						target.html('Bezig...');
						window.location.href = (modifyUrl + modifyid);
					} else {
						
						updateCart();
						
						var productTitle = target.parents('div.product').find('h3');
						if (!productTitle.hasClass('incart')) {
							productTitle.addClass('incart').append('<span></span>');
							//target.after('<a href="' + finishUrl + '" title="Bestelling afronden" class="finish_order">Bestelling afronden</a>');
							target.parents('div.contents').find('p.info-detail').append(' &nbsp; &nbsp; <a href="' + finishUrl + '" title="Bestelling afronden" class="finish_order">Bestelling afronden</a>');
						}
						target.addClass('small').html('Toegevoegd aan bestelling').fadeOut(300, function() {
							$(this).fadeIn(300, function() {
								$(this).fadeOut(300, function() {
									$(this).fadeIn(300, function() {
										$(this).html("1 extra bestellen");
									});
								});
							});
						});
						if (target.parents('div.contents').find('a.finish_order').length == 0) {
							target.parents('div.contents').find('p.info-detail').append(' &nbsp; &nbsp; <a href="' + finishUrl + '" title="Bestelling afronden" class="finish_order">Bestelling afronden</a>');
						}
					}
				} else {
					target.html('Product is niet toegevoegd.');
				}
			}
		);
	});
});

function updateCart() {
	if ($('#cart div.cartorder').length == 1) {
		// hide 'empty cart message'
		$('#cart div.cartempty').fadeOut(300);
		$('#cart div.cartorder').fadeIn(300);
	}
	$('#cart div.contents').html('&nbsp;&nbsp;Vernieuwen...');
	$.post(
		ajaxUrl,
		{ action: 'get-cartcontents-small'},
		function(content){
			$('#cart div.contents').html(content);
		}
	);
}

function demoScrollNext() {
	if (demoScroll != 1) {
		return;
	}
	var targetCat = $('#content div.category:first');
	targetCat.find('a.navnext').addClass('demo');
	targetCat.find('a.navnext').trigger('click');
	setTimeout(function() { targetCat.find('a.navnext').removeClass('demo'); }, 500);
	setTimeout(demoScrollNext, 6000);
}

function demoScrollPrev() {
	if (demoScroll != 1) {
		return;
	}
	var targetCat = $('#content div.category:eq(1)');
	targetCat.find('a.navprev').addClass('demo');
	targetCat.find('a.navprev').trigger('click');
	setTimeout(function() { targetCat.find('a.navprev').removeClass('demo'); }, 500);
	setTimeout(demoScrollPrev, 6000);
}

/**
 * @author Stéphane Roucheray 
 * @extends jquery
 */


jQuery.fn.carousel = function(){
	var sliderList = jQuery(this).children()[0];
	
	if (sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		if (elmnts.length <= shownInViewport) {
			// no scrolling needed
			// disable navigation elements
			jQuery(sliderList).parents('div.category').find('a.scrollernav').addClass('disabled');
			return;
		}
		for (i = 0; i < shownInViewport; i++) {
			jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
		}
		
		jQuery(sliderList).parents('div.category').find('a.navprev').click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort == 1) {
					jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
					firstElementOnViewPort = numElmts;
				}
				else {
					firstElementOnViewPort--;
				}
				
				jQuery(sliderList).animate({
					left: "+=" + increment,
					y: 0,
					queue: true
				}, 400, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
			
		});
		
		jQuery(sliderList).parents('div.category').find('a.navnext').click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 2;
					jQuery(sliderList).css('left', "0px");
				}
				else {
					firstElementOnViewPort++;
				}
				jQuery(sliderList).animate({
					left: "-=" + increment,
					y: 0,
					queue: true
				}, 400, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
		});
	}
};

