var filterData = [];
var um_change = false;

jQuery(document).ready(function($){
	"use strict";
	
	$('.loadMoreCustomRooms').on('click',function(e){

		e.preventDefault();		
		$(this).addClass('isLoading');
		pageRoomsCustom++;
		
		var ajax_data = {
			paged : pageRoomsCustom,
			um_page : true,
		};
		
		$.post(document.URL, ajax_data, function(data){
			if(data){
			
				$('.loadMoreCustomRooms').removeClass('isLoading');
				
				$(data).imagesLoaded(function(){
					$('.articles').append(data);
				});
								
				if(lastCustomPageRooms <= pageRoomsCustom ){
						$('.loadMoreCustomRooms').hide();
					}
			}
		}).error(function(){
                $(".loadMoreRooms").hide();
        	});
	});
		
	
	$('.loadMoreRooms').on('click',function(e){


		e.preventDefault();		
		$(this).addClass('isLoading');
		pageRooms++;
		
		var ajax_data = {
			paged : pageRooms,
			um_page : true,
			um_change : um_change,
			um_cat : filterData
		};
		
		$.post(document.URL, ajax_data, function(data){
			if(data){
			
				$('.loadMoreRooms').removeClass('isLoading');
				
				$(data).imagesLoaded(function(){
					$('.articles').append(data);
				});
								
				if(lastPageRooms <= pageRooms ){
						$('.loadMoreRooms').hide();
					}
				
				
			}
		}).error(function(){
                $(".loadMoreRooms").hide();
        	});
		});
		
		
	
	$('.custom_filter li').on('click', function(){
		
		var $classes = $(this).closest(".custom_filter").attr('class');
		var $class = $classes.match(/um_custom_filter_[^\s]/g);
		
		$class = '.' + $class;
		var $select = $($class);
		
		if($select.attr('multiple') == 'multiple'){
  			
	  			var index =	$(this).index();
		  		
		  		if(index == 0){	
		  			 $select.selectpicker('deselectAll');
		  			   $select.selectpicker('render');
		  		}	  		
		  		else{
		  			if($select.find('.selectpicker').find('li.selected').find('a').attr('class') == 'um_all' ){
		  				  $select.selectpicker('deselectAll');
		  			   	  $select.selectpicker('render');
		  				
		  			}
		  		} 
	  		}	

	});
	 	
	$('.um_category_filter li').on('click', function () {
	  
	  
	  		var $select = $('.um_category_filter');
  			
  			if($select.attr('multiple') == 'multiple'){
  			
	  			var index =	$(this).index();
		  		
		  		if(index == 0){	
		  			 $select.selectpicker('deselectAll');
		  			   $select.selectpicker('render');
		  		}	  		
		  		else{
		  			if($select.find('.selectpicker').find('li.selected').find('a').attr('class') == 'um_all' ){
		  				  $select.selectpicker('deselectAll');
		  			   	  $select.selectpicker('render');
		  				
		  			}
		  		} 
	  		}	
	});
	
		
	
	
	
	$('.um_filter').each(function(){		
		var $selectpicker = $(this).find('.selectpicker').data('selectpicker').$newElement;
			$selectpicker.on('hide.bs.dropdown', function() {
				if($(this).find('.selectpicker').find('li.selected').text() != ""){				
					$('body').trigger('um_filter');					
				}	  			   
		});
	});
	



	$('body').on('um_filter',function(){
		
		$('.um_shadow').addClass('active');	
				
		var data = [];		
		$('.um_filter').each(function(){			
			var $that = $(this);
			var tax = $that.find('.selectpicker').attr('data-um_title');
			var innerdata = [];
			
			$that.find('li').each(function(){
				
				if($(this).hasClass('selected'))
				{
					var selected = $(this).find('a').attr('class');
						selected = selected.replace(/_/g, " ");
					
					if(selected == "um all")
					{
						selected = 'um_all';
					}
					innerdata.push({selected : selected, tax : tax });
					
				}
			});
			data.push(innerdata);
		});					
			filterData = data;	
			um_change = true;
			
			var ajaxData = {
				"action" : "um_filter",
				"um_cat" : filterData,
				"post_id" : postId
			}; 
			
			$.post(ajax_url, ajaxData, function(data){
				
				$('.loadMoreRooms').show();
				
				$('.um_articlesWrapper').find('script').remove();
				$('.articles').remove();
				$(data).imagesLoaded(function(){
					$('.um_articlesWrapper').append(data);
					
					if(lastPageRooms <= pageRooms ){
						$('.loadMoreRooms').hide();
					}
					
					$('.um_shadow').removeClass('active');	
				});
				
				
			});

	});
});
