var flux = { src: sDomain+'flash/sifr.swf' };
sIFR.activate(flux);

$(document).ready(function(){
	initSIFR();
	menu();
	initShoppingbag();	
	clearInput();
});

function initSIFR(){
	sIFR.replace(flux, {
		selector: 'h1',
		css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #371900; text-transform: uppercase; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h6',
		css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #371900; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h1.right_title',
		css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #371a01; text-transform: uppercase; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h1.aanmelding',
		css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #2C5A00; text-transform: uppercase; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h1.aanmelding1',
		css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #371900; text-transform: uppercase; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h3',
		css: '.sIFR-root { font-size: 19px; font-weight: bold; color: #204002; text-transform: uppercase; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h2.aanmelding',
		css: '.sIFR-root { font-size: 16px; color: #371900; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h2.aanmelding.vragen',
		css: '.sIFR-root { font-weight: bold;  }',
		wmode: 'transparent'
	});
	sIFR.replace(flux, {
		selector: 'h4',
		css: '.sIFR-root { font-size: 19px; font-weight: bold; color: #ffffff; text-transform: uppercase; }',
		wmode: 'transparent'
	});
}

function menu() {
	$('.menu_item').hover(function() {
		var nWidth = $(this).width();
		
		$(this).addClass('hover');
		$(this).children('.submenu, .bottom_left, .bottom_right').show();
		$(this).children('.bottom_left').css('width',nWidth);
	}, function() {
		$(this).removeClass('hover');
		$(this).children('.submenu, .bottom_left, .bottom_right').hide();
	});
}

function initShoppingbag() {
	$('.shoppingbag').hover(function() {
		$(this).children('#shop_product_list').show();
	}, function() {
		$(this).children('#shop_product_list').hide();
	});
}

function clearInput() {
	$('.fade').focus(function() {
	   $(this).select();
	   $(this).prev().fadeTo(150, 0);
	   $(this).keydown(function() {
		   $(this).css('background-repeat', 'repeat');                  
	   });
	});
	$('.fade').blur(function() {
	   if($(this).val() == '' ) {
		   $(this).css('background-repeat', 'no-repeat');
		   $(this).prev().fadeTo(150, 1);
	   }
	});
	$('.fade').each(function(){
	   if($(this).val() != '') {
		   $(this).css('background-repeat', 'repeat');
	   };
	});
}

function productMenu() {
	$('#productcats li.sub').click(function() {
		if(!$(this).hasClass('active')) {
			$('li.active ul.subcat').slideUp(600);
			$('li.active').removeClass('active');
			$(this).addClass('active');
			$(this).children('ul.subcat').slideDown(600);
		}
	});
}

function hoverProducts() {
	$('.product_item').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
}

function sfeerCycle() {
	$('#cycle').after('<div id="nav">').cycle({
		fx:     'fade',
		timeout: 6000,
		speed:	 900,
		pager: '#nav'
	});
}

function sortProducts() {
	$('#sorteren_container ul li').click(function() {
		var sSort = $(this).attr('id').replace('sorteren_input_', '');
		
		$.ajax({
			type: 'POST',
			url: sDomain+'controllers/products.php',
			data: 'nPageID='+nPageID+'&sSort='+sSort+'&sBrand='+sBrand,
			success: function(data){
				$('#right_producten').html(data);
			},
			error: function(message){
				alert('error'+message);
			}
		});
		return false;
	});
}

function changeImage(){
	$('.product_hoofd_foto_nav a').click(function(){
		var sFoto = $(this).attr('id');
		var sSrc = $(this).children('img').attr('src').replace('thumb','normal');
		var sHref = sSrc.replace('normal','lightbox');
		$('#hoofdfoto img:not(.zoom_foto)').attr('src',sSrc);
		$('#hoofdfoto img:not(.zoom_foto)').attr('class',sFoto);
		$('.product_hoofd_foto_nav a').removeClass('active');
		$(this).addClass('active');
	});
	$('#hoofdfoto').click(function() {
		var sFoto = $(this).children('img').attr('class');
		$('.lb_fake .'+sFoto).trigger('click');
	});
}

function addProduct() {
	$('#toevoegen').click(function() {
		var nAantal = $('#nAantal').val();
		var nProduct = $('#nProduct').val();
		var sKleur = '';
		var sMaat = '';
		
		if($('#select_maat_container ul li.selected').length == 0) {
			$('#select_maat_container ul li:first').addClass('selected');	
		}
		
		if($('#select_maat_input').length > 0) {
			sMaat = $('#select_maat_container ul li.selected').attr('id').replace('select_maat_input_', '');
		}
		
		if($('#select_kleur_input').length > 0) {
			//sKleur = $('#select_kleur_container ul li.selected').attr('id').replace('select_kleur_input_', '');
		}
		
		if(nAantal > 0) {
			$.ajax({
				type: 'POST',
				url: sDomain+'requests/addproduct.req.php',
				data: 'nAantal='+nAantal+'&nProduct='+nProduct+'&sKleur='+sKleur+'&sMaat='+sMaat,
				success: function(result){
					aResult = result.split('|');
					if(aResult[0] == 1) {
						$('.shoppingbag').hide();
						$('html, body').animate({
							scrollTop: 0
						}, 500, function(){
							var sProducten = ' producten';
							if(aResult[1] == 1) {
								sProducten = ' product';
							}
							$('#total_products').html('( '+aResult[1]+sProducten+' )');
							$('#shop_product_list').html(aResult[2]);
							$('.shoppingbag').fadeIn(800);
							initShoppingbag();
						});
					}
				}
			});
		}
	});
}

function shoppingbag() {
	$('.invul_aantal_producten').change(function(){
		var nProduct = $(this).attr('id').replace('product_','');
		var nAantal = $(this).val();		
		loadWinkelwagen('productID=' + nProduct + '&updateaantal=' + nAantal);
	});
	$('.remove_product a').click(function(){
		var nProduct = $(this).parents('.shoppingbag_item').find('.invul_aantal_producten').attr('id').replace('product_','');
		loadWinkelwagen('productID=' + nProduct + '&updateaantal=0');
	});
}

function loadWinkelwagen(p_sData) {
	$.ajax({
		type: 'POST',
		url: 'controllers/shoppingbag.php',
		data: p_sData,
		success: function(result){
			aResult = result.split('||');
			var sProducten = ' producten';
			if(aResult[0] == 1) {
				sProducten = ' product';
			}
			$('#total_products').html('( '+aResult[0]+sProducten+' )');
			$('#shop_product_list').html(aResult[1]);
			if(aResult[2] != '') {
				$('#shoppingbag_wrapper').html(aResult[2]);
			} else {
				$('#shoppingbag_wrapper').html('<div class="right_content"><p>U heeft geen producten in uw shoppingbag.</p></div>');
				$('.shoppingbag_lijst_top, .algem_voorwaarden, .bestelling_afronden').hide();	
			}
			shoppingbag();
		}
	});
}

function radioButtons() {
	$('.radio').click(function() {
		$('label.radio').removeClass('checked');	
		$('label.radio').find('input').attr('checked','');
		$(this).addClass('checked');
		$(this).find('input').attr('checked','checked');
	});
	$('.radio2').click(function() {
		$('label.radio2').removeClass('checked');	
		$('label.radio2').find('input').attr('checked','');
		$(this).addClass('checked');
		$(this).find('input').attr('checked','checked');
	});
	$('.checkbox').toggle(
		function() {
			$(this).prev().attr('checked','checked');
			$(this).addClass('checked');
			$(this).children('input').val('Ja');
			},
		function(){
			$(this).prev().attr('checked','');
			$(this).removeClass('checked');
			$(this).children('input').val('');
		}	
	);
}

function anderAdres(){
	if( $('#sAnderAdres').is(':checked') ) {
		$('.ander_adres').show();
		$('.ander_adres .entry.required input:text').addClass('required');
	} 
	$('#sAnderAdres').click(function(){
		if( $(this).is(':checked') ) {
			$('.ander_adres .entry.required input:text').addClass('required');
			$('.ander_adres').slideDown(300);
		} else {
			$('.ander_adres').slideUp(300);
			$('.ander_adres .entry.required input:text').removeClass('required');
		}
  });	
}

function algVoorwaarden() {
	$('.voorwaarden #nAkkoord, .voorwaarden label').click(function() {
		if($(this).parents('.voorwaarden').find('input').is(':checked') ) {
			$('.letop_algem').hide();
		}
	});
}

function betaalmethodes() {
	$('.method:not(.betaal_ideal)').click(function() {
		if($('.betaal_ideal .button').hasClass('checked')) {
			$('#maak_keuze_input_').trigger('click');
			$('#maak_keuze_container ul li').removeClass('selected');		
		}
		$(this).parent('.betaalmethodekeuze').find('.button input').attr('checked','');
		$(this).parent('.betaalmethodekeuze').find('.button').removeClass('checked');
		$(this).find('.button input').attr('checked','checked');
		$(this).find('.button').addClass('checked');
	});
	$('#maak_keuze_container ul li').click(function() {
		$(this).parents('.betaalmethodekeuze').find('.button input').attr('checked','');
		$(this).parents('.betaalmethodekeuze').find('.button').removeClass('checked');
		$(this).parents('.betaal_ideal').find('.button input').attr('checked','checked');
		$(this).parents('.betaal_ideal').find('.button').addClass('checked');
	});
}

function validateForm(p_sForm) {
	$.validator.addMethod('group', function(value, element) {
		var obj = $('.form');
		return obj.find('.group:filled').length;
	}, '');	

	switch(p_sForm) {
		
		case 'registreren':
		
			var rules = {};
			rules[oFields[p_sForm][1]] = {
				required: true,
				email: true,
				notags: true
			};
			rules[oFields[p_sForm][2]] = {
				required: true,
				minlength: 6,
				notags: true
			};
			rules[oFields[p_sForm][3]] = {
				required: true,
				notags: true,
				minlength: 6,
				equalTo: '#'+oFields[p_sForm][2]
			};
			rules[oFields[p_sForm][4]] = {
				required: true,
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][5]] = {
				required: true,
				notags: true
			};
			rules[oFields[p_sForm][6]] = {
				required: true,
				notags: true,
				minlength: 6
			};
			rules[oFields[p_sForm][7]] = {
				required: true,
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][8]] = {
				notags: true
			};
			rules[oFields[p_sForm][9]] = {
				notags: true,
				minlength: 2
			};
			rules[oFields[p_sForm][10]] = {
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][11]] = {
				notags: true
			};
			rules[oFields[p_sForm][12]] = {
				notags: true,
				minlength: 6
			};
			rules[oFields[p_sForm][13]] = {
				notags: true,
				minlength: 2
			};
			
			var messages = {};
			messages[oFields[p_sForm][1]] = {
				required: 'Vul uw e-mailadres in.',
				email: 'Vul een geldig e-mailadres in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][2]] = {
				required: 'Vul uw gewenste wachtwoord in.',
				minlength: 'Vul minimaal 6 tekens in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][3]] = {
				required: 'Vul nogmaals het wachtwoord in.',
				minlength: 'Het wachtwoord komt niet overeen.',
				notags: 'Er zijn geen tags toegestaan.',
				equalTo: 'Het wachtwoord komt niet overeen.'
			};
			messages[oFields[p_sForm][4]] = {
				required: 'Vul uw naam in.',
				nonumber: 'Vul een geldige naam in.',
				minlength: 'Vul een geldige naam in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][5]] = {
				required: 'Vul uw straat + huisnr in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][6]] = {
				required: 'Vul uw postcode in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Vul een geldige postcode in.'
			};
			messages[oFields[p_sForm][7]] = {
				required: 'Vul uw plaats in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Vul een geldige plaats in.'
			};
			messages[oFields[p_sForm][8]] = {
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][9]] = {
				minlength: 'Vul een geldige bedrijfsnaam in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][10]] = {
				required: 'Vul een naam in.',
				nonumber: 'Vul een geldige naam in.',
				minlength: 'Vul een geldige naam in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][11]] = {
				required: 'Vul een straat + huisnr in.',
				notags: 'Er zijn geen tags toegestaan.'
			};
			messages[oFields[p_sForm][12]] = {
				required: 'Vul een postcode in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Vul een geldige postcode in.'
			};
			messages[oFields[p_sForm][13]] = {
				required: 'Vul een plaats in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Vul een geldige plaats in.'
			};
		
		break;
		case 'contactform':
		
			var rules = {};
			rules[oFields[p_sForm][1]] = {
				required: true,
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][2]] = {
				required: true,
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][3]] = {
				required: true,
				notags: true,
				minlength: 6,
				maxlength: 13
			};
			rules[oFields[p_sForm][4]] = {
				required: true,
				notags: true,
				email: true
			};
			rules[oFields[p_sForm][5]] = {
				notags: true,
				minlength: 1
			};
			
			var messages = {};
			messages[oFields[p_sForm][1]] = {
				required: 'Vul uw naam in.',
				notags: 'Er zijn geen tags toegestaan.',
				nonumber: 'Dit is geen geldige naam.',
				minlength: 'Dit is geen geldige naam.'
			};
			messages[oFields[p_sForm][2]] = {
				required: 'Vul uw woonplaats in.',
				notags: 'Er zijn geen tags toegestaan.',
				nonumber: 'Dit is geen geldige woonplaats.',
				minlength: 'Dit is geen geldige woonplaats.'
			};
			messages[oFields[p_sForm][3]] = {
				required: 'Vul uw telefoonnummer in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Dit is geen geldig telefoonnummer.',
				maxlength: 'Dit is geen geldig telefoonnummer.'
			};
			messages[oFields[p_sForm][4]] = {
				required: 'Vul uw emailadres in.',
				notags: 'Er zijn geen tags toegestaan.',
				email: 'Dit is geen geldige emailadres.'
			};
			messages[oFields[p_sForm][5]] = {
				notags: 'Er zijn geen tags toegestaan.',
				minlength: ''
			};
		
		break;
		case 'productform':
		
			var rules = {};
			rules[oFields[p_sForm][1]] = {
				required: true,
				notags: true,
				nonumber: true,
				minlength: 2
			};
			rules[oFields[p_sForm][2]] = {
				required: true,
				notags: true,
				minlength: 6,
				maxlength: 13
			};
			rules[oFields[p_sForm][3]] = {
				required: true,
				notags: true,
				email: true
			};
			rules[oFields[p_sForm][4]] = {
				required: true,
				notags: true,
				minlength: 1
			};
			
			var messages = {};
			messages[oFields[p_sForm][1]] = {
				required: 'Vul uw naam in.',
				notags: 'Er zijn geen tags toegestaan.',
				nonumber: 'Dit is geen geldige naam.',
				minlength: 'Dit is geen geldige naam.'
			};
			messages[oFields[p_sForm][2]] = {
				required: 'Vul uw telefoonnummer in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Dit is geen geldig telefoonnummer.',
				maxlength: 'Dit is geen geldig telefoonnummer.'
			};
			messages[oFields[p_sForm][3]] = {
				required: 'Vul uw emailadres in.',
				notags: 'Er zijn geen tags toegestaan.',
				email: 'Dit is geen geldige emailadres.'
			};
			messages[oFields[p_sForm][4]] = {
				required: 'Vul uw vraag en/of opmerking in.',
				notags: 'Er zijn geen tags toegestaan.',
				minlength: 'Vul uw vraag en/of opmerking in.'
			};
		
		break;
	}

	$('#'+p_sForm).validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

function wachtwoordVergeten() {
	$('.ww_vergeten').click(function() {
		$('.ik_ben_geen_klant').hide();								
		$('.wachtwoord_vergeten').fadeIn(1500);		
		sIFR.replace(flux, {
			selector: 'h1.aanmelding',
			css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #2C5A00; text-transform: uppercase; }',
			wmode: 'transparent'
		});
		sIFR.replace(flux, {
			selector: 'h2.aanmelding',
			css: '.sIFR-root { font-size: 16px; color: #371900; }',
			wmode: 'transparent'
		});
	});
	$('.account_registreren').click(function() {
		$('.wachtwoord_vergeten').hide();								
		$('.ik_ben_geen_klant').fadeIn(1500);		
		sIFR.replace(flux, {
			selector: 'h1.aanmelding',
			css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #2C5A00; text-transform: uppercase; }',
			wmode: 'transparent'
		});
		sIFR.replace(flux, {
			selector: 'h2.aanmelding',
			css: '.sIFR-root { font-size: 16px; color: #371900; }',
			wmode: 'transparent'
		});
	});
}

function changeMeasure() {
	$('#select_maat_container ul li').click(function() {
		var sValue = $(this).attr('id').replace('select_maat_input_', '');
		var sData = 'nProductID='+nProductID+'&sChange=measure&sMeasure='+sValue;
		
		changeOptions(sData, 'measure');
	});
}

function changeColor() {
	$('#select_kleur_container ul li').click(function() {
		var sValue = $(this).attr('id').replace('select_kleur_input_', '');
		var sData = 'nProductID='+nProductID+'&sChange=color&sColor='+sValue;
		
		changeOptions(sData, 'color');
	});
}

function changeOptions(p_sData, p_sType) {
	$.ajax({
		type: 'POST',
		url: 'requests/changeoptions.req.php',
		data: p_sData,
		success: function(result) {
			aResult = result.split('|/|');
			
			if(aResult[0] == 'measure') {
				if(aResult[1] == '') {
					$('#ajax-colors').prev('.detail_lijst_selectbox').addClass('last');	
				}
				
				$('#ajax-colors').html(aResult[1]);
				
				if(aResult[1] != '') {
					$('#ajax-colors').prev('.detail_lijst_selectbox').removeClass('last');
					$('#select_kleur').selectbox();
				}
			}
			
			$('#ajax-price').removeClass('sIFR-replaced').html(aResult[2]);
			$('#ajax-stock').html(aResult[3]);
			
			sIFR.replace(flux, {
				selector: 'h6',
				css: '.sIFR-root { font-size: 21px; font-weight: bold; color: #371900; }',
				wmode: 'transparent'
			});
			
			if(p_sType == 'measure') {
				changeColor();
			}
		}
	});
}
