function OCTgeturlparam(param) {
	var urlParams = new URLSearchParams(window.location.search);
	return urlParams.get(param) !== null ? param+'='+urlParams.get(param) : '';
}
var OCT4_GET_LANG = '';
var fdbypcm_ur1201 = 'index.php?route=total/fdbypcm/';
var fdbypcm_url230 = 'index.php?route=extension/total/fdbypcm/';
var fdbypcm_url401 = 'index.php?route=extension/fdbypcm/total/fdbypcm|';
var fdbypcm_url402 = 'index.php?route=extension/fdbypcm/total/fdbypcm.';

var ribarray = ["fdbypcm_rib1", "fdbypcm_rib2", "fdbypcm_rib3", "fdbypcm_rib4", "fdbypcm_rib5", "fdbypcm_rib6", "fdbypcm_rib7", "fdbypcm_rib8"];
var fdbypcm = {
	'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('fdbypcm_atgridcol') == false) {
				target.addClass('fdbypcm_atgridcol').addClass('fdbypcm_atgridcol'+product_id);
				product_ids.push(product_id);
 			}
		});
		if(product_ids.length) { 
			//console.log(product_ids.length);
 			$.ajax({
				url: fdbypcm_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('.fdbypcm'+bgid).length == 0) {
										cnt = cnt+1; if(cnt==8) { cnt = 1;}
										if(val['rib'] !== undefined && val['rib'].length) { 
											target.prepend('<div class="'+ribarray[cnt]+' fdbypcm'+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: fdbypcm_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');
						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('.fdbypcm'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target.prepend('<div class="'+ribarray[cnt]+' fdbypcm'+bgid+'">'+val['rib']+'</div>');
									}
								}
								
								// for OC4
								if(target_OC4.length && target_OC4.find('.fdbypcm'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_OC4.prepend('<div class="'+ribarray[cnt]+' fdbypcm'+bgid+'">'+val['rib']+'</div>');
									}
								}
								
								// for j2
								if(target_J2.length && target_J2.find('.fdbypcm'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_J2.prepend('<div class="'+ribarray[cnt]+' fdbypcm'+bgid+'">'+val['rib']+'</div>');
									}
								}
								
								// for j3
								if(target_J3.length && target_J3.find('.fdbypcm'+bgid).length == 0) {
									cnt = cnt+1; if(cnt==8) { cnt = 1;}
									if(val['rib'] !== undefined && val['rib'].length) { 
										target_J3.prepend('<div class="'+ribarray[cnt]+' fdbypcm'+bgid+'">'+val['rib']+'</div>');
									}
								}
							});
						}					
					}
				}
			});
		}
	},
	'initjson': function() {
		fdbypcm.atgrid(); 
		$(document).ajaxStop(function() { fdbypcm.atgrid(); });
		fdbypcm.prodpage();
	}	
}
$(document).ready(function() {
fdbypcm.initjson();
});