$(document).ready(function(){
	$('.back').css({
		cursor:'pointer'
	});
	$('.back').click(function(){
		history.back();
	});
	$('.slideshow').each(function(){
		var data = eval('('+$(this).attr('ssData')+')');
		$('.ssImage').css({visibility:'visible'});
		$(this).cycle(data);
		$(this).height(data.height);
		$(this).width(data.width);
	});

	$('.googleMap').each(function(){
		var opts = eval('('+$(this).attr('mapOptions')+')');
		var center = eval('('+opts.center+')');
		opts.center = new google.maps.LatLng(center.b,center.c);
		opts.zoom = parseFloat(opts.zoom);
		var el = document.getElementById($(this).attr('id'));
		// Markers ophalen
		var markers = [];
		$(this).find('.marker').each(function(){
			markers[markers.length] = {
				data:eval('('+$(this).attr('markerOptions')+')'),
				description:$(this).html()
			}
		});
		var map = null;
		if(el){
			map = new google.maps.Map(el,opts);
		}
		if(map != null && markers.length > 0){
			for(var i = 0; i < markers.length; i++){
				var opts = markers[i].data;
				if(opts.cmsDraggable != undefined){
					opts.draggable = opts.cmsDraggable;
				}
				if(opts.cmsClickable != undefined){
					opts.clickable = opts.cmsClickable;
				}
				var position = eval('('+opts.position+')');
				opts.position = new google.maps.LatLng(position.b,position.c);
				opts.map = map;
				var m = new google.maps.Marker(opts);
				if(markers[i].description != ''){
					var w = new google.maps.InfoWindow({
						content:markers[i].description,
						position:opts.position
					});
					w.open(map);
					google.maps.event.addListener(m, 'click', function(e){
						w.open(map);
					});
				}
			}
		}
	});

//	$('#backButton').click(function(){
//		history.back();
//	});

	var webshop = {};
	webshop.basket = {
		timer:null
	};
	webshop.replace = function(replace){
		for(var i in replace){
			if(typeof replace[i] != 'function'){
				if(i != 'popup'){
					$('#'+i).replaceWith(replace[i]);
				} else {
					$(document.body).append(replace[i]);
				}
			}
		}
		addBasketEvents();
	}

	$('.continueShopping').click(function(){
		var href = $(this).attr('href');
		if(href == 'Verder winkelen'){
			history.back();
			return false;
		}
	});

	$('[orderData]').click(function(){
		var data = ajax.decode($(this).attr('orderData'));
		data.task = 'updateBasket';
		data.replace = [
			'smallBasket',
			'popup'
		];
		ajax.request({
			url:'customer/plugins/webshop/render/basket.php',
			method:'GET',
			params:{
				data:ajax.encode(data)
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = ajax.decode(result);
					switch(result.success){
						case true:
							if(result.redirect){
								document.location = result.redirect;
							} else if(result.replace){
								webshop.replace(result.replace);
							}
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen antwoord van basket.php bij updateBasket');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});
	});

	function addMaskEvents(){
		$('.mask,#productPopup,.errors').click(function(){
			$('.mask,#productPopup,.errors').remove();
		});
	}

	function addBasketEvents(){
		$('[inputData]').keydown(function(){
			if(webshop.basket.timer != null){
				clearTimeout(webshop.basket.timer);
			}
			var input = $(this);
			webshop.basket.timer = setTimeout(function(){
				var value = parseFloat(input.val().replace(',','.'));
				var data = ajax.decode(input.attr('inputData'));
				var error = false;
				if(isNaN(value)){
					error = true;
					value = data.old;
				} else {
					if(value%Math.floor(value) > 0 && data.decimals === false){
						error = true;
						value = data.old;
					}
				}
				if(!error){
					data.old = value;
					input.attr('inputData', ajax.encode(data));
					var ajaxData = {
						id:data.id,
						amount:value,
						add:false,
						task:'updateBasket',
						replace:['smallBasket','basket']
					};
					$('#loader_'+data.id).css({
						visibility:'visible'
					});
					ajax.request({
						url:'customer/plugins/webshop/render/basket.php',
						method:'GET',
						params:{
							data:ajax.encode(ajaxData)
						},
						success:function(response){
							var result = response.responseText;
							if(result != ''){
								result = ajax.decode(result);
								switch(result.success){
									case true:
										if(result.redirect){
											document.location = result.redirect;
										} else if(result.replace){
											webshop.replace(result.replace);
										}
										$('.loadingimg').css({
											visibility:'hidden'
										});
									break;
									case false:
										alert(result.msg);
									break;
								}
							} else {
								alert('Geen antwoord van basket.php bij updateBasket');
							}
						},
						failure:function(response){
							alert(response.responsText);
						}
					});
				}
				input.val(value);
			},500);
		});
		addMaskEvents();
	}
	addBasketEvents();

	$('#openPassword').click(function(){
		$('#passwordTable').animate({
			height:50
		},500);
	});
	$('#closePassword').click(function(){
		$('#passwordTable').animate({
			height:0
		},500);
	});

	$('#openDelivery').click(function(){
		if($('#firstOpen').val() === '1'){
			$('#firstOpen').val('0');
			var fields = [
				{src:'c_initials',		dest:'d_initials'},
				{src:'c_middleName',	dest:'d_middleName'},
				{src:'c_lastName',		dest:'d_lastName'},
				{src:'c_email',			dest:'d_email'},
				{src:'c_country',		dest:'d_country'},
				{src:'c_phone',			dest:'d_phone'}
			];
			for(var i = 0; i < fields.length; i++){
				var s = $('#'+fields[i].src);
				if(s){
					var d = $('#'+fields[i].dest);
					if(d){
						d.val(s.val());
					}
				}
			}
		}
		$('#delivery').animate({
			height:380
		},500);
	});

	$('#closeDelivery').click(function(){
		$('#delivery').animate({
			height:0
		},500);
	});

	function addCheckboxEvents(){
		$('.checkboxInput').click(function(){
			$(this).addClass('checkboxInputChecked');
			$(this).removeClass('checkboxInput');
			$('#'+$(this).attr('cbid')).attr('checked', true);
			addCheckboxEvents();
		});
		$('.checkboxInputChecked').click(function(){
			$(this).addClass('checkboxInput');
			$(this).removeClass('checkboxInputChecked');
			$('#'+$(this).attr('cbid')).attr('checked', false);
			addCheckboxEvents();
		});
		$('.checkboxInputError').click(function(){
			$(this).addClass('checkboxInputChecked');
			$(this).removeClass('checkboxInputError');
			$('#'+$(this).attr('cbid')).attr('checked', true);
			addCheckboxEvents();
		});
	}
	addCheckboxEvents();

	var cursus = {
		moving:false
	};
	$('.dummyLink').click(function(){
		var ul = $(this).parent().find('ul');
		if(ul.length > 0){
			if(!cursus.moving){
				var items = ul.find('li');
				var newHeight = 0;
				if(ul.height() == 0){
					newHeight = (items.length*26);
				}
				cursus.moving = true;
				ul.animate({
					height:newHeight
				},500, 'swing', function(){
					cursus.moving = false;
				});
			}
		}
	});

	$('#date').change(function(){
		var s = $(this).find('option:selected');
		var value = '';
		var field = null;
		if(s.length > 0){
			var fieldSet = s.attr('fieldSet');
			if(fieldSet){
				fieldSet = eval('('+fieldSet+')');
				field = $('#'+fieldSet.field);
				if(field.length > 0){
					value = fieldSet.set;
				}
			}
		}
		if(field != null){
			field.val(value);
		}
	});

	$('#Zoekwoord').focus(function(){
		if($(this).val() == 'Zoekwoord'){
			$(this).val('');
		}
	});
	$('#Zoekwoord').blur(function(){
		if($(this).val() == ''){
			$(this).val('Zoekwoord');
		}
	});
});
