function OCTgeturlparam(param) {
	var urlParams = new URLSearchParams(window.location.search);
	return urlParams.get(param) !== null ? param+'='+urlParams.get(param) : '';
}
var OCT4_GET_LANG = '';
var bogofr_ur1201 = 'index.php?route=total/bogofr/';
var bogofr_url230 = 'index.php?route=extension/total/bogofr/';
var bogofr_url401 = 'index.php?route=extension/bogofr/total/bogofr|';
var bogofr_url402 = 'index.php?route=extension/bogofr/total/bogofr.';

var ribarray = ["bogofr_rib1", "bogofr_rib2", "bogofr_rib3", "bogofr_rib4", "bogofr_rib5", "bogofr_rib6", "bogofr_rib7", "bogofr_rib8"];
var bogofr = {
	'settimer': function() {
		var countDownDate = [];
		var xtimer = [];
		$.each($("[class*='bogofrtimer']"), function() {
			if(! $(this).attr('endt')) { 
			return false;
			}
			bogofr_id = $(this).attr('bogofr_id').toString();			
			endt = $(this).attr('endt').toString();
			countDownDate[bogofr_id] = new Date(endt).getTime();
			//console.log(new Date(endt)); console.log(new Date());
 			(function(bogofr_id) {
				xtimer[bogofr_id] = setInterval(function() {
					now = new Date().getTime();
					distance = countDownDate[bogofr_id] - now;
					days = Math.floor(distance / (1000 * 60 * 60 * 24));
					hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
					minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
					seconds = Math.floor((distance % (1000 * 60)) / 1000);
					format = days + "d " + hours + "h "+ minutes + "m " + seconds + "s ";
					if(days == 0) {
						format = hours + "h "+ minutes + "m " + seconds + "s ";
					}
 					$('.bogofrtimer'+bogofr_id).html(format + ' <b class="fa fa-clock-o"></b>');
					if (distance < 0) {
						clearInterval(xtimer[bogofr_id]);
						$('.bogofr'+bogofr_id).remove();					
					}
				}, 1000);
			})(bogofr_id);
 		});
	},
	'setpophtml': function(bgid, title, htmldata) {		
		var pophtml = '<div id="pop-bogofr'+bgid+'" class="modal fade" role="dialog"> <div class="modal-dialog modal-lg"> <div class="modal-content">';
		pophtml += '<div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4>'+title+'</h4> </div>';
		pophtml += '<div class="modal-body">'+htmldata+'</div>';
		pophtml += '</div></div></div>';
		$('body').append(pophtml);
		bogofr.settimer();
	},
	'setpophtmloc4': function(bgid, title, htmldata) {		
		var pophtml = '<div id="pop-bogofr'+bgid+'" class="modal fade" role="dialog"> <div class="modal-dialog modal-lg"> <div class="modal-content">';
		pophtml += '<div class="modal-header"> <h4>'+title+'</h4> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div>';
		pophtml += '<div class="modal-body">'+htmldata+'</div>';
		pophtml += '</div></div></div>';
		$('body').append(pophtml);
		bogofr.settimer();
	},
	'atgrid': function() {
  		var product_ids = [];
 		$("[onclick*='cart.add'], [onclick*='addToCart'], .product-thumb form input[name='product_id']").each(function() {
			if($(this).closest('form').find("[name*='product_id']").length) {
				var target = $(this).closest('.product-thumb, .product-wrapper');
				var product_id = $(this).val(); 
			} else {
				var target = $(this).closest('.product-thumb, .product-wrapper');
				var product_id = $(this).attr('onclick').match(/[0-9]+/).toString(); 
			}
			if(target.length && target.hasClass('bogofr_atgridcol') == false) {
				target.addClass('bogofr_atgridcol').addClass('bogofr_atgridcol'+product_id);
				product_ids.push(product_id);
 			}
		});
		if(product_ids.length) { 
			//console.log(product_ids.length);
 			$.ajax({
				url: bogofr_url230 + 'getcache' + OCT4_GET_LANG,
				type: 'post',
				data: {product_ids:product_ids},
				cache: true,
				complete: function() { 
				},
				success: function(json) {
					if(json) {
						//console.log(json);
						$("[onclick*='cart.add'], [onclick*='addToCart'], .product-thumb form input[name='product_id']").each(function() {
 							if($(this).closest('form').find("[name*='product_id']").length) {
								var target = $(this).closest('.product-thumb, .product-wrapper');
								var product_id = $(this).val(); 
							} else {
								var target = $(this).closest('.product-thumb, .product-wrapper');
								var product_id = $(this).attr('onclick').match(/[0-9]+/).toString(); 
							}
							if(json[product_id]) {
								var cnt = -1;
								$.each(json[product_id], function(bgid,val) {
									if(target.length && target.find('.bogofr'+bgid).length == 0) {
										cnt = cnt+1; if(cnt==8) { cnt = 1;}
										if(val['rib'] !== undefined && val['rib'].length) { 
											target.prepend('<div class="bogofr_rib bogofr'+bgid+'">'+val['rib']+'</div>');
										}
									}
								});
							}
						});
					}
				}
			});
		}		
  	},	
	'prodpage': function() {
		var product_ids = [];
		var product_id = false;
		
		if($(".button-group-page input[name='product_id']").length) {
			product_id = $(".button-group-page input[name='product_id']").val();
			product_ids.push(product_id);
		} else if($('#input-product-id').length) {
			product_id = $('#input-product-id').val();
			product_ids.push(product_id);
		} else if($(".product-info input[name='product_id']").length) {
			product_id = $(".product-info input[name='product_id']").val();
			product_ids.push(product_id);
		} else if($("#product input[name='product_id']").length) {
			product_id = $("#product input[name='product_id']").val();
			product_ids.push(product_id);
		} 
			
		if (product_ids.length) {
			$.ajax({
				url: bogofr_url230 + 'getcache' + OCT4_GET_LANG,
				type: 'post',
				data: {product_ids:product_ids},
				cache: true,
				complete: function() { 
				},
				success: function(json) {
					if(json) {
						//console.log(json);
						var target = $('.col-sm-8 ul.thumbnails li:eq(0)');
						var target_J2 = $('.route-product-product .product-info .left .image');
						var target_J3 = $('.product-left .product-image');
						var target_OC4 = $('.magnific-popup');
						
						if(json[product_id]) {
							var cnt = -1;
							$.each(json[product_id], function(bgid,val) {
								if(target.length && target.find('.bogofr'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target.prepend('<div class="bogofr_rib bogofr'+bgid+'">'+val['rib']+'</div>');
									}
									
									var offer_heading = val['offer_heading'];
									var offer_content = val['offer_content'];
									
									if(val['showofferat'] == 0) {
										$('#product').before(offer_content);
									}
									if(val['showofferat'] == 1) {
										$('#product').before('<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#pop-bogofr'+bgid+'">'+offer_heading+'</button>');
										bogofr.setpophtml(bgid, offer_heading, offer_content);
									}
									if(val['showofferat'] == 2) {
										$('ul.nav-tabs').before(offer_content);
									}
									if(val['showofferat'] == 3) {
										$('ul.nav-tabs').append('<li><a href="#tab-bogofr'+bgid+'" data-toggle="tab">'+offer_heading+'</a></li>');
										$('.tab-content').append('<div class="tab-pane" id="tab-bogofr'+bgid+'">'+offer_content+'</div>');
									}
								}
								
								// for OC4
								if(target_OC4.length && target_OC4.find('.bogofr'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_OC4.prepend('<div class="bogofr_rib bogofr'+bgid+'">'+val['rib']+'</div>');
									}
									
									var offer_heading = val['offer_heading'];
									var offer_content = val['offer_content'];
									
									if(val['showofferat'] == 0) {
										$('#product').before(offer_content);
									}
									if(val['showofferat'] == 1) {
										$('#product').before('<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#pop-bogofr'+bgid+'">'+offer_heading+'</button>');
										bogofr.setpophtmloc4(bgid, offer_heading, offer_content);
									}
									if(val['showofferat'] == 2) {
										$('ul.nav-tabs').before(offer_content);
									}
									if(val['showofferat'] == 3) {
										$('ul.nav-tabs').append('<li class="nav-item"><a class="nav-link" href="#tab-bogofr'+bgid+'" data-bs-toggle="tab">'+offer_heading+'</a></li>');
										$('.tab-content').append('<div class="tab-pane" id="tab-bogofr'+bgid+'">'+offer_content+'</div>');
									}
								}
								
								// for j2
								if(target_J2.length && target_J2.find('.bogofr'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_J2.prepend('<div class="bogofr_rib bogofr'+bgid+'">'+val['rib']+'</div>');
									}
									
									var offer_heading = val['offer_heading'];
									var offer_content = val['offer_content'];
									
									if(val['showofferat'] == 0) {
										$('.route-product-product #product ul.price').after(offer_content);
									}
									if(val['showofferat'] == 1) {
										$('.route-product-product #product .cart').after('<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#pop-bogofr'+bgid+'">'+offer_heading+'</button>');
										bogofr.setpophtml(bgid, offer_heading, offer_content);
									}
									if(val['showofferat'] == 2) {
										$('.route-product-product .product-tabs').prepend(offer_content);
									}
									if(val['showofferat'] == 3) {
										$('.route-product-product .product-tabs ul.nav-tabs').append('<li><a href="#tab-bogofr'+bgid+'" data-toggle="tab">'+offer_heading+'</a></li>');
										$('.route-product-product .product-tabs .tabs-content').append('<div class="tab-pane tab-content" id="tab-bogofr'+bgid+'">'+offer_content+'</div>');
									}
								}
								
								// for j3
								if(target_J3.length && target_J3.find('.bogofr'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_J3.prepend('<div class="bogofr_rib bogofr'+bgid+'">'+val['rib']+'</div>');
									}
									
									var offer_heading = val['offer_heading'];
									var offer_content = val['offer_content'];
									
									if(val['showofferat'] == 0) {
										$('#product').before(offer_content);
									}
									if(val['showofferat'] == 1) {
										$('.button-group-page').append('<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#pop-bogofr'+bgid+'">'+offer_heading+'</button>');
										bogofr.setpophtml(bgid, offer_heading, offer_content);
									}
									if(val['showofferat'] == 2) {
										$('.product_tabs-top ul.nav-tabs').before(offer_content);
									}
									if(val['showofferat'] == 3) {
										$('.product_tabs-top ul.nav-tabs').append('<li><a href="#tab-bogofr'+bgid+'" data-toggle="tab">'+offer_heading+'</a></li>');
										$('.product_tabs-top .tab-content').append('<div class="tab-pane" id="tab-bogofr'+bgid+'">'+offer_content+'</div>');
									}
								}
							});
						}					
					}
				}
			});
		}
	},
	'initjson': function() {
		bogofr.atgrid(); 
		$(document).ajaxStop(function() { bogofr.atgrid(); bogofr.settimer();});
		bogofr.prodpage();
	}	
}
$(document).ready(function() {
bogofr.initjson();
});