
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LIB.JS
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

var categoryFilterClass = function(){
	
	/**
	* reference to self object
	**/
	var self = this;
	
	/**
	* show/hide other sub-categories
	**/
	this.moreFilters = function(){
		$('div.filter-area ul').each(function(){
			if ( $(this).find('li.hide').length > 0 )
			{
				$(this).next().after('<ul class="hide"></ul>');
				$(this).next().next().append($(this).find('li.hide').show());
				
			}
		});
		
		$('div.filter-area a.more').click(function(){
			$('div.other_filters_tmp').remove();
			
			var pos = $(this).offset();
			var sub_cats = $(this).next().html();
			var tmp = '<div class="other_filters_tmp side_block"><div class="block_bg"><ul></ul></div></div>'
			$('body').append(tmp);
			$('div.other_filters_tmp div ul').html(sub_cats);
			
			var rest = rlLangDir == 'ltr' ? 0 : $('div.other_filters_tmp').width();
			var side = rlLangDir == 'ltr' ? 'Left' : 'Right';
			$('div.other_filters_tmp').css({
				top: pos.top,
				left: pos.left-rest,
				display: 'block'
			});
			var width = $(this).width()+5;
			$('div.other_filters_tmp div').css('margin'+side, width+'px');
		});
		
		$(document).click(function(event){
			if ( $(event.target).closest('.other_filters_tmp').length <= 0 && $(event.target).attr('class') != 'dark_12 more' )
			{
				$('div.other_filters_tmp').remove();
			}
		});
	};
	
	/**
	* checkbox call method
	**/
	this.checkbox = function(obj, empty){
		obj.parent().find('ul li input').click(function(){
			self.checkboxAction(obj, empty);
		});
		
		this.checkboxAction(obj, empty);
	};
	
	/**
	* checkbox action method
	**/
	this.checkboxAction = function(obj, empty){
		var values = new Array();
		obj.parent().find('ul li').each(function(){
			if ( $(this).find('input').is(':checked') )
			{
				//values.push(parseInt($(this).find('input').val()));
				values.push($(this).find('input').val());
			}
		});
		
		if ( values.length > 0 )
		{
			var href = obj.find('a:first').attr('accesskey');
			href = href.replace('[replace]', values.join(','));
			obj.find('a:first').attr('href', href);
			
			obj.find('span').hide();
			obj.removeClass('dark single');
			obj.find('a:first').show();
			
			if ( empty )
			{
				obj.find('a:last').hide();
				obj.find('span').html(cf_apply_filter);
			}
		}
		else
		{
			obj.find('a:first').hide();
			obj.addClass('dark single');
			obj.find('span').show();
			
			if ( empty )
			{
				obj.find('a:last').show();
				obj.removeClass('single');
				obj.find('span').html(cf_remove_filter);
			}
		}
	};

	/**
	* category tree handler (available for boxes on listing type page only)
	*
	**/
	this.categoryTree = function(){
		var count = $('ul.cat-tree > li').length;
		var desktop_limit_top = 10;
		var desktop_limit_bottom = 25;

		if ( count <= 0 )
			return;

		if ( $('div.cat-tree-cont div.cat-toggle').attr('accesskey') ) {
			desktop_limit_top = parseInt($('div.cat-tree-cont div.cat-toggle').attr('accesskey'));
		}

		$('ul.cat-tree > li span.toggle').click(function(){
			$(this).parent().find('ul').toggle();

			var parent = $(this).closest('.cat-tree-cont');
			if ( parent.hasClass('mCustomScrollbar') ) {
				parent.addClass('limit-height').mCustomScrollbar('update');
			}

			$(this).text(trim($(this).text()) == '+' ? '-' : '+');
		});
		
		if ( $('ul.cat-tree > li span.toggle:contains("+")').length == 0 ) {
			$('ul.cat-tree > li span.toggle').hide();
		}

		var current_media_query = media_query;
		$(window).resize(function(){
			if ( media_query != current_media_query ) {
				$('div.cat-tree-cont').addClass('limit-height').mCustomScrollbar('update');
				current_media_query = media_query;
			}
		});

		if ( count > desktop_limit_top && count <= desktop_limit_bottom ) {
			var gt = desktop_limit_top - 1;
			$('ul.cat-tree > li:gt('+gt+')').addClass('rest');

			$('div.cat-tree-cont div.cat-toggle').removeClass('hide').click(function(){
				$(this).prev().find('> li.rest').toggle();
			});
			$('div.cat-tree-cont').removeClass('limit-height');
		}
		else if (count > desktop_limit_bottom) {
			$('div.cat-tree-cont').mCustomScrollbar();
		}
		else {
			$('div.cat-tree-cont').removeClass('limit-height');
		}
	};
}

var categoryFilter = new categoryFilterClass();