$(document).ready(function () {
	
	if($('#map_canvas').length > 0) {
		initialize(); 
	}
	
	$('a[rel*=lightbox]').lightBox();
	
	$('a[href="#"]').live('click', function() { return false; }); //Force not to reload
	
	$('#fldZoeken').live('click', function() {
		$(this).select();
		
		$(this).keypress(function(e) {
			if(e.which == 13) {
				$('#btnZoeken').click();
			}
		});
	}).live('blur', function() {
		if($(this).val() == '') {
			$(this).val('Zoeken');
		}
	});

	$('#btnZoeken').live('click', function() {
		var ZoekTerm = $('#fldZoeken').val();
		
		location.href = WebURL + 'Zoeken/'+php_urlencode(ZoekTerm);
	}).live('mouseover', function() {
		$(this).css({'cursor' : 'pointer'});
	});
			
	$('.hasChilds').live('click', function() {
		var current = $('+ ul', this).css('display');
		closeallmenus();
		
		if(current == 'none') {
			$('+ ul', this).css('display', 'block');
			$('img', this).attr('src', WebURL + 'images/arrow_down_magenta.gif');
			$(this).css('color', '#ED194E');
		}
		else {
			$('ul', this).css('display', 'none');
			$('img', this).attr('src', WebURL + 'images/arrow_down_grey.gif');
			$(this).css('color', '#545454');
		}
		
		
		return false;
	});
	
	$('#TopNav > .Last').live('mouseover', function() {
		$('a', this).css('color', '#666666');
	}).mouseleave(function() { 
		$('a', this).css('color', '');
	});

	
	$('.buttonContainer').live('mouseover', function() {
		$(this).css('cursor', 'pointer');
	});
	
	$('.KleurSelector').live('change', function() {
		var value = $('option:selected', this).attr('id').split('_')[1];
		var image = $('#kleur_'+value).attr('href');
		
		$('.ProductPreviewBig > a > img').fadeOut(500, function() {
			$('.ProductPreviewBig > a > img').attr('src', WebURL + 'media/artikelen/thumbs/280_'+image);
			$('.ProductPreviewBig > a').attr('href', WebURL + 'media/artikelen/'+image);
			$('.ProductPreviewBig > a > img').fadeIn(500);
		});
		
		loadVoorraad();
	});
	
	$('.ToBig').live('click', function() {
		var image = $(this).attr('href');
		
		$('.ProductPreviewBig > a > img').fadeOut(500, function() {
			$('.ProductPreviewBig > a > img').attr('src', WebURL + 'media/artikelen/thumbs/280_'+image);
			$('.ProductPreviewBig > a').attr('href', WebURL + 'media/artikelen/'+image);
			$('.ProductPreviewBig > a > img').fadeIn(500);
		});
		
		var id = $(this).attr('id');
		var id = id.split('_');
		
		$('#kleurselect_'+id[1]).attr('selected', 'selected');
		
		loadVoorraad();
		
		
		return false;
	});
	
	$('.changeBucket').live('change', function() {
		var val = php_urlencode($(this).val());
		
		var id = $(this).attr('id');
		id = id.split('_');
		
		var field = id[0];
		var MandjeID = id[1];
		
		$.ajax({ 
			url: WebURL + 'jquery/Winkelwagen.php?w=change&MandjeID='+MandjeID+'&field='+field+'&value='+val, 
			success: function(data) {
				$('#WinkelwagenMandje').html(data);
			}
		});
	});
	
	$('.delItem').live('click', function() {
		var id = $(this).attr('id');
		id = id.split('_');
		
		$(this).parent().parent().fadeOut("slow");
		
		$.ajax({
			url: WebURL + 'jquery/Winkelwagen.php?w=del&value='+id[1],
			success: function(data) {
				$('#WinkelwagenMandje').html(data);

				$.ajax({
					url: WebURL + 'jquery/Winkelwagen.php?w=isempty',
					success: function(data) {
						if(data == '1') {
							$('.Kassa').toggleClass('Hidden');
						}
					}
				});
			}
		});
	}).live('mouseover', function() {
		$(this).css('cursor', 'pointer');
	});
	
	$('.Home').live('click', function() {
		location.href=WebURL;
		
		return false;
	});
	
	$('.Kassa').live('click', function() {
		location.href=WebURL + 'Kassa';
	});
	
	$('.Winkelwagen').live('click', function() {
		location.href=WebURL + 'Winkelwagen';
	});
	
	$('.Idealbetalen').live('click', function() {
		location.href=WebURL + 'Kassa/IDEAL';
	});
	
	$('.Rembours').live('click', function() {
		location.href=WebURL + 'Kassa/Rembours';
	});
	
	$('.ideal').live('change', function() {
		var value = $(this).val();
		
		if(value != 'kies') {
			var id = $(this).attr('id');
			var split = id.split('_');
			var id = split[1];
			
			$.ajax({
				url: WebURL + 'jquery/Winkelwagen.php?w=ideal&id='+id+'&value='+value, 
				success: function(data) {
					if(data == '-1') {
						alert('Er is een fout opgetreden met IDEAL. Probeer het later opniew.');
					}
					else {
						
						var js = "location.href='"+data+"';";
						var newClick = new Function(js);
						
						$('#ideal_button').attr('onclick', '').click(newClick);
						$('#ideal_button').css('display', '');
					}
				}
			});			
				 
		}
	});
});

$('.NotActive').live('focus', function() {
	$('.AnderAdres').trigger('click');
});

$('input[name="aflevering"]').live('change', function() {
	if($(this + ':checked').val() == 1) {
		var table = $('.TableAnders');
		$('.TableAnders').addClass('TableNotActive');
		$('.stdAdres').removeClass('stdAdresNotActive');
		$('.stdAfhalen').addClass('stdAfhalenNotActive');
		$('input', table).addClass('NotActive');
	}
	else if($(this + ':checked').val() == 2) {
		var table = $('.TableAnders');
		$('.TableAnders').addClass('TableNotActive');
		$('.stdAdres').addClass('stdAdresNotActive');
		$('.stdAfhalen').removeClass('stdAfhalenNotActive');
		$('input', table).addClass('NotActive');
	}
	else if($(this + ':checked').val() == 3) {
		var table = $('.TableAnders');
		$('.TableAnders').removeClass('TableNotActive');
		$('.stdAdres').addClass('stdAdresNotActive');
		$('.stdAfhalen').addClass('stdAfhalenNotActive');
		$('input', table).removeClass('NotActive');
	}
	
});

function loadVoorraad() {
	var maat = $('select[name="maat"]').val();
	var kleur = $('select[name="kleur"]').val();
	var product = $('input[name="productid"]').val();
	
	if(typeof(maat) == 'undefined') { maat = 'undefined'; }
	if(typeof(kleur) == 'undefined') { kleur = 'undefined'; }
	
	maat = $.base64.encode(maat);
	kleur = $.base64.encode(kleur);
	
	$.ajax({
		url: WebURL + 'jquery/Product.php?w=voorraad&product='+product+'&kleur='+kleur+'&maat='+maat,
		success: function(data) {
			$('#voorraad_container').html(data);
		}
	});
}
function closeallmenus() {
	$('.hasChilds').each(function(index) {
		$('+ ul', this).css('display', 'none');
		$('img', this).attr('src', WebURL + 'images/arrow_down_grey.gif');
		$(this).css('color', '#545454');
	});
}

function voegtoe(id) {
	var kleur = $('select[name="kleur"]').val();
	var maat = $('select[name="maat"]').val();
	
	$.ajax ({
		url: WebURL + 'jquery/Winkelwagen.php?w=add&value='+id+'&kleur='+php_urlencode(kleur)+'&maat='+php_urlencode(maat),
		success: function() {
			location.href = WebURL + 'Winkelwagen';
		}
	});
}

function ToonMelding(Pagina){
	$('#DivAlertBoxAchtergrond').css({ height : $(document).height(), width : $(document).width() });
	$('#DivAlertBoxAchtergrond').fadeIn(200);
	
	var NieuweBreedteDiv = 600;
	var NieuweHoogteDiv = 200;
	
	var TopPos = ($(window).scrollTop() + 60);
	
	TopPositie = TopPos+'px';
	document.getElementById('DivAlertBoxContent').style.top = TopPositie;
	
	var LinkerPositie = (($(document).width() - NieuweBreedteDiv)/2);
	LinkerPositie = LinkerPositie+'px';
	document.getElementById('DivAlertBoxContent').style.left = LinkerPositie;
	
	LaadMeldingInDiv(Pagina, 'DivAlertBoxContent');
}

function LaadMeldingInDiv(option) {
	var urls = option;
	
	$.get(option, function(data){
		document.getElementById('DivAlertBoxContent').innerHTML = data;
		$('#DivAlertBoxAchtergrond').css({ backgroundColor: '#000', opacity: '0.7' }).fadeIn(200);
		$('#DivAlertBoxContent').fadeIn(300,function(){
		});	
	});
}

function MeldingSluiten(Pagina){
	$('#DivAlertBoxContent').fadeOut(150);
	$('#DivAlertBoxAchtergrond').fadeOut(150);
}

function AanmeldenNieuwsbrief(){
	if(document.getElementById('EmailAdresNieuwsbrief').value == ''){
		document.getElementById('EmailAdresNieuwsbrief').style.backgroundColor = '#cc0000';
	} else {
		if(document.getElementById('NieuwsbriefAanmelden').checked == true){
			var AanAfmelden = '1';
		} else {
			var AanAfmelden = '2';
		}
		$.post(URLPref + 'nieuwsbrief.php', { 'Email': document.getElementById('EmailAdresNieuwsbrief').value, 'GroepID' : document.getElementById('NieuwsbriefGebruikersGroepID').value, 'AanAfmelden' : AanAfmelden }, function(data){
			document.getElementById('DivNieuwsbrief').innerHTML = data;
		});
	}
}

function php_urlencode (str) {
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g,
	function (s) {
	switch (s) {
	case "*": s = "%2A"; break;
	case "+": s = "%2B"; break;
	case "/": s = "%2F"; break;
	case "@": s = "%40"; break;
	case "%20": s = "+"; break;
	}
	return s;
	}
	);
	}

function str_replace (search, replace, subject, count) {
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = r instanceof Array,
        sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function checkGegevens() {
	var bDoorgaan = true;
	
	if($('#naam').val() == '') {
		$('#naam_response').html('<font color="#ff0000">verplicht</font>');
		$('#naam').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		$('#naam_response').html('');
		$('#naam').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	if($('#adres').val() == '') {
		$('#adres_response').html('<font color="#ff0000">verplicht</font>');
		$('#adres').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		$('#adres_response').html('');
		$('#adres').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	if($('#postcode').val() == '') {
		$('#postcode_plaats_response').html('<font color="#ff0000">verplicht</font>');
		$('#postcode').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		$('#postcode_plaats_response').html('');
		$('#postcode').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	if($('#plaats').val() == '') {
		if($('#postcode_plaats_response').html() == '') {
			$('#postcode_plaats_response').html('<font color="#ff0000">verplicht</font>');
		}
		else {
			$('#postcode_plaats_response').html('<font color="#ff0000">verplicht</font>');
		}
		
		$('#plaats').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		if($('#postcode_plaats_response').html() == '') {
			$('#postcode_plaats_response').html('');
		}
		$('#plaats').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	
	if($('#telefoon').val() == '') {
		$('#telefoon_response').html('<font color="#ff0000">verplicht</font>');
		$('#telefoon').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		$('#telefoon_response').html('');
		$('#telefoon').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	if($('#email').val() == '' || validateemail($('#email').val()) == false) {
		if($('#email').val() == '') {
			$('#email_response').html('<font color="#ff0000">verplicht</font>');
		}
		else {
			$('#email_response').html('<font color="#ff0000">Email ongeldig</font>');
		}
		$('#email').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
		bDoorgaan = false;
	} else {
		$('#email_response').html('');
		$('#email').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
	}
	
	if(bDoorgaan == true) {
		document.mijn_gegevens.submit();
	}
}

function checkAfleverGegevens() {
	var bDoorgaan = true;
	var type = $('input[name="aflevering"]:checked').val();
	
	if(type == 3) {
		if($('#TAV').val() == '') {
			$('#TAV_response').html('<font color="#ff0000">verplicht</font>');
			$('#TAV').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
			bDoorgaan = false;
		} else {
			$('#TAV_response').html('');
			$('#TAV').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
		}
		
		if($('#adres').val() == '') {
			$('#adres_response').html('<font color="#ff0000">verplicht</font>');
			$('#adres').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
			bDoorgaan = false;
		} else {
			$('#adres_response').html('');
			$('#adres').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
		}
		
		if($('#postcode').val() == '') {
			$('#postcode_response').html('<font color="#ff0000">verplicht</font>');
			$('#postcode').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
			bDoorgaan = false;
		} else {
			$('#postcode_response').html('');
			$('#postcode').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
		}
		
		if($('#plaats').val() == '') {
			if($('#plaats_response').html() == '') {
				$('#plaats_response').html('<font color="#ff0000">verplicht</font>');
			}
			else {
				$('#plaats_response').html('<font color="#ff0000">verplicht</font>');
			}
			
			$('#plaats').css({'border' : '1px solid #FF0000', 'background' : '#FFAFAF'});
			bDoorgaan = false;
		} 
		else {
			$('#plaats_response').html('');
			$('#plaats').css({'border' : '1px solid #666666', 'background' : '#FFFFFF'});
		}
	}
	
	if(bDoorgaan == true) {
		document.mijn_aflevering.submit();
	}
}

function validateemail(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(email) == false) {
	      return false;
	   }
	}

var directionDisplay;
var directionsService = new google.maps.DirectionsService();

function initialize() {
  var latlng = new google.maps.LatLng(52.640416,5.059883);
  directionsDisplay = new google.maps.DirectionsRenderer();
  var myOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("directionsPanel"));
  var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
    title:"Babyplus"
  }); 
}

function calcRoute() {
  var start = document.getElementById("routeStart").value;
  var end = "52.640416,5.059883";
  var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
 
  return false;
}
