<?php
/* Product Option Image PRO module  */ 
if ( !empty($poip_installed) ) {  ?>
	<script  type = "text/javascript" ><!--
		
		var poip_list_custom = function(){
			poip_list_default.call(this);
		};
		poip_list_custom.prototype = Object.create(poip_list_default.prototype);
		poip_list_custom.prototype.constructor = poip_list_custom;
			
		poip_list_custom.prototype.showThumb = function(elem) {
						
			if ($(elem).attr('data-thumb') && $(elem).attr('data-image_id')) {
							
				var main_img = $('img[data-poip_id="image_'+$(elem).attr('data-image_id')+'"]');
				if (main_img.length == 0) {
					main_img = $('img[data-poip_id="'+$(elem).attr('data-image_id')+'"]');
				}
				
				var prev_img = main_img.attr('src'); // journal2 compatibility
				main_img.attr('src', $(elem).attr('data-thumb'));
				main_img.closest('a').attr('href', $(elem).attr('href'));
				
				// journal2 compatibility
				var $main_a = main_img.parent();
				if ($main_a.hasClass('has-second-image')) {
					//main_img.parent().attr('style', main_img.parent().attr('style').replace(prev_img, $(elem).attr('data-thumb')) );
					if ( !$main_a.attr('poip-original-background') ) {
						$main_a.attr('poip-original-background', $main_a.css('background'));
					}
					$main_a.css('background-image', 'url("'+$(elem).attr('data-thumb')+'")');
				}
				
			}
		};
			
		poip_list_custom.prototype.restoreSecondProductImage = function(elem) {
			if ($(elem).attr('data-thumb') && $(elem).attr('data-image_id')) {
				var main_img = $('img[data-poip_id="image_'+$(elem).attr('data-image_id')+'"]');
				if (main_img.length == 0) {
					main_img = $('img[data-poip_id="'+$(elem).attr('data-image_id')+'"]');
				}
				var $main_a = main_img.parent();
				if ($main_a.hasClass('has-second-image') && $main_a.attr('poip-original-background')) {
					$main_a.css('background', $main_a.attr('poip-original-background'));
				}
			}
		};
		
		poip_list_custom.prototype.thumbOnMouseOut = function(elem) {
			var this_object = this;
			if ( !poip_settings.img_category_click ) {
				this_object.restoreSecondProductImage(this);
			}
		};
		
		poip_list_custom.prototype.showOneProductImages = function(elem_image, poip_data) {
					
			var current_a_elem = elem_image.closest('a');
			var current_class_image_id = current_a_elem.find('img').attr('data-poip_id');
			var product_href = encodeURI( current_a_elem.attr('href') );
		
			var html = '';
		
			for (var product_option_id in poip_data) {
				html+='';
				for (var poip_data_i in poip_data[product_option_id]) {
					var option_image = poip_data[product_option_id][poip_data_i];
					var product_option_value_id = option_image['product_option_value_id'];
					
					var title = (typeof(option_image['title'])!='undefined' && option_image['title']) ? option_image['title'] : '';
					
					// SEO option URLs
					if ( option_image.keyword && option_image.product_keyword ) { // seo
						current_option_href = poip_list.getOptionSeoUrlByProductUrlAndKeywords(product_href, option_image.product_keyword, option_image.keyword);
					} else { // std
						current_option_href = product_href+(product_href.indexOf('?')==-1?'?':'&amp;')+'poip_ov='+product_option_value_id;
					}
					
					html+='<a onmouseover="poip_list.thumbOnMouseOver(this);" onmouseout="poip_list.thumbOnMouseOut(this);" onclick="return poip_list.thumbOnClick(this);" ';
					//html+='<a onmouseover="poip_list.showThumb(this);" onmouseout="poip_list.restoreSecondProductImage(this);"';
					//html+=' href="'+product_href+(product_href.indexOf('?')==-1?'?':'&amp;')+'poip_ov='+product_option_value_id+'"';
					html+=' href="'+current_option_href+'"';
					html+=' style="padding-left:6px; padding-top:2px; margin:0px; width:25px; height:25px; "';
					html+=' title="'+title+'"';
					html+=' data-thumb="'+option_image['thumb']+'"';
					html+=' data-image_id="'+current_class_image_id+'">';
					html+=' <img style="width:24px; height:24px; display: inline;" src="'+option_image['icon']+'"></a>';
					html+='</a>';
				}
			}
			if ( html ) {
				html='<div data-poip_id="poip_img" style="z-index: 5; margin-top: 5px; position: relative; ">'+html+'</div>';
				
				elem_image.closest('.image').after(html);
			}
			
		};
		
		
		poip_list_custom.prototype.after_showProductListImages = function(json) {
			var this_object = this;
      
			// add empty divs to make height of product blocks the same
			for (var poip_product_id in json['products']) {
				
				$('img[data-poip_id$="_'+poip_product_id+'"]').each( function(){
					
					if ( !$(this).closest('.image').siblings('[data-poip_id="poip_img"]').length ) {
					
						let max_height = 0;
						let $poip_thumb_containers = $(this).closest('.carousel-product, .section-product, .main-products, .box-content').first().find('[data-poip_id="poip_img"]');
						$poip_thumb_containers.each(function(){
							max_height = Math.max( max_height, $(this).height() );
						});
						
						if ( max_height ) {
							
							$poip_thumb_containers.each(function(){
								$(this).css('height', ''+max_height+'px');
							});
							
							var html='<div data-poip_id="poip_img" style="z-index: 5; margin-top: 5px; position: relative; height: '+max_height+'px;"> </div>';
							$(this).closest('.image').after(html);
						}
					}
				});
			}
			
			// for the home page box of products (custom-sections) - refresh to increase height automatically to have enough place for option images
			if ( typeof(Journal.equalHeight) == 'function' && $('div[data-poip_id="poip_img"]').closest('.box-content').length ) {
				var $isotope = $('div[data-poip_id="poip_img"]').closest('.product-grid');
				var $filter = $isotope.closest('.custom-sections').find('.box-heading a[data-option-value].selected');
				Journal.equalHeight($isotope.find('.product-grid-item'), '[data-poip_id="poip_img"]');
        
				if ( typeof($isotope.isotope) == 'function' ) {
					if ( $filter.length ) {
						$isotope.isotope({
							filter: '.' + $filter.attr('data-option-value')
						});
					} else if ( $isotope.closest('.custom-sections').is('.no-heading') ) {
						$isotope.isotope({});
					}
				}
        
			}
			
		};
		
		// SEO option URLs
		poip_list_custom.prototype.getOptionSeoUrlByProductUrlAndKeywords = function(href, product_keyword, keyword) {
			let str_pos = -1;
			let new_href = href;
			if ( href.substr( -(1+product_keyword.length) ) == '/'+product_keyword ) {
				new_href = href.substr(0, href.length-product_keyword.length) + keyword;
			} else {
				new_href = new_href.replace('/'+product_keyword+'&', '/'+keyword+'&').replace('/'+product_keyword+'?', '/'+keyword+'?');
			}
			return new_href;
		};
			
		
		var poip_list = new poip_list_custom();
	
	//--></script>
<?php } ?>	