$(document).ready(function() {
	// Pop-up	
	$('.bubbleInfo').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $('.trigger', this);
            var info = $('.popup', this).css('opacity', 0);
			//var triggerpos = trigger.offset();

            $([trigger.get(0), info.get(0)]).mouseover(function () {
				console.log(beingShown);
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    hideDelayTimer = setTimeout(function () {
						beingShown = true;
						hideDelayTimer = null;
						//alert(info.position(), trigger.position());
						info.css({
							top: -(info.height()-20),
							left: -(Math.abs((info.width()/2)-(trigger.width()*1.15))),
							display: 'block'
						  }).animate({
							top: '-=' + distance + 'px',
							opacity: 1
						}, time, 'swing', function() {
							beingShown = false;
							shown = true;
						});
					}, 500);
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, 250);

                return false;
            });
        });
	
	
	//right page banner
	var pagepos = $("#divFullBody").offset();
	
	$("#fixedbanner").offset({ left : (pagepos.left+1179) });
	$("#fixedbanner").css({ display : 'block' });
	$(window).bind('resize',function() {
	  	pagepos = $("#divFullBody").offset();
		$("#fixedbanner").offset({ left : (pagepos.left+1179) });
	});
		
	//Set default open/close settings
	$('#product_specs_right .acc_container').show(); //Hide/close all containers
	$('#product_specs_left .acc_container').show(); //Hide/close all containers
	$('.functie_categorie .acc_container').hide();
	//On Click
	$('.acc_trigger').click(function(){
		if( $(this).hasClass('active') ) { //If immediate next container is closed...
			$(this).toggleClass('active').next().slideUp(); //Add "active" state to clicked trigger and slide down the immediate next container
			$(this).children().children().children().children().children('.section_arrow').html('&#9668;');
		} else {
			$(this).toggleClass('active').next().slideDown();
			$(this).children().children().children().children().children('.section_arrow').html('&#9660;');
		}
		return false; //Prevent the browser jump to the link anchor
	});
	
	$('.comp_spec_container').show(); //Hide/close all containers
	//On Click
	
	$('.comp_spec_trigger').click(function(){
		if( $(this).hasClass('active') ) { //If immediate next container is closed...
			$(this).toggleClass('active').next().slideUp(); //Add "active" state to clicked trigger and slide down the immediate next container
			$(this).children().children().children('.section_arrow').html('&#9668;');
		} else {
			$(this).toggleClass('active').next().slideDown();
			$(this).children().children().children('.section_arrow').html('&#9660;');
		}
		//return false; //Prevent the browser jump to the link anchor
	});
	
	$('.uitklappen_specs a').click(function(){
		if( $(this).hasClass('active') ) {
			$('.acc_trigger').toggleClass('active').next().slideUp();
			$('.comp_spec_trigger').toggleClass('active').next().slideUp();
			$('.acc_trigger').children().children().children().children().children('.section_arrow').html('&#9668;');
			$('.comp_spec_trigger').children().children().children('.section_arrow').html('&#9668;');
			$(this).html('Alles uitklappen');
		} else {
			$('.acc_trigger').toggleClass('active').next().slideDown();
			$('.comp_spec_trigger').toggleClass('active').next().slideDown();
			$('.acc_trigger').children().children().children().children().children('.section_arrow').html('&#9660;');
			$('.comp_spec_trigger').children().children().children('.section_arrow').html('&#9660;');
			$(this).html('Alles inklappen');
		}
		$(this).toggleClass('active');
		return false; //Prevent the browser jump to the link anchor
	});
	
	$('.inputtel').keydown(function(event){		
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 ) {
            // let it happen, don't do anything
			if($(this).val().length == 1) {
				$('font').remove();
				$('#frmUser input[type="submit"]').removeAttr('disabled');
			} else {
				$('font').remove();
				$('.functie_mijn_gegevens_content').prepend('<font class="info telefoon">Een telefoonnummer bestaat uit 10 cijfers</font>');
				$('#frmUser input[type="submit"]').attr('disabled','disabled');  
			}
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }
			if($(this).val().length == 9){
				$('font').remove();
				$('#frmUser input[type="submit"]').removeAttr('disabled');
			} else {
				$('font').remove();
				$('.functie_mijn_gegevens_content').prepend('<font class="info telefoon">Een telefoonnummer bestaat uit 10 cijfers</font>');
				$('#frmUser input[type="submit"]').attr('disabled','disabled');  
			}
        }
	});
	
	$('.inputzip').keyup(function(){
		if($(this).val().length == 6){
			var info;
			$(this).parent().append('<img class="loading" src="http://www.prijskiezer.nl/images/loading3.gif">')
			$.get('postcode_validatie.php?cijfers='+$(this).val().substring(0, 4)+'&letters='+$(this).val().substring(4, 6), function(data){
				info = data.split(',');
				$('.loading').remove();
				if(info.length == 3){
					$('input#city').val(info[0]);
					$('input#address').val(info[1]);
					$('input#state').val(info[2]);
					$('font.postcode').remove();
					$('#frmUser input[type="submit"]').removeAttr('disabled');
				} else {
					$('input#city').val(' ');
					$('input#address').val(' ');
					$('input#state').val(' ');
					$('font').remove();
					$('.functie_mijn_gegevens_content').prepend('<font class="info postcode">Ongeldige postcode</font>');
					$('#frmUser input[type="submit"]').attr('disabled','disabled');
				}
			});
		} else if ($(this).val().length == 0){
			$('input#city').val(' ');
			$('input#address').val(' ');
			$('input#state').val(' ');
			$('font').remove();
			$('#frmUser input[type="submit"]').removeAttr('disabled');
		} else {
			$('#frmUser input[type="submit"]').attr('disabled','disabled');
			$('input#city').val(' ');
			$('input#address').val(' ');
			$('input#state').val(' ');
			$('font').remove();
			$('.functie_mijn_gegevens_content').prepend('<font class="info postcode">Ongeldige postcode</font>');
		}
	});
});

