var PROCATEAJAXTAB2 = {
	init:function(){
		PROCATEAJAXTAB2.ActTab();
		PROCATEAJAXTAB2.LoadMoreData();
		
		window.addEventListener('load', function (){
		    PROCATEAJAXTAB2.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 checkTab = jQuery("#dorTabAjax2").attr("data-defaulttab");
		var processHtml = PROCATEAJAXTAB2.ReloadProcess();
		if(typeof checkTab == "undefined") return false;
		type = 1;
		if(isNaN(parseInt(checkTab))){
			type = 0;
		}
		var limit = parseInt(jQuery("#cate-tab-data-"+checkTab+" .load-more-tab").attr("data-limit"));
		jQuery("#dorTabProductCategory2Content.tab-content").css({
			"min-height":420,
			"max-height":420,
			"overflow":"hidden"
		});
		jQuery("#cate-tab-data-"+checkTab+" .dor-content-items > div").css({
			"opacity":0,
			"visibility":"hidden"
		});
		jQuery("#dorTabProductCategory2Content .loaddingAjaxTab").remove();
		jQuery("#dorTabProductCategory2Content .graph__preloader").remove();
		jQuery("#dorTabProductCategory2Content").append(processHtml);
		var urlAjax = jQuery("#dorTabAjax2").attr("data-ajaxurl");
		var params = {}
		params.cateID = checkTab;
		params.type = type;
		jQuery.ajax({
            url: urlAjax,
            data:params,
            type:"POST",
            success:function(data){
            	var results = JSON.parse(data);
            	jQuery("#cate-tab-data-"+checkTab+" .dor-content-items > div").html(results);
            	var lengthVal = jQuery(results).find(".product-miniature").length;
            	if(lengthVal < limit){
            		jQuery("#cate-tab-data-"+checkTab+" .load-more-tab").remove();
            	}
            	jQuery("#dorTabAjax2 li a").removeClass("active");
            	setTimeout(function(){
            		PROCATEAJAXTAB2.TabCarousel("#cate-tab-data-"+checkTab+" .dor-tab2-contents .row-data");
            		jQuery("#dorTabProductCategory2Content.tab-content").css({
            			"min-height":"auto",
            			"overflow":"inherit",
						"max-height":"none"
            		});
            		jQuery("#cate-tab-data-"+checkTab+" .dor-content-items > div").css({
						"opacity":1,
						"visibility":"visible"
					});
            		jQuery("#dorTabProductCategory2Content .graph__preloader").remove();
            		jQuery(".loaddingAjaxTab").remove();
            	},200);
            	
				if(typeof DORTHEME != "undefined" && typeof DORTHEME.ToolTipBootstrap == "function"){
					DORTHEME.ToolTipBootstrap();
				}
				if(typeof DORTHEME != "undefined" && typeof DORTHEME.DorQuantity != "undefined" && typeof DORTHEME.DorQuantity == "function"){
            		//DORTHEME.DorQuantity();
            	}
                return false;
            }
        });
	},
	ActTab:function(){

		var objElement = $('#dor-tab-product-category2 .tab-pane.active').attr("id");
		if(typeof objElement != "undefined"){
			var objId = "#"+objElement+" .dor-tab2-contents .row-data";
			PROCATEAJAXTAB2.TabCarousel(objId);
		}

		jQuery("#dorTabAjax2 > li a").unbind("click");
		jQuery("#dorTabAjax2 > li a").click(function(){
			var _this = this;
			var processHtml = PROCATEAJAXTAB2.ReloadProcess();
			var idTxt = jQuery(this).attr("href");
			var idObjTxt = idTxt;
			idTxt = idTxt.replace("#cate-tab-data-","");
			type = 1;
			var idObj = parseInt(idTxt);
			if(isNaN(idObj)){
				idObj = idTxt;
				type = 0;
			}
			var limit = parseInt(jQuery("#cate-tab-data-"+idObj+" .load-more-tab").attr("data-limit"));
			var checkExist = jQuery("#cate-tab-data-"+idObj+" .dor-content-items > div").html();
			if(jQuery.trim(checkExist).length == 0 || checkExist == undefined){
				jQuery("#dorTabProductCategory2Content.tab-content").css("min-height",420);
				jQuery("#dorTabProductCategory2Content .loaddingAjaxTab").remove();
				jQuery("#dorTabProductCategory2Content .graph__preloader").remove();
				//jQuery("#dorTabProductCategoryContent").append('<span class="loaddingAjaxTab">Loadding...</span>');
				jQuery("#dorTabProductCategory2Content").append(processHtml);
				var urlAjax = jQuery(this).closest("ul").attr("data-ajaxurl");
				var params = {}
				params.cateID = idObj;
				params.type = type;
				jQuery.ajax({
		            url: urlAjax,
		            data:params,
		            type:"POST",
		            success:function(data){
		            	setTimeout(function(){
		            		var results = JSON.parse(data);
			            	//jQuery(".tab_container.dorlistproducts .sliderLoadingTab").remove();
			            	jQuery("#dorTabProductCategory2Content .graph__preloader").remove();
			            	jQuery("#cate-tab-data-"+idObj+" .dor-content-items .row-data").html(results);
			            	var lengthVal = jQuery(results).find(".product-miniature").length;
			            	if(lengthVal < limit){
			            		jQuery("#cate-tab-data-"+idObj+" .load-more-tab").remove();
			            	}
			            	jQuery(".loaddingAjaxTab").remove();
			            	jQuery("#dorTabProductCategory2Content.tab-content").css("min-height","auto");
			            	jQuery("#dorTabAjax2 li a").removeClass("active");
							jQuery(_this).addClass("active");
							jQuery(_this).closest("li").addClass("in active");

							var objId = idObjTxt+" .dor-tab2-contents .row-data";
							PROCATEAJAXTAB2.TabCarousel(objId);
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.ToolTipBootstrap == "function"){
								DORTHEME.ToolTipBootstrap();
							}
							if(typeof DORTHEME != "undefined" && typeof DORTHEME.DorQuantity != "undefined" && typeof DORTHEME.DorQuantity == "function"){
			            		//DORTHEME.DorQuantity();
			            	}
			            	/*if(typeof DORCORE != "undefined" && typeof DORCORE.LazyLoad != "undefined" && typeof DORCORE.LazyLoad == "function"){
			            		DORCORE.LazyLoad();
			            	}*/
		            	},600);
		                return false;
		            }
		        });
			}else{
				jQuery("#dorTabAjax2 li a").removeClass("active");
				jQuery("#dorTabAjax2 li").removeClass("active");
				jQuery(_this).addClass("active");
				jQuery(_this).closest("li").addClass("active");
				jQuery("#dorTabProductCategory2Content .tab-pane").removeClass('active');
				jQuery(idObjTxt+".tab-pane").addClass('in active');
			}
		});
	},
	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+" .product_list").append(results);
		            	var lengthVal = jQuery(results).find(".product-miniature").length;
		            	if(lengthVal >= limit){
		            		jQuery(_this).attr("data-page",parseInt(page)+1);
		            	}else{
		            		jQuery(_this).remove();
		            	}
		            	jQuery(".load-more-small").remove();
		            	if(typeof OREGON != "undefined" && typeof OREGON.DorQuantity != "undefined" && typeof OREGON.DorQuantity == "function"){
		            		OREGON.DorQuantity();
		            	}
		                return false;
		            }
		        });
	        },1000);
		});
	},
	TabCarousel:function(objElement){
		$(objElement).owlCarousel({
	        items: 1,
	        loop: false,
	        nav: false,
	        autoplay: false,
	        margin:10,
	        dots:true,
	        autoplaySpeed : 200,
			navSpeed : 200,
			dotsSpeed : 200,
			autoplayHoverPause: true,
	        responsive: {
	            0: {items: 1},
	            1200: {items: 1},
	            992: {items: 1},
	            650: {items: 1, margin:10},
	            370: {items: 1, margin:10},
	            300: {items: 1, margin:0}
	        },
	        onInitialize: function (event) {
		        if ($(objElement).find('.row-data-fields').length <= 1) {
		           this.settings.loop = false;
		           this.settings.dots = false;
		        }
		    },
	        navText: ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"]
	    });
	}
};

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