var TOPPRODUCTS = {
	init:function(){
		window.addEventListener('load', function (){
		    TOPPRODUCTS.LoadDefault();
		});
		TOPPRODUCTS.SlideImage();
		TOPPRODUCTS.TabCarousel("#dorTopProductCategory .dor-topproduct-category-data .product_list");
	},
	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 urlAjax = jQuery("#dorTopProductCategory").attr("data-defaulttab");
		var processHtml = TOPPRODUCTS.ReloadProcess();
		if(typeof urlAjax == "undefined") return false;
		var cateId = $("#dorTopProductCategory").attr("data-cateid");
		jQuery("#dorTabProductCategoryContent.tab-content").css("min-height",620);
		jQuery("#dorTopProductCategory .loaddingAjaxTab").remove();
		jQuery("#dorTopProductCategory .graph__preloader").remove();
		jQuery("#dorTopProductCategory .dor-topproduct-category-data").append(processHtml);
		var params = {}
		params.cateID = parseInt(cateId);
		jQuery.ajax({
            url: urlAjax,
            data:params,
            type:"POST",
            success:function(data){
            	setTimeout(function(){
            		var results = JSON.parse(data);
	            	jQuery("#dorTopProductCategory .dor-topproduct-category-data .graph__preloader").remove();
	            	jQuery("#dorTopProductCategory .dor-topproduct-cate-wrapper").html(results);
	            	TOPPRODUCTS.TabCarousel("#dorTopProductCategory .dor-topproduct-category-data .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();
	            	}
	            	
            	},600);
                return false;
            }
        });
	},
	
	TabCarousel:function(objElement){
		var checkImgBanner = $("#dorTopProductCategory .image-banner-top ul").html();
		if(typeof checkImgBanner == "undefined"){
			$(objElement).owlCarousel({
		        items: 4,
		        loop: ($(this).find('.owl-item').length > 1) ? true : false,
		        nav: true,
		        autoplay: false,
		        margin:20,
		        lazyLoad: true,
		        responsive: {
		            0: {items: 1},
		            1200: {items: 4},
		            992: {items: 3, margin:20},
		            650: {items: 3, margin:20},
		            370: {items: 2, margin:20},
		            300: {items: 1, margin:0}
		        },
		        navText: ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"]
		    });
		}else{
			$(objElement).owlCarousel({
		        items: 3,
		        loop: ($(this).find('.owl-item').length > 1) ? true : false,
		        nav: true,
		        autoplay: false,
		        margin:20,
		        lazyLoad: true,
		        responsive: {
		            0: {items: 1},
		            1200: {items: 3},
		            992: {items: 3, margin:15},
		            650: {items: 3, margin:20},
		            370: {items: 2, margin:20},
		            300: {items: 1, margin:0}
		        },
		        navText: ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"]
		    });
		}
		
	},
	SlideImage:function(){
		$("#dorTopProductCategory .image-banner-top ul").owlCarousel({
	        items: 1,
	        loop: true,
	        nav: false,
	        autoplay: true,
	        margin:0,
	        lazyLoad: true,
	        responsive: {
	            0: {items: 1},
	            1200: {items: 1},
	            992: {items: 1},
	            650: {items: 1, margin:0},
	            370: {items: 1, margin:0},
	            300: {items: 1, margin:0}
	        },
	        onInitialize: function (event) {
		        if ($("#dorTopProductCategory .image-banner-top ul").find('li').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(){
	TOPPRODUCTS.init();
});