var PROCATEAJAXTABPRO = {
	init:function(){
		PROCATEAJAXTABPRO.ActTabPro();
		var checkTab = jQuery("#dorTabAjaxPro > li a.active").attr("href");
		PROCATEAJAXTABPRO.TabCarousel(checkTab+" .product_list");
		window.addEventListener('load', function (){
		    PROCATEAJAXTABPRO.LoadDefault();
		});
	},
	ReloadProcess:function(){
		var html = "";
		html += '<div class="graph__preloader">';
	        html += '<div class="js-preloader preloader">';
	          html += '<b class="preloader__bar -bar1"></b>';
	          html += '<b class="preloader__bar -bar2"></b>';
	          html += '<b class="preloader__bar -bar3"></b>';
	          html += '<b class="preloader__bar -bar4"></b>';
	          html += '<b class="preloader__bar -bar5"></b>';
	        html += '</div>';
	    html += '</div>';
	    return html;
	},
	LoadDefault:function(){
			var processHtml = PROCATEAJAXTABPRO.ReloadProcess();
			var tabchose = jQuery(".dor-tabpro-product-category-wrapper").attr("data-tab-id");
			var idTxt = jQuery("#dorTabAjaxPro li a.active").attr("href");
			if(typeof idTxt == "undefined") return false;
			idTxt = idTxt.replace("#cate-tab-data-pro-","");
			type = 1;
			var idObj = parseInt(idTxt);
			if(isNaN(idObj)){
				idObj = idTxt;
				type = 0;
			}
			var checkExist = jQuery("#cate-tab-data-pro-"+idObj+" .dor-content-items > div.row-items").html();
			if(jQuery.trim(checkExist).length == 0 || typeof checkExist == "undefined"){
				jQuery("#dorTabProductCategoryContentPro .title, #dorTabProductCategoryContentPro .product-description, .view-all").hide();
				jQuery("#dorTabProductCategoryContentPro .loaddingAjaxTab").remove();
				jQuery("#dorTabProductCategoryContentPro .graph__preloader").remove();
				jQuery("#dorTabProductCategoryContentPro").append(processHtml);
				jQuery("#dorTabProductCategoryContentPro > div.tab-pane").css({
					'opacity':0,
					'min-height':350
				});
				var urlAjax = jQuery("#dorTabAjaxPro").attr("data-ajaxurl");
				var params = {}
				params.tabchose = tabchose;
				params.cateID = idObj;
				params.type = type;
				jQuery.ajax({
		            url: urlAjax,
		            data:params,
		            type:"POST",
		            success:function(data){
		            	setTimeout(function(){
		            		jQuery("#dorTabProductCategoryContentPro .title, #dorTabProductCategoryContentPro .product-description, .view-all").show();
		            		var results = JSON.parse(data);
			            	jQuery(".tab_container.dorlistproducts .sliderLoadingTab").remove();
			            	jQuery("#cate-tab-data-pro-"+idObj+" .dor-content-items > div").html(results);
			            	jQuery(".loaddingAjax").remove();
			            	jQuery("#dorTabProductCategoryContentPro > div.tab-pane").css({
								'opacity':1,
								'min-height':200
							});
							PROCATEAJAXTABPRO.TabCarousel("#cate-tab-data-pro-"+idObj+" .product_list");
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.ToolTipBootstrap == "function"){
								DORTHEME.ToolTipBootstrap();
							}
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.DorQuantity != "undefined" && typeof DORTHEME.DorQuantity == "function"){
			            		//DORTHEME.DorQuantity();
			            	}
							jQuery("#dorTabProductCategoryContentPro .graph__preloader").remove();
		            	},600);
		                return false;
		            }
		        });
			}
	},
	ActTabPro:function(){
		jQuery("#dorTabAjaxPro > li a").click(function(){
			var processHtml = PROCATEAJAXTABPRO.ReloadProcess();
			var tabchose = jQuery(this).closest(".dor-tabpro-product-category-wrapper").attr("data-tab-id");
			var idTxt = jQuery(this).attr("href");
			jQuery("#dorTabProductCategoryContentPro .tab-pane").removeClass("in active");
			jQuery(idTxt).addClass("in active");
			jQuery("#dorTabAjaxPro > li a").removeClass("active");
			idTxt = idTxt.replace("#cate-tab-data-pro-","");
			type = 1;
			var idObj = parseInt(idTxt);
			if(isNaN(idObj)){
				idObj = idTxt;
				type = 0;
			}
			var checkExist = jQuery("#cate-tab-data-pro-"+idObj+" .dor-content-items > div").html();
			if(jQuery.trim(checkExist).length == 0 || checkExist == undefined){
				jQuery("#dorTabProductCategoryContentPro .title, #dorTabProductCategoryContentPro .product-description, .view-all").hide();
				jQuery("#dorTabProductCategoryContentPro .loaddingAjaxTab").remove();
				jQuery("#dorTabProductCategoryContentPro .graph__preloader").remove();
				jQuery("#dorTabProductCategoryContentPro").append(processHtml);
				//jQuery("#dorTabProductCategoryContentPro").append('<span class="loaddingAjax">Loadding...</span>');
				jQuery("#dorTabProductCategoryContentPro > div.tab-pane").css({
					'opacity':0,
					'min-height':350
				});
				var urlAjax = jQuery(this).closest("ul").attr("data-ajaxurl");
				var params = {}
				params.tabchose = tabchose;
				params.cateID = idObj;
				params.type = type;
				jQuery.ajax({
		            url: urlAjax,
		            data:params,
		            type:"POST",
		            success:function(data){
		            	setTimeout(function(){
		            		jQuery("#dorTabProductCategoryContentPro .title, #dorTabProductCategoryContentPro .product-description, .view-all").show();
		            		var results = JSON.parse(data);
			            	jQuery(".tab_container.dorlistproducts .sliderLoadingTab").remove();
			            	jQuery("#cate-tab-data-pro-"+idObj+" .dor-content-items > div").html(results);
			            	jQuery(".loaddingAjax").remove();
			            	jQuery("#dorTabProductCategoryContentPro > div.tab-pane").css({
								'opacity':1,
								'min-height':200
							});
							PROCATEAJAXTABPRO.TabCarousel("#cate-tab-data-pro-"+idObj+" .product_list");
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.ToolTipBootstrap == "function"){
								DORTHEME.ToolTipBootstrap();
							}
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.DorQuantity != "undefined" && typeof DORTHEME.DorQuantity == "function"){
			            		//DORTHEME.DorQuantity();
			            	}
			            	jQuery("#dorTabProductCategoryContentPro .graph__preloader").remove();
		            	},600);
		                return false;
		            }
		        });
			}
		});
	},
	LoadMoreData:function(){
		jQuery(".load-more-tab").click(function(){
			var _this=this;
			var limit = parseInt(jQuery(_this).attr("data-limit"))
			var urlAjax = jQuery(_this).attr("data-ajax");
			var page = jQuery(_this).attr("data-page");
			var idTxt = jQuery(_this).closest(".tab-pane").attr("id");
			jQuery("#"+idTxt).append("<span class='load-more-small'></span>");
			idTxt = idTxt.replace("cate-tab-data-","");
			type = 1;
			var idObj = parseInt(idTxt);
			if(isNaN(idObj)){
				idObj = idTxt;
				type = 0;
			}
			var params = {}
			params.cateID = idObj;
			params.type = type;
			params.page = page;
			setTimeout(function(){
				jQuery.ajax({
		            url: urlAjax,
		            data:params,
		            type:"POST",
		            success:function(data){
		            	var results = JSON.parse(data);
		            	jQuery("#cate-tab-data-"+idObj+" .dor-content-items > .row-tabcontent").append(results);
		            	var lengthVal = jQuery(results).find(".dor-product-lists").length;
		            	if(lengthVal >= limit){
		            		jQuery(_this).attr("data-page",parseInt(page)+1);
		            	}else{
		            		jQuery(_this).remove();
		            	}
		            	jQuery(".load-more-small").remove();
		                return false;
		            }
		        });
	        },1000);
		});
	},

	TabCarousel:function(objElement){
		$(objElement).owlCarousel({
	        items: 6,
	        loop: true,
	        nav: true,
	        autoplay: false,
	        margin:20,
	        dots: true,
	        lazyLoad: true,
	        responsive: {
	            0: {items: 1},
	            1500: {items: 6},
	            1200: {items: 5},
	            992: {items: 4},
	            650: {items: 3, margin:20},
	            370: {items: 2, margin:20},
	            300: {items: 1, margin:0}
	        },
	        onInitialize: function (event) {
		        if ($(objElement).find('article').length <= 1) {
		           this.settings.loop = false;
		        }
		    },
	        navText: ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"]
	    });
	}
};

jQuery(document).ready(function(){
	PROCATEAJAXTABPRO.init();
});