// JavaScript Document

function loadSuggestionForm() {
	$('body').prepend('<div id="layer"></div>');
	$('body').prepend('<div id="frame">Bezig met laden van formulier...</div>');
	$('#layer').css({
		position	:	'fixed',
		left		:	'0px',
		top			:	'0px',
		width		:	'100%',
		height		:	'100%',
		background	:	'#fff',
		zIndex		:	'75',
		opacity		:	'0.5'
	});
	$('#frame').css({
		position	:	'fixed',
		left		:	((parseInt($(window).width()) - 510) / 2) + 'px',
		top			:	((parseInt($(window).height()) - 400) / 2) + 'px',
		width		:	'510px',
		height		:	'315px',
		lineHeight	:	'350px',
		background	:	'#fff',
		zIndex		:	'100',
		border		:	'1px #d80d5f solid',
		padding		:	'10px',
		fontSize	:	'16px',
		fontWeight	:	'bold',
		textAlign	:	'center',
		color		:	'#d80d5f',
		overflow	:	'hidden',
		overflowX	:	'hidden'		
	});
	//$('#frame').css({lineHeight:'20px',fontSize:'11px',fontWeight:'normal',textAlign:'left'}).html("hoi");	
	$.ajax({
		type		:	'GET',
		url			:	'/request.php?showSearchSuggestionForm=1',
		success		:	function(response) {
			window.setTimeout(function() {
				$('#frame').css({lineHeight:'20px',fontSize:'11px',fontWeight:'normal',textAlign:'left'}).html(response);				
		   }, 500);
		}
	})
}
 
//Function for determining position of an element on the page
function findElementPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oElement.x, oElement.y ];
	}
}



$(document).ready(function() {
	
	setFilterFunctions();
	setProductOverViewFilterFunctions();
	
	function removeFilters(filterType,reloadProducts){
	
	if(filterType!=="remove_all"){
		$('#'+filterType+'_filter input[type=checkbox]').removeAttr("checked"); 
	}
	
	$.ajax({
			type	:	'GET',
			url		:	'/request.php?remove_category=' + filterType+'&sid='+Math.random(),
			cache	: false,
			success	:	function(response) {		

            if(reloadProducts){   
			var url = getProductCategoryUrl();
				if(url!=window.location.href){
						window.location.href = url;
				}else{			
				//Update filter
				updateFilterPanel(filterType);				
				updateProductOverView();
				}
             }             
			}                            
		});
         
}

function setProductOverViewFilterFunctions(){
		//link filters product overview leeftijdscategorie
	$('a.filterLeeftijdLink').click(function() {
		var linkFilterId = $(this).attr('id');
		var linkFilterValues = linkFilterId.split("-");
		var linkFilterValue = linkFilterValues[1];	
		var linkFilterType = linkFilterValues[0];
		
		//Remove leeftijdscategorie filters
		$.ajax({
			type	:	'GET',
			url		:	'/request.php?remove_category=' + linkFilterType+'&sid='+Math.random(),
			cache	: false,
			success	:	function(response) {	
				filterRequest(linkFilterType, linkFilterValue, true);	
			}
		});		
		
	});		
	
	$('.productoverviewLinkFilter_wissen').click(function() {
					
			var linkFilterId = $(this).attr('id');
			var linkFilterValues = linkFilterId.split("-");
			var linkFilterValue = linkFilterValues[1];	

			removeFilters(linkFilterValue,true);
		
	});
	}
	
	function setFilterFunctions(){
	$('.filterMenu').click(function() {
		$('.wrapperFilter').each(function() {
			$(this).slideUp();
		});
		if($(this).next().css('display') == 'none') {
			$(this).next().slideDown();
		}
	});
	
	
	
	//Checkbox filters
	$('.wrapperFilter input[type=checkbox]').click(function() {
		filterRequest($(this).attr('class'), $(this).attr('value'), $(this).attr('checked'));	
		
	});
	
	//link filters product detail
	$('.linkfilter a').click(function() {
		var linkFilterId = $(this).attr('id');
		var linkFilterValues = linkFilterId.split("-");
		var linkFilterValue = linkFilterValues[1];	
		
		//remove all current filters
		removeFilters("remove_all",false);	
		
        filterRequest($(this).attr('class'), linkFilterValue, true);	
        
		
	});
	
	
	
	
	
	$('.wissen').click(function() {
		
		removeFilters($(this).attr('id'),true);
		
	});
	
	$('.selectedfilters_wissen').click(function() {
					
			var linkFilterId = $(this).attr('id');
			var linkFilterValues = linkFilterId.split("-");
			var linkFilterValue = linkFilterValues[1];	

			removeFilters(linkFilterValue,true);
		
	});
	
	
}
	
	function filterRequest(type, value, check) {
	var currentProductCategory = document.getElementById("current_product_category").value;
	
	
		$.ajax({
			type	:	'GET',
			cache   : 	false,
			url		:	'/request.php?' + (check ? 'add' : 'remove') + '=' + type + '&value=' + value + '&current_product_category=' + currentProductCategory,
			success	:	function(response) {		
			  //alert('/request.php?' + (check ? 'add' : 'remove') + '=' + type + '&value=' + value + '&current_product_category=' + currentProductCategory);
				//	Show delete 

		
				$('.filterMenuselected.' + type + ' .wissen').show();
				
				var url = getProductCategoryUrl();
				if(url!=window.location.href){
						window.location.href = url;
				}else{
				
				//Update filter
				updateFilterPanel(type);
				updateProductOverView();
				}
				
				
				
				//alert(window.location.href);
			
				
				
			}
		});
	}
	
	function getProductCategoryUrl(){
		var pathName = window.location.pathname;
		var hostName = window.location.hostname;
		var locationHref = window.location.href;
		
		var pathNameParts = pathName.split('/');
		
		if(pathNameParts.length > 3){
			locationHref = 'http://'+ hostName + '/producten/' + pathNameParts[2] + '.html';
			
			//window.location.href = locationHref;
		}		
		return locationHref;
	}
	
	function updateProductOverView(){
	
	var url = getProductCategoryUrl();
	
		$.ajax({
					type	:	'POST',
					url		:	url,
					cache   : 	false,
					data	:	'ajax_request=update_product_overview',
					success	:	function(response) {
					$("#productOverview").html(response);	
					
					$('.selectedfilters_wissen').click(function() {
					
						var linkFilterId = $(this).attr('id');
						var linkFilterValues = linkFilterId.split("-");
						var linkFilterValue = linkFilterValues[1];	
		
						removeFilters(linkFilterValue,true);
		
					});
					
					setProductOverViewFilterFunctions();
						
					
						/*$("#filterHolder").html(response);
						//Update filter panel
						$.ajax({
						type	:	'POST',
						url		:	window.location.href,
						cache   : 	false,
						data	:	'ajax_request=update_product_overview',
						success	:	function(response) {
						$("#productOverview").html(response);						
						
						//setFilterFunctions();
					}
				});*/
					}
				});	
	}
	
	function updateFilterPanel(type){
	
	var url = getProductCategoryUrl();	
	
				$.ajax({
						type	:	'POST',
						url		:	url,
						cache   : 	false,
						data	:	'ajax_request=update_filter_panel&filter_type='+type,
						success	:	function(response) {
						$("#filterHolder").html(response);	

					
						setFilterFunctions();
					}
				});
		
		
	}
	
	
	function applyFilterResult(result) {
		for(i = 0; i < result.length; i++) {
			var fItem	=	result[i].split('>');
			$('.' + fItem[0] + ' span').text(fItem[1]);
			if(parseInt(fItem[1]) == 0) {
				$('.' + fItem[0] + ' input[type=checkbox]').attr('disabled', 'disabled');
				$('.' + fItem[0] + ' input[type=checkbox]').attr('checked', '');
			}
			else {
				$('.' + fItem[0] + ' input[type=checkbox]').attr('disabled', '');
			}
		};
	}
	
	/**
	*	PRODUCT DETAIL
	*/
	
	$('#maat').change(function() {
		var maat = $(this).val();
		$.ajax({
			type	:	'GET',
			url		:	'/request.php?loadMaat=' + maat,
			success	:	function(response) {
				var info 	=	response.split('#');
				//$('#artikelnummer').text(info[1]);
				//$('#voorraad').text(info[0]);
				$('#aantal').html('');
				$('#kleur').html(info[2]);
				
				if(info[3]){
					$('#lengte').text(info[3]);
				}	
				if(info[4]){
					$('#lengte_mouw').text(info[4]);
				}	
				if(info[5]){
					$('#breedte').text(info[5]);
				}					
				
				/*for(i = 1; i <= info[0]; i++) {
					$('#aantal').append('<option>' + i + '</option>');
				} */
                
                if(maat==''){
                    $('#voorraad').text('');
                    $('#kleur').html('');
                     $('#order_button').show();
                }else if(info[0]==0){
				  $('#voorraad').text('Deze maat is uitverkocht');
				  $('#order_button').hide(); 				  
				}else{				
				  $('#order_button').show(); 
				}
			}
		});
	});
	
	$('#kleur').change(function() {
		var product_maat_kleur_id = $(this).val();
        
        if(product_maat_kleur_id!==''){
		    $.ajax({
			    type	:	'GET',
			    url		:	'/request.php?loadVoorraad=' + product_maat_kleur_id,
			    success	:	function(response) {
			    var info = response.split('#');
			    
			    $('#artikelnummer').text(info[1]);			
			    $('#voorraad').text(info[0]);		
			    $('#aantal').html('');			
				    
				    for(i = 1; i <= info[0]; i++) {
					    $('#aantal').append('<option>' + i + '</option>');
				    }
				    
				    if(info[0]==0){
				      $('#voorraad').text('Uitverkocht');
				      $('#order_button').hide(); 				  
				    }else{
				      $('#order_button').show(); 
				    }
			    }
		    });
        }else{
            $('#artikelnummer').text('');            
            $('#voorraad').text('');        
            $('#aantal').html('');  
            $('#order_button').show();          
        }
	});
	
});
	
//	Categorie sorteren
function changeSort(sortType) {
	$.ajax({
		type	:	'GET',
		url		:	'/request.php?productSort=' + sortType,
		success	:	function(response) {
			//location.reload(true);
			window.location = window.location.href;
		}
	});
}
