if(!sn){ var sn={}; } if(typeof(sn) != 'object'){ alert("Variable sn must be a javascript object"); } sn.cart = { cartContainerId: '#cartContainer', webShopRequestContextPath: '', webShopUrl: '/webshop', webShopCheckOutUrl: '/webshop_kassa/', itemIdQuantityDelimiter: '#', iframeIds: ["#googlesearch", "#googlematerialsearch", "#googlepersonsearch"], indicateChange: function() { var speedOn = 200, speedOff = 500, cartContainerId = sn.cart.cartContainerId; var settingsOn = {opacity: 0.25}; //{backgroundColor: '#F9EA68'}; var settingsOff = {opacity: 1}; //{backgroundColor: '#FFFFFF'}; window.scrollTo($(cartContainerId).position().left, $(cartContainerId).position().top); $(cartContainerId).animate(settingsOn, {duration:speedOn, complete:function(){ $(cartContainerId).animate(settingsOff, {duration:speedOff, complete:function(){ $(cartContainerId).animate(settingsOn, {duration:speedOn, complete:function(){ $(cartContainerId).animate(settingsOff, {duration:speedOff, complete:function(){ $(cartContainerId).animate(settingsOn, {duration:speedOn, complete:function(){ $(cartContainerId).animate(settingsOff, {duration:speedOff}); } });//end 5 } });// end 4 } });//end 3 } });// end 2 } });//end 1 }, doSyncJobb: function (id, articleId) { var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webShopUrl, cache:false, data:{a:'sync',itemid:id, articleid:articleId}, dataType:'html' }); }, gotoCheckOutUrl: function() { window.location=this.webShopCheckOutUrl; }, allowedNumberFormatRegExp: new RegExp("^-?\\d+$"), validateNumberFormat: function (str){ return this.allowedNumberFormatRegExp.test(str); }, removeCart: function () { var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webShopUrl, cache:false, data:"a=r", dataType:'html', success:function(msg) { var objCartTableBody = $('#cartContainer #cartItems tbody:first'); objCartTableBody.html(""); $('#webShopContainer').hide(); }, error:function (XMLHttpRequest, textStatus, errorThrown) { if(window.console){ console.log('No data received: '+textStatus); } } }); }, updateCart: function () { this.doAjaxRequestAndlistItemsInCart({a:'g'}); }, animateDiv: function (arPositionAndDimension){ var scope = this; var newDiv = $("
"); newDiv.css({ "background-color":"transparent", "border":"1px solid #C945A2", "left":arPositionAndDimension.left, "top":arPositionAndDimension.top, "width":arPositionAndDimension.width, "height":arPositionAndDimension.height, "float":"left", "z-index":300, "position":"absolute"}); $("body").append(newDiv); //scope.debug("before left:"+newDiv.position().left+",top"+newDiv.position().top); newDiv.animate({ left:$(sn.cart.cartContainerId).position().left, top:$(sn.cart.cartContainerId).position().top, width:$(sn.cart.cartContainerId).width(), height:$(sn.cart.cartContainerId).height() },{ duration:600, complete:function(){ //scope.debug("after left:"+newDiv.position().left+",top"+newDiv.position().top); newDiv.remove(); } } ); }, convertButtonToArrayPositionAndDemension: function (htmlElementButton){ if(htmlElementButton !== undefined){ var jButton = $(htmlElementButton); return {left:jButton.position().left, top:jButton.position().top, width:jButton.width(), height:jButton.height()}; } }, addIframePositionToArPositionAndDimension: function(arPositionAndDimension){ var iframeIdsLocal = this.iframeIds; for(var i=0; i < iframeIdsLocal.length; i++){ var currentIframe = $(iframeIdsLocal[i]); if(currentIframe.length > 0 ){ var left = currentIframe.position().left + arPositionAndDimension.left; var top = currentIframe.position().top + arPositionAndDimension.top; var width = arPositionAndDimension.width; var height = arPositionAndDimension.height; if( left > 0 && top > 0 && width > 0 && height > 0){ //this.debug("iframeIdsLocal[i]:" + iframeIdsLocal[i]); return {left: left, top: top, width: width, height: height}; } } } return {left:100, top:100, width:100, height:100}; }, convertButtonToArrayPositionAndDemension: function (htmlElementButton){ if(htmlElementButton !== undefined){ jButton = $(htmlElementButton); return {left:jButton.position().left, top:jButton.position().top, width:jButton.width(), height:jButton.height()}; } }, debug: function (obj){ if(window.console){ console.log(obj); } }, addToCartWithUserInteraction: function(id, numberOfItems, articleId, htmlElementButton){ if(this.validateNumberFormat(numberOfItems)){ this.addToCart(id, numberOfItems, articleId, htmlElementButton); }else{ this.addToCart(id, 1, articleId, htmlElementButton); } }, addToCart: function(id, numberOfItems, articleId, htmlElementButton, arPositionAndDimension){ if($(htmlElementButton).data('inactive') != 'true') { $(htmlElementButton).data('inactive','true').addClass('inactive'); if( numberOfItems !== undefined ){ strIdAndNumberOfItems = id+this.itemIdQuantityDelimiter+numberOfItems; this.doAjaxRequestAndlistItemsInCart({a:'ai', itemid:strIdAndNumberOfItems, articleid:articleId}, htmlElementButton, true, arPositionAndDimension); } else { this.doAjaxRequestAndlistItemsInCart({a:'ai', itemid:id, articleid:articleId}, htmlElementButton, true, arPositionAndDimension); } this.doSyncJobb(id, articleId); } }, removeItem: function(id, numberOfRows){ if(numberOfRows > 1){ this.doAjaxRequestAndlistItemsInCart({a:'ri', itemid:id}); } else { this.removeCart(); } }, doAjaxRequestAndlistItemsInCart: function(objData, htmlElementButton, addToCartEvent, arPositionAndDimension){ var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webShopUrl, cache:false, data: objData, dataType:'json', success:function(json) { var totalCost = 0; var shippingCost = 0; var totalCostIncludingShiping = 0; var strCurrency=""; var objCartTableBody = $('#cartContainer #cartItems tbody:first'); var objCartTableFoot = $('#cartContainer #cartItems tfoot:first'); objCartTableBody.html(""); objCartTableFoot.find(".totalCost").text(totalCost); objCartTableFoot.find(".shippngCost").text(shippingCost); objCartTableFoot.find(".totalCostIncludingShipping").text(totalCostIncludingShiping); if(json.numOfItems > 0) { strCurrency = ' '+ json.currency; $('#itemsincart').html(json.numOfItems); $.each(json.items, function(i, objItem) { var strId = objItem['itemId']; var strRowClass = (i%2) ? 'even' : 'odd'; $(''+ '' + ''+ objItem['title'] +'' + '' + '' + ''+ objItem['quantity'] +'st á '+ objItem['price']+ strCurrency +'' + ''+objItem['total']+ strCurrency+'' + '').appendTo(objCartTableBody); }); totalCost = parseInt(json.totalCost); totalCostIncludingShiping = totalCost + shippingCost; objCartTableFoot.find(".totalCost").text(totalCost + "" + strCurrency); objCartTableFoot.find(".shippngCost").text(shippingCost + "" + strCurrency); objCartTableFoot.find(".totalCostIncludingShipping").text(totalCostIncludingShiping + "" + strCurrency); $('#webShopContainer').show(); if(addToCartEvent){ if(htmlElementButton && arPositionAndDimension === undefined ){ scope.animateDiv(scope.convertButtonToArrayPositionAndDemension(htmlElementButton)); } else { scope.animateDiv(scope.addIframePositionToArPositionAndDimension(arPositionAndDimension)); } scope.indicateChange(); } } else { $('#webShopContainer').hide(); } if(htmlElementButton !== undefined && $(htmlElementButton).data('inactive') == 'true') { $(htmlElementButton).data('inactive','false').removeClass('inactive'); } }, error:function (XMLHttpRequest, textStatus, errorThrown) { if(window.console){ console.log('No data received: '+textStatus); } } }); } }; $(document).ready(function() { sn.cart.updateCart(); });