var Product = Product||{};
(function() {
	'use strict';
	
	var _$priceDiv = null;
	var _$dimmensionsSelect = null;
	var _$quantitySelect = null;
	var _$colorContainer = null;
	
	var _$cartContainer = null;
	var _$cartContainerMobile = null;
	
	var _$actionPriceContainer = null;
	var _$regularPriceContainer = null;
	
	var _get$priceDiv = function() {
		if(_$priceDiv == null) {
			_$priceDiv = jQuery('#priceDiv');
		}
		return _$priceDiv;
	};
	
	var _get$dimmensionsSelect = function() {
		if(_$dimmensionsSelect == null) {
			_$dimmensionsSelect = jQuery('#dimSelect');
		}
		return _$dimmensionsSelect;
	};
	
	var _get$quantitySelect = function() {
		if(_$quantitySelect == null) {
			_$quantitySelect = jQuery('#quantSelect');
		}
		return _$quantitySelect;
	};
	
	var _get$colorContainer = function() {
		if(_$colorContainer == null) {
			_$colorContainer = jQuery('#colSelect');
		}
		return _$colorContainer;
	};
	
	var _get$cartContainer = function() {
		if(_$cartContainer == null) {
			_$cartContainer = jQuery('#cartNumContainer');
		}
		return _$cartContainer;
	};
	
	var _get$mobileCartContainer = function() {
		if(_$cartContainerMobile == null) {
			_$cartContainerMobile = jQuery('#cartNumContainerMobile');
		}
		return _$cartContainerMobile;
	};
	
	var _get$actionPriceContainer= function() {
		if(_$actionPriceContainer == null) {
			_$actionPriceContainer = jQuery('#ppriceAction');
		}
		return _$actionPriceContainer;
	};
	
	var _get$regularPriceContainer = function() {
		if(_$regularPriceContainer == null) {
			_$regularPriceContainer = jQuery('#ppriceRegular');
		}
		return _$regularPriceContainer;
	};
	
	/**
	 * Event when select for dimmensions or hardness is changed.
	 * It updates price.
	 */
	Product.onProductPropertySelectChange = function(sender) {
		var productId = _getProductIdFromProperties();
		if(productId != null) {
			var pData = jabox.getProduct(productId);
			_setPriceDivs(pData);
		} else {
			_setPriceDivs(null);
		}
		
		_setColorsDiv();
	};
	
	var _setPriceDivs = function(pData) {
		if(!pData) {
			_get$regularPriceContainer().text("/");
			_get$actionPriceContainer().text("");
			return;
		}
		_get$regularPriceContainer().text(pData.regularPrice);
		_get$actionPriceContainer().text(pData.actionPrice != null ? pData.actionPrice : pData.regularPrice);
		
	}
	
	var _setColorsDiv = function() {
		var dimkey = _get$dimmensionsSelect().val();
		var plist = prodsByPriceProp[dimkey];
		if(plist.length < 2) {
			_get$colorContainer().hide();
		} else {
			_get$colorContainer().show();
			var dimColors = {};
			var selectedColorId = _get$colorContainer().find(":selected").val();
			
			for(var i=0; i check first visible in return it 
			colorId = _get$colorContainer().find('option').first().val();
			if(Common.isEmptyString(colorId)) {
				return null;
			}
			return _returnTheOneWithColor(plist, colorId);
		}
	};
	
	
	/**
	 * Event when button for ordering is clicked.
	 * It sends ajax request with code and quantity.
	 */
	Product.onOrderButtonClick = function(sender) {
		var orderProductId = _getProductIdFromProperties();
		if(orderProductId == null) {
			return; //Wrong product
		}
		
		var requestData = {};
		requestData['m'] = 'sp';
		requestData['pid'] = orderProductId;
		requestData['qnt'] = _get$quantitySelect().val();
		Common.removeAllMsgs();
		jQuery.ajax({
			url: '/ajax/addtocontainer',
			data: (requestData),
			type: 'post',
			success: function(json) {
				if(json == null) {
					_onOrderButtonClickError();
				} else if(json.stat == '0') {
					//Some error in quantities
					_onOrderButtonClickError(Utf8.replaceChars('{Zh}al je pri{sh}lo do napake pri naro{ch}anju izdelka.'));
				} else if(json.stat == '1') {
					//All OK
					Common.showInfoMsg(Utf8.replaceChars('Izdelek je bil dodan v ko{sh}arico.'));
					//Update container string
					Common.updateBasketData(json);
					
					// Update cart num
					let currCartNum = parseInt(_get$cartContainer().html());
					currCartNum = !!currCartNum ? currCartNum : 0; 
					
					currCartNum = currCartNum + 1;
					_get$cartContainer().html(currCartNum);	
					_get$mobileCartContainer().html(currCartNum);
				
					_get$cartContainer().removeClass("hidden");
					_get$mobileCartContainer().removeClass("hidden");
					
					
				} else {
					_onOrderButtonClickError(JSON.getErrorMessageFromResponse(json));
				}
			},
			error: function() {
				_onOrderButtonClickError();
			},
			dataType: 'json'
		}); 
	};
	
	var _onOrderButtonClickError = function(error) {
		Common.showErrorMsg(error==null?Utf8.replaceChars('{Zh}al je pri{sh}lo do napake pri naro{ch}anju izdelka.'):error);
	};
	
	Product.onColorDivClick = function(sender) {
		jQuery(sender).closest('.colors-cont').find('.col-div').removeClass('selected').find('input').prop('checked', false);
		jQuery(sender).closest('.col-div').addClass('selected').find('input').prop('checked', true);
		
		var productId = _getProductIdFromProperties();
		if(productId != null) {
			var pdata = jabox.getProduct(productId);
			_constructPriceImages(pdata.getRegularPrice(), pdata.getActionPrice());
		}
	};
	
	Product.initCall = function() {
		let initProductId = _getProductIdFromProperties();
		
		var initPData = jabox.getProduct(initProductId);
		_setPriceDivs(initPData);
	}
})();