
function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('fast');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('fast');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('fast');
				checkElement.slideDown('fast');
				return false;
			}
		}
	);
}
$(document).ready(function() {initMenus();});

$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};



$(document).ready(function(){
	$("#s").search();	
	$("#s2").search();	
	$("#contactForm").validate();
	$("#registreerForm").validate();
	$(".bestellen").colorbox({width: 600, opacity:0.7, height:280 });
	$(".colorbox").colorbox({opacity:0.7 });
});


function bestellen( ID ){
	$.ajax({
		type: "POST",
   		url: "/ajax_add_bestelling.php",
   		data: "product_id=" + ID,
   		success: function(msg){
     		
   		}
	});
	return false;
}


var originele_prijs = 0;
$( document ).ready( function(){
	set_prijs();
});

function set_originele_prijs(){
	setTimeout( function(){originele_prijs = $( 'span#product_prijs' ).html();}, 500 );
}

function set_prijs(){
	var aantal = $( '#txtAantal' ).val();

	var voorraad = $( '#txtVoorraad' ).val();
	var threshold = parseInt( $( '#threshold' ).html() );
	var hoeveelheid_prijs = $( '#hoeveelheid_prijs' ).html();
	
	if( threshold == 0 ){
		$( '.grey').html( '' );
	}
	
	if( !isNaN( aantal ) ){
		
		aantal = parseInt( aantal );
		
		
		
		if( aantal >= threshold && threshold != 0 ){
			$( '#product_prijs' ).html( hoeveelheid_prijs );
			$( '.grey').css({'color':'black'});
			$( '.kleurInfo').addClass('kleurActief').removeClass('kleurInfo');
		}else if( aantal < threshold ){
			$( '#product_prijs' ).html( originele_prijs );
			$( '.grey').css({'color':'grey'});
			$( '.kleurActief').addClass('kleurInfo').removeClass('kleurActief');
		}
		
		
		if(voorraad>1){
			if (aantal > voorraad) { $( '#txtAantal' ).val(voorraad); $( '.red' ).css({'color':'red'}); $( '#txtAantal' ).css({'color':'red'});  }
			// if (voorraad > aantal) { $( '#txtAantal' ).css({'color':'black'}); }
		}
		
		
	}
}

function bereken_prijs(){
	$( '#txtAantal' ).css({'color':'black'});
	set_prijs();
	var aantal = parseInt( $( '#txtAantal' ).val() );
	
		
	
	
	var prijs_convert = str_replace( ',', '.', $( '#product_prijs' ).html() );
	var prijs = parseFloat( prijs_convert );

	var totaal_prijs = aantal * prijs;
	totaal_prijs = parseInt( totaal_prijs * 100 ) / 100;
	totaal_prijs = totaal_prijs.toFixed(2);

	
	if( isNaN( totaal_prijs ) ){
		totaal_prijs = 0;
	}
	
	$( '#totaal' ).html( totaal_prijs );
	$( '#totaal' ).css({'font-weight':'bold'});
}

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 = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object 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];
}

