jQuery(document).ready(function($){
	//$.preventDefault();
    //AJAX
    //$("#productdrp, #technologydrp, #finishdrp, #environmentaldrp, #branddrp").customselect();
	jQuery('body .gloss-accordion-head').click(function(){
	    jQuery(this).find('.textChange').toggleClass('open-hide');
	    jQuery(this).find('.textChange2').toggleClass('close-show');
	    jQuery(this).find('.textChange').toggle();
	    jQuery(this).find('.textChange2').toggle();
	    jQuery(this).find('.arrowBtn').toggleClass('active');    
	    jQuery(this).next('.gloss-accordion-contant').slideToggle();
  	});
  	jQuery('body .letterBoxTop').click(function(){
	    jQuery( this ).parents('.glossLetterBox').toggleClass('active');
	  	jQuery( this ).find('.openChange').toggle();
	  	jQuery( this ).find('.openChange2').toggle();
	   jQuery( this ).next('.letterBoxBtm').slideToggle();
  	});
    jQuery(".custom-select").customselect();
 
	 $('#searchbtn').click(function() {
		$(".ajaxLoader").show();
	});
	$('#resetbtn').click(function(e) {
		$(".ajaxLoader").show();
		e.preventDefault();
		e.stopImmediatePropagation();
		//var optionSelected = $("option:selected", this);
		//var valueSelected = this.value;
		
		var prd = '';
		var env = '';
		var finish = '';
		var tech = '';
		var brd ='';
		$(".ajaxLoader").show();
		$.ajax({
				type: "POST",
				dataType: "json",
				url: product_finder_data.ajaxurl,
				data: {
					action: "findproduct",
					prdval: prd,
					finishval: finish,
					envval: env,
					techval: tech,
					brdval: brd,
					resetval : 1
				}
			})
			.done( function( response ) {
							
				if( prd == null || prd == ''){//product
					//$('#productdrp option').remove();
					//$('.blue-select li').remove();
					$('#productdrp').find('option').remove().end().append('<option value="" selected="">Product Range(All)</option>');
					//$(".blue-select ul").append('<li class="active" data-value="Product Range(All)">Product Range(All)</li>');
					//$('.blue-select a span').html('Product Range(All)');
					//for(var p in response.producttype ) {
					$.each( response.producttype, function( key, value ) {
						//var o = new Option(response.producttype[p], response.producttype[p]);
						//$(".blue-select ul").append('<li class="active" data-value="'+response.producttype[p]+'">'+response.producttype[p]+'</li>');
						//$(o).html(response.producttype[p]);
						$("#productdrp").append('<option value="'+key+'">'+value+'</option>');
					});
					$("#productdrp").customselect();
				}
				
				if( env == null || env == ''){//environment
					//$('#environmentaldrp option').remove();
					//$('.yellow-select li').remove();
					$('#environmentaldrp').find('option').remove().end().append('<option value="" selected="">Environmental(All)</option>');
					//$(".yellow-select ul").append('<li class="active" data-value="Environmental(All)">Environmental(All)</li>');
					//$('.yellow-select a span').html('Environmental(All)');
					for(var e in response.environment ) {
						var o = new Option(response.environment[e], response.environment[e]);
						//$(".yellow-select ul").append('<li class="active" data-value="'+response.environment[e]+'">'+response.environment[e]+'</li>');
						$(o).html(response.environment[e]);
						$("#environmentaldrp").append(o);
					}
					$("#environmentaldrp").customselect();
				}
					
				if( brd == null || brd == ''){//brand
					//$('#branddrp option').remove();
					//$('.purple-select li').remove();
					$('#branddrp').find('option').remove().end().append('<option value="" selected="">Brand(All)</option>');
					//$(".purple-select ul").append('<li class="active" data-value="Brand(All)">Brand(All)</li>');
					//$('.purple-select a span').html('Brand(All)');
					for(var b in response.brand ) {
						var o = new Option(response.brand[b], response.brand[b]);
						//$(".purple-select ul").append('<li class="active" data-value="'+response.brand[b]+'">'+response.brand[b]+'</li>');
						$(o).html(response.brand[b]);
						$("#branddrp").append(o);
					}
					$("#branddrp").customselect();
				}
				
				if( finish == null || finish == ''){//finish
					//$('#finishdrp option:').remove();
					//$('.red-select li').remove();//finish
					$('#finishdrp').find('option').remove().end().append('<option value="" selected="">Finish(All)</option>');
					//$(".red-select ul").append('<li class="active" data-value="Finish(All)">Finish(All)</li>');
					//$('.red-select a span').html('Finish(All)');
					for(var f in response.finish ) {	
						var o = new Option(response.finish[f], response.finish[f]);
						//$(".red-select ul").append('<li class="active" data-value="'+response.finish[f]+'">'+response.finish[f]+'</li>');
						$(o).html(response.finish[f]);
						$("#finishdrp").append(o);
					}
					$("#finishdrp").customselect();
				}
				
				if( tech == null || tech == ''){//technology
					//$('#technologydrp option:gt(0)').remove();
					//$('.green-select li').remove();//technology
					$('#technologydrp').find('option').remove().end().append('<option value="" selected="">Technology(All)</option>');
					//$(".green-select ul").append('<li class="active" data-value="Technology(All)">Technology(All)</li>');
					//$('.green-select a span').html('Technology(All)');
					for(var t in response.technology ) {
						
						var o = new Option(response.technology[t], response.technology[t]);
						//$(".green-select ul").append('<li class="active" data-value="'+response.technology[t]+'">'+response.technology[t]+'</li>');
						$(o).html(response.technology[t]);
						$("#technologydrp").append(o);
					}
					
					$("#technologydrp").customselect();
				}
				
				$(".ajaxLoader").hide();
			});
	});
	$('select').on('change', function (e) {
		
	   	e.preventDefault();
		e.stopImmediatePropagation();
		//var optionSelected = $("option:selected", this);
		//var valueSelected = this.value;
		
		var prd = $('#productdrp').val();
		var env = $('#environmentaldrp').val();
		var finish = $('#finishdrp').val();
		var tech = $('#technologydrp').val();
		var brd = $('#branddrp').val();
		$(".ajaxLoader").show();
		$.ajax({
				type: "POST",
				dataType: "json",
				url: product_finder_data.ajaxurl,
				data: {
					action: "findproduct",
					prdval: prd,
					finishval: finish,
					envval: env,
					techval: tech,
					brdval: brd
				}
			})
			.done( function( response ) {
							
				if( prd == null || prd == ''){//product
					$('#productdrp option:gt(0)').remove();
					//$('.blue-select li:gt(0)').remove();
					
					//for(var p in response.producttype ) {
						$.each( response.producttype, function( key, value ) {
						console.log('Response type');
						console.log(key);
							console.log(value);
						var o = new Option(key, value);
						//$(".blue-select ul").append('<li data-value="'+response.producttype[p]+'">'+response.producttype[p]+'</li>');
						$(o).html(value);
						$("#productdrp").append('<option value="'+key+'">'+value+'</option>');
					});
					$("#productdrp").customselect();
				}
				 
				if( env == null || env == ''){//environment
					$('#environmentaldrp option:gt(0)').remove();
					//$('.yellow-select li:gt(0)').remove();
					
					for(var e in response.environment ) {
						var o = new Option(response.environment[e], response.environment[e]);
						//$(".yellow-select ul").append('<li data-value="'+response.environment[e]+'">'+response.environment[e]+'</li>');
						$(o).html(response.environment[e]);
						$("#environmentaldrp").append(o);
					}
					$("#environmentaldrp").customselect();
				}
					
				if( brd == null || brd == ''){//brand
					$('#branddrp option:gt(0)').remove();
					//$('.purple-select li:gt(0)').remove();
					for(var b in response.brand ) {
						var o = new Option(response.brand[b], response.brand[b]);
						//$(".purple-select ul").append('<li data-value="'+response.brand[b]+'">'+response.brand[b]+'</li>');
						$(o).html(response.brand[b]);
						$("#branddrp").append(o);
					}
					$("#branddrp").customselect();
				}
				
				if( finish == null || finish == ''){//finish
					$('#finishdrp option:gt(0)').remove();
					//$('.red-select li:gt(0)').remove();//finish
					for(var f in response.finish ) {	
						var o = new Option(response.finish[f], response.finish[f]);
						//$(".red-select ul").append('<li data-value="'+response.finish[f]+'">'+response.finish[f]+'</li>');
						$(o).html(response.finish[f]);
						$("#finishdrp").append(o);
					}
					$("#finishdrp").customselect();
				}
				
				if( tech == null || tech == ''){//technology
					$('#technologydrp option:gt(0)').remove();
					//$('.green-select li:gt(0)').remove();//technology
					for(var t in response.technology ) {
						
						var o = new Option(response.technology[t], response.technology[t]);
						//$(".green-select ul").append('<li data-value="'+response.technology[t]+'">'+response.technology[t]+'</li>');
						$(o).html(response.technology[t]);
						$("#technologydrp").append(o);
					}
					$("#technologydrp").customselect();
				}
				$(".ajaxLoader").hide();
				
				
			});
		});
});