$.ajaxSetup({cache:false});

vWinkelWagenTimeOut = false;

$(document).ready(function(){
	if(jQuery.browser['msie'] && jQuery.browser['version'] < 8){
		// $('.shadow').dropShadow({left: 5, top: 5, blur: 5 , color:"#BBB"});
	}
	else if(jQuery.browser['msie'] && jQuery.browser['version'] == 8)
		$('.shadow').css('filter', 'progid:DXImageTransform.Microsoft.Shadow(color=#BBBBBB, strength=5, direction=135)');
	
	$(".slider").easySlider({
		auto: true,
		continuous: true,
		speed: 1500,
		pause: 4000,
		numeric: true,
		controlsShow: true
	});
	
	if($('#winkelmandje_overzicht').length > 0){
		if((jQuery.browser['msie'] && jQuery.browser['version'] > 6) || !jQuery.browser['msie']){
			$('#winkelmandje')
				.bind('mouseover', function(){
					$('#winkelmandje_overzicht').slideDown('normal');
					
					clearTimeout(vWinkelWagenTimeOut);
				})
				.bind('mouseout', function(){
					vWinkelWagenTimeOut = setTimeout(function(){
						$('#winkelmandje_overzicht').slideUp('fast');
					}, 3000);
				});
			
			shoppingcart.load();
		}
	}
});

function mail(fTo, fDomain){
	if(!fDomain)
		fDomain = 'eetcadeau.nl';
	
	location.href = 'mailto:'+ fTo +'@'+ fDomain;
}
function inputInfo(fElement, fValue){
	var fPassword = ($(fElement).attr('type') == 'password');
	if(fPassword)
		fValue = '';
	
	function submit(){
		if($(fElement).attr('value') == fValue)
			$(fElement).attr('value', '');
	}
	function focus(){
		if($(fElement).attr('value') == fValue && !fPassword){
			$(fElement)
				.css({
					'fontStyle':'normal',
					'color':'#000000'
				})
				.attr('value', '');
		}
		else if($(fElement).attr('value') == '' && fPassword){
			$(fElement)
				.css('background', 'none');
		}
	}
	function blur(){
		if($(fElement).attr('value') == '' || $(fElement).attr('value') == fValue){
			if(fPassword){
				$(fElement)
					.css('background', 'url(/images/wachtwoord.gif) no-repeat left center');
			}
			else{
				$(fElement)
					.css({
						'fontStyle':'italic',
						'color':'#777777'
					})
					.attr('value', fValue);
			}
		}
	}
	
	
	$(fElement)
		.bind('focus', focus)
		.bind('blur', blur)
		.closest('form')
		.bind('submit', submit);
	
	blur();
}
product = {
	remove_message: function(){
		$('#right .msg, #right .msg_error').remove();
	},
	message: function(fMessage, fError){
		product.remove_message();
		
		if(fMessage){
			$('#right').prepend(
				$(document.createElement('div'))
					.css('display', 'none')
					.addClass((fError?'msg_error':'msg'))
					.html(fMessage)
					.slideDown('normal')
			);
		}
	},
	disable_order: function(){
		shoppingcart.loader(false);
		
		product.remove_message();
	},
	enable_order: function(){
	
	}
}
shoppingcart = {
	add: function(fData){
		if(!fData.success){
			product.message(fData.message, true);
			
			shoppingcart.loader(false);
		}
		else{
			$('input[name=amount]')
				.effect("transfer", {to:$('#winkelmandje_link')}, 1000, function(){
					product.message(fData.message);
				})
				.val('');
			
			shoppingcart.load();
		}
	},
	change: function(){
		function done(fData){
			if(fData.success)
				shoppingcart.load();
			else{
				alert(fData.message);
				$(fInput).val(fAmount);
				shoppingcart.loader(false);
			}
		}
		function check(){
			if($(this).val() != fAmount){
				shoppingcart.loader(false);
				$.post('/winkelwagen_ajax/', {action: 'change', product_id: $(this).attr('name'), amount: $(this).val()}, done, "json");
			}
			
			$(this).unbind('blur').removeClass('focus');
		}
		
		var fInput = $(this)
			.bind('blur', check)
			.addClass('focus')
			.keyup(function(e){
					if(e.keyCode == 13){
						$(this)[0].blur();
					}
				});
		var fAmount = fInput.val();
	},
	deleting: function(fProductId){
		shoppingcart.loader(false);
		
		$.post('/winkelwagen_ajax/', {action:'delete', product_id: fProductId}, function(){shoppingcart.load();}, "json");
	},
	load: function(){
		if(window.location.href.indexOf('/shoppingcart/') > -1)
			window.location.href = '/shoppingcart/';
		else{
			shoppingcart.loader(true);
			$.getJSON("/winkelwagen_ajax/", {'action': 'content'}, function(fData){
				if(!fData.cart){
					$('#winkelmandje_table').html(fData.message);
				} else {
					fTbodyCart = $(document.createElement('tbody'));
					fTotaalCart = 0;
					
					$.each(fData.products, function(fProducts) {
						fTbodyCart.append($(document.createElement('tr'))
							.addClass((parseInt(fProducts)%2)?'even':'odd')
							.append($(document.createElement('td'))
								.html(fData.products[fProducts].name))
							.append($(document.createElement('td'))
								.css('textAlign', 'right')
								.append($(document.createElement('input'))
									.attr({'type':'text', 'name':fData.products[fProducts].id})
									.val(fData.products[fProducts].amount)
									.bind('focus', shoppingcart.change)
									.addClass('text')))
							.append($(document.createElement('td'))
								.html('&euro; '))
							.append($(document.createElement('td'))
								.css('textAlign', 'right')
								.html(fData.products[fProducts].total.toFixed(2)))
							.append($(document.createElement('td'))
								.css({'textAlign': 'right', 'width': '32px'})
								.append($(document.createElement('div'))
									.addClass('options')
									.append($(document.createElement('a'))
										.addClass('change')
										.attr({
											'href': 'javascript:void(0);',
											'title': 'Wijzigen'
										})
										.click(function(){
											$(this).closest('tr').find('input.text')[0].focus();
										}))
									.append($(document.createElement('a'))
										.addClass('delete')
										.attr({
											'href': 'javascript:void(0);',
											'title': 'Verwijderen'
										})
										.click(function(){
											if(confirm('Weet u zeker dat u `'+ fData.products[fProducts].name +'` uit uw winkelwagen wilt verwijderen?')){
												shoppingcart.deleting(fData.products[fProducts].id +'');
											}
										}))
									)
								)
							)
							
						fTotaalCart += fData.products[fProducts].total;
					});
					
					/*
					fTbodyTransport = $(document.createElement('tbody'));
					fTotaalTransport = 0;
					
					$.each(fData.transport, function(fTransport){
						fTbodyTransport.append($(document.createElement('tr'))
							.addClass((parseInt(fTransport)%2)?'even':'odd')
							.append($(document.createElement('td'))
								.html((fTransport == 0)?"Transportkosten":""))
							.append($(document.createElement('td'))
								.html(fData.transport[fTransport].name))
							.append($(document.createElement('td'))
								.html('&euro; '))
							.append($(document.createElement('td'))
								.css('textAlign', 'right')
								.html(fData.transport[fTransport].price.toFixed(2)))
							.append($(document.createElement('td'))
								.css({'textAlign':'right', 'width':'32px'})
								.append($(document.createElement('div'))
									.addClass('options')))
							);
									
							fTotaalTransport += fData.transport[fTransport].price;
						});
					*/
					
					
					$('#winkelmandje_table')
						.html('')
						.append($(document.createElement('table'))
							.addClass('data')
							.append(fTbodyCart)
							.append($(document.createElement('tfoot'))
								.append($(document.createElement('tr'))
									.append($(document.createElement('td'))
										.html('Subtotaal'))
									.append($(document.createElement('td')))
									.append($(document.createElement('td'))
										.html('&euro; '))
									.append($(document.createElement('td'))
										.css('textAlign', 'right')
										.html(fTotaalCart.toFixed(2)))
									.append($(document.createElement('td')))
									)
								)
							)
						// .append($(document.createElement('table'))
							// .addClass('data')
							// .css('marginTop', '10px')
							// .append(fTbodyTransport)
							// .append($(document.createElement('tfoot'))
								// .append($(document.createElement('tr'))
									// .append($(document.createElement('td'))
										// .html('Subtotaal'))
									// .append($(document.createElement('td')))
									// .append($(document.createElement('td'))
										// .html('&euro; '))
									// .append($(document.createElement('td'))
										// .css({'textAlign':'right', 'width':'32px'})
										// .html(fTotaalTransport.toFixed(2)))
									// .append($(document.createElement('td')))
									// )
								// )
							// )
						.append($(document.createElement('table'))
							.addClass('data')
							.css('marginTop', '10px')
							.append($(document.createElement('tfoot'))
								.append($(document.createElement('tr'))
									.append($(document.createElement('td'))
										.html('Transportkosten <img src="/images/flag_netherlands_16.png" alt="" />'))
									.append($(document.createElement('td'))
										.css({'textAlign':'right'})
										.html('&euro; '+ fData.transport.toFixed(2)))
									.append($(document.createElement('td'))
										.css({'textAlign':'right', 'width':'32px'}))
									)
								)
							)
						.append($(document.createElement('table'))
							.addClass('data')
							.css('marginTop', '10px')
							.append($(document.createElement('tfoot'))
								.append($(document.createElement('tr'))
									.append($(document.createElement('td'))
										.html('Totaal'))
									.append($(document.createElement('td'))
										.css({'textAlign':'right'})
										// .html('&euro; '+ (fTotaalCart + fTotaalTransport).toFixed(2)))
										.html('&euro; '+ (fTotaalCart + fData.transport).toFixed(2)))
									.append($(document.createElement('td'))
										.css({'textAlign':'right', 'width':'32px'}))
									)
								)
							)
						.append($(document.createElement('a'))
							.attr({id:'winkelmandje_afrekenen', 'href':'/bestellen/account/'})
							.html('Afrekenen&gt;&gt;&gt;'));
				}
				
				shoppingcart.loader(false);
			});
		}
	},
	loader: function(fShow){
		$('#winkelmandje_loader').css('display', (fShow?'block':'none'));
		$('#winkelmandje_table').css('visibility', (fShow?'hidden':'visible'));
	}
}
