var iwish_shop = "crown-nine.myshopify.com"; var iWishCnt=0; var iWishlistmain={}; var iWishsync=false; var iWishRemoveMsg = ''; var noItemsMsg = ''; getSession(); if (typeof iWishSelectClass === 'undefined') { var iWishSelectClass = "#product-select, .single-option-selector, .single-option-selector__radio, select[id|='product-select'], select[id|='product-variants-option'], select[id|='variant-listbox-option']"; } if (typeof iwishWrapperClass === 'undefined') { var iwishWrapperClass = 'form'; } var iwishProductWrap = ''; if (typeof iwish_added_txt === 'undefined') { var iwish_added_txt = 'Added in Wishlist'; } if (typeof iwish_add_txt === 'undefined') { var iwish_add_txt = 'Add to Wishlist'; } if (typeof iwish_wait_msg === 'undefined') { var iwish_wait_msg = 'Please wait...'; } var iWishpId = 0; var iWishvarId = 0; var iWishvarIdOld = 0; function iWishPost(e,t){method="post";var n=document.createElement("form");n.setAttribute("method",method);n.setAttribute("action",e);for(var r in t){if(t.hasOwnProperty(r)){var i=document.createElement("input");i.setAttribute("type","hidden");i.setAttribute("name",r);i.setAttribute("value",t[r]);n.appendChild(i)}}document.head.appendChild(n);n.submit()} function getSession() { if (typeof(Storage) !== "undefined") { if(iwish_cid == '' && localStorage.iWishsync == 'true') { console.log('logout! clear storage...'); localStorage.removeItem('iWishlistmain'); localStorage.removeItem('iWishCnt'); localStorage.removeItem('setupTime'); localStorage.removeItem('iWishsync'); } if (localStorage.iWishlistmain) {iWishlistmain = JSON.parse(localStorage.getItem('iWishlistmain'));} if (localStorage.iWishCnt && !isNaN(localStorage.iWishCnt)) {iWishCnt = parseInt(localStorage.iWishCnt);} if(localStorage.iWishsync) { if(parseInt(iwish_cid)>0 && localStorage.iWishsync == 'true') {iWishsync = true;} } localStorage.setItem('iWishsync',iWishsync); } } function pushToSession() { if (typeof(Storage) !== "undefined") { if (iWishlistmain) { localStorage.setItem("iWishlistmain", JSON.stringify(iWishlistmain)); } localStorage.setItem("iWishCnt",iWishCnt); } } function isInWishlist(pId,vId) { if(typeof iWishlistmain[pId] === 'undefined') {return false;} else {if(iWishlistmain[pId].indexOf(vId)>=0) {return true;} else {return false;}} } function iwish_init() { jQuery('.iWishCount').html(iWishCnt); console.log('iWishsync status:: '+iWishsync+' iwish_cid ::'+iwish_cid); if(parseInt(iwish_cid)>0) {if(!iWishsync){console.log('Sync Need to be done');syncWithServer();}} if (typeof iwish_qvButton != 'undefined' && typeof iwish_qvWrapper != 'undefined') { iwish_onInitqv(); } if (typeof iWishinitFn !== 'undefined' && typeof iWishinitFn === 'function') { iWishinitFn();} } function syncWithServer() { var dataString = JSON.stringify(iWishlistmain); jQuery.ajax({ url: '//iwish.shopapps.in/ajax/syncWishlist.php', data: { shop:iwish_shop, cId:iwish_cid, iwishlist:dataString }, dataType: 'jsonp', jsonpCallback: 'iWishCallback', success: function(json) { jQuery.extend( iWishlistmain, json.jsonDt ); iWishCnt = json.iwishCnt; pushToSession(); jQuery('.iWishCount').html(iWishCnt); console.log('iWishsync done :: '+JSON.stringify(iWishlistmain)); iWishsync=true; localStorage.setItem('iWishsync',iWishsync); if (typeof iWishsyncFn !== 'undefined' && typeof iWishsyncFn === 'function') { iWishsyncFn();} }, error: function(XMLHttpRequest, textStatus, errorThrown) {} }); } function iwish_onInitqv() { iCanUseOn = !!jQuery.fn.on; if(iCanUseOn) { jQuery("body").on('click', iwish_qvButton, function () { iwish_qvDelay(); }); } else { jQuery("body").delegate(iwish_qvButton, 'click', function () { iwish_qvDelay(); }); } } function iwish_qvDelay() { setTimeout(function() { if (jQuery(iwish_qvWrapper+':visible').find('.iWishAdd').length > 0) { iWishvarId = jQuery(iwish_qvWrapper+':visible').find('[name=id]').val(); checkIwish(jQuery(iwish_qvWrapper+':visible').find(iwishWrapperClass), iWishvarId); if (typeof iWishQvFn !== 'undefined' && typeof iWishQvFn === 'function') { iWishQvFn();} } else { iwish_qvDelay(); } }, 300); } function iWishOnSelectChange() { iCanUseOn = !!jQuery.fn.on; if(iCanUseOn) { jQuery(document).on('change', iWishSelectClass, function () { selectedClass = jQuery(this); setTimeout(function(){ iWishvarId = selectedClass.parents(iwishWrapperClass).find('[name=id]').val(); checkIwish(selectedClass.parents(iwishWrapperClass), iWishvarId); },400); }); } else { jQuery(document).delegate(iWishSelectClass, 'change', function () { selectedClass = jQuery(this); setTimeout(function(){ iWishvarId = selectedClass.parents(iwishWrapperClass).find('[name=id]').val(); checkIwish(selectedClass.parents(iwishWrapperClass), iWishvarId); },400); }); } } function checkIwish(iwishFrmObj, iWishvId) { var iwishCurrObj = iwishFrmObj.find('.iWishAdd'); iWishpId = iwishCurrObj.attr('data-product'); console.log("checkIwish "+iWishpId+ ' :: ' + iWishvId); iwishCurrObj.html(iwish_wait_msg); var checkAganId = isInWishlist(iWishpId,iWishvId); if(checkAganId) { iwishCurrObj.addClass('iwishAdded').html(iwish_added_txt); } else { iwishCurrObj.removeClass('iwishAdded').html(iwish_add_txt); } if (typeof iWishSelectChangeFn !== 'undefined' && typeof iWishSelectChangeFn === 'function') { iWishSelectChangeFn(iwishCurrObj);} } function iwish_add(iwishAdd, iWishvId) { iWishpId = iwishAdd.attr("data-product"); if(iwishAdd.hasClass("iwishAdded") || iWishvId=="" || iWishpId==""){return false;} iwishAdd.html(iwish_wait_msg); var iwishpTitle = iwishAdd.attr("data-pTitle"); iwish_addOnly(iWishpId, iWishvId, iwishpTitle); if(iWishlistmain[iWishpId].indexOf(iWishvId)>=0) { iwishAdd.addClass("iwishAdded").html(iwish_added_txt); if(iwish_cid==''){ var iwishFrmObj = iwishAdd.parents(iwishWrapperClass); iwishFrmObj.find(".iWishLoginMsg").fadeIn(500); setTimeout(function(){ iwishFrmObj.find('.iWishLoginMsg').fadeOut(); }, 6000); } } if (typeof iWishAddFn !== "undefined" && typeof iWishAddFn === "function") { iWishAddFn();} } function iwish_addCollection(iwishAdd,iWishvId) { iWishpId = iwishAdd.attr("data-product"); if(iwishAdd.hasClass("iwishAdded") || iWishvId=="" || iWishpId==""){return false;} iwishAdd.html(iwish_wait_msg); var iwishpTitle = iwishAdd.attr("data-pTitle"); iwish_addOnly(iWishpId, iWishvId, iwishpTitle); if(iWishlistmain[iWishpId].indexOf(iWishvId)>=0) { iwishAdd.addClass("iwishAdded").html(iwish_added_txt); } if (typeof iWishaddCollFn !== "undefined" && typeof iWishaddCollFn === "function") { iWishaddCollFn();} } function iwish_addOnly(iWishpId, iWishvId, iwishpTitle) { if (typeof(iWishlistmain[iWishpId]) === "undefined"){ iWishlistmain[iWishpId]=[]; } console.log("Adding "+iWishpId+" :: "+iWishvId); if(iWishlistmain[iWishpId].indexOf(iWishvId)<0){ iWishlistmain[iWishpId].push(iWishvId); iWishCnt++; pushToSession(); jQuery(".iWishCount").html(iWishCnt); if(parseInt(iwish_cid)>0){ jQuery.ajax({ url: '//iwish.shopapps.in/ajax/iwishAdd.php', data: { shop:iwish_shop, cId:iwish_cid, pId:iWishpId, vId:iWishvId, pTitle:iwishpTitle }, dataType: "jsonp", jsonpCallback: "iWishCallback", success: function(json) {}, error: function(XMLHttpRequest, textStatus, errorThrown) {} }); } } } function iwish_remove(iwishRemove, vId, iwishProxy) { pId = iwishRemove.attr("data-product"); if(typeof iWishlistmain[pId] === 'undefined') { return false; } else { var delindex = iWishlistmain[pId].indexOf(vId); //console.log('iWishlistmain :: '+JSON.stringify(iWishlistmain)); //console.log('delindex :: '+delindex); if(delindex>=0){ iWishlistmain[pId].splice(delindex, 1); if(iWishCnt>0) { iWishCnt--; } pushToSession(); jQuery('.iWishCount').html(iWishCnt); //if(parseInt(iwish_cid)>0){ jQuery.ajax({ url: '//iwish.shopapps.in/ajax/iwishRemove.php', data: { shop:iwish_shop, cId:iwish_cid, pId:pId, vId:vId}, dataType: 'jsonp', jsonpCallback: 'iWishCallback', success: function(json) { iWishRemoveMsg = json.pro_remove_msg; noItemsMsg = json.no_items_msg; iwishRemove.removeClass("iwishAdded"); var iwishProxy = iwishProxy || false; if (iwishProxy == false) { iwishRemove.html(iwish_add_txt); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { } }).done(function() { if(jQuery(".iwishMsgSuccess").length==0) { jQuery(".iwishMsg").append(iWishRemoveMsg); } setTimeout( function() { jQuery(".iwishMsgSuccess").fadeOut().remove(); if(iWishCnt==0 && jQuery(".iwishMsgInfo").length==0 ) { jQuery(".iwishMsg").append(noItemsMsg); } }, 3000); }); //} if (typeof iWishRemoveFn !== 'undefined' && typeof iWishRemoveFn === 'function') { iWishRemoveFn();} return true; } else { if (typeof iWishRemoveFn !== 'undefined' && typeof iWishRemoveFn === 'function') { iWishRemoveFn();} return false; } } } /* Let Rock-n-Roll*/ jQuery( document ).ready(function() { console.log('iWishlistmain :: '+JSON.stringify(iWishlistmain)); iwishProductWrap = jQuery(".iWishAdd:visible").parents(iwishWrapperClass); if (iwishProductWrap.length==1) { iWishpId = iwishProductWrap.find(".iWishAdd").attr('data-product'); iWishvarId = iwishProductWrap.find('[name=id]').val(); console.log('Wishlist init :: '+iWishpId+' :: '+iWishvarId); checkIwish(iwishProductWrap,iWishvarId); /*var checkAganId = isInWishlist(iWishpId,iWishvarId); if(checkAganId) { iwishProductWrap.find('.iWishAdd').addClass('iwishAdded').html(iwish_added_txt); } else { iwishProductWrap.find('.iWishAdd').removeClass('iwishAdded').html(iwish_add_txt).show(); }*/ } iwish_init(); /* Remove product from wishlist */ jQuery(".iwishRemoveBtn").click(function() { console.log('iwishRemove called'); if(jQuery(this).hasClass('iwishRemoved')){return false;} //var currentPid=jQuery(this).attr('data-pid'); var currentVid=jQuery(this).attr('data-variant'); if(iwish_remove(jQuery(this), currentVid, true)){ jQuery(this).addClass('iwishRemoved'); jQuery(this).parents('.iwishItem').remove(); } return false; }); /* update wishlist Quantity */ jQuery( "input[name=iwishProQuantity]" ).change(function() { // console.log(jQuery(this).val()); if(parseInt(iwish_cid)>0){ var currentiwishInpQty = jQuery(this); var iwishProQty = parseInt(currentiwishInpQty.val()); var iwishUpdateVid = currentiwishInpQty.parents('.iwishItem').find('[name=id]').val(); if(iwishProQty>0) { jQuery.ajax({ url: '//iwish.shopapps.in/ajax/iwishUpdateQty.php', data: { shop:iwish_shop, cId:iwish_cid, iwishpQty:iwishProQty, vId:iwishUpdateVid }, dataType: 'jsonp', jsonpCallback: 'iWishCallback', success: function(json) { currentiwishInpQty.parents('.iwishItem').find('[name=quantity]').val(iwishProQty); currentiwishInpQty.addClass('iwishProQtyAdded'); setTimeout(function () { currentiwishInpQty.removeClass('iwishProQtyAdded'); }, 2000); //currentiwishInpQty.parents('.iwishItem').find('.iwishQtyMsg').html('updated wishlist quantity!').fadeIn().delay(3000).fadeOut(); currentiwishInpQty.parents('.iwishItem').find('.iwishQtyMsg').fadeIn().delay(3000).fadeOut(); if (typeof iwishUpdateQtyFn !== 'undefined' && typeof iwishUpdateQtyFn === 'function') { iwishUpdateQtyFn();} }, error: function(XMLHttpRequest, textStatus, errorThrown) {} }); } else{ currentiwishInpQty.val(1).addClass('iwishProQtyError'); setTimeout(function () { currentiwishInpQty.removeClass('iwishProQtyError'); }, 1000); } } }); }); if (window.addEventListener) { window.addEventListener("load", iWishOnSelectChange, false); } else { window.attachEvent("onload", iWishOnSelectChange) }