(function( $j ) { 'use strict'; var alopeyk = alopeyk || { wcshm : {} }; alopeyk.wcshm.public = {}; /*========================================== = Defining Variables = ==========================================*/ alopeyk.wcshm.public.vars = { common : { info : window.awcshm, activeClass : 'active', loadingClass : 'loading', disabledClass : 'disabled', alopeykPrefix : 'awcshm-', paymentMethodInput : 'input[name="payment_method"]', }, maps : { selector : '.map-canvas', mapContainerClass : 'map-container', destinationLatInput : '#destination_latitude, #_shipping_address_latitude', destinationLngInput : '#destination_longitude, #_shipping_address_longitude', destinationCityInput : '#destination_city, #_shipping_address_location_city', destinationAddressInput : '#destination_address, #_shipping_address_location', destinationNumberInput : '#destination_number, #_shipping_address_number', destinationUnitInput : '#destination_unit, #_shipping_address_unit', shippingAddress1Input : '#shipping_address_1, #_shipping_address_1', shippingAddress2Input : '#shipping_address_2, #_shipping_address_2', billingAddress1Input : '#billing_address_1, #_billing_address_1', billingAddress2Input : '#billing_address_2, #_billing_address_2', shipToDifferentAddressInput : 'input[name="ship_to_different_address"]', editAddressButton : 'a.edit_address:eq(1)', destinationLocatorMapId : 'destination-locator-map', destinationLocatorMapClass : 'map-canvas destination-locator-map', destinationLocatorCtaClass : 'destination-locator-cta', destinationLocatorInputWrapperId : 'destination-locator-input-wrapper', destinationLocatorInputWrapperClass : 'destination-locator-input-wrapper', destinationLocatorAutocompleteResultsClass : 'destination-locator-autocomplete-results', destinationLocatorAutocompleteResultClass : 'destination-locator-autocomplete-result', destinationLocatorHiddenableInput : '.hide-parent-row', autocompletePlaceholderDataAttr : 'autocomplete-placeholder', autocompleteKeyupTimeout : null, positionKeyupTimeout : null, autoCompleteKeyupDelay : 500, positionKeyupDelay : 500, defaultZoom : 15, defaultCenter : { lat : 35.744989, lng : 51.375284 } }, }; /*================================== = Prototypes = ==================================*/ /*========================================== = Defining Functions = ==========================================*/ alopeyk.wcshm.public.fn = { addPrefix : function ( classes ) { var prefixedClasses = [], classesArray = classes.split ( ' ' ); for ( var i = 0; i < classesArray.length; i++ ) { prefixedClasses.push ( alopeyk.wcshm.public.vars.common.alopeykPrefix + classesArray[ i ] ) } return prefixedClasses.join ( ' ' ); }, injectScript : function ( src ) { if ( ! $j( '[src="' + src + '"]' ).length ) { var s, t; s = document.createElement ( 'script' ); s.type = 'text/javascript'; s.async = true; s.src = src; t = document.getElementsByTagName ( 'script' )[ 0 ]; t.parentNode.insertBefore ( s, t ); } }, loadGoogleMaps : function () { window[ 'alopeykHandleMapsPublic' ] = alopeyk.wcshm.public.fn.handleMaps; if ( typeof alopeykHandleMapsPublic === 'function' ) { if ( typeof window.google != 'undefined' && window.google.maps ) { window.googleMapIsLoading = false; alopeyk.wcshm.public.fn.handleMaps(); } else if ( window.googleMapIsLoading ) { alopeyk.wcshm.public.vars.loadingMapInterval = setInterval ( function () { if ( window.google && window.google.maps ) { window.googleMapIsLoading = false; clearInterval ( alopeyk.wcshm.public.vars.loadingMapInterval ); alopeyk.wcshm.public.fn.handleMaps(); } }, 500 ); } else { window.googleMapIsLoading = true; alopeyk.wcshm.public.fn.injectScript('https://maps.googleapis.com/maps/api/js?key=' + alopeyk.wcshm.public.vars.common.info.alopeyk.wcshm.map.api_key + '&libraries=places&language=fa®ion=IR&callback=alopeykHandleMapsPublic'); } } }, handleMaps : function () { $j( document ).trigger ( 'alopeyk:public:map:loaded' ); }, initMaps : function () { $j( document ).on ( 'alopeyk:public:map:loaded', function () { var destinationLatInput = $j( alopeyk.wcshm.public.vars.maps.destinationLatInput ), destinationLngInput = $j( alopeyk.wcshm.public.vars.maps.destinationLngInput ), destinationCityInput = $j( alopeyk.wcshm.public.vars.maps.destinationCityInput ), destinationAddressInput = $j( alopeyk.wcshm.public.vars.maps.destinationAddressInput ), destinationNumberInput = $j( alopeyk.wcshm.public.vars.maps.destinationNumberInput ), destinationUnitInput = $j( alopeyk.wcshm.public.vars.maps.destinationUnitInput ), shippingAddress1Input = $j( alopeyk.wcshm.public.vars.maps.shippingAddress1Input ), shippingAddress2Input = $j( alopeyk.wcshm.public.vars.maps.shippingAddress2Input ), billingAddress1Input = $j( alopeyk.wcshm.public.vars.maps.billingAddress1Input ), billingAddress2Input = $j( alopeyk.wcshm.public.vars.maps.billingAddress2Input ), shipToDifferentAddressInput = $j( alopeyk.wcshm.public.vars.maps.shipToDifferentAddressInput ), editAddressButton = $j( alopeyk.wcshm.public.vars.maps.editAddressButton ); if ( destinationLatInput.length && destinationLngInput.length && destinationCityInput.length && destinationAddressInput.length ) { alopeyk.wcshm.public.fn.initDestinationLocator( destinationLatInput, destinationLngInput, destinationCityInput, destinationAddressInput, destinationNumberInput, destinationUnitInput, shippingAddress1Input, shippingAddress2Input, billingAddress1Input, billingAddress2Input, shipToDifferentAddressInput, editAddressButton ); } }); alopeyk.wcshm.public.fn.loadGoogleMaps(); }, setCurrentGeolocation : function ( forceRelocation ) { var destinationMap = alopeyk.wcshm.public.vars.common.destinationMap, map = destinationMap ? destinationMap.mapObject : null, marker = destinationMap ? destinationMap.markerObject : null, destinationLatInput = $j( alopeyk.wcshm.public.vars.maps.destinationLatInput ), destinationLngInput = $j( alopeyk.wcshm.public.vars.maps.destinationLngInput ); if ( navigator.geolocation && map && marker && destinationLatInput.length && destinationLngInput.length && ( forceRelocation || ( ! destinationLatInput.val().length && ! destinationLngInput.val().length ) || ( destinationLatInput.val() == alopeyk.wcshm.public.vars.maps.defaultCenter.lat && destinationngInput.val() == alopeyk.wcshm.public.vars.maps.defaultCenter.lng ) ) ) { navigator.geolocation.getCurrentPosition ( function ( position ) { if ( alopeyk.wcshm.public.vars.maps.fetchAddressConnection ) { alopeyk.wcshm.public.vars.maps.fetchAddressConnection.abort(); } map.setCenter ({ lat : position.coords.latitude, lng : position.coords.longitude }); marker.setPosition ( new google.maps.LatLng ( position.coords.latitude, position.coords.longitude ) ); alopeyk.wcshm.public.vars.maps.defaultCenter.lat = position.coords.latitude; alopeyk.wcshm.public.vars.maps.defaultCenter.lng = position.coords.longitude; destinationLatInput.val ( position.coords.latitude ); destinationLngInput.val ( position.coords.longitude ).trigger ( 'change' ); }); } }, initDestinationLocator : function ( destinationLatInput, destinationLngInput, destinationCityInput, destinationAddressInput, destinationNumberInput, destinationUnitInput, shippingAddress1Input, shippingAddress2Input, billingAddress1Input, billingAddress2Input, shipToDifferentAddressInput, editAddressButton ) { var initialize = function () { var mapCanvas = $j('
').attr ({ id : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorMapId ), class : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorMapClass ), }), destinationAutocompleteInput = destinationAddressInput.clone().attr ({ id : destinationAddressInput.attr ( 'id' ) + '_autocomplete', class : destinationAddressInput.attr ( 'class' ), placeholder : destinationAddressInput.data ( alopeyk.wcshm.public.vars.maps.autocompletePlaceholderDataAttr ), name : '', style : '', type : 'text', spellcheck : 'false', autocapitalize : 'off', autocorrect : 'off', autocomplete : 'off' }).removeClass ( alopeyk.wcshm.public.vars.common.disabledClass ), destinationAutocompleteInputWrapper = $j( '
' ).attr ({ id : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorInputWrapperId ), class : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorInputWrapperClass ), }), autoCompleteList = $j( '