var hideprcatcifstck_ur115_20X = 'index.php?route=module/hideprcatcifstck/';
var hideprcatcifstck_url23X_30X = 'index.php?route=extension/module/hideprcatcifstck/';
var hideprcatcifstck_url401X = 'index.php?route=extension/hideprcatcifstck/module/hideprcatcifstck|';
var hideprcatcifstck_url402X = 'index.php?route=extension/hideprcatcifstck/module/hideprcatcifstck.';

var hideprcatcifstck = {	
	'atgrid': function() {
  		var product_ids = [];
 		$("[onclick*='cart.add'], [onclick*='addToCart'], button[formaction*='checkout/cart.add']").each(function() {
			if($(this).closest('form').find("[name*='product_id']").length) {
				var product_id = $(this).closest('form').find("[name*='product_id']").val();	
				var target = $(this).closest('.product-thumb, .product-wrapper');
			} else if($(this).attr('onclick')) {
				var product_id = $(this).attr('onclick').match(/[0-9]+/).toString();
				var target = $(this).closest('.product-thumb, .product-wrapper');
			}
			if(target.length && target.hasClass('main_hideprcatcifstckatgrid') == false) {
				target.addClass('main_hideprcatcifstckatgrid').addClass('main_hideprcatcifstckatgrid'+product_id);
				product_ids.push(product_id);
 			}
		});
		if(product_ids.length) { 
			//console.log(product_ids.length);
 			$.ajax({
				url: hideprcatcifstck_url23X_30X + 'getcache',
				type: 'post',
				data: {product_ids:product_ids},
				cache: true,
				success: function(json) {
					if(json) {
						//console.log(json);
						$("[onclick*='cart.add'], [onclick*='addToCart'], button[formaction*='checkout/cart.add']").each(function() {
 							if($(this).closest('form').find("[name*='product_id']").length) {
								var product_id = $(this).closest('form').find("[name*='product_id']").val();	
								var target = $(this).closest('.product-thumb, .product-wrapper');
							} else if($(this).attr('onclick')) {
								var product_id = $(this).attr('onclick').match(/[0-9]+/).toString();
								var target = $(this).closest('.product-thumb, .product-wrapper');
							}	
							if(json[product_id] && target.length) {
								if (json['hideprc'] == 1) { 
									target.find('.price').remove(); 
								}
								if (json['hideatc'] == 1) { 
									$(this).remove();
									target.find('.btn-1-extra').remove(); 
								}
 							}
						});
					}
				}
			});
		}		
  	},	
	'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: hideprcatcifstck_url23X_30X + 'getcache',
				type: 'post',
				data: {product_ids:product_ids, prdpgflag:1},
				cache: true,
				success: function(json) {
					if(json) {
						if(json[product_id]) {
							if (json['hideprc'] == 1) { 
								$('#product').parent().find('ul').eq(1).remove(); 
								$('.product-info .price').remove();
								$("#product ul.price").remove();
								$('#product .price-wrapper').remove();
							}
							if (json['hideatc'] == 1) { 
								$('#button-cart').closest('#form-product').remove();
								$('#button-cart').remove();	
								$('#product .btn-1-extra').remove(); 
								$('#input-quantity').remove();
								$('label.control-label').remove();
								//$('div.price-cartbox').remove();

							}
						}
					}
				}
			});
 		}
  	},
	'initjson': function() {
		hideprcatcifstck.atgrid(); 
		$(document).ajaxStop(function() { hideprcatcifstck.atgrid(); 
			$("[onclick*='cart.add'], [onclick*='addToCart'], button[formaction*='checkout/cart.add'], #button-cart, .price, .col-sm-4 ul.list-unstyled, #product-info ul.list-unstyled, #product ul.price, #product .price-wrapper").attr('style','opacity:1;'); 
		});
		hideprcatcifstck.prodpage();
	}
}
$(document).ready(function() {
hideprcatcifstck.initjson();
});