$('document').ready(function(){
    externalLinks();
		$('.img_promo:hidden').css('opacity', 0);
	// Hide M$ Outline border
	$('a').focus(function(){
	     $(this).attr("hideFocus", "hidefocus");
	});
	
	$('.largeMenuItem a').hover(function(){
		var btn_id = $(this).parent().attr('id');
		$('#' + btn_id + ' .btn').stop();
		$('#' + btn_id + ' .btn').fadeTo(250, 0.65);
	}, function(){
		var btn_id = $(this).parent().attr('id');
		$('#' + btn_id + ' .btn').stop();
		$('#' + btn_id + ' .btn').fadeTo(350, 1);
	});
	$('#largeMenu a.btn img').hover(function(){
		$(this).stop().fadeTo(250, 0.65);
	}, function() {
		$(this).stop().fadeTo(350, 1);
	});
	
	// highlight first input field
	$('input[type=text]:first').not('label').focus();
});

showMVO = function(sType, x, y) {
	if ($('#mvo_info').length == 0) $('#content').before("<div id=\"mvo_info\"></div>");
	
	// check if we've got some HTML
	if (aMvoInfo[sType])
	html = aMvoInfo[sType];
	
	// position the info div and show
	x = $('#mvo_bar').position().left + x;
	y = $('#mvo_bar').position().top + y;
	$('#mvo_info').css('left', x).css('top', y).html(html).fadeIn(400).mouseleave(function(){
		$(this).hide();
	});
	
}

updating = false;
updateKoffieKiezer = function() {
	if (updating) return;
	
	updating = true;
	
	$('#results .coffee, #results .err').hide();
	
	var ktype = $('#KoffieType').val();
	var ksoort = $('#KoffieSoort').val();
	var kracht = (($("#slider").slider("value")/100)+1);
			kracht = kracht > 0 ? kracht : 4;
	
	var srt = '';
	switch (ksoort) {
		case "1":
			srt = 'creme';
			break;
		case "2":
			srt = 'espresso';
			break;
		case "4":
			srt = 'ristretto';
			break;
		default:
			srt = 'cappuccino';
			break;
	}

	// loop through 'koffie' and check if it fits search
	$('#results .coffee').each(function(){
		if ( $(this).find('.ktype').val() == ktype && $(this).find('.k'+srt).val() == '1' && $(this).find('.kkracht').val() == kracht )
		$(this).fadeIn(250);
	});

	// check if results are empty
	srt = '';
	kracht = '';
	if ($('#results .coffee:visible').length == 0) {
		// show err
		$('#results .err').fadeIn(250);
		
		$('#results .coffee').each(function(){

			if (srt != '' && $(this).find('.k'+srt).val() != '1') return true;
			if (kracht > 0 && $(this).find('.kkracht').val() != kracht)	return true;
			
			if ( $(this).find('.ktype').val() == ktype) {
				// check for type and set select
				if ($(this).find('.kcreme').val()) {
					$('#KoffieSoort').val(1);
					srt = 'creme';
				} else if ($(this).find('.kespresso').val()) {
					$('#KoffieSoort').val(2);
					srt = 'espresso';
				} else if ($(this).find('.kristretto').val()) {
					$('#KoffieSoort').val(4);
					srt = 'ristretto';
				} else if ($(this).find('.kcappuccino').val()) {
					srt = 'cappuccino';
					$('#KoffieSoort').val(3);
				}
				
				// set strength
				kracht = $(this).find('.kkracht').val();
				$('#slider').slider('value', (kracht-1)*100);
				
				
				// show it
				$(this).fadeIn(250);
			}
			
			
		});
	}
	updating = false;
}

addToCart = function(soort, id, verpakking, amount, doelgroep) {
	if ($('#cart').length == 0)
	$('#left').append('<div id=\"cart\" style=\"display: none;\"></div>');
			
	$('#cart').load('/inc/cart.ajax.php', {'soort':soort, 'id':id, 'verpakking':verpakking, 'amount':amount, 'd':parseInt(doelgroep)}, function(){
		$(this).fadeIn(250, function(){
			$(this).fadeOut(100, function(){
				$(this).fadeIn(250);
			});
		});
		hideDetail();
	});
}

popup = function(tpl){
	if ($('#bg').length == 0)
	$("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="popup"><div class=\"tpl\"></div></div>');
	$('#popup .tpl').load('/inc/tpl.ajax.php', {'tpl': tpl} ,function(){
		$('#popup').css('textAlign', 'left');
		$('#bg').css('left',0).css('top',0).click(hideDetail);

		// hide all the selects

		$('#popup').css('left', ($(document).width()/2-$('#popup').width()/2) + 'px');
		$('#popup').css('top', "100px");
		$('#popup').css('opacity', 0).show();

		$('#popup').height(400);
		$('#popup .tpl').height(400);

		$('a.close').css('left',($(document).width()/2-$('#popup').width()/2+$('#popup').width()+25)).css('top','87px').click(hideDetail);

		// re-set opacity so IE works
		$('#bg').css('opacity', 0.5);
		$('#bg, a.close').fadeIn(250);
		$('#popup').fadeTo(250, 1);
		
	});	
}
	
showDetail = function(id){
	html = $('#spec_'+id).html();
		
	if ($('#bg').length == 0)
	$("#content").before('<div id="bg"></div><a href="javascript:;" class="close pnghack"></a><div id="popup"></div>');
	$('#popup').html(html);
	$('#popup .content').append($('#overview_'+id+' .order').html());
	$('#popup').css('textAlign', 'left');
	$('#bg').css('left',0).css('top',0).click(hideDetail);

	$('#popup').css('left', ($(document).width()/2-$('#popup').width()/2) + 'px');
	$('#popup').css('top', "100px");
	$('#popup').css('opacity', 0).show();
	$('#main :input').hide();
	if ($('#popup').height() > 400) {
		$('#popup').height(400);
		$('#popup .content .txt').height(350);
	} else if ($('#popup .content .txt').height() < 110) {
		$('#popup .content .txt').height(110);
	}

	$('a.close').css('left',($(document).width()/2-$('#popup').width()/2+$('#popup').width()+25)).css('top','87px').click(hideDetail);

	// re-set opacity so IE works
	$('#bg').css('opacity', 0.5);
	$('#bg, a.close').fadeIn(250);
	$('#popup').fadeTo(250, 1);
}
hideDetail = function() {
	$('#main :input').show();
	$('#bg, #popup, a.close').fadeOut(150, function(){$(this).remove()});
}
toggleDelAddr = function(cb){
	if($(cb).attr('id') == 'del_new' || ($(cb).attr('id') == 'difdeladdr' && $(cb).is(':checked'))){
		$('#newAddress').fadeIn();
	} else {
		$('#newAddress').fadeOut();
	}
}

validate = function( id ) {
	var err = false;

	$('#'+id+' :input').each(function(){
		$(this).removeClass('ferr');
		if ($(this).attr('type') != 'hidden' && $(this).attr('rel') == 'req') {
			if ($(this).attr('name').toLowerCase().indexOf('email') >= 0) {
				var reg = new RegExp("^[0-9a-zA-Z\-\_\.]+@[0-9a-zA-Z\-]+[\.]{1}[0-9a-zA-Z\-]+[\.]?[0-9a-zA-Z]+$");
				if(!reg.test($(this).val())) {
					$(this).addClass('ferr');
					err = true;
				}
			} else if ($(this).val().length == 0) {
				$(this).addClass('ferr');
				err = true;
			}
		}
	});
	$('#'+id+' :input.ferr:first').focus();
	
	
	return !err;
}

updateStatus = function(id, status) {
	if (confirm('Weet u zeker dat u de status wilt veranderen?\n\nLET OP: ER WORDT DAN OOK EEN E-MAIL NAAR DE KLANT GESTUURD!')) {
	
		$.post('/inc/webshop_admin.ajax.php', {'id': id, 'status':status}, function(data, textStatus) {
			if (textStatus != 'success') {
				alert('Status voor bestelling '+id+' kon niet worden aangepast, probeer opnieuw.');
			} else {
				alert('Status voor bestelling '+id+' is aangepast.');
			}
		});
	} else {
		$('#StatUpdateForm')[0].reset();
	}
}

checkDelivery = function(){
	check = true;
	if($('#del_new').is(':checked'))
		check = validate('address');
	return check;
}

// checks the addresses used when someone orders without creating an account.
checkAddress = function(){
	check = true;
	// validate invoice address
	check = validate('factAddr');

	if($('#difdeladdr').is(':checked')){
		check = validate('newAddress');
	} 
	return check;
}
function setReq(id){
	$('#E-mailadres, #Telefoonnummer, #Adres, #Postcode, #Plaats').attr('rel', '');
	switch(id){
		case 'Telefonisch':
			$('#Telefoonnummer').attr('rel', 'req');
			break;
		case 'Email':
			$('#E-mailadres').attr('rel', 'req');
			break;
		case 'Post':
			$('#Adres, #Postcode, #Plaats').attr('rel', 'req');
			break;

	}
}
function showPromo(id){
	id = id.toString();
	if(id.indexOf('btn_promo_') !== -1){
		// strip stuff
		id = id.substr(10);
	}

	num = id;
	if($('#btn_promo_'+id).length != 0){
		if(!$('#btn_promo_'+id).hasClass('active')){
			$('.promo_button.active').removeClass('active');
			$('#btn_promo_'+id).addClass('active');
			$('.img_promo:visible').stop().animate({opacity: 0}, 500);
			$('#img_promo_'+id).show().stop().animate({opacity: 1}, 400);
		}
	}
}

function scrollPromo(){
	// make sure the opacity is set
	num++;
	if(num > 2) num=0;
	showPromo(num);
	t1 = setTimeout(function(){scrollPromo(num)}, 4000);				
}