(function(){
	$.fn.dorCountDown = function( options ) {
	 	return this.each(function() { 
			// get instance of the dorCountDown.
			new  $.dorCountDown( this, options ); 
		});
 	 }
	$.dorCountDown = function( obj, options ){
		
		this.options = $.extend({
				autoStart: true,
				LeadingZero:true,
				DisplayFormat:"<div>%%D%% Days</div><div>%%H%% Hours</div><div>%%M%% Minutes</div><div>%%S%% Seconds</div>",
				FinishMessage:"Expired",
				CountActive:true,
				finishDate:null
		}, options || {} );
		if( this.options.finishDate == null || this.options.finishDate == '' ){
			return ;
		}
		this.timer  = null;
		this.element = obj;
		this.CountStepper = -1;
		this.CountStepper = Math.ceil(this.CountStepper);
		this.SetTimeOutPeriod = (Math.abs(this.CountStepper)-1)*1000 + 990;
		var dthen = new Date(this.options.finishDate);
		var dnow = new Date();
		if( this.CountStepper > 0 ) {
			ddiff = new Date(dnow-dthen);
		}
		else {
			 ddiff = new Date(dthen-dnow);
		}
		gsecs = Math.floor(ddiff.valueOf()/1000); 
		this.CountBack(gsecs, this);
	};
	 $.dorCountDown.fn =  $.dorCountDown.prototype;
     $.dorCountDown.fn.extend =  $.dorCountDown.extend = $.extend;
	 $.dorCountDown.fn.extend({
		calculateDate:function( secs, num1, num2 ){
			  var s = ((Math.floor(secs/num1))%num2).toString();
			  if ( this.options.LeadingZero && s.length < 2) {
					s = "0" + s;
			  }
			  return "<span>" + s + "</span>";
		},
		CountBack:function( secs, self ){
			 if (secs < 0) {
				self.element.innerHTML = '<div class="dor-labelexpired"> '+self.options.FinishMessage+"</div>";
				return;
			  }
			  clearInterval(self.timer);
			  DisplayStr = self.options.DisplayFormat.replace(/%%D%%/g, self.calculateDate( secs,86400,100000) );
			  DisplayStr = DisplayStr.replace(/%%H%%/g, self.calculateDate(secs,3600,24));
			  DisplayStr = DisplayStr.replace(/%%M%%/g, self.calculateDate(secs,60,60));
			  DisplayStr = DisplayStr.replace(/%%S%%/g, self.calculateDate(secs,1,60));
			  self.element.innerHTML = DisplayStr;
			  if (self.options.CountActive) {
			   	self.timer = null;
				self.timer = setTimeout( function(){
					self.CountBack((secs+self.CountStepper),self);			
				},( self.SetTimeOutPeriod ) );
			 }
		}
					
	})
})(jQuery)




var DORDEALS = {
	init:function(){
		DORDEALS.DealCarousel();
		window.addEventListener('load', function (){
		    DORDEALS.AjaxLoadData();
		});
	},
	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;
	},
	
	AjaxLoadData:function(){
		var urlAjax = jQuery("#countdown-data").attr("data-ajaxurl");
		var processHtml = DORDEALS.ReloadProcess();
		if(typeof urlAjax == "undefined") return false;
		
		jQuery("#countdown-data").css("min-height",435);
		jQuery("#countdown-data .dailydeal-content").css("min-height",435);
		jQuery("#countdown-data .graph__preloader").remove();
		jQuery("#countdown-data").append(processHtml);
		var params = {}
		jQuery.ajax({
            url: urlAjax,
            data:params,
            type:"POST",
            success:function(data){
            	setTimeout(function(){
            		var results = JSON.parse(data);
	            	jQuery("#countdown-data .graph__preloader").remove();
	            	jQuery("#countdown-data .dailydeal-content").html(results);
	            	jQuery("#countdown-data").css("min-height","auto");
	            	jQuery("#countdown-data .dailydeal-content").css("min-height","auto");
	            	DORDEALS.DealCarousel();
					if(typeof DORTHEME != "undefined" && typeof DORTHEME.ToolTipBootstrap == "function"){
						DORTHEME.ToolTipBootstrap();
					}
					
					if(typeof DORTHEME.DorQuantity != "undefined" && typeof DORTHEME.DorQuantity == "function"){
	            		//DORTHEME.DorQuantity();
	            	}
					if(typeof DORTHEME != "undefined" && typeof DORTHEME.LazyLoad != "undefined" && typeof DORTHEME.LazyLoad == "function"){
	            		DORTHEME.LazyLoad();
	            	}
            	},300);
                return false;
            }
        });
	},
	DealCarousel:function(){
		jQuery(".countdown-daily").each(function(){
	    	var productID = jQuery(this).attr("data-id");
	    	var time = jQuery(this).attr("data-time");
	    	var objID = "#countdown-timer-"+productID;
	    	$(objID).countdown(time, function (event) {
	            var $this = $(this).html(event.strftime(''
	                    + '<div class="item-time"><span class="dw-time">%D</span> <span class="dw-txt">Day</span></div>'
	                    + '<div class="item-time"><span class="dw-time">%H</span> <span class="dw-txt">Hour</span></div>'
	                    + '<div class="item-time"><span class="dw-time">%M</span> <span class="dw-txt">Min</span></div>'
	                    + '<div class="item-time"><span class="dw-time">%S</span> <span class="dw-txt">Sec</span></div>'));
	        });
	    });
		$('.dailydeal-content > .product_list').owlCarousel({
	        items: 1,
	        loop: true,
	        nav: true,
	        autoplay: false,
	        autoplayTimeout: 3000,
	        margin:0,
	        responsive: {
	            0: {items: 1},
	            1500: {items: 1},
	            1200: {items: 1},
	            990: {items: 1},
	            767: {items: 1},
	            551: {items: 1},
	            320: {items: 1}
	        },
	        onInitialize: function (event) {
		        if ($('.dailydeal-content > .product_list').find('article').length <= 1) {
		           this.settings.loop = false;
		        }
		    },
	        navText: ["<i class='fa fa-long-arrow-left'></i>", "<i class='fa fa-long-arrow-right'></i>"]
	    });
	}
};



jQuery(document).ready(function($){
	DORDEALS.init();

	$("body").on("click",".special-thumb-image > li a",function(event){
		event.preventDefault();
		var fullImage = $(this).attr("href");
		$(this).closest("ul.special-thumb-image").find("li").removeClass("active");
		$(this).closest("li").addClass("active");
		$(this).closest("article").find(".thumbnail-image-1").attr("src",fullImage);
		$(this).closest("article").find(".thumbnail-image-2").attr("src",fullImage);
	});
	
	var hMainImage = $("#countdown-data .product-image-container").height();
	if(hMainImage == 0){
		hMainImage = $("#countdown-data .ajax_block_product").height();
	}
	$(".special-thumb-data").css("height",hMainImage+"px");
	$( window ).resize(function() {
		var hMainImage = $("#countdown-data .product-image-container").height();
		if(hMainImage == 0){
			hMainImage = $("#countdown-data .ajax_block_product").height();
		}
		$(".special-thumb-data").css("height",hMainImage+"px");
	});
	jQuery("body").on("click",".deal-arrow span",function(event){
		event.preventDefault();
		var marginBottom = 15;
		var checkPos = $(this).hasClass("arrow-bottom");
		var productId = $(this).closest("article").attr("data-id-product");
		var totalThumb = $(".owl-item.active #deal-thumb-productid-"+productId+" li").length;
		var heightThumb = $("#deal-thumb-productid-"+productId).css("height");
		heightThumb = parseInt(heightThumb);
		var heightPerThumb = $(".owl-item.active #deal-thumb-productid-"+productId+" .special-thumb-image li").height();
		var heightTotalThumbs = heightPerThumb*totalThumb + (marginBottom*totalThumb);
		var checkHThumb = heightThumb;
		var getMarginTop = $(".owl-item.active #deal-thumb-productid-"+productId+" .special-thumb-image").css("margin-top");
		getMarginTop = parseInt(getMarginTop)*(-1);
		checkHThumb = checkHThumb + getMarginTop;
		if(checkPos){
			if(checkHThumb < heightTotalThumbs){
				$('.special-thumb-data-inner .special-thumb-image').animate({
					marginTop: '-='+(heightPerThumb+marginBottom)
				}, 500);
				checkHThumb = checkHThumb + (heightPerThumb+marginBottom);
			}
		}else{
			if(checkHThumb > heightTotalThumbs && getMarginTop > 0){
				$('.special-thumb-data-inner .special-thumb-image').animate({
					marginTop: '+='+(heightPerThumb+marginBottom)
				}, 500);
				checkHThumb = checkHThumb - (heightPerThumb+marginBottom);
			}
		}
	});
});