// http://interface.eyecon.ro/demos/cart.html
// cart


//http://jqueryui.com/demos/effect/


$(function () {
	check_session_items();
	add_cart_links();
	cart_scroller();
	check_empty_cart();
	add_cart_links_main_menu();
});
var addProductToCart = function(dragged)
{
	$dragged = $(dragged);
	var cart_total   = 0;
	var cartItem;
	var productImage = $('div > img', $dragged).not(':visible').attr('src').replace('_thumb','');
	var productName  = $('h5 > a', $dragged).html();
	var categorieName= $('input[name=category]', $dragged).val();
	var linkwidth    = $('h5 > a', $dragged).width() + 60;
	var productPrice = parseFloat($('span', $dragged).html()).toFixed(2);
	var productId    = $dragged.attr('id');
	var isInCart     = $('#' + productId + '_cart');
	if (isInCart.size() == 1) {
		return;
	}
	$.post('/addtocart/js/',{id:  productId, price: productPrice,type: 'add'});
	check_empty_cart();
	$('#cart_total').fadeOut();
	var text = '<a href="' + productImage + '?width=500&amp;height=500"  rel="cartfoto[cart]" title="' + productName + '">' + productName + '</a>' +'<br /><br />Prijs: &euro; <b class="price">'+ productPrice +'</b><br />Categorie: ' + categorieName + '<br /><form><input class="remove" type="submit" value="Verwijderen" style="width:100px;height:18px;padding:0px" /></form>';
	$('#cart > ul').prepend('<li  class="productCart" id="' + productId + '_cart"></li>');

	check_empty_cart();

	$('#' + productId + '_cart').css({width: '0px'}).animate({width: linkwidth +'px'}).delay(1200,function(){
		$('#' + productId + '_cart').fadeIn(300).html(text).css({width: 'auto'});
		$('#' + productId + '_cart').find('.remove').click(function(){
			$(this).parent().parent().effect("drop", { direction: "down" }, 500,function() { 
				$.post('/addtocart/js/',{id:  productId, price: productPrice,type: 'remove'});
				$(this).remove();
				remove_from_total(productPrice);
				cart_scroller();					
				check_empty_cart();
				add_cart_links();
			});
			$('#' + productId + ' > form > p input.mid-button').fadeIn().next().remove();
			return false;
		});
		add_to_total(productPrice);
		fotocart();
	});
	$dragged.effect("transfer", {to: '#' + productId + '_cart',className:'transferProduct'},1300,function(){
		cart_scroller();
		add_cart_links();
	});
	// return cartItem;
};
function add_to_total (price) {
	$('#cart_total').html(parseFloat(parseFloat($('#cart_total').html()) + parseFloat(price)).toFixed(2)).fadeIn(300);
}
function remove_from_total (price) {
	$('#cart_total').html(parseFloat(parseFloat($('#cart_total').html()) - parseFloat(price)).toFixed(2)).fadeIn(300);
}

function check_empty_cart () {
	// console.log($('ul.sc_menu > li').size());
	// console.log($('#no_cart_items').size());
	if ($('ul.sc_menu > li').size() > 1) {
		
		$('#no_cart_items').fadeOut(function(){
			$(this).remove();
		});
	}else if ($('ul.sc_menu > li').size() > 1 && $('#no_cart_items').size() == 1) {
		// console.log('here');
	}else if ($('ul.sc_menu > li').size() < 1) {
		$('ul.sc_menu').html('<li id="no_cart_items" style="display:none">Je hebt nog niks in je winkelwagen <b style="visibility: hidden;" class="price">0</b></li>');
		$('#cart_total').html('0.00');
		$('#no_cart_items').fadeIn('slow');
	}else{
		// console.log('do nothing');
	}
}

function add_cart_links () {
	$('#listview > li').each(function(){
		var $item = $(this);
		var productId = $item.attr('id');
		var itemincart = '<p class="already_exist">Dit item zit in je winkelwagen!</p>';
		add_cart_link($item);
		if ($item.find('input.mid-button').is(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 0) {
			// console.log('bt is visible, in cart, already_exist = 0');
			//zit in cart en kopen button
			$item.find('input.mid-button').fadeOut();
			$item.find('input.mid-button').hide().after(itemincart);
		}else if ($item.find('input.mid-button').is(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 1) {
			// console.log('bt is visible, in cart, already_exist = 1');		
			//zit in cart en niet kopen
		}else if ($item.find('input.mid-button').not(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 0) {
			// console.log('bt is NOT visible, in cart, already_exist = ' + $item.find('p.already_exist').length);
			// console.log($(this).find('p.already_exist'));
			//zit niet in cart en kan niet kopen
			$item.find('input.mid-button').after(itemincart);
			$item.find('p.already_exist').show();
		}else if ($item.find('input.mid-button').not(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 1) {
			$item.find('p.already_exist').show();
			// console.log('bt is NOT visible, in cart, already_exist = 1');
			//zit niet in cart en kan niet kopen
		}else{
			// console.log('else');
			//zit niet in cart en kan kopen
		}
	});
}
function add_cart_links_main_menu () {
	$('#discount ul.article > li').each(function(){
		var $item = $(this);
		var productId = $item.attr('id');
		var itemincart = '<p class="already_exist">Dit item zit in je winkelwagen!</p>';
		add_cart_link($item);
		if ($item.find('input.mid-button').is(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 0) {
			// console.log('bt is visible, in cart, already_exist = 0');
			//zit in cart en kopen button
			$item.find('input.mid-button').fadeOut().hide().after(itemincart);
		}else if ($item.find('input.mid-button').is(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 1) {
			// console.log('bt is visible, in cart, already_exist = 1');		
			//zit in cart en niet kopen
		}else if ($item.find('input.mid-button').not(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 0) {
			// console.log('bt is NOT visible, in cart, already_exist = ' + $item.find('p.already_exist').length);
			// console.log($(this).find('p.already_exist'));
			//zit niet in cart en kan niet kopen
			$item.find('input.mid-button').after(itemincart);
			$item.find('p.already_exist').show();
		}else if ($item.find('input.mid-button').not(':visible') && $('#' + productId + '_cart').size() > 0 && $item.find('p.already_exist').length == 1) {
			$item.find('p.already_exist').show();
			// console.log('bt is NOT visible, in cart, already_exist = 1');
			//zit niet in cart en kan niet kopen
		}else{
			// console.log('else');
			//zit niet in cart en kan kopen
		}
	});
}
function add_cart_link ($item) {
	$item.find('p.already_exist').hide();
	$item.find('input.mid-button').click(function(){
		addProductToCart($item);
		$(this).fadeOut();
		return false;
	});
}
function cart_scroller () {
	var div = $('div.sc_menu'),
	ul = $('ul.sc_menu'),
	// unordered list's left margin
	ulPaddingRight = 195;
	ulPaddingLeft = -165;

	//Get menu width
	var divWidth = div.width();

	//Remove scrollbars
	div.css({overflow: 'hidden'});

	//Find first image container
	var lastLi = ul.find('li:last-child');
	if (!lastLi[0]) {
		return;
	};
	//When user move mouse over menu
	div.mousemove(function(e){

		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPaddingRight;

		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth + ulPaddingLeft;
		div.scrollLeft(left);
	});
	ul.mouseout(function(e){
		div.scrollLeft(0);
	});
}

function remove_from_cart (elm) {
	$(elm).hide();
	return false;
}

function check_session_items () {
	$('ul.sc_menu > li').each(function(){
		var $item = $(this);
		var productId    = $item.attr('id').replace('_cart','');
		var productPrice = $item.find('b.price').html();
		productPrice = parseFloat(productPrice).toFixed(2);
		$item.find('.remove').click(function(){
			$item.effect("drop", { direction: "down" }, 500,function() { 
				$.post('/addtocart/js/',{id:  productId, price: productPrice,type: 'remove'});
				$item.remove();
				remove_from_total(productPrice);
				cart_scroller();
				check_empty_cart();
			});
		$('#' + productId + ' > form > p input.mid-button').fadeIn(function(){
			add_cart_link($('#' + productId ));
			}).next().remove();
			return false;
		});
		add_to_total(productPrice);
	});
}
