function adjustStyle(width) {
    width = parseInt(width);
    if (width < 1229) {
        $(".sheetWidth").attr("href", "/css/style_1000.css");
    } else if (width >= 1230) {
        $(".sheetWidth").attr("href", "/css/style_1250.css");
    } 
}

function klapMenuIn(e){
	if($(e).parent().is('.active')) {
		$('ul',$(e).parent()).css({display:'none'}); 
		$(e).parent().removeClass('active');
	}else{
		$('ul',$(e).parent()).css({display:'block'}); 
		$(e).parent().addClass('active');
	}
}

function setButtons(){
	/*Hover for the first A element of a block*/
	$('.block > h3').hoverParent({cl:'blockHover'});
	$('.block > a').hoverParent({cl:'blockHover'});
	$('.po_producten a').hoverParent({cl:'productHover'});
	
	/*set onclick function inputs*/
	$('.erase').each(function(index) {
		$(this).addClass('inputColor');
		$(this).bind('focus', function() {
		  	eraseInput($(this).val(),this);
		});
	});
}


/****Maakt input formulier leeg bij click****/
function eraseInput(check,e){
	if(check==$(e).val()){
		$(e).val('');
		$(e).removeClass('inputColor');
		$(e).unbind('click');
	}
}


/****	JQUERY FUNCTIONS WRITTEN BY STUDIOBORGER							****/
/****	It is not allowed to copy or use those functions without permission	****/

jQuery.fn.hoverParent = function(options) {
   	var s = jQuery.extend({
     	cl: false
   	}, options);
	if(s.cl!=false){
		$(this).hover(
			function () {
				$(this).parent().addClass(s.cl);
			}, 
			function () {
				$(this).parent().removeClass(s.cl);
			}
		);
	}
}


jQuery.fn.sideSlide = function(options) {
   	var s = jQuery.extend({
     	container: "#container", 
		margin: 10,
		zindex:999
   	}, options);
	
	//Bevat container waarbinnen het object zich mag bewegen
	var container = {
		name:s.container,
		pos:$(s.container).position()
	};
	
	//Bevat het te bewegen object
	var object = {
		name:this,
		pos:this.offset()
	};
			
		//IE8.0 fix
		if(object.pos!==null && object.pos.top==0){
			object.pos.top = this.offset().top;
		}

	if($(window).width()>1215 && object.pos!==null){
		moveObject();
	}
	
	$(window).bind('scroll resize', function() {
		if($(window).width()>1215 && object.pos!==null){
			if(object.pos.top==0){
				object.pos = $(object.name).position();
			}
			moveObject();
		}
	});
	
	function moveObject(){
			var sP 	= $(window).scrollTop();
			var oby = ($(object.name).height()+s.margin);
			var cby = ($(container.name).height()+container.pos.top);
			
			if(sP>=(object.pos.top-s.margin) && $(window).height()>oby){
				//Als object niet meer zichtbaar is vanaf begin, dan zet hem vast
				if(cby > (sP+s.margin+oby)){
					//Als object niet via onderkant uit container behoud positie
					cssTop = s.margin;
				}else{
					//Als object via onderkant uit container berekenen nieuwe positie
					cssTop = (s.margin-(sP+s.margin+oby-cby));
				}
				//Hoogte van bovenliggend(parent) object instellen zodat er geen verspringen zijn
				$(object.name).parent().css({'min-height':$(object.name).parent().height()});
				//Object vast zetten
				$(object.name).css({position:'fixed',top:cssTop, 'z-index':s.zindex});
			}else{
				//Als object zichtbaar is vanuit begin positie dit zo laten
				$(object.name).css({position:'static'});
			}
	}
 };
 
 
 //Popup for short information
 jQuery.fn.cloudPopup = function() {
	function cloudOver(){
		$('section',this).clearQueue();
		$('.kolomRight').clearQueue();
		$('.cloudLeft').clearQueue();
		$('section',this).css({top:$(this).offset().top+'px'});
		$('.kolomRight').fadeTo(500,0.4);
		$('.cloudLeft').fadeTo(500,0.4);
		$('h3',this).addClass('hover');
		$('section',this).fadeIn('fast');
	}
	
	function cloudOut(){
			$('section', this).hide();
			$('.kolomRight').fadeTo(500,1);
			$('.cloudLeft').fadeTo(500,1);
			$('h3',this).removeClass('hover');
		}
	$(this).hoverIntent({
						over:cloudOver,
						sensitivity:10,
						out:cloudOut});
 }
 
 /*end jquery functions*/

function loadProductOverzicht(table, type, letter){
	/*AJAX Request*/
	$.post("/pages/ajax/productOverzichtNav.php", { //Bij succes herlaad het assortiment
		  'table':table,
		  'type':type,
		  'letter':letter
	},
	function(data){
		$('.product_overzicht_nav').html(data);
	});
}


function cart(type, id, aantal,e){
	if(type=='add'){
		eHTML = $(e).html();
		eHEIGHT = $(e).height();
		eBACKGROUND = $(e).css('background');
		$(e).html('');
		$(e).css({height:eHEIGHT});
		$(e).append('<img src="/tmpl/ajax-loader.gif" />');
		$(e).css({background:'#F08E00'});
	}
	
	$.post("/js/json/cart.php", { //Bij succes herlaad het assortiment
		  'type':type,
		  'id':id,
		  'aantal':aantal
	},
	function(data){
		switch(type){
			case 'add':
				$('.winkelwagen').html(data);
			//	$('.winkelwagen_'+id).css({background:'#FFCC00', display:'none'});
				$('.winkelwagen_'+id).fadeIn('slow');
				setTimeout(function(){ 
					$('.winkelwagen_'+id).css({background:'#fff'});
				 },1500);
				$(e).html(eHTML);
				$(e).css({background:eBACKGROUND});
				showCartPopup(data)
				updateCartCount();
				setCartFunctions();
				break;
			case 'empty':
				$('.winkelwagen').html(data);
				updateCartCount();
				break;
			case 'delete':
				$('.winkelwagen_'+id).slideUp('fast', function(){
					$('.subtotaal span').html(data);
				});
				updateCartCount();
				break;
			case 'changequantity':
				data = data.split('|');
				$('.winkelwagen_'+id+' input').val(data[0]);
				$('.winkelwagen_'+id+' .prijs').html(data[1]);
				$('.subtotaal span').html(data[3]);
				
				$('.winkelwagen_'+id+' .prijs').css({'font-size':'15px'});
				setTimeout(function(){ 
					$('.winkelwagen_'+id+' .prijs').css({'font-size':'13px'});
				 },1000);
				updateCartCount();
				break;
		}
	});
}

function showCartPopup(data){
	position = $('#cartPopupLoc').offset();
	newData = $(data).find("li:first-child").html();
	$('body').append('<div class="cartPopup" style="left:'+Math.round(position.left)+'px;top:'+Math.round(position.top+28)+'px; display:none;"><span style="font-size:11px; padding-bottom:10px; color:#009900;"><img src="/tmpl/op_voorraad.png" style="margin-bottom:-5px; margin-right:5px;" />Product succesvol toegevoegd</span><br/>'+newData+'</div>');
	$('.cartPopup input').attr('disabled','disabled');
	$.scrollTo(0,500);
	$('.cartPopup').slideDown(500)
	$('.cartPopup').delay(5000).slideUp(500, function(){$(this).remove();});
}


function updateCartCount(){
	$.post("/js/json/cart.php", {
		'type':'prijs'
	},
	function(data){
		$('.winkelwagenAantal').html(data);
	});
}

var delay = 250;
var isLoading = false;
var isDirty = false;


function setCartFunctions(){
	$('.winkelwagen input.aantal').bind('keyup', function(){
		isDirty = true;
		var vid = $(this).attr('id');
      	reloadSearch(vid);
	});
}


function reloadSearch(vid) { //Winkelwagen aantallen aanpassen
  if(!isLoading){
	  	id = vid.replace('aantal_','');
		aantal = $('#'+vid).val();
       if (aantal.length >= 1) {
          isLoading = true;
			cart('changequantity',id,aantal);
           // enforce the delay
           setTimeout(function(){
             isLoading=false;
             if(isDirty){
               isDirty = false;
               reloadSearch(vid);
             }
           }, delay);
       }
     }
}


function checkboxShow(id){
	if($(id).attr('checked')){
		$(id+"_weergave").css({display:"block"});
	}else{
		$(id+"_weergave").css({display:"none"});
	}
}


//Functie om bij 'afrekenen' meer opties te tonen
function show(id,e){
	$(e).css({display:"none"});
	$(id).css({display:"block"});
}

function hide(id,e){
	$(id).css({display:"none"});
}

//Functie om bij 'afrekenen' de betaal methode bij belgie aan te passen
function activateSendMethod(e,x){
	if(x===true){
		land = e;
	}else{
		land = $(e).val();
	}
	if(land=='Nederland'){
		zone = 1;
	}else if(land=='Belgie' || land=='Duitsland'){
		zone = 2;
	}else{
		zone = 3;
	}
	
	$.post("/js/json/cart.php", {
		'type':'verzendkosten',
		'landcode':zone
	},
	function(data){
		$('.verzendkosten').html('&euro; '+data);
		$('.verzendkosten').css({'font-size':'15px'});
		setTimeout(function(){ 
			$('.verzendkosten').css({'font-size':'12px'});
		 },1000);
		
		ww_totaal();
	});
}

function activatePaymentMethod(id){
	$('#method_'+id).attr('checked', true);
	subtotaal = $('.subtotal').html();
	subtotaal = convertPrice(subtotaal);
	paymethod = $('input:radio[name="afrekenen[pay_method]"]:checked').val();
	if(paymethod==null){
		paymethod = $('input:radio[name="herafrekenen[pay_method]"]:checked').val();
	}
	
	$.post("/js/json/cart.php", {
		'type':'betaalkosten',
		'subtotal':subtotaal,
		'id':paymethod
	},
	function(data){
		if(id==1){
			show('#eenmaligemachtiging');
		}else{
			hide('#eenmaligemachtiging');
		}
		$('.betaalwijzekosten').html('&euro; '+data);
		$('.betaalwijzekosten').css({'font-size':'15px'});
		setTimeout(function(){ 
			$('.betaalwijzekosten').css({'font-size':'12px'});
		 },1000);
		
		ww_totaal();
	});
}

function ww_totaal(){
	verzendkosten = $('.verzendkosten').html();
	verzendkosten = convertPrice(verzendkosten);
	betaalkosten = $('.betaalwijzekosten').html();
	betaalkosten = convertPrice(betaalkosten);
	extra = (verzendkosten+betaalkosten);
	
		subtotaal = $('.subtotal').html();
		subtotaal = convertPrice(subtotaal);
		
		totaal = ((subtotaal+extra)/100).toFixed(2).replace('.',',');
		$('#ww_totaal').html(totaal);
}

function convertPrice(price){
	return parseFloat(price.replace('-','00').replace(/[^0-9]/g, ''));
}

function activateRadioItem(id){
	$(id).attr('checked', true);
}



