var Product = Product||{};
(function() {
'use strict';
var _$priceDiv = null;
var _$dimmensionsSelect = null;
var _$quantitySelect = null;
var _$colorContainer = 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('#allColorsDiv');
}
return _$colorContainer;
};
/**
* Event when select for dimmensions or hardness is changed.
* It updates price.
*/
Product.onProductPropertySelectChange = function(sender) {
//Set colors div
_setColorsDiv();
var productId = _getProductIdFromProperties();
if(productId != null) {
var pdata = jabox.getProduct(productId);
_constructPriceImages(pdata.getRegularPrice(), pdata.getActionPrice());
}
};
var _setColorsDiv = function() {
var dimkey = _get$dimmensionsSelect().val();
var plist = prodsByPriceProp[dimkey];
if(plist.length < 2) {
_get$colorContainer().hide();
} else {
var dimColors = {};
var prevSelectedColor = _get$colorContainer().find('input:checked').val();
for(var i=0; i check first visible in return it
colorId = _get$colorContainer().find('.col-div:visible:first').addClass('selected').find('input').prop('checked', true).val();
if(Common.isEmptyString(colorId)) {
return null;
}
return _returnTheOneWithColor(plist, colorId);
}
};
/**
* Construct price from images.
* @param regularPrice {String} String representing regular price.
* @param actionPrice {String} String representing action price (null if product is not in action).
*/
var _constructPriceImages = function(regularPrice, actionPrice) {
if(_get$priceDiv().prev().hasClass('regularprice')) {
_get$priceDiv().prev().remove();
}
_get$priceDiv().empty();
if(Common.isEmptyString(regularPrice) || parseFloat(regularPrice)<=0) {
return;
}
var activePrice = actionPrice==null?regularPrice:actionPrice;
if(actionPrice != null) {
if(_get$priceDiv().prev().hasClass('regularprice')) {
_get$priceDiv().prev().html('' + regularPrice + '');
} else {
var regDiv = Common.createEl('div', {c:'regularprice'});
jQuery(regDiv).append('' + regularPrice + '');
_get$priceDiv().before(regDiv);
}
}
Common.priceToImage(_get$priceDiv(), activePrice);
/*
var afterSep = false;
var basicImgSrc = '/images/project/price/cena_';
_get$priceDiv().append(Common.createEl('img', {'border':0, 'src':basicImgSrc+'od.png'}));
for(var i=0; i
* 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);
//Show floater
jQuery.fancybox(json.cfhtml, {'autoDimensions':true});
} 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());
}
};
})();