// JavaScript Document

$(document).ready(function() {
	
	$('#melding').css({
		opacity		: 0
	});
	
	$('#melding').animate({
		bottom		: 	'0px',
		opacity		:	0.7
	}, 500);
	
	$('#slideshow').mouseover(function() {
		$('#pauze').show();
		$('#prev').show();
		$('#next').show();
	}).mouseout(function() {
		$('#pauze').hide();
		$('#prev').hide();
		$('#next').hide();
	});
	
	$('#pauze').click(function() {
		if($(this).html().toLowerCase() == '<img src="images/pauze.png">') {
			$(this).html('<img src="images/play.png" />');
			$('#slideshow').cycle('pause'); 
		}
		else {
			$(this).html('<img src="images/pauze.png" />');
			$('#slideshow').cycle('resume'); 
		}
	}).mouseover(function() {
		$('#pauze').show();
		$('#prev').show();
		$('#next').show();		
	});
	$('#next').click(function() {
		$('#slideshow').cycle('next'); 
	}).mouseover(function() {
		$('#pauze').show();
		$('#prev').show();
		$('#next').show();		
	});
	$('#prev').click(function() {
		$('#slideshow').cycle('prev'); 
	}).mouseover(function() {
		$('#pauze').show();
		$('#prev').show();
		$('#next').show();		
	});
	
	$('#slideshow img').css({opacity:0});
	$('#slideshow img').animate({opacity:1}, 1000);
	
	$('#menu a').mouseover(function() {
		if($(this).html().substr(0,5) != 'plat_') {
			$(this).html('plat_' + $(this).html());
		}
	}); 
	$('#menu a').mouseleave(function() {
		var value 	= 	$(this).html().split('_');
		var string	= 	'';
		for(i = 1; i <= (value.length - 1); i++) {
			string = string + value[i];
			if(i < (value.length - 1)) {
			string = string + '_';
			}
		}
		$(this).html(string);
		$('#menu a').each(function() {
			if($(this).attr('class') == 'active') {
				if($(this).html().substr(0,5) != 'plat_') {
					$(this).html('plat_' + $(this).html());
				}
			}
		});
	});
	
	var map 		= 	'';
	var provincie	=	'';
	var kols 		= 	1;
	var kol	 		= 	0;
	var left 		= 	0;
	var disabled	=	false;
	
	$('#Kaart_NL area').mouseover(function() {
		if(!disabled) {
			if(map == '') {
				map =	$('#Kaart').attr('src');
			}
			provincie 	= 	$(this).attr('href').replace('#', '');
			$('#Kaart').attr('src', 'images/winkels/' + provincie + '.png');
			switch(provincie) {
				case 'zeeland' :
					name = 'Zeeland';
				break;
				case 'noord-brabant' :
					name = 'Noord-brabant';
				break;
				case 'limburg' :
					name = 'Limburg';
				break;
				case 'zuid-holland' :
					name = 'Zuid-holland';
				break;
				case 'gelderland' :
					name = 'Gelderland';
				break;
				case 'utrecht' :
					name = 'Utrecht';
				break;
				case 'noord-holland' :
					name = 'Noord-holland';
				break;
				case 'flevoland' :
					name = 'Flevoland';
				break;
				case 'friesland' :
					name = 'Friesland';
				break;
				case 'groningen' :
					name = 'Groningen';
				break;
				case 'overijsel' :
					name = 'Overijsel';
				break;
				case 'drente' :
					name = 'Drente';
				break;
			}
		}
	}).mousemove(function(e) {
		$('body').append('<div id="img_info"></div>');
		var x 		= e.pageX;
		var y 		= e.pageY;
		$('#img_info').show();
		$('#img_info').css({
			position	: 	'absolute',
			left		:	(x + 25),
			top			:	(y + 25),
			zIndex		:	1000,
			background	:	'url(images/pixel.png)',
			color		:	'#282828',
			lineHeight	:	'15px',
			padding		:	'0px 3px'
		});
		var width		=	parseInt($('#img_info').css('width'));
		$('#img_info').css({
			left		:	x
		});
		$('#img_info').text(name);
		
	}).mouseout(function() {
		
		$('#Kaart').attr('src', map);
		$('#img_info').hide();
		
	}).click(function() {
		
		disabled	=	true;
		provincie 	= 	$(this).attr('href').replace('#', '');
		$('#Kaart').attr('src', 'images/winkels/' + provincie + '.png');

		switch(provincie) {
			case 'zeeland' :
				name = 'Zeeland';
			break;
			case 'noord-brabant' :
				name = 'Noord-brabant';
			break;
			case 'limburg' :
				name = 'Limburg';
			break;
			case 'zuid-holland' :
				name = 'Zuid-holland';
			break;
			case 'gelderland' :
				name = 'Gelderland';
			break;
			case 'utrecht' :
				name = 'Utrecht';
			break;
			case 'noord-holland' :
				name = 'Noord-holland';
			break;
			case 'flevoland' :
				name = 'Flevoland';
			break;
			case 'friesland' :
				name = 'Friesland';
			break;
			case 'groningen' :
				name = 'Groningen';
			break;
			case 'overijsel' :
				name = 'Overijsel';
			break;
			case 'drente' :
				name = 'Drente';
			break;
		}
		
		map 	=	$('#Kaart').attr('src');
		
		if($('#overzicht').css('display').toLowerCase() == 'none') {
			$('#overzicht').slideDown(500);
			$('#overzicht .titel').text(name);
			$.ajax({
				url		:	'request.php',
				type	:	'POST', 
				data	:	'&provincie=' + provincie + '&rows=1',
				success	:	function(rows) {
					$.ajax({
						url		:	'request.php',
						type	:	'POST', 
						data	:	'&provincie=' + provincie,
						success	:	function(response) {
							$('#overzicht .lijst').html(response);
							$('#overzicht .lijst').css({left:'0px'});
							kols 		= 	rows;
							kol	 		= 	0;
							left 		= 	0;
							$('.vorige').hide();
							if(kols == 0) {
								$('.volgende').hide();
							}
							else {
								$('.volgende').show();									
							}
							disabled	=	false;
						}
					});
				}
			});
		}
		else {
			$('#overzicht').slideUp(500);
			window.setTimeout(function() {
				$('#overzicht .titel').text(name);
				$.ajax({
					url		:	'request.php',
					type	:	'POST', 
					data	:	'&provincie=' + provincie + '&rows=1',
					success	:	function(rows) {
						$.ajax({
							url		:	'request.php',
							type	:	'POST', 
							data	:	'&provincie=' + provincie,
							success	:	function(response) {
								$('#overzicht .lijst').html(response);	
								$('#overzicht .lijst').css({left:'0px'});
								$('#overzicht').slideDown(500);
								kols 		= 	rows;
								kol	 		= 	0;
								left 		= 	0;
								$('.vorige').hide();
								if(kols <= 1) {
									$('.volgende').hide();
								}
								else {
									$('.volgende').show();									
								}
								disabled	=	false;
							}
						});
					}
				});
			}, 500);
		}
		
	});
	
	$('.volgende').click(function() {
		if(kols > 1) {
			if((kol + 1) < kols) {
				kol++;
				left = left - 230;
				$('.lijst').animate({
					left		:	left+'px'
				}, 500);
				$('.vorige').show();
			}
			if((kol + 1) == kols) {
				$('.volgende').hide();
			}
		}
	});
	$('.vorige').click(function() {
		if(kols > 1) {
			if(kol > 0) {
				kol--;
				left = left + 230;
				$('.lijst').animate({
					left		:	left+'px'
				}, 500);
				$('.volgende').show();	
			}
		}
		if(kol == 0) {
			$('.vorige').hide();
		}
	});
	$('.stem').click(function() {
		if($('.stempel').length == 0) {
			$('#container').prepend('<img class="stempel" src="images/stempel.png" />');
			$('.stempel').animate({
				width	: '200px',
				height	: '192px',
				right	: '100px',
				top		: '270px'
			}, 100);
			$(this).hide();
			$.ajax({
				url		:	'request.php',
				type	:	'POST', 
				data	:	'&stem=1',
				success	:	function(r) {
					$.ajax({
						url		:	'request.php',
						type	:	'POST', 
						data	:	'&stemmen=',
						success	:	function(response) {
							$('.aantal_stemmen').text(response);
							$('.aantal_stemmen').show();
						}
					});
				}
			});
		}
	});
	
});

function loadStamp(id) {
	if($('.stempel').length == 1) {
		$('.stempel').remove();
	}
	$.ajax({
		url		:	'request.php',
		type	:	'POST', 
		data	:	'&stamp=' + id,
		success	:	function(response) {
			$('#stempel').html('<table cellpadding="0" cellspacing="0"><tr><td align="center"><img src="files/stempels/' + response + '" /></td></tr></table>');
		}
	});
	$.ajax({
		url		:	'request.php',
		type	:	'POST', 
		data	:	'&stemmen=' + id,
		success	:	function(response) {
			$('.aantal_stemmen').text(response);
		}
	});
	$.ajax({
		url		:	'request.php',
		type	:	'POST', 
		data	:	'&check_stemmen=1',
		success	:	function(response) {
			if(response == 'ja') {		
				$('.stem').show();
			}
		}
	});
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++)  { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}


function addCart(id, aantal) {
	if(!IsNumeric(aantal)) {
		alert('Vul een geldig aantal in, tussen de 1 en 999.');
	}
	else {
		$.ajax({
			url		:	'request.php',
			type	:	'POST', 
			data	:	'&addcart='+id+'&aantal='+aantal,
			success	:	function(response) {
				location.href = 'index.php?pagina=12';
			}
		});
	}
}

function loadStore(id) {
	$('.vorige').hide();
	$('.volgende').hide();
	
	var store_name		=	'';
	var store_adress	= 	'';
	
	$.ajax({
		url		:	'request.php',
		type	:	'POST', 
		data	:	'&store_name='+id,
		success	:	function(response) {
			store_name = response;
			$.ajax({
				url		:	'request.php',
				type	:	'POST', 
				data	:	'&store_adress='+id,
				success	:	function(response) {
					store_adress = response;
					$('#overzicht .lijst').css({left:'0px'});
					$('#overzicht .lijst').html('<span class="winkel"><span class="titel inactive">' + store_name + '</span><span class="adres">' + store_adress + '</span></span>');
				}
			});
		}
	});
}
