function window_resized()
{
	var lst = jQuery(".adc-responsive");
	for(var i=0; i < lst.length; i++){
		var el = jQuery(lst[i]);
		var w = el.width();		
		if (w <= 1170){
			el.addClass("size-md");
		} else {
			el.removeClass("size-md");			
		}
		if (w <= 876){
			el.addClass("size-sm");
		} else {
			el.removeClass("size-sm");
		}
		if (w <= 583){
			el.addClass("size-xs");
		} else{
			el.removeClass("size-xs");			
		}
	}
}

jQuery(window).resize(function (){
	window_resized();
})

jQuery(document).ready(function(){
	window_resized();
});

function processLinks()
{
	var cnt = jQuery(".adc-tiles-container").children().length;
	var total = jQuery(".adc-vendors-tiles").data("total");
	var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
	jQuery(".adc-vendors-tiles .counter-showing").html(cnt);
	jQuery(".adc-vendors-tiles .counter-total").html(total);
	if (cnt >= total){
		jQuery(".adc-show-all").addClass("disabled");
		jQuery(".adc-view-more").addClass("disabled");
	} else {
		jQuery(".adc-show-all").removeClass("disabled");			
		jQuery(".adc-view-more").removeClass("disabled");
	}
	if (cnt <= tiles){
		jQuery(".adc-view-less").addClass("disabled");
	} else {
		jQuery(".adc-view-less").removeClass("disabled");			
	}
	var state = getCurrentState();
	if (state.q != "" || state.category != 0) {
		jQuery(".adc-search-box").addClass("reset");
	} else {
		jQuery(".adc-search-box").removeClass("reset");
	}

}

function load(count, rebuild){
	if (rebuild){			
		jQuery(".adc-tiles-container").html("");			
		jQuery(".adc-vendors-tiles").data("total", 0);
		processLinks();
		var start = 0;
	} else {
		var start = jQuery(".adc-tiles-container").children().length;
	}
	var url = jQuery(".adc-vendors-tiles").data("ajax-url") + "ajax-search.php?offset="+start+"&count="+count+"&q="+jQuery(".adc-vendors-tiles").data("search")+"&category="+jQuery(".adc-vendors-tiles").data("category");
	jQuery(".adc-vendors-tiles").addClass("loading");
	jQuery.ajax({ "url" : url} ).done(function (a, b, c){
		if (b == "success"){
			jQuery(".adc-tiles-container").append(a)
			if (jQuery(".adc-tiles-container").children().length > 1){				
				var scrollTarget =  jQuery(".adc-tiles-container").children()[start+1];
			} else {
				var scrollOffset = -1 * (jQuery(".adc-tiles-container").position().top + jQuery(".adc-vendors-tiles .top-controls").outerHeight(false));
				var scrollTarget = jQuery(".adc-tiles-container");
			}
			jQuery(document.body).scrollTo(scrollTarget, 800, {
				offset : -1 * (window.adc_sticky_offset + jQuery(".adc-vendors-tiles .top-controls").outerHeight(false))
			});
			jQuery(".adc-vendor-tile").mouseenter(tileMouseEnter);
			jQuery(".adc-vendor-tile").mouseleave(tileMouseLeave);

			jQuery(".adc-vendors-tiles").data("total", c.getResponseHeader("X-Total-Results"));
			processLinks();
			if (jQuery(".adc-tiles-container").children().length == 0){
				jQuery(".adc-vendors-tiles").addClass("no-results");
			} else {
				jQuery(".adc-vendors-tiles").removeClass("no-results");
			}
			history.pushState(getCurrentState(), document.title);
	}
	}).always(function() {
   		jQuery(".adc-vendors-tiles").removeClass("loading");
 	});
}

function viewLess(cnt)
{
	var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
	var currentcnt = jQuery(".adc-tiles-container").children().length;
	if (currentcnt % tiles > 0)
		cnt = currentcnt % tiles
	if (currentcnt - cnt < cnt)
		cnt = currentcnt - cnt;
	
	jQuery(document.body).scrollTo(jQuery(".adc-tiles-container").children()[currentcnt-cnt-1], 800, {
		offset : -1*(window.adc_sticky_offset+jQuery(".adc-vendors-tiles .top-controls").outerHeight(false)),
		onAfter : function (){
			jQuery('.adc-tiles-container > .adc-vendor-tile').slice(-1 * cnt).remove();		
			processLinks();
		}
	});

}

function getCurrentState()
{
	var state = {
		"offset" 	: 0,
		"count"  	: jQuery(".adc-tiles-container").children().length,
		"q"		 	: jQuery(".adc-vendors-tiles").data("search"),
		"category" 	: jQuery(".adc-vendors-tiles").data("category")
	};
	return state;	
}

var tile_hover_timeout = false;

function tileMouseEnter(e){
	if (tile_hover_timeout){
		clearTimeout(tile_hover_timeout);
	}
	tile_hover_timeout = setTimeout(function(){
		jQuery(e.currentTarget).find(".learn-more").css("display","inline-block");
		jQuery(e.currentTarget).find(".learn-more .bg").fadeTo("slow", 0.8, function (){
			jQuery(e.currentTarget).find(".learn-more .text").css("display", "block");
		});				
	}, parseInt(jQuery(e.currentTarget).attr("data-fade-timeout")));
}

function tileMouseLeave(e){
	if (tile_hover_timeout){
		clearTimeout(tile_hover_timeout);	
	}
	jQuery(e.currentTarget).find(".learn-more .text").css("display", "none");
	jQuery(e.currentTarget).find(".learn-more .bg").fadeTo("slow", 0, function (){
		jQuery(e.currentTarget).find(".learn-more").css("display","none");
	});						
}

// tiles page
jQuery(document).ready(function (){
	if (jQuery(".adc-vendors-tiles").length > 0){
		var stickyOffset = jQuery(".adc-vendors-tiles .top-controls").data("sticky-offset");
		var h = 0;
		if (Number.isInteger(stickyOffset))
			var h = stickyOffset;
		else{
			jQuery(stickyOffset).each(function(idx, el){ h += jQuery(el).outerHeight(); });
		}
		window.adc_sticky_offset = h;
		jQuery(".adc-vendors-tiles .top-controls").stick_in_parent({
			"offset_top" : h
		});

		if (history.state == null){
			var url = location.href;
			console.log(url);
			url = url.replace(/adc-q=?([^&]+)?&?/g,'');
			url = url.replace(/adc-category=?[^&]?&?/g,'');
			url = url.replace(/\?$/g,'');
			console.log(url);
			history.replaceState(getCurrentState(), document.title, url);
		}
		window.addEventListener("popstate", function(e) {
			if (history.state){
				jQuery(".adc-vendors-tiles").data("search", history.state.q);
				jQuery("#adc-search").val(history.state.q)
				jQuery(".adc-vendors-tiles").data("category", history.state.category);
				jQuery("#adc-categories").val(history.state.category);
				jQuery("#adc-categories").trigger("chosen:updated");
				load(history.state.count, true);
			}    		
		}, false)

		jQuery(".adc-vendors-tiles .adc-show-all").click(function (){
			load(0, false);
		});
	
		jQuery(".adc-vendors-tiles .adc-view-less").click(function (){
			if (!jQuery(".adc-view-less").hasClass("disabled")){
				var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
				viewLess(tiles);
			}
		});
		jQuery(".adc-vendors-tiles .adc-view-more").click(function (){
			if (!jQuery(".adc-view-more").hasClass("disabled")){
				var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
				load(tiles, false);
			}
		});
		jQuery(".adc-vendors-tiles #adc-search").keypress(function(e) {
	    	if(e.which == 13) {
				jQuery(document).trigger("adc.search", jQuery("#adc-search").val());
	    		jQuery(".adc-vendors-tiles").data("search", jQuery("#adc-search").val());        	
				var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
				load(tiles, true);
	    	}
		});

		jQuery(".adc-vendors-tiles #adc-categories").change(function (e){
	   		jQuery(".adc-vendors-tiles").data("category", jQuery("#adc-categories").val());        	
			var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
			load(tiles, true);
		});
		processLinks();

		jQuery(".adc-search-reset").click(function (){
			jQuery(".adc-vendors-tiles").data("search", ""),
			jQuery("#adc-search").val("")
			jQuery(".adc-vendors-tiles").data("category", 0)
			jQuery("#adc-categories").val(0);
			jQuery("#adc-categories").trigger("chosen:updated");
			var tiles = jQuery(".adc-vendors-tiles").data("number-of-tiles");
			load(tiles, true);			
		});

		jQuery(".adc-vendor-tile").mouseenter(tileMouseEnter);
		jQuery(".adc-vendor-tile").mouseleave(tileMouseLeave);
	}
});

jQuery(document).ready(function (){
	if (jQuery(".adc-search-form").length > 0){
		var stickyOffset = jQuery(".adc-search-form").data("sticky-offset");
		var h = 0;
		if (Number.isInteger(stickyOffset))
			var h = stickyOffset;
		else{
			jQuery(stickyOffset).each(function(idx, el){
				h += jQuery(el).outerHeight();
			});
		}
		window.adc_sticky_offset = h;
	}	
	jQuery(".adc-category-filter")
		.chosen({disable_search_threshold: 100, inherit_select_classes : true})
		.on("chosen:showing_dropdown keyup change", function (){
			jQuery(".adc-category-filter .chosen-drop li.active-result[data-option-array-index=0]").html("All");
		});

	jQuery(".adc-search-box").click(function (e){
		if (e.target.tagName == "SPAN"){
			var ev = jQuery.Event("keypress");
			ev.which = 13; 
			ev.keyCode = 13; 
			jQuery(e.target).find("input[type=text]").trigger(ev);
		}
	});

	jQuery(".adc-search-form .adc-category-filter").change(function (e){
    	var url = "/search-products?adc-q=&adc-category="+jQuery(".adc-search-form .adc-category-filter").val();
    	location.href = url;
	});

	jQuery(".adc-search-form .adc-search-box input[type=text]").keypress(function(e) {
    	if(e.which == 13) {
			jQuery(document).trigger("adc.search", jQuery(".adc-search-form .adc-search-box input[type=text]").val());
    		var url = "/search-products?adc-q="+jQuery(".adc-search-form .adc-search-box input[type=text]").val()+"&adc-category="+jQuery(".adc-search-form .adc-category-filter").val();
    		location.href = url;
    	}
	})	

})