function toggle(list){
	for(i in list){
		var el = document.getElementById(i);
		if(el){
			el.className = list[i];
		}
	}
}

function setSize(args){
	var el = document.getElementById(args.id);
	if(el){
		$('#'+el.id).animate(args.style, 500, 'swing', function(){
			if(typeof args.finish == 'function'){
				args.finish();
			}
		});
	}
}

function renderErrors(errors){
	if(errors.length > 0){
		var mask = $('<div id="mask"></div>');
		mask.click(function(){
			$(this).remove();
		});
		var div = $('<div id="errors" class="errors"></div>');
		var span = $('<span class="errorHeader"></span>');
		var html = '';
		if(errors[0] == 'Geen betaal methode ingevuld.'){
			html+='We hebben helaas niet alle noodzakelijke gegevens ontvangen.<br />';
			html+='Zou u daarom zo vriendelijk willen zijn om een betaalmethode te selecteren?<br />';
			html+='U kunt ook contact met ons opnemen via 06-25 22 65 26 of via <a href="mailto:info@voordeelstoffen.nl">info@voordeelstoffen.nl</a><br />';
		} else {
			html+='We hebben helaas niet alle noodzakelijke gegevens ontvangen.<br />';
			html+='Zou u daarom zo vriendelijk willen zijn om de rode velden te voorzien van de juiste informatie?<br />';
			html+='U kunt ook contact met ons opnemen via 06-25 22 65 26 of via <a href="mailto:info@voordeelstoffen.nl">info@voordeelstoffen.nl</a><br />';
		}
		html+='<ul>';
		for(var i = 0; i < errors.length; i++){
			html+='<li>'+errors[i]+'</li>';
		}
		html+='</ul></div>';
		span.html(html);
		div.append(span);
		mask.append(div);
		$(document.body).append(mask);
//		mask.delay(4000).animate({opacity:0},1000,'swing',function(){
//			$(this).remove();	
//		});
	}
}

// Onready functie
$(function(){
	$('#logout').click(function(){
		ajax.request({
			url:'logout.php',
			method:'GET',
			success:function(response){
				// Geen antwoord, gewoon redirect;
				document.location.reload(true);
			}
		});
	});

	$('.finishOrder').click(function(){
		ajax.request({
			url:'customer/plugins/webshop/render/check.php',
			method:'GET',
			params:{
				task:'checkPaymentMethod'
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = eval('('+result+')');
					switch(result['success']){
						case true:
							document.location = ajax.data.base+'betaling.html';
						break;
						case false:
							renderErrors(result.errors);
						break;
					}
				} else {
					alert('Geen antwoord van check.php bij checkPaymentMethod');
				}
			}
		});
	});
	$('#openPassword').click(function(){
		if($('#passwordTable').height() == 0){
			$('#passwordTable').animate({height:44},500);
			$('#register').animate({height:$('#register').height()+44},500);
		}
	});
	$('#closePassword').click(function(){
		if($('#passwordTable').height() > 0){
			$('#passwordTable').animate({height:0},500);
			$('#register').animate({height:$('#register').height()-44},500);
		}
	});
	$('#openDelivery').click(function(){
		if(parseInt($('#delivery').css('right')) == 250){
			$('#delivery').css({
				display:'block'
			});
			// Hidden input, controle of het afleveradres formulier voor het eerst wordt geopend.
			var fo = $('#firstOpen');
			if(fo.val() === '1'){
				fo.val(0);
				// Gender
				$('#d_country').val($('#c_country').val());
				var l = [
					'lastName',
					'email',
					'phone'
				];
				for(var i = 0; i < l.length; i++){
					$('#d_'+l[i]).val($('#c_'+l[i]).val());
				}
			}
			setSize({id:'delivery', style:{right:0, opacity:100, filter:'alpha(opacity = 100)'}});
			setSize({id:'registerForm', style:{left:0}});
		}
	});

	$('#closeDelivery').click(function(){
		if(parseInt($('#delivery').css('right')) == 0){
			setSize({
				id:'delivery',
				style:{
					right:'250px',
					opacity:0,
					filter:'alpha(opacity = 0)'
				},
				finish:function(){
					$('#delivery').css({
						 display:'none'
					});
				}
			});
			setSize({id:'registerForm', style:{left:'250px'}});
		}
	});

	$('.textInputError,.checkboxInputError').mouseenter(function(e){
		if($('.errorInfoDiv').length == 0){
			var errorInfo = $(this).attr('errorInfo');
			if(errorInfo != null && errorInfo != ''){
				var div = $('<div class="errorInfoDiv"></div>');
				div.html(errorInfo);
				div.css({
					left:e.pageX+5,
					top:e.pageY+5
				});
				$(document.body).append(div);
			}
		}
	});

	$('.textInputError').keydown(function(){
		$(this).addClass('textInput');
		$(this).removeClass('textInputError');
		$(this).removeAttr('errorInfo');
		$('.errorInfoDiv').remove();
	});

	$('.textInputError,.checkboxInputError').mouseout(function(e){
		$('.errorInfoDiv').remove();
	});

	$('input.button').mouseover(function() {
		$(this).toggleClass('buttonHover');
	});
	$('input.button').mouseout(function() {
		$(this).toggleClass('buttonHover');
	});

	$('div.divbutton').mouseover(function() {
		$(this).toggleClass('divbuttonHover');
	});
	$('div.divbutton').mouseout(function() {
		$(this).toggleClass('divbuttonHover');
	});

	$('#searchField').focus(function() {
		if(this.value == 'trefwoord...') {
			this.value = '';
		}
	});
	$('#searchField').blur(function() {
		if(this.value == '') {
			this.value = 'trefwoord...';
		}
	});

	var hover = false;
	$('.cat').mouseover(function(e){
		if($('#catImgDiv').length == 0){
			var $catImgDiv = $('<div id="catImgDiv"></div>');
			$catImgDiv.css({
				position:'absolute',
				width:200,
				background:'#fff',
				border:'1px #000 solid',
				display:'none',
				zIndex:100
			});
			$('#categoryNav').append($catImgDiv);
		}
		var $catImgDiv = $('#catImgDiv');
		if(hover == false){
			hover = true;
			if($(this).attr('hover') != ''){
				var $img = $('<img src="/customer/plugins/webshop/uploadedfiles/'+$(this).attr('hover')+'" />');
				$img.css({
					'max-width':200
				});
				$('#catImgDiv > img').remove();
				$catImgDiv.append($img);
				$catImgDiv.css({
					display:'block',
					left:($('#categoryNav').width()+4),
					top:36
				});
			} else {
				$catImgDiv.css({display:'none'});
			}
		}
	});
	$('.cat').mouseout(function(e){
		$('#catImgDiv').css({
			display:'none'
		});
		hover = false;
	});

	/*
	if($('.errors').length > 0){
		var $div = $('<div class="mask"></div>');
		$(document.body).append($div);
	}
	*/

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

	$('.info > img').mouseover(function(e){
		var $info = $('<div id="infoHover"></div>');
		$span = $('<span>'+$(this).attr('info')+'</span>');
		$info.append($span);
		$info.css({
			zIndex:900,
			display:'block',
			left:e.pageX,
			top:e.pageY
		});
		$(document.body).append($info);
	});
	$('.info > img').mouseout(function(){
		$('#infoHover').remove();
	});
//onclick="addToBasket({id:'.$product->getId().', field:\'product\'});"
	$('.addToBasket').click(function(){
		var values = this.id.split('_');
		if(values.length > 2){
			addToBasket({id:values[1], field:values[2]});
			if(tracker!=null){
				tracker._trackPageview('/nl/product/inwinkelwagen');
			}
		}
	});

	$('.unitDescriptionLabel').mouseover(function(e){
		var div = document.createElement('div');
		div.id = 'unitDescription';
		div.innerHTML = $(this).attr('desc');
		div.style.position = 'absolute';
		div.style.left = 0+'px';
		div.style.top = 25+'px';
		$(this).append(div);
	});

	$('.unitDescriptionLabel').mouseout(function(e){
		$('#unitDescription').remove();
	});

	$('.cartInfoText').mouseover(function(e){
		$('#webshopCountrySendCostTable').css({display:'block'});
	});

	$('.cartInfoText').mouseout(function(e){
		$('#webshopCountrySendCostTable').css({display:'none'});
	});

	$('.floatbox').click(function(){
		fb.start(this);
	});
	$('.continueShopping').click(function(){
		var href = $(this).attr('href');
		if(href == ''){
			history.back();
			return false;
		}
	});
	initCheckbox();
	initMask();

	$('#webshopPriceSearchSelect').change(function(){
		var value = $(this).val();
		ajax.request({
			url:'customer/plugins/webshop/render/update.php',
			method:'GET',
			params:{
				task:'setPriceFilter',
				value:value
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = ajax.decode(result);
					switch(result.success){
						case true:
							document.location.reload(true);
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen antwoord van update.php bij setPriceFilter');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});
	});

	$('#webshopOfferFilterSelect').change(function(){
		var value = $(this).val();
		ajax.request({
			url:'customer/plugins/webshop/render/update.php',
			method:'GET',
			params:{
				task:'setOfferFilter',
				value:value,
				loc:document.location.href
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = ajax.decode(result);
					switch(result.success){
						case true:
							if(typeof result['location'] != 'undefined'){
								document.location = result['location'];
							}
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen antwoord van update.php bij setOfferFilter');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});
	});

	$('#offerLink').click(function(){
		var href = $(this).attr('href');
		ajax.request({
			url:'customer/plugins/webshop/render/update.php',
			method:'GET',
			params:{
				task:'setOfferFilter',
				value:'1',
				loc:href
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = ajax.decode(result);
					switch(result.success){
						case true:
							if(typeof result['location'] != 'undefined'){
								document.location = result['location'];
							}
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen antwoord van update.php bij setOfferFilter');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});

		return false;
	});

	$('#bottomSitemapToggle').click(function(){
		var img = $(this).find('img');
		var c = $('#sitemapParentContainer');
		var newc = c.parent().find('#nieuw');
		var data = ajax.decode(c.attr('footerdata'));
		if(data.expanded === false){
			c.animate({
				height:data.height
			},300, 'swing', function(){
				data.expanded = true;
				c.attr('footerdata', ajax.encode(data));
				var src = img.attr('src');
				src = src.replace('plus', 'min');
				img.attr('src', src);
				newc.css({
					opacity:0,
					display:'block'
				});
				newc.animate({
					opacity:1
				},200, 'swing', function(){
					$(this).css({'filter': ''});
				});
			});
		} else {
			newc.animate({
				opacity:0
			},200,'swing', function(){
				$(this).css({
					display:'none'
				});
				c.animate({
					height:0
				},300, 'swing', function(){
					data.expanded = false;
					c.attr('footerdata', ajax.encode(data));
					var src = img.attr('src');
					src = src.replace('min', 'plus');
					img.attr('src', src);
				});
			
			});
		}
	});
	init();
	var tracker = null;
	function checkTracker(){
		setTimeout(function(){
			if(typeof _gat == 'object'){
				tracker = _gat._createTracker('UA-3609586-2');
			} else {
				checkTracker();
			}
		},100);
	}
	checkTracker();
});
var home = {};
home.moving = false;
function init(){
	$('#basketSmallHomeToggle').click(function(){
		var el = $(this).parent().find('#basketSmallHomeContent');
		if(el.length > 0){
			var h = el.height();
			if(h != 90){
				el.animate({
					height:90
				},300,'swing', function(){
					$('#cartSmallLink').css({display:'block'});
				});
			} else {
				el.animate({
					height:0
				},300,'swing', function(){
					$('#cartSmallLink').css({display:'none'});
				});
			}
		}
	});

	$('.tab').click(function(){
		if(!home.moving){
			home.moving = true;
			ajax.request({
				url:'customer/plugins/webshop/render/update.php',
				method:'GET',
				mask:false,
				params:{
					task:'setOfferSession',
					id:$(this).attr('id')
				}
			});
			var tab = $(this);
			var contentId = '#'+tab.attr('id').substr(0, tab.attr('id').length-3)+'Content';
			var content = $(contentId);
			var data = ajax.decode(content.attr('tabContentData'));
			content.css({
				opacity:0,
				display:'block'
			});
			$('#tabContent .active').animate({
				opacity:0
			}, 300,'swing', function(){
				$(this).css({display:'none'});
				$(this).removeClass('active');
			});
			$('#tabContent').animate({
				height:data.height
			},300);
			content.animate({
				opacity:1
			},300,'swing', function(){
				$(this).addClass('active');
				$('.tabActive').addClass('tab');
				$('.tabActive').removeClass('tabActive');
				tab.addClass('tabActive');
				tab.removeClass('tab');
				init();
			});
		}
	});
	home.moving = false;
}


function initCheckbox(){
	$('.checkboxInput').click(function(){
		$(this).addClass('checkboxInputChecked');
		$(this).removeClass('checkboxInput');
		$('#'+$(this).attr('CBID')).attr('checked',true);
		initCheckbox();
	});

	$('.checkboxInputError').click(function(){
		$(this).addClass('checkboxInputChecked');
		$(this).removeClass('checkboxInputError');
		$(this).removeAttr('errorInfo');
		$('#'+$(this).attr('CBID')).attr('checked',true);
		initCheckbox();
	});

	$('.checkboxInputChecked').click(function(){
		$(this).addClass('checkboxInput');
		$(this).removeClass('checkboxInputChecked');
		$('#'+$(this).attr('CBID')).attr('checked',false);
		initCheckbox();
	});
}

function initMask(){
	$('#mask').click(function(){
		$(this).remove();
	});
	$('#productPopup').css({
		marginTop:-($('#productPopup').height()/2),
		display:'block'
	});
	$('#mask').delay(3000).animate({opacity:0}, 500, 'swing', function(){
		$(this).remove();
	});
}
