$(function() {	
	
	$(".pngfix").pngfix();
	
	jQuery.httpbase = function( ){
		return $('base')[0].href;
	}
	
	$('#top_zoeken .input').each( function() {
		if(this.value == ''){
			this.value = 'Zoeken';
		}
		
		$(this).blur( function(){
			if(this.value == ''){
				this.value = 'Zoeken';
			}
		})
		
		$(this).focus( function(){
			if(this.value == 'Zoeken'){
				this.value = '';
			}
		})
	});		
	
//	$('#artikeldetail div.productgroep').mouseover( function(){
//		$(this).css('background-color', '#EEEEEE');
//		$(this).css('cursor', 'pointer');
//	}).mouseout( function() {
//		$(this).css('background-color', '');
//	});
	
	// a tag met de class new-window openen in een nieuw venster
	$('a.new-window').click(function(){
	   window.open(this.href);
	   return false;
	});
	
	var curdate = new Date();
	
	/*$.datepicker.setDefaults({
		   showOn: 'both',
		   buttonImageOnly: true,
		   buttonImage: 'img/icons/kalender.gif',
		   buttonText: 'Kalender',
		   dayNames : ['zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag'],
		   dayNamesMin : ['zo','ma','di','wo','do','vr','za'],
		   dayNamesShort : ['zon','maa','din','woe','don','vri','zat'],
		   monthNames : ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],		   
		   monthNamesShort : ['jan','feb','mrt','apr','mei','jun','jul','aug','sept','okt','nov','dec'],
		   nextText : 'volgende',
		   numberOfMonths: 1,
		   prevText : 'vorige',
		   changeMonth: true,
		   changeYear: true,
		   yearRange: curdate.getFullYear() + ':' + curdate.getFullYear() + 1,		   
		   dateFormat: 'dd-mm-yy',
		   hideIfNoPrevNext: true,
		   minDate: '+1d',
		   maxDate: '+1y'
	});*/
	
	// PrettyPhoto (lightbox)
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', 				/* fast/slow/normal */
		padding: 40, 							/* padding for each side of the picture */
		opacity: 0.5, 							/* Value between 0 and 1 */
		showTitle: true, 						/* true/false */
		allowresize: true, 					/* true/false */
		counter_separator_label: ' van de ', 	/* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', 				/* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
	
});

$(document).ready(function(){
	/*PRODUCTDETAILS*/
	var aChilds = $('#artikeldetail .bestel .bestellen td.productgroepen').children();
	var iBreedte = 0;

	// in IE blijft er nog een kleine rest over: netjes opvullen
	iBreedte = 0;
	// bereken de breedte van alle childs
	for (x=0; x<aChilds.length; x++) {
		iBreedte += $(aChilds[x]).width();
	}
	// coreectie als breedte minder dan 301 pixels
	if ((301 - iBreedte) > 0) {
		$(aChilds[aChilds.length - 1]).width(((301 - iBreedte) + $(aChilds[aChilds.length - 1]).width()));
	}
	$('#artikeldetail .bestel .bestellen td.label div.bottom_line').width((301-iBreedte));
	
	$('div.productgroep:first').addClass('productgroep_selected');
	
	$("div.productgroep").mouseover(function(){
		$(this).addClass('productgroep_mouseover');
	})
	$("div.productgroep").mouseout(function(){
		$(this).removeClass('productgroep_mouseover');
	})
	
	$("div.productgroep").click(function(){
		$('div.productgroep').each(function(){
			$(this).removeClass('productgroep_selected');
		});
		$(this).addClass('productgroep_selected');
	})

	$("div.imgs_small img.prod_img_small").addClass('opac');
	$("div.imgs_small img.prod_img_small:first").addClass('selected');
	
	$("div.imgs_small div.prod_img_small_cont").mouseover(function(){
		$(this).children("img.prod_img_small").removeClass('opac');
	});

	$("div.imgs_small div.prod_img_small_cont").mouseout(function(){
		$(this).children("img.prod_img_small").addClass('opac');
	});

	$("div.imgs_small div.prod_img_small_cont").click(function(){
		$(this).siblings("div.prod_img_small_cont").each(function(){
			if($(this).children('img.prod_img_small').hasClass('selected')){
				$(this).children('img.prod_img_small').removeClass('selected');
			}
		})
		//$("div.imgs_small img.prod_img_small").removeClass('selected');
		$(this).children("img.prod_img_small").addClass('selected');
		$("img#img_big").attr('src', $(this).children("img.prod_img_small").attr('src'));

		width = (screen.width - 400) < 800 ? (screen.width - 400) : 800;
		height = (screen.height - 400) < 800 ? (screen.height - 400) : 800;
		
		test = new RegExp(/\/max\/[0-9]+\/(fixed|max)\/[0-9]+\//g);
		x_max = test.test($(this).children("img.prod_img_small").attr('src'));
					
		test = new RegExp(/\/(fixed|max)\/[0-9]+\/max\/[0-9]+\//g);
		y_max = test.test($(this).children("img.prod_img_small").attr('src'));

		test = new RegExp(/\/img\/[a-z0-9]*\/(max)\/240\/(max)\/240\//g);
		src = test.test($(this).children("img.prod_img_small").attr('src'));

		src = $(this).children("img.prod_img_small").attr('src').replace(/\/(fixed|max)\/[0-9]+\/(fixed|max)\/[0-9]+\//,'/'+(x_max ? 'max' : 'fixed')+'/'+width+'/'+(y_max ? 'max' : 'fixed')+'/'+height+'/');
		$("div.img_big a").attr('href', src);	
	});
	
	/*FAQ*/
	$("div.vraag").click(function(){
		$("div.vraag").each(function(){
			$(this).removeClass("selected");
			$('div.antwoord',this).slideUp();
		});

		if(!$('div.antwoord:visible',this).length){
			$(this).addClass("selected");
			$('div.antwoord',this).slideToggle();
		} 
	});
	
	//$("#bestel").click(function(){
	//	$(this).attr("disabled","disabled");
	//})
});

$.copyAfleveradres = function( form, gelijk ){
	if( gelijk == 'ja' ){
		$('#afleveradres').hide();
		$("input[name='afleveradres_straat']").val($("input[name='factuuradres_straat']").val());
		$("input[name='afleveradres_huisnummer']").val($("input[name='factuuradres_huisnummer']").val());
		$("input[name='afleveradres_huisnummer_tv']").val($("input[name='factuuradres_huisnummer_tv']").val());
		$("input[name='afleveradres_postcode']").val($("input[name='factuuradres_postcode']").val());
		$("input[name='afleveradres_plaats']").val($("input[name='factuuradres_plaats']").val());		
	} else if(gelijk == 'nee' && $('#afleveradres').css('display') == 'none') {
		$('#afleveradres').show();	
		$("input[name='afleveradres_straat']").val('');
		$("input[name='afleveradres_straat']").val('');
		$("input[name='afleveradres_huisnummer']").val('');
		$("input[name='afleveradres_huisnummer_tv']").val('');
		$("input[name='afleveradres_postcode']").val('');
		$("input[name='afleveradres_plaats']").val('');
	} 

}

function IsNumeric(strString)
//  check for valid numeric strings	
{
var strValidChars = "0123456789,.";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
return blnResult;
}


function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}

function setCurrProductGroup(group_id) {
	$('#artikeldetail tbody#tb_productgroep_' + currProductGroupId).each(function(){
		$(this).css('display', 'none');
	});

	$('#artikeldetail tbody#tb_productgroep_' + group_id).each(function(){
		$(this).css('display', '');
		$('.productgroep a').removeClass('product_groep_selected');
		$('.product_groep_'+group_id+' a').addClass('product_groep_selected');
	});
	
	currProductGroupId = group_id;		
}