if(!pmaInstance){
var pmaInstance = {
debug: false,
cssId: "pma-",
baseUrl: "https://public.pricemania.sk",
loadedModals: [],
inicializedElements: [],
setDebug: function(debug){
if(debug){
this.debug = debug;
}
else{
this.debug = true;
}
},
initWidgets: function() {
this.initProductWidgets();
this.initModalProductWidget();
this.initButtonProductWidget();
},
initProductWidgets: function() {
var productWidgetElements = this.findProductWidgetElements();
if(productWidgetElements && productWidgetElements.length){
for(var productWidgetElement of productWidgetElements){
this.insertProductWidgetToIframe(productWidgetElement.elements,productWidgetElement.dataId);
}
}
},
insertProductWidgetToIframe: function(elements,dataId) {
for(var element of elements){
element.innerHTML = `
`;
}
},
// ==================== MODAL WIDGET ========================
initModalProductWidget: function() {
var instance = this;
var modalOnClickFunction = function(event,dataId){
event.preventDefault();
if(!instance.loadedModals.includes(dataId)){
instance.loadedModals.push(dataId);
instance.appendModalCss();
instance.appendModalHtml(dataId);
// instance.insertModalProductScript(dataId);
var modalWidgetElement = instance.findModalWidgetProductElement(dataId);
instance.insertProductWidgetToIframe([modalWidgetElement],dataId);
var span = document.getElementsByClassName(`${instance.cssId}close`)[0];
span.onclick = function() {
modal.style.display = "none";
}
iFrameResize({autoResize:true},'.pma-widget-iframe');
}
var modal = instance.findModalWidget(dataId);
modal.style.display = "block";
};
var modalProductWidgetElements = this.findModalProductWidgetElements();
if(modalProductWidgetElements && modalProductWidgetElements.length){
var counter = 0;
for(var modalProductWidgetElement of modalProductWidgetElements){
for(const element of modalProductWidgetElement.elements){
counter++;
element.onclick = function(event){
if(event.target.dataset.id){
modalOnClickFunction(event,event.target.dataset.id);
window.onclick = function(event) {
var modal = instance.findModalWidget(event.target.dataset.id);
if (event.target == modal) {
modal.style.display = "none";
}
}
}
}
}
}
}
},
findModalWidget: function(widgetId){
var modalWidgets = document.getElementsByClassName(`${this.cssId}modal-widget-wrapper`);
for(var modalWidget of modalWidgets){
if(modalWidget.getAttribute('data-id') == widgetId){
return modalWidget;
}
}
return null;
},
findModalWidgetProductElement: function(widgetId){
var modalWidgets = document.getElementsByClassName(`${this.cssId}widget`);
for(var modalWidget of modalWidgets){
if(modalWidget.getAttribute('data-id') == widgetId){
return modalWidget;
}
}
return null;
},
insertModalProductScript: function(widgetId){
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = this.baseUrl + `/v1/widget/product/script.js?w_id=${widgetId}&force_init=true`;
try {
document.body.appendChild(s);
} catch (e) {
document.body.appendChild(s);
}
},
appendModalHtml: function(widgetId) {
var html = this.generateModalHtml(widgetId);
var bodyElement = document.getElementsByTagName("BODY")[0];
if(bodyElement){
bodyElement.insertAdjacentHTML('afterbegin', html);
}
},
appendModalCss: function() {
var css = this.generateModalCss();
var element = document.createElement('style');
element.setAttribute('type', 'text/css');
if ('textContent' in element) {
element.textContent = css;
} else {
element.styleSheet.cssText = css;
}
document.getElementsByTagName('head')[0].appendChild(element);
},
generateModalCss: function(){
var css = `
.${this.cssId}modal-widget-wrapper {
display: none;
position: fixed;
z-index: 2147483647;
padding-top: 100px;
padding-bottom: 100px;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.${this.cssId}modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 95%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: ${this.cssId}animatetop;
-webkit-animation-duration: 0.4s;
animation-name: ${this.cssId}animatetop;
animation-duration: 0.4s
}
@-webkit-keyframes ${this.cssId}animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes ${this.cssId}animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
.${this.cssId}close {
color: black;
float: right;
font-size: 40px;
font-weight: bold;
}
.${this.cssId}close:hover,
.${this.cssId}close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.${this.cssId}modal-header {
min-height: 50px;
padding: 2px 16px;
background-color: #ffffff;
color: white;
}
.${this.cssId}modal-body {padding: 2px 16px;}
.${this.cssId}modal-footer {
min-height: 50px;
padding: 2px 16px;
background-color: #ffffff;
color: white;
}
@media only screen and (min-width: 450px) {
width: 80%;
}
`;
return css;
},
generateModalHtml: function(widgetId) {
var html = `
`;
return html;
},
// =================== END MODAL WIDGET =====================
// =================== BUTTON WIDGET ========================
initButtonProductWidget: function() {
const instance = this;
const buttonWidgetElements = this.findButtonProductWidgetElements();
for(const buttonWidgetElement of buttonWidgetElements){
const originalDisplayStyles = this.hideButtonVariables(buttonWidgetElement.elements);
this.getButtonWidgetData(buttonWidgetElement.dataId,buttonWidgetElement.elements,originalDisplayStyles,function(elements,widgetId,pOriginalDisplayStyles,response){
if(response.success && response.exit_url){
for(var element of elements){
instance.insertLinkToButton(element,response.exit_url);
var originalDisplayStyle = null;
var displayStyle = pOriginalDisplayStyles.find(item => item.element === element);
if(displayStyle){
originalDisplayStyle = displayStyle.originalDisplayStyle;
}
instance.insertButtonVariables(element,originalDisplayStyle,response.shop_name, response.shop_logo, response.product_price, response.product_shipping, response.product_availability);
if(originalDisplayStyle){
element.style.display = originalDisplayStyle;
}
else{
element.style.display = "";
}
}
}
else{
for(var element of elements){
if(response.hide_without_offer === false){
var displayStyle = pOriginalDisplayStyles.find(item => item.element === element);
if(displayStyle){
element.style.display = displayStyle.originalDisplayStyle;
}
if(response.link_to_pricemania_without_offer && response.web_url){
instance.insertLinkToButton(element,response.web_url);
}
}
instance.insertButtonVariables(element,originalDisplayStyle,response.shop_name, response.shop_logo, response.product_price, response.product_shipping, response.product_availability);
}
}
});
}
},
insertLinkToButton: function(element,exitLink){
if(element.tagName == "A"){
element.href = exitLink;
element.target = "_blank";
element.rel = "nofollow";
}
else {
element.onclick = function(event){
event.stopPropagation();
window.open(exitLink);
}
}
},
hideButtonVariables: function(elements){
var originalDisplayStyles = [];
for(var element of elements){
var originalDisplayStyle = element.style.display;
element.style.display = "none";
originalDisplayStyles.push({
element: element,
originalDisplayStyle: originalDisplayStyle
})
}
return originalDisplayStyles;
},
insertButtonVariables: function(element,originalDisplayStyle,shop_name,shop_logo,product_price,product_shipping,product_availability){
element.innerHTML = element.innerHTML.replaceAll("{{shop_name}}",shop_name?shop_name:"");
element.innerHTML = element.innerHTML.replaceAll("{{shop_logo}}",shop_logo?shop_logo:"");
element.innerHTML = element.innerHTML.replaceAll("{{product_price}}",product_price?product_price:"");
element.innerHTML = element.innerHTML.replaceAll("{{product_shipping}}",product_shipping?product_shipping:"");
element.innerHTML = element.innerHTML.replaceAll("{{product_availability}}",product_availability?product_availability:"");
},
// =================== BUTTON WIDGET END ====================
findButtonProductWidgetElements: function() {
return this.findWidgetElements(['pma-button-widget']);
},
findModalProductWidgetElements: function() {
return this.findWidgetElements(['pma-modal-widget']);
},
findProductWidgetElements: function() {
return this.findWidgetElements(['pma-widget']);
},
findWidgetElements: function(classes) {
var elements = [];
for(var singleClass of classes){
const widgetElements = document.getElementsByClassName(singleClass);
for(var widgetElement of widgetElements){
var dataId = widgetElement.getAttribute('data-id');
if(dataId){
if(!this.inicializedElements.includes(widgetElement)){
var currentElement = elements.find(element => element.dataId === dataId);
if(currentElement){
currentElement.elements.push(widgetElement)
}
else{
elements.push({
dataId: dataId,
elements: [widgetElement]
});
}
this.inicializedElements.push(widgetElement);
}
else{
console.log("ELEMENT ALREADY INICIALIZED");
}
}
}
}
return elements;
},
push: function(pmaArguments){
if(pmaArguments.length){
var pmaArgumentsData = [].slice.call(pmaArguments, 0);
if(pmaArgumentsData.length){
var methodName = pmaArgumentsData[0];
var methodArguments = pmaArgumentsData.slice(1);
if(pmaInstance.hasOwnProperty(methodName)){
pmaInstance[methodName].apply(pmaInstance,methodArguments);
}
}
}
},
initTracker: function(){
var trackerPreData = Array.isArray(window.pmaWidgetLayer)?window.pmaWidgetLayer:[];
for(var i=0;ie&&(e=c,h(V,"Set "+d+" to min value")),e>b&&(e=b,h(V,"Set "+d+" to max value")),U[d]=""+e}function g(){function b(){function a(){var a=0,b=!1;for(h(V,"Checking connection is from allowed list of origins: "+d);aP[x]["max"+a])throw new Error("Value for min"+a+" can not be greater than max"+a)}b("Height"),b("Width"),a("maxHeight"),a("minHeight"),a("maxWidth"),a("minWidth")}function f(){var a=d&&d.id||S.id+F++;return null!==document.getElementById(a)&&(a+=F++),a}function g(a){return R=a,""===a&&(c.id=a=f(),G=(d||{}).log,R=a,h(a,"Added missing iframe ID: "+a+" ("+c.src+")")),a}function i(){switch(h(x,"IFrame scrolling "+(P[x].scrolling?"enabled":"disabled")+" for "+x),c.style.overflow=!1===P[x].scrolling?"hidden":"auto",P[x].scrolling){case!0:c.scrolling="yes";break;case!1:c.scrolling="no";break;default:c.scrolling=P[x].scrolling}}function k(){("number"==typeof P[x].bodyMargin||"0"===P[x].bodyMargin)&&(P[x].bodyMarginV1=P[x].bodyMargin,P[x].bodyMargin=""+P[x].bodyMargin+"px")}function l(){var a=P[x].firstRun,b=P[x].heightCalculationMethod in O;!a&&b&&r({iframe:c,height:0,width:0,type:"init"})}function m(){Function.prototype.bind&&(P[x].iframe.iFrameResizer={close:n.bind(null,P[x].iframe),resize:u.bind(null,"Window resize","resize",P[x].iframe),moveToAnchor:function(a){u("Move to anchor","moveToAnchor:"+a,P[x].iframe,x)},sendMessage:function(a){a=JSON.stringify(a),u("Send Message","message:"+a,P[x].iframe,x)}})}function o(d){function e(){u("iFrame.onload",d,c,a,!0),l()}b(c,"load",e),u("init",d,c,a,!0)}function p(a){if("object"!=typeof a)throw new TypeError("Options is not an object")}function q(a){for(var b in S)S.hasOwnProperty(b)&&(P[x][b]=a.hasOwnProperty(b)?a[b]:S[b])}function s(a){return""===a||"file://"===a?"*":a}function t(a){a=a||{},P[x]={firstRun:!0,iframe:c,remoteHost:c.src.split("/").slice(0,3).join("/")},p(a),q(a),P[x].targetOrigin=!0===P[x].checkOrigin?s(P[x].remoteHost):"*"}function w(){return x in P&&"iFrameResizer"in c}var x=g(c.id);w()?j(x,"Ignored iFrame, already setup."):(t(d),i(),e(),k(),o(v(x)),m())}function x(a,b){null===Q&&(Q=setTimeout(function(){Q=null,a()},b))}function y(){function a(){function a(a){function b(b){return"0px"===P[a].iframe.style[b]}function c(a){return null!==a.offsetParent}c(P[a].iframe)&&(b("height")||b("width"))&&u("Visibility change","resize",P[a].iframe,a)}for(var b in P)a(b)}function b(b){h("window","Mutation observed: "+b[0].target+" "+b[0].type),x(a,16)}function c(){var a=document.querySelector("body"),c={attributes:!0,attributeOldValue:!1,characterData:!0,characterDataOldValue:!1,childList:!0,subtree:!0},e=new d(b);e.observe(a,c)}var d=window.MutationObserver||window.WebKitMutationObserver;d&&c()}function z(a){function b(){B("Window "+a,"resize")}h("window","Trigger event: "+a),x(b,16)}function A(){function a(){B("Tab Visable","resize")}"hidden"!==document.visibilityState&&(h("document","Trigger event: Visiblity change"),x(a,16))}function B(a,b){function c(a){return"parent"===P[a].resizeFrom&&P[a].autoResize&&!P[a].firstRun}for(var d in P)c(d)&&u(a,b,document.getElementById(d),d)}function C(){b(window,"message",l),b(window,"resize",function(){z("resize")}),b(document,"visibilitychange",A),b(document,"-webkit-visibilitychange",A),b(window,"focusin",function(){z("focus")}),b(window,"focus",function(){z("focus")})}function D(){function b(a,b){function c(){if(!b.tagName)throw new TypeError("Object is not a valid DOM element");if("IFRAME"!==b.tagName.toUpperCase())throw new TypeError("Expected