$(document).ready(function() {
	initCombImgOnProductList();
});

function initCombImgOnProductList() {
	var timerCatAttr = new Array();
	$(CIOP_JqSelector).each(function(i) {
		if(!$(this).children('.product_hide_elements').length)$(this).append('<div class="product_hide_elements"><div class="pm_combimgonproductlist"></div></div>');
		var curSrc = $(this).children('.center_block').children('.product_img_link').children('img').attr('src');
		var ids = curSrc.match(/[0-9]+\-[0-9]+\-/g);
		if(ids != null) {
			var reg = new  RegExp("[-]+", "g");
			var id = ids[0].split(reg);
			var id_product = id[0];
			var curIdImg = id[1];
			if(CIOP_ALWAYS_DISPLAY_SQUARE) {
				var e = $(this).children('.product_hide_elements');
				$(e).children('.pm_combimgonproductlist').load(pathCombImgOnCat+'ajax.php', {action: 'getCombination',id_product: id_product,id_image:curIdImg},function(response, status, xhr) {
					$(e).show();
					initAttributesImageLoad($(this));
				});
			}
			else {
				$(this).hover(
				function() {
					var e = $(this).children('.product_hide_elements');
					if(timerCatAttr[i] != false) clearTimeout(timerCatAttr[i]);
					if($(this).children('.product_hide_elements').children('.pm_combimgonproductlist').html() == '') {
						$(e).children('.pm_combimgonproductlist').load(pathCombImgOnCat+'ajax.php', {action: 'getCombination',id_product: id_product,id_image:curIdImg},function(response, status, xhr) {
							$(e).show();
							initAttributesImageLoad($(this));
						});
					}else { $(this).children('.product_hide_elements').fadeIn('fast');}
				},function () {
					if($(this).children('.product_hide_elements').children('.pm_combimgonproductlist').html() == '') var timerLenght = 1000;
					else  var timerLenght = 50;
					var e = $(this).children('.product_hide_elements');
					timerCatAttr[i] = setTimeout(function() {
						$(e).fadeOut('fast');
					},timerLenght);
				});
			}
		}
	});
}

function initAttributesImageLoad(e) {
	if(CIOP_tooltip) {
		$(e).children('.color_picker').children('a').tooltip({
		    track: true,
		    delay: 0,
		    showURL: false,
		    showBody: " - ",
		    fade: 250,
		    id: "pm_combimgonproductlist_tooltip"
		});
	}
	$(e).children('.color_picker').children('a').click(function() {
		  var curid = $(this).attr('id');
		  var reg = new  RegExp("[_]+", "g");
		  var id = curid.split(reg);
		  var id_image = id[1];
		  changeCatImgProduct($(this),id_image);
	});
}

function changeCatImgProduct(e,id_image) {
	if(id_image == '0') return;
	var curImg = $(e).parents(".ajax_block_product").find('.product_img_link').children('img');
	var curSrc = $(curImg).attr("src");
	var newSrc = curSrc.replace(/([0-9]+\-)[0-9]+(\-)/g,"$1"+id_image+"$2");
	if(newSrc && newSrc != curSrc) {
		$(e).parent(".color_picker").children('.color_pick').removeClass('selected');
		$(e).addClass('selected');
		$('<img />')
	    .attr('src', newSrc)
	    .load(function(){
	    	$(curImg).fadeOut("fast",function() {
				$(this).attr("src",newSrc);
				$(this).fadeIn("fast");
			});
	    });

	}
}
