$(document).ready( function() {
	
	
	// SLIDER
	var values = [0,200,500,1000,2000,'2000%2B'];
	var pr_min = $(".buy_assistant_price_0").val();
	var pr_max = $(".buy_assistant_price_1").val();
	var d_min = 0;
	var d_max = 5;
	$.each(values, function( i, e) {
		if (pr_min == e) {
			d_min = i;
		}
		if (pr_max == e) {
			d_max = i;
		} 			
	});
	
	$("#slider").slider({
		range: true,
	    min: 0,
	    max: 5,
		step: 1,
		values: [ d_min, d_max ],
		change: function(event, ui) {			
			var priceFrom = $(this).slider('values', 0);
			var priceTo	= $(this).slider('values', 1);					
			setAddFilterAttr('C0{'+values[priceFrom]+'}C1{'+values[priceTo]+'}');
		}
	});
	
	$('#searchform-s').submit( function() {
		var sattr = $('#search-s input[name=keyword]').val();
		var attr = 'S0{' +  sattr + '}';
		//nastav filtr a presmeruj
		
		 $.ajax({
             type: "POST",
             url: '/ajax/shop/shop.ajax.php?sign=32-1-'+EFS.isoLang+'-CZ&ev=filters&ac=setSearchHistory&addfilter=' + sattr,
             data: EFS,
             dataType: "json",
             error: function(xhr, msg) {                                    
                 console.log("Při provádění došlo k chybě!");                                    
             },
             success: function(data) {
                 if (data.status == "ok") {               	
                    //console.log('OK'); 
                	 setAddFilterAttr( attr,true );
                 } else {
                	 console.log(data.content);                                    
                 }                                                                                                           
             }
         });        				
		if ( EFS.isoLang == 'en') { 			
			EFF.uiInfo('String was added to filter box');
		} else {
			EFF.uiInfo('Výraz byl přidán mezi filtry.');
		}
		return false;
	});
	
	var searchDefaultText = $('#search-s input[name=keyword]').val();
	$('#search-s input[name=keyword]').click( function() {
		if ($(this).val() == searchDefaultText ) {
			$(this).attr('value','');
		} 
	});
	$('#search-s input[name=keyword]').blur( function() {
		if ($(this).val() == '' ) {
			$(this).attr('value', searchDefaultText );
		} 
	});

	
	
	$('.addfilterattr').click( function() {		
		var attr = $('>span', this).html();		
		setAddFilterAttr( attr, false );
	});
	
	setAddFilterAttr = function( attr, redirect ) {
		var hash = $('#prefilterhash').val();
		var IDCategory = EFS.Shop.IDCategory;
		 $.ajax({
             type: "POST",
             url: '/ajax/shop/shop.ajax.php?sign=32-1-'+EFS.isoLang+'-CZ&ev=filters&ac=AddFilter&IDCategory='+ IDCategory +'&addfilter=' + hash+attr,
             data: EFS,
             dataType: "json",
             error: function(xhr, msg) {                                    
                 console.log("Při provádění došlo k chybě!");                                    
             },
             success: function(data) {
                 if (data.status == "ok") {               	
                    //console.log('OK');                     
                	 $('.main_filter_box').html( data.content );
                    if (redirect) {
                    	console.log('redirection');
                    	console.log($('#shownewfilter').attr('href'));
                    	if ( $('#shownewfilter').attr('href') == null ) {
                    		if ( EFS.isoLang == 'en') { 			
								EFF.uiAlert('Any product was found');
							} else {
								EFF.uiAlert('Nebyl nalezen žádný produkt');
							}
                    	} else {                    	
                    		window.location = $('#shownewfilter').attr('href');
                    	}
                    } else {
                    	setDropFilterAttr();
                    }
                 } else {
                	 console.log(data.content);                                    
                 }                                                                                                           
             }
         });        			
	};
	
	setDropFilterAttr = function() {		
		$('.dropfilterattr').click(function() {			
			var attr =  $('span', this).html() ;
			var hash = $('#prefilterhash').val();
			var IDCategory = $('#IDCategory').val();
			 $.ajax({
	             type: "POST",
	             url: '/ajax/shop/shop.ajax.php?sign=32-1-'+EFS.isoLang+'-CZ&ev=filters&ac=DropFilter&IDCategory='+ IDCategory +'&hash='+hash+'&dropfilter=' + attr,
	             data: EFS,
	             dataType: "json",
	             error: function(xhr, msg) {                                    
	                 console.log("Při provádění došlo k chybě!");                                    
	             },
	             success: function(data) {
	                 if (data.status == "ok") {               	
	                    //console.log('OK'); 
	                    $('.main_filter_box').html( data.content );
	                    setDropFilterAttr();
	                    	                   	        		
	                 } else {
	                	 console.log(data.content);                                    
	                 }                                                                                                           
	             }
	         });        	
		});		
	};
	
	setDropFilterAttr();
} );
